]>
Commit | Line | Data |
---|---|---|
427c49bc A |
1 | // |
2 | // SOSAccountTesting.h | |
3 | // sec | |
4 | // | |
5 | // Created by Mitch Adler on 6/18/13. | |
6 | // | |
7 | // | |
8 | ||
9 | #ifndef SEC_SOSAccountTesting_h | |
10 | #define SEC_SOSAccountTesting_h | |
11 | ||
12 | #include <CoreFoundation/CoreFoundation.h> | |
13 | #include <SecureObjectSync/SOSAccount.h> | |
14 | ||
15 | // | |
16 | // Account comparison | |
17 | // | |
18 | ||
19 | #define kAccountsAgreeTestMin 9 | |
20 | #define kAccountsAgreeTestPerPeer 1 | |
21 | #define accountsAgree(x) (kAccountsAgreeTestMin + kAccountsAgreeTestPerPeer * (x)) | |
22 | ||
23 | static void unretired_peers_is_subset(const char* label, CFArrayRef peers, CFArrayRef allowed_peers) | |
24 | { | |
25 | CFArrayForEach(peers, ^(const void *value) { | |
26 | SOSPeerInfoRef pi = (SOSPeerInfoRef) value; | |
27 | CFErrorRef leftError = NULL; | |
28 | CFErrorRef rightError = NULL; | |
29 | ||
30 | ok(SOSPeerInfoIsRetirementTicket(pi) || SOSPeerInfoIsCloudIdentity(pi) || CFArrayContainsValue(allowed_peers, CFRangeMake(0, CFArrayGetCount(allowed_peers)), pi), "Peer is allowed (%s) Peer: %@, Allowed %@", label, pi, allowed_peers); | |
31 | ||
32 | CFReleaseNull(leftError); | |
33 | CFReleaseNull(rightError); | |
34 | }); | |
35 | } | |
36 | ||
37 | static void accounts_agree_internal(char *label, SOSAccountRef left, SOSAccountRef right, bool check_peers) | |
38 | { | |
39 | CFErrorRef error = NULL; | |
40 | { | |
41 | CFArrayRef leftPeers = SOSAccountCopyActivePeers(left, &error); | |
42 | ok(leftPeers, "Left peers (%@) - %s", error, label); | |
43 | CFReleaseNull(error); | |
44 | ||
45 | CFArrayRef rightPeers = SOSAccountCopyActivePeers(right, &error); | |
46 | ok(rightPeers, "Right peers (%@) - %s", error, label); | |
47 | CFReleaseNull(error); | |
48 | ||
49 | ok(CFEqual(leftPeers, rightPeers), "Matching peers (%s) Left: %@, Right: %@", label, leftPeers, rightPeers); | |
50 | ||
51 | if (check_peers) { | |
52 | CFMutableArrayRef allowed_identities = CFArrayCreateMutableForCFTypes(kCFAllocatorDefault); | |
53 | ||
54 | CFArrayRef leftIdentities = SOSAccountCopyAccountIdentityPeerInfos(left, kCFAllocatorDefault, &error); | |
55 | ok(leftIdentities, "Get identities (%@)", error); | |
56 | CFReleaseNull(error); | |
57 | ||
58 | CFArrayAppendArray(allowed_identities, leftIdentities, CFRangeMake(0, CFArrayGetCount(leftIdentities))); | |
59 | ||
60 | CFReleaseNull(leftIdentities); | |
61 | ||
62 | CFArrayRef rightIdentities = SOSAccountCopyAccountIdentityPeerInfos(right, kCFAllocatorDefault, &error); | |
63 | ok(rightIdentities, "Get identities (%@)", error); | |
64 | CFReleaseNull(error); | |
65 | ||
66 | CFArrayAppendArray(allowed_identities, rightIdentities, CFRangeMake(0, CFArrayGetCount(rightIdentities))); | |
67 | ||
68 | CFReleaseNull(rightIdentities); | |
69 | ||
70 | unretired_peers_is_subset(label, leftPeers, allowed_identities); | |
71 | } | |
72 | ||
73 | CFReleaseNull(leftPeers); | |
74 | CFReleaseNull(rightPeers); | |
75 | } | |
76 | { | |
77 | CFArrayRef leftConcurringPeers = SOSAccountCopyConcurringPeers(left, &error); | |
78 | ok(leftConcurringPeers, "Left peers (%@) - %s", error, label); | |
79 | ||
80 | CFArrayRef rightConcurringPeers = SOSAccountCopyConcurringPeers(right, &error); | |
81 | ok(rightConcurringPeers, "Right peers (%@) - %s", error, label); | |
82 | ||
83 | ok(CFEqual(leftConcurringPeers, rightConcurringPeers), "Matching concurring peers Left: %@, Right: %@", leftConcurringPeers, rightConcurringPeers); | |
84 | ||
85 | CFReleaseNull(leftConcurringPeers); | |
86 | CFReleaseNull(rightConcurringPeers); | |
87 | } | |
88 | { | |
89 | CFArrayRef leftApplicants = SOSAccountCopyApplicants(left, &error); | |
90 | ok(leftApplicants, "Left Applicants (%@) - %s", error, label); | |
91 | ||
92 | CFArrayRef rightApplicants = SOSAccountCopyApplicants(right, &error); | |
93 | ok(rightApplicants, "Left Applicants (%@) - %s", error, label); | |
94 | ||
95 | ok(CFEqual(leftApplicants, rightApplicants), "Matching applicants (%s) Left: %@, Right: %@", label, leftApplicants, rightApplicants); | |
96 | ||
97 | CFReleaseNull(leftApplicants); | |
98 | CFReleaseNull(rightApplicants); | |
99 | } | |
100 | } | |
101 | ||
102 | static inline void accounts_agree(char *label, SOSAccountRef left, SOSAccountRef right) | |
103 | { | |
104 | accounts_agree_internal(label, left, right, true); | |
105 | } | |
106 | ||
107 | ||
108 | // | |
109 | // Change handling | |
110 | // | |
111 | ||
112 | static CFMutableDictionaryRef ExtractPendingChanges(CFMutableDictionaryRef changes) | |
113 | { | |
114 | CFMutableDictionaryRef extracted = CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, changes); | |
115 | ||
116 | CFDictionaryRemoveAllValues(changes); | |
117 | ||
118 | return extracted; | |
119 | } | |
120 | ||
121 | #define kFeedChangesToMultieTestCountPer 1 | |
122 | static inline void FeedChangesToMulti(CFMutableDictionaryRef changes, ...) | |
123 | { | |
124 | CFDictionaryRef changes_to_send = ExtractPendingChanges(changes); | |
125 | ||
126 | SOSAccountRef account; | |
127 | ||
128 | secerror("Change block: %@", changes_to_send); | |
129 | ||
130 | CFErrorRef error = NULL; | |
131 | va_list argp; | |
132 | va_start(argp, changes); | |
133 | while((account = va_arg(argp, SOSAccountRef)) != NULL) { | |
134 | ok(SOSAccountHandleUpdates(account, changes_to_send, &error), "SOSAccountHandleUpdates failed (%@)", error); | |
135 | CFReleaseNull(error); | |
136 | } | |
137 | } | |
138 | ||
139 | static inline void InjectChangeToMulti(CFStringRef changeKey, CFStringRef changeValue, ...) | |
140 | { | |
141 | CFMutableDictionaryRef changes_to_send = CFDictionaryCreateMutable(NULL, 1, NULL, NULL); | |
142 | CFDictionaryAddValue(changes_to_send, changeKey, changeValue); | |
143 | ||
144 | SOSAccountRef account; | |
145 | ||
146 | secerror("Change block: %@", changes_to_send); | |
147 | ||
148 | CFErrorRef error = NULL; | |
149 | va_list argp; | |
150 | va_start(argp, changeValue); | |
151 | while((account = va_arg(argp, SOSAccountRef)) != NULL) { | |
152 | ok(SOSAccountHandleUpdates(account, changes_to_send, &error), "SOSAccountHandleUpdates failed (%@)", error); | |
153 | CFReleaseNull(error); | |
154 | } | |
155 | CFReleaseNull(changes_to_send); | |
156 | } | |
157 | ||
158 | ||
159 | ||
160 | #define kFeedChangesToTestCount 1 | |
161 | static inline void FeedChangesTo(CFMutableDictionaryRef changes, SOSAccountRef account) | |
162 | { | |
163 | CFDictionaryRef changes_to_send = ExtractPendingChanges(changes); | |
164 | ||
165 | secerror("Change block: %@", changes_to_send); | |
166 | ||
167 | CFErrorRef error = NULL; | |
168 | ok(SOSAccountHandleUpdates(account, changes_to_send, &error), "SOSAccountHandleUpdates failed (%@)", error); | |
169 | CFReleaseNull(error); | |
170 | CFReleaseNull(changes_to_send); | |
171 | } | |
172 | ||
173 | ||
174 | static SOSAccountRef CreateAccountForLocalChanges(CFMutableDictionaryRef changes, CFStringRef name, CFStringRef data_source_name) | |
175 | { | |
176 | SOSAccountKeyInterestBlock interest_block = ^(bool getNewKeysOnly, CFArrayRef alwaysKeys, CFArrayRef afterFirstUnlockKeys, CFArrayRef unlockedKeys) {}; | |
177 | SOSAccountDataUpdateBlock update_block = ^ bool (CFDictionaryRef keys, CFErrorRef *error) { | |
178 | CFDictionaryForEach(keys, ^(const void *key, const void *value) { | |
179 | CFDictionarySetValue(changes, key, value); | |
180 | }); | |
181 | return true; | |
182 | }; | |
183 | ||
184 | SOSDataSourceFactoryRef factory = SOSTestDataSourceFactoryCreate(); | |
185 | SOSTestDataSourceFactoryAddDataSource(factory, data_source_name, SOSTestDataSourceCreate()); | |
186 | ||
187 | CFDictionaryRef gestalt = SOSCreatePeerGestaltFromName(name); | |
188 | ||
189 | SOSAccountRef result = SOSAccountCreate(kCFAllocatorDefault, gestalt, factory, interest_block, update_block); | |
190 | ||
191 | CFReleaseNull(gestalt); | |
192 | ||
193 | return result; | |
194 | } | |
195 | ||
196 | ||
197 | static inline int countPeers(SOSAccountRef account) { | |
198 | CFErrorRef error = NULL; | |
199 | CFArrayRef peers; | |
200 | ||
201 | peers = SOSAccountCopyPeers(account, &error); | |
202 | int retval = (int) CFArrayGetCount(peers); | |
203 | CFReleaseNull(error); | |
204 | CFReleaseNull(peers); | |
205 | return retval; | |
206 | } | |
207 | ||
208 | static inline int countActivePeers(SOSAccountRef account) { | |
209 | CFErrorRef error = NULL; | |
210 | CFArrayRef peers; | |
211 | ||
212 | peers = SOSAccountCopyActivePeers(account, &error); | |
213 | int retval = (int) CFArrayGetCount(peers); | |
214 | CFReleaseNull(error); | |
215 | CFReleaseNull(peers); | |
216 | return retval; | |
217 | } | |
218 | ||
219 | static inline int countActiveValidPeers(SOSAccountRef account) { | |
220 | CFErrorRef error = NULL; | |
221 | CFArrayRef peers; | |
222 | ||
223 | peers = SOSAccountCopyActiveValidPeers(account, &error); | |
224 | int retval = (int) CFArrayGetCount(peers); | |
225 | CFReleaseNull(error); | |
226 | CFReleaseNull(peers); | |
227 | return retval; | |
228 | } | |
229 | ||
230 | static inline int countApplicants(SOSAccountRef account) { | |
231 | CFErrorRef error = NULL; | |
232 | CFArrayRef applicants = SOSAccountCopyApplicants(account, &error); | |
233 | int retval = 0; | |
234 | ||
235 | if(applicants) retval = (int)CFArrayGetCount(applicants); | |
236 | CFReleaseNull(error); | |
237 | CFReleaseNull(applicants); | |
238 | return retval; | |
239 | } | |
240 | ||
241 | ||
242 | static inline void showActiveValidPeers(SOSAccountRef account) { | |
243 | CFErrorRef error = NULL; | |
244 | CFArrayRef peers; | |
245 | ||
246 | peers = SOSAccountCopyActiveValidPeers(account, &error); | |
247 | CFArrayForEach(peers, ^(const void *value) { | |
248 | SOSPeerInfoRef pi = (SOSPeerInfoRef) value; | |
249 | ok(0, "Active Valid Peer %@", pi); | |
250 | }); | |
251 | CFReleaseNull(peers); | |
252 | } | |
253 | ||
254 | #endif |