]> git.saurik.com Git - apple/security.git/blob - OSX/sec/SOSCircle/Regressions/sc-40-circle.c
Security-57336.10.29.tar.gz
[apple/security.git] / OSX / sec / SOSCircle / Regressions / sc-40-circle.c
1 /*
2 * Copyright (c) 2012-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25
26 #include <Security/SecBase.h>
27 #include <Security/SecItem.h>
28 #include <Security/SecKey.h>
29
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>
36
37 #include <utilities/SecCFWrappers.h>
38
39 #include <CoreFoundation/CoreFoundation.h>
40
41 #include <stdlib.h>
42 #include <unistd.h>
43
44 #include <securityd/SOSCloudCircleServer.h>
45
46 #include "SOSCircle_regressions.h"
47
48 #include "SOSRegressionUtilities.h"
49
50 static int kTestGenerationCount = 2;
51 static void test_generation(void)
52 {
53 SOSGenCountRef generation = SOSGenerationCreate();
54 SOSGenCountRef olderGeneration = SOSGenerationCreateWithBaseline(generation);
55 SOSGenCountRef evenOlderGeneration = SOSGenerationCreateWithBaseline(olderGeneration);
56
57 ok(SOSGenerationIsOlder(olderGeneration, generation), "should be older");
58 ok(SOSGenerationIsOlder(evenOlderGeneration, olderGeneration), "should be older");
59
60 CFReleaseNull(generation);
61 CFReleaseNull(olderGeneration);
62 CFReleaseNull(evenOlderGeneration);
63 }
64
65
66 static int kTestTestCount = 26;
67 static void tests(void)
68 {
69 SOSCircleRef circle = SOSCircleCreate(NULL, CFSTR("TEST DOMAIN"), NULL);
70
71 ok(NULL != circle, "Circle creation");
72
73 ok(0 == SOSCircleCountPeers(circle), "Zero peers");
74
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);
82
83 ok(cfpassword, "no password");
84
85 CFDataRef parameters = SOSUserKeyCreateGenerateParameters(&error);
86 ok(parameters, "No parameters!");
87 ok(error == NULL, "Error: (%@)", error);
88 CFReleaseNull(error);
89
90 SecKeyRef user_privkey = SOSUserKeygen(cfpassword, parameters, &error);
91 CFReleaseNull(parameters);
92
93 SOSFullPeerInfoRef peer_a_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer A"), &dev_a_key, NULL);
94
95 SOSFullPeerInfoRef peer_b_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer B"), &dev_b_key, NULL);
96
97 SOSFullPeerInfoRef peer_c_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer C"), &dev_c_key, NULL);
98
99 SOSFullPeerInfoRef peer_d_full_info = SOSCreateFullPeerInfoFromName(CFSTR("Peer D"), &dev_d_key, NULL);
100
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));
104
105 ok(SOSCircleAcceptRequest(circle, user_privkey, peer_a_full_info, SOSFullPeerInfoGetPeerInfo(peer_a_full_info), NULL));
106
107 ok(!SOSCircleRequestAdmission(circle, user_privkey, peer_a_full_info, NULL));
108 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_b_full_info, NULL));
109
110 ok(SOSCircleCountPeers(circle) == 1, "Peer count");
111
112 size_t size = SOSCircleGetDEREncodedSize(circle, &error);
113 uint8_t buffer[size];
114 uint8_t* start = SOSCircleEncodeToDER(circle, &error, buffer, buffer + sizeof(buffer));
115
116 ok(start, "successful encoding");
117 ok(start == buffer, "Used whole buffer");
118
119 const uint8_t *der = buffer;
120 SOSCircleRef inflated = SOSCircleCreateFromDER(NULL, &error, &der, buffer + sizeof(buffer));
121
122 ok(inflated, "inflated");
123 ok(CFEqualSafe(inflated, circle), "Compares");
124
125
126 ok(SOSCircleRemovePeer(circle, user_privkey, peer_a_full_info, SOSFullPeerInfoGetPeerInfo(peer_a_full_info), NULL));
127 ok(SOSCircleCountPeers(circle) == 0, "Peer count");
128
129 // Try multiple peer removal:
130
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));
134
135 ok(SOSCircleAcceptRequests(circle, user_privkey, peer_a_full_info, NULL));
136
137 ok(SOSCircleRequestAdmission(circle, user_privkey, peer_d_full_info, NULL));
138
139 CFArrayRef peer_array = CFArrayCreateForCFTypes(kCFAllocatorDefault,
140 SOSFullPeerInfoGetPeerInfo(peer_b_full_info),
141 SOSFullPeerInfoGetPeerInfo(peer_c_full_info),
142 SOSFullPeerInfoGetPeerInfo(peer_d_full_info),
143 NULL);
144
145 CFSetRef peers_to_remove = CFSetCreateMutableForSOSPeerInfosByIDWithArray(kCFAllocatorDefault, peer_array);
146 CFReleaseNull(peer_array);
147
148 ok(SOSCircleRemovePeers(circle, user_privkey, peer_a_full_info, peers_to_remove, NULL));
149 CFReleaseNull(peers_to_remove);
150
151 ok(SOSCircleCountPeers(circle) == 1);
152 ok(SOSCircleCountApplicants(circle) == 0);
153
154 CFReleaseNull(dev_a_key);
155 CFReleaseNull(dev_b_key);
156 CFReleaseNull(dev_c_key);
157 CFReleaseNull(dev_d_key);
158
159 CFReleaseNull(cfpassword);
160
161 CFReleaseNull(peer_a_full_info);
162 CFReleaseNull(peer_b_full_info);
163 CFReleaseNull(peer_c_full_info);
164 CFReleaseNull(peer_d_full_info);
165 }
166
167 int sc_40_circle(int argc, char *const *argv)
168 {
169 plan_tests(kTestGenerationCount + kTestTestCount);
170
171 test_generation();
172
173 tests();
174
175 return 0;
176 }