2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
28 #include <Security/SecKey.h>
30 #include <Security/SecureObjectSync/SOSCircle.h>
31 #include <Security/SecureObjectSync/SOSCloudCircle.h>
32 #include <Security/SecureObjectSync/SOSPeerInfo.h>
33 #include <Security/SecureObjectSync/SOSPeerInfoCollections.h>
34 #include <Security/SecureObjectSync/SOSInternal.h>
35 #include <Security/SecureObjectSync/SOSUserKeygen.h>
37 #include <utilities/SecCFWrappers.h>
39 #include <CoreFoundation/CoreFoundation.h>
44 #include <securityd/SOSCloudCircleServer.h>
46 #include "SOSCircle_regressions.h"
48 #include "SOSRegressionUtilities.h"
50 static int kTestGenerationCount
= 2;
51 static void test_generation(void)
53 SOSGenCountRef generation
= SOSGenerationCreate();
54 SOSGenCountRef olderGeneration
= SOSGenerationCreateWithBaseline(generation
);
55 SOSGenCountRef evenOlderGeneration
= SOSGenerationCreateWithBaseline(olderGeneration
);
57 ok(SOSGenerationIsOlder(olderGeneration
, generation
), "should be older");
58 ok(SOSGenerationIsOlder(evenOlderGeneration
, olderGeneration
), "should be older");
60 CFReleaseNull(generation
);
61 CFReleaseNull(olderGeneration
);
62 CFReleaseNull(evenOlderGeneration
);
66 static int kTestTestCount
= 26;
67 static void tests(void)
69 SOSCircleRef circle
= SOSCircleCreate(NULL
, CFSTR("TEST DOMAIN"), NULL
);
71 ok(NULL
!= circle
, "Circle creation");
73 ok(0 == SOSCircleCountPeers(circle
), "Zero peers");
75 //SecKeyRef publicKey = NULL;
76 SecKeyRef dev_a_key
= NULL
;
77 SecKeyRef dev_b_key
= NULL
;
78 SecKeyRef dev_c_key
= NULL
;
79 SecKeyRef dev_d_key
= NULL
;
80 CFErrorRef error
= NULL
;
81 CFDataRef cfpassword
= CFDataCreate(NULL
, (uint8_t *) "FooFooFoo", 10);
83 ok(cfpassword
, "no password");
85 CFDataRef parameters
= SOSUserKeyCreateGenerateParameters(&error
);
86 ok(parameters
, "No parameters!");
87 ok(error
== NULL
, "Error: (%@)", error
);
90 SecKeyRef user_privkey
= SOSUserKeygen(cfpassword
, parameters
, &error
);
91 CFReleaseNull(parameters
);
93 SOSFullPeerInfoRef peer_a_full_info
= SOSCreateFullPeerInfoFromName(CFSTR("Peer A"), &dev_a_key
, NULL
);
95 SOSFullPeerInfoRef peer_b_full_info
= SOSCreateFullPeerInfoFromName(CFSTR("Peer B"), &dev_b_key
, NULL
);
97 SOSFullPeerInfoRef peer_c_full_info
= SOSCreateFullPeerInfoFromName(CFSTR("Peer C"), &dev_c_key
, NULL
);
99 SOSFullPeerInfoRef peer_d_full_info
= SOSCreateFullPeerInfoFromName(CFSTR("Peer D"), &dev_d_key
, NULL
);
101 ok(SOSCircleRequestAdmission(circle
, user_privkey
, peer_a_full_info
, NULL
));
102 ok(SOSCircleRequestAdmission(circle
, user_privkey
, peer_a_full_info
, NULL
));
103 ok(SOSCircleRequestAdmission(circle
, user_privkey
, peer_a_full_info
, NULL
));
105 ok(SOSCircleAcceptRequest(circle
, user_privkey
, peer_a_full_info
, SOSFullPeerInfoGetPeerInfo(peer_a_full_info
), NULL
));
107 ok(!SOSCircleRequestAdmission(circle
, user_privkey
, peer_a_full_info
, NULL
));
108 ok(SOSCircleRequestAdmission(circle
, user_privkey
, peer_b_full_info
, NULL
));
110 ok(SOSCircleCountPeers(circle
) == 1, "Peer count");
112 size_t size
= SOSCircleGetDEREncodedSize(circle
, &error
);
113 uint8_t buffer
[size
];
114 uint8_t* start
= SOSCircleEncodeToDER(circle
, &error
, buffer
, buffer
+ sizeof(buffer
));
116 ok(start
, "successful encoding");
117 ok(start
== buffer
, "Used whole buffer");
119 const uint8_t *der
= buffer
;
120 SOSCircleRef inflated
= SOSCircleCreateFromDER(NULL
, &error
, &der
, buffer
+ sizeof(buffer
));
122 ok(inflated
, "inflated");
123 ok(CFEqualSafe(inflated
, circle
), "Compares");
126 ok(SOSCircleRemovePeer(circle
, user_privkey
, peer_a_full_info
, SOSFullPeerInfoGetPeerInfo(peer_a_full_info
), NULL
));
127 ok(SOSCircleCountPeers(circle
) == 0, "Peer count");
129 // Try multiple peer removal:
131 ok(SOSCircleRequestAdmission(circle
, user_privkey
, peer_a_full_info
, NULL
));
132 ok(SOSCircleRequestAdmission(circle
, user_privkey
, peer_b_full_info
, NULL
));
133 ok(SOSCircleRequestAdmission(circle
, user_privkey
, peer_c_full_info
, NULL
));
135 ok(SOSCircleAcceptRequests(circle
, user_privkey
, peer_a_full_info
, NULL
));
137 ok(SOSCircleRequestAdmission(circle
, user_privkey
, peer_d_full_info
, NULL
));
139 CFArrayRef peer_array
= CFArrayCreateForCFTypes(kCFAllocatorDefault
,
140 SOSFullPeerInfoGetPeerInfo(peer_b_full_info
),
141 SOSFullPeerInfoGetPeerInfo(peer_c_full_info
),
142 SOSFullPeerInfoGetPeerInfo(peer_d_full_info
),
145 CFSetRef peers_to_remove
= CFSetCreateMutableForSOSPeerInfosByIDWithArray(kCFAllocatorDefault
, peer_array
);
146 CFReleaseNull(peer_array
);
148 ok(SOSCircleRemovePeers(circle
, user_privkey
, peer_a_full_info
, peers_to_remove
, NULL
));
149 CFReleaseNull(peers_to_remove
);
151 ok(SOSCircleCountPeers(circle
) == 1);
152 ok(SOSCircleCountApplicants(circle
) == 0);
154 CFReleaseNull(dev_a_key
);
155 CFReleaseNull(dev_b_key
);
156 CFReleaseNull(dev_c_key
);
157 CFReleaseNull(dev_d_key
);
159 CFReleaseNull(cfpassword
);
161 CFReleaseNull(peer_a_full_info
);
162 CFReleaseNull(peer_b_full_info
);
163 CFReleaseNull(peer_c_full_info
);
164 CFReleaseNull(peer_d_full_info
);
167 int sc_40_circle(int argc
, char *const *argv
)
169 plan_tests(kTestGenerationCount
+ kTestTestCount
);