5 // Created by Richard Murphy on 3/3/15.
11 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
13 * @APPLE_LICENSE_HEADER_START@
15 * This file contains Original Code and/or Modifications of Original Code
16 * as defined in and that are subject to the Apple Public Source License
17 * Version 2.0 (the 'License'). You may not use this file except in
18 * compliance with the License. Please obtain a copy of the License at
19 * http://www.opensource.apple.com/apsl/ and read it before using this
22 * The Original Code and all software distributed under the License are
23 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
24 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
25 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
27 * Please see the License for the specific language governing rights and
28 * limitations under the License.
30 * @APPLE_LICENSE_HEADER_END@
35 #include <Security/SecBase.h>
36 #include <Security/SecItem.h>
37 #include <Security/SecKeyPriv.h>
39 #include <Security/SecureObjectSync/SOSRing.h>
40 #include <Security/SecureObjectSync/SOSRingTypes.h>
41 #include <Security/SecureObjectSync/SOSRingUtils.h>
42 #include <Security/SecureObjectSync/SOSPeerInfo.h>
43 #include <Security/SecureObjectSync/SOSInternal.h>
44 #include <Security/SecureObjectSync/SOSUserKeygen.h>
46 #include <utilities/SecCFWrappers.h>
48 #include <CoreFoundation/CoreFoundation.h>
53 #include "SOSCircle_regressions.h"
54 #include "SOSRegressionUtilities.h"
56 static SOSFullPeerInfoRef
SOSCreateApplicantFullPeerInfoFromName(CFStringRef peerName
, SecKeyRef user_private_key
,
57 SecKeyRef
* outSigningKey
, CFErrorRef
*error
)
59 SOSFullPeerInfoRef result
= NULL
;
60 SOSFullPeerInfoRef fullPeer
= SOSCreateFullPeerInfoFromName(peerName
, outSigningKey
, error
);
62 if (fullPeer
&& SOSFullPeerInfoPromoteToApplication(fullPeer
, user_private_key
, error
))
63 CFTransferRetained(result
, fullPeer
);
65 CFReleaseNull(fullPeer
);
69 static int kTestTestCount
= 24;
70 static void tests(void)
73 //SecKeyRef publicKey = NULL;
74 SecKeyRef dev_a_key
= NULL
;
75 SecKeyRef dev_b_key
= NULL
;
76 SecKeyRef dev_c_key
= NULL
;
77 CFErrorRef error
= NULL
;
78 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
80 ok(cfpassword
, "no password");
82 CFDataRef parameters
= SOSUserKeyCreateGenerateParameters(&error
);
83 ok(parameters
, "No parameters!");
84 ok(error
== NULL
, "Error: (%@)", error
);
87 SecKeyRef user_privkey
= SOSUserKeygen(cfpassword
, parameters
, &error
);
88 CFReleaseNull(parameters
);
90 SecKeyRef user_pubkey
= SecKeyCreatePublicFromPrivate(user_privkey
);
93 SOSFullPeerInfoRef peer_a_full_info
= SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer A"), user_privkey
, &dev_a_key
, NULL
);
94 SOSFullPeerInfoRef peer_b_full_info
= SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer B"), user_privkey
, &dev_b_key
, NULL
);
95 SOSFullPeerInfoRef peer_c_full_info
= SOSCreateApplicantFullPeerInfoFromName(CFSTR("Peer C"), user_privkey
, &dev_c_key
, NULL
);
96 CFStringRef peerID_a
= SOSPeerInfoGetPeerID(SOSFullPeerInfoGetPeerInfo(peer_a_full_info
));
97 CFStringRef peerID_b
= SOSPeerInfoGetPeerID(SOSFullPeerInfoGetPeerInfo(peer_b_full_info
));
98 SOSRingRef Ring
= SOSRingCreate(CFSTR("TESTRING"), peerID_a
, kSOSRingBase
, NULL
);
100 ok(Ring
, "Ring creation");
103 ok(0 == SOSRingCountPeers(Ring
), "Zero peers");
105 ok(SOSRingApply(Ring
, user_pubkey
, peer_a_full_info
, NULL
));
106 ok(SOSRingApply(Ring
, user_pubkey
, peer_b_full_info
, NULL
));
108 ok(2 == SOSRingCountPeers(Ring
), "Two peers");
110 ok(SOSRingWithdraw(Ring
, user_privkey
, peer_b_full_info
, NULL
));
112 ok(1 == SOSRingCountPeers(Ring
), "One peer");
114 ok(kSOSRingMember
== SOSRingDeviceIsInRing(Ring
, peerID_a
), "peer_a is in Ring");
115 ok(kSOSRingNotInRing
== SOSRingDeviceIsInRing(Ring
, peerID_b
), "peer_b is not in Ring");
116 CFStringRef lastmod
= SOSRingGetLastModifier(Ring
);
117 ok(CFEqual(lastmod
, peerID_b
), "peer_b_full_info did last mod");
119 ok(SOSRingResetToEmpty(Ring
, peerID_a
, NULL
), "Reset the circle");
120 ok(kSOSRingNotInRing
== SOSRingDeviceIsInRing(Ring
, peerID_a
), "peer_a is not in Ring");
122 ok(SOSRingResetToOffering(Ring
, NULL
, peer_a_full_info
, NULL
), "Reset Ring to Offering for PeerA");
123 ok(kSOSRingMember
== SOSRingDeviceIsInRing(Ring
, peerID_a
), "peer_a is in Ring");
124 ok(kSOSRingNotInRing
== SOSRingDeviceIsInRing(Ring
, peerID_b
), "peer_b is not in Ring");
126 CFDataRef ringDER
= SOSRingCopyEncodedData(Ring
, NULL
);
127 ok(ringDER
, "Successful encoding to DER of Ring");
128 SOSRingRef Ring2
= SOSRingCreateFromData(NULL
, ringDER
);
129 ok(Ring2
, "Successful decoding of DER to Ring");
131 ok(CFEqualSafe(Ring
, Ring2
), "Compares");
133 ok(SOSRingApply(Ring
, user_pubkey
, peer_c_full_info
, NULL
));
134 ok(SOSRingApply(Ring
, user_pubkey
, peer_b_full_info
, NULL
));
136 CFReleaseNull(ringDER
);
137 CFReleaseNull(Ring2
);
138 ringDER
= SOSRingCopyEncodedData(Ring
, NULL
);
139 Ring2
= SOSRingCreateFromData(NULL
, ringDER
);
140 ok(CFEqualSafe(Ring
, Ring2
), "Compares");
142 CFReleaseNull(ringDER
);
143 CFReleaseNull(Ring2
);
144 CFReleaseNull(dev_a_key
);
145 CFReleaseNull(cfpassword
);
147 CFReleaseNull(user_privkey
);
148 CFReleaseNull(user_pubkey
);
151 int sc_150_Ring(int argc
, char *const *argv
)
153 plan_tests(kTestTestCount
);