]> git.saurik.com Git - apple/security.git/blob - keychain/TrustedPeersHelperUnitTests/ContainerSync.swift
Security-59306.61.1.tar.gz
[apple/security.git] / keychain / TrustedPeersHelperUnitTests / ContainerSync.swift
1 //
2 // SessionSync.swift
3 // Security_ios
4 //
5 // Created by Ben Williamson on 6/8/18.
6 //
7
8 import XCTest
9
10 extension Container {
11
12 func dumpSync(test: XCTestCase) -> ([AnyHashable: Any]?, Error?) {
13 let expectation = XCTestExpectation(description: "dump replied")
14 var reta: [AnyHashable: Any]?, reterr: Error?
15 self.dump { a, err in
16 reta = a
17 reterr = err
18 expectation.fulfill()
19 }
20 test.wait(for: [expectation], timeout: 10.0)
21 return (reta, reterr)
22 }
23
24 func resetSync(resetReason: CuttlefishResetReason, test: XCTestCase) -> Error? {
25 let expectation = XCTestExpectation(description: "reset replied")
26 var reterr: Error?
27 self.reset(resetReason: resetReason) { error in
28 reterr = error
29 expectation.fulfill()
30 }
31 test.wait(for: [expectation], timeout: 10.0)
32 return reterr
33 }
34
35 func localResetSync(test: XCTestCase) -> Error? {
36 let expectation = XCTestExpectation(description: "reset replied")
37 var reterr: Error?
38 self.localReset { error in
39 reterr = error
40 expectation.fulfill()
41 }
42 test.wait(for: [expectation], timeout: 10.0)
43 return reterr
44 }
45
46 func prepareSync(test: XCTestCase,
47 epoch: UInt64,
48 machineID: String,
49 bottleSalt: String,
50 bottleID: String,
51 modelID: String,
52 deviceName: String = "test device name",
53 serialNumber: String = "456",
54 osVersion: String = "123",
55 policyVersion: UInt64? = nil,
56 policySecrets: [String: Data]? = nil,
57 signingPrivateKeyPersistentRef: Data? = nil,
58 encryptionPrivateKeyPersistentRef: Data? = nil
59 ) -> (String?, Data?, Data?, Data?, Data?, Error?) {
60 let expectation = XCTestExpectation(description: "prepare replied")
61 var reta: String?, retb: Data?, retc: Data?, retd: Data?, rete: Data?, reterr: Error?
62 self.prepare(epoch: epoch,
63 machineID: machineID,
64 bottleSalt: bottleSalt,
65 bottleID: bottleID,
66 modelID: modelID,
67 deviceName: deviceName,
68 serialNumber: serialNumber,
69 osVersion: osVersion,
70 policyVersion: policyVersion,
71 policySecrets: policySecrets,
72 signingPrivateKeyPersistentRef: signingPrivateKeyPersistentRef,
73 encryptionPrivateKeyPersistentRef: encryptionPrivateKeyPersistentRef
74 ) { a, b, c, d, e, err in
75 reta = a
76 retb = b
77 retc = c
78 retd = d
79 rete = e
80 reterr = err
81 expectation.fulfill()
82 }
83 test.wait(for: [expectation], timeout: 10.0)
84 return (reta, retb, retc, retd, rete, reterr)
85 }
86
87 func establishSync(test: XCTestCase,
88 ckksKeys: [CKKSKeychainBackedKeySet],
89 tlkShares: [CKKSTLKShare],
90 preapprovedKeys: [Data]?) -> (String?, [CKRecord], Error?) {
91 let expectation = XCTestExpectation(description: "prepare replied")
92 var reta: String?, retkhr: [CKRecord]?, reterr: Error?
93 self.establish(ckksKeys: ckksKeys,
94 tlkShares: tlkShares,
95 preapprovedKeys: preapprovedKeys) { a, khr, err in
96 reta = a
97 retkhr = khr
98 reterr = err
99 expectation.fulfill()
100 }
101 test.wait(for: [expectation], timeout: 10.0)
102 return (reta, retkhr!, reterr)
103 }
104
105 func vouchSync(test: XCTestCase,
106 peerID: String,
107 permanentInfo: Data,
108 permanentInfoSig: Data,
109 stableInfo: Data,
110 stableInfoSig: Data,
111 ckksKeys: [CKKSKeychainBackedKeySet]) -> (Data?, Data?, Error?) {
112 let expectation = XCTestExpectation(description: "vouch replied")
113 var reta: Data?, retb: Data?, reterr: Error?
114 self.vouch(peerID: peerID,
115 permanentInfo: permanentInfo,
116 permanentInfoSig: permanentInfoSig,
117 stableInfo: stableInfo,
118 stableInfoSig: stableInfoSig,
119 ckksKeys: ckksKeys) { a, b, err in
120 reta = a
121 retb = b
122 reterr = err
123 expectation.fulfill()
124 }
125 test.wait(for: [expectation], timeout: 10.0)
126 return (reta, retb, reterr)
127 }
128
129 func preflightVouchWithBottleSync(test: XCTestCase, bottleID: String) -> (String?, Error?) {
130 let expectation = XCTestExpectation(description: "preflightVouchWithBottle replied")
131 var reta: String?, reterr: Error?
132 self.preflightVouchWithBottle(bottleID: bottleID) { a, err in
133 reta = a
134 reterr = err
135 expectation.fulfill()
136 }
137 test.wait(for: [expectation], timeout: 10.0)
138 return (reta, reterr)
139 }
140
141 func vouchWithBottleSync(test: XCTestCase, b: String, entropy: Data, bottleSalt: String, tlkShares: [CKKSTLKShare]) -> (Data?, Data?, Error?) {
142 let expectation = XCTestExpectation(description: "vouchWithBottle replied")
143 var reta: Data?, retb: Data?, reterr: Error?
144 self.vouchWithBottle(bottleID: b, entropy: entropy, bottleSalt: bottleSalt, tlkShares: tlkShares) { a, b, err in
145 reta = a
146 retb = b
147 reterr = err
148 expectation.fulfill()
149 }
150 test.wait(for: [expectation], timeout: 10.0)
151 return (reta, retb, reterr)
152 }
153
154 func joinSync(test: XCTestCase,
155 voucherData: Data,
156 voucherSig: Data,
157 ckksKeys: [CKKSKeychainBackedKeySet],
158 tlkShares: [CKKSTLKShare],
159 preapprovedKeys: [Data]? = nil) -> (String?, [CKRecord]?, Error?) {
160 let expectation = XCTestExpectation(description: "join replied")
161 var reta: String?, retkhr: [CKRecord]?, reterr: Error?
162 self.join(voucherData: voucherData,
163 voucherSig: voucherSig,
164 ckksKeys: ckksKeys,
165 tlkShares: tlkShares,
166 preapprovedKeys: preapprovedKeys) { a, khr, err in
167 reta = a
168 retkhr = khr
169 reterr = err
170 expectation.fulfill()
171 }
172 test.wait(for: [expectation], timeout: 10.0)
173 return (reta, retkhr, reterr)
174 }
175
176 func preapprovedJoinSync(test: XCTestCase,
177 ckksKeys: [CKKSKeychainBackedKeySet],
178 tlkShares: [CKKSTLKShare],
179 preapprovedKeys: [Data]? = nil) -> (String?, [CKRecord]?, Error?) {
180 let expectation = XCTestExpectation(description: "preapprovedjoin replied")
181 var reta: String?
182 var retkhr: [CKRecord]?
183 var reterr: Error?
184 self.preapprovedJoin(ckksKeys: ckksKeys,
185 tlkShares: tlkShares,
186 preapprovedKeys: preapprovedKeys) { a, khr, err in
187 reta = a
188 retkhr = khr
189 reterr = err
190 expectation.fulfill()
191 }
192 test.wait(for: [expectation], timeout: 10.0)
193 return (reta, retkhr, reterr)
194 }
195
196 func updateSync(test: XCTestCase,
197 deviceName: String? = nil,
198 serialNumner: String? = nil,
199 osVersion: String? = nil,
200 policyVersion: UInt64? = nil,
201 policySecrets: [String: Data]? = nil) -> (TrustedPeersHelperPeerState?, Error?) {
202 let expectation = XCTestExpectation(description: "update replied")
203 var reterr: Error?
204 var retstate: TrustedPeersHelperPeerState?
205 self.update(deviceName: deviceName,
206 serialNumber: serialNumner,
207 osVersion: osVersion,
208 policyVersion: policyVersion,
209 policySecrets: policySecrets) { state, err in
210 retstate = state
211 reterr = err
212 expectation.fulfill()
213 }
214 test.wait(for: [expectation], timeout: 10.0)
215 return (retstate, reterr)
216 }
217
218 func setAllowedMachineIDsSync(test: XCTestCase, allowedMachineIDs: Set<String>, listDifference: Bool = true) -> (Error?) {
219 let expectation = XCTestExpectation(description: "setAllowedMachineIDs replied")
220 var reterr: Error?
221 self.setAllowedMachineIDs(allowedMachineIDs) { differences, err in
222 XCTAssertEqual(differences, listDifference, "Reported list difference should match expectation")
223 reterr = err
224 expectation.fulfill()
225 }
226 test.wait(for: [expectation], timeout: 10.0)
227 return reterr
228 }
229
230 func addAllowedMachineIDsSync(test: XCTestCase, machineIDs: [String]) -> Error? {
231 let expectation = XCTestExpectation(description: "addAllow replied")
232 var reterr: Error?
233 self.addAllow(machineIDs) { err in
234 reterr = err
235 expectation.fulfill()
236 }
237 test.wait(for: [expectation], timeout: 10.0)
238 return reterr
239 }
240
241 func removeAllowedMachineIDsSync(test: XCTestCase, machineIDs: [String]) -> Error? {
242 let expectation = XCTestExpectation(description: "removeAllow replied")
243 var reterr: Error?
244 self.removeAllow(machineIDs) { err in
245 reterr = err
246 expectation.fulfill()
247 }
248 test.wait(for: [expectation], timeout: 10.0)
249 return reterr
250 }
251
252 func fetchAllowedMachineIDsSync(test: XCTestCase) -> (Set<String>?, Error?) {
253 let expectation = XCTestExpectation(description: "fetchMIDList replied")
254 var retlist: Set<String>?
255 var reterr: Error?
256 self.fetchAllowedMachineIDs() { list, err in
257 retlist = list
258 reterr = err
259 expectation.fulfill()
260 }
261 test.wait(for: [expectation], timeout: 10.0)
262 return (retlist, reterr)
263 }
264
265 func departByDistrustingSelfSync(test: XCTestCase) -> Error? {
266 let expectation = XCTestExpectation(description: "departByDistrustingSelf replied")
267 var reterr: Error?
268 self.departByDistrustingSelf { error in
269 reterr = error
270 expectation.fulfill()
271 }
272 test.wait(for: [expectation], timeout: 10.0)
273 return reterr
274 }
275
276 func distrustSync(test: XCTestCase, peerIDs: Set<String>) -> Error? {
277 let expectation = XCTestExpectation(description: "distrustSync replied")
278 var reterr: Error?
279 self.distrust(peerIDs: peerIDs) { error in
280 reterr = error
281 expectation.fulfill()
282 }
283 test.wait(for: [expectation], timeout: 10.0)
284 return reterr
285 }
286
287 func getStateSync(test: XCTestCase) -> ContainerState {
288 let expectation = XCTestExpectation(description: "getState replied")
289 var retstate: ContainerState?
290 self.getState { state in
291 retstate = state
292 expectation.fulfill()
293 }
294 test.wait(for: [expectation], timeout: 10.0)
295 return retstate!
296 }
297
298 func loadSecretSync(test: XCTestCase,
299 label: String) -> (Data?) {
300 var secret: Data?
301 do {
302 secret = try loadSecret(label: label)
303 } catch {
304
305 }
306 return secret
307 }
308
309 func setRecoveryKeySync(test: XCTestCase, recoveryKey: String, recoverySalt: String, ckksKeys: [CKKSKeychainBackedKeySet]) -> (Error?) {
310 let expectation = XCTestExpectation(description: "setRecoveryKey replied")
311 var reterr: Error?
312
313 self.setRecoveryKey(recoveryKey: recoveryKey, salt: recoverySalt, ckksKeys: ckksKeys) { error in
314 reterr = error
315 expectation.fulfill()
316 }
317 test.wait(for: [expectation], timeout: 10.0)
318 return (reterr)
319 }
320
321 func fetchViableBottlesSync(test: XCTestCase) -> ([String]?, [String]?, Error?) {
322 let expectation = XCTestExpectation(description: "fetchViableBottles replied")
323 var retescrowRecordIDs: [String]?
324 var retpartialEscrowRecordIDs: [String]?
325 var reterror: Error?
326 self.fetchViableBottles { escrowRecordIDs, partialEscrowRecordIDs, error in
327 retescrowRecordIDs = escrowRecordIDs
328 retpartialEscrowRecordIDs = partialEscrowRecordIDs
329 reterror = error
330 expectation.fulfill()
331 }
332 test.wait(for: [expectation], timeout: 10.0)
333 return (retescrowRecordIDs, retpartialEscrowRecordIDs, reterror)
334 }
335
336 func trustStatusSync(test: XCTestCase) -> (TrustedPeersHelperEgoPeerStatus, Error?) {
337 let expectation = XCTestExpectation(description: "trustStatus replied")
338 var retEgoStatus = TrustedPeersHelperEgoPeerStatus(egoPeerID: nil,
339 status: .unknown,
340 viablePeerCountsByModelID: [:],
341 peerCountsByMachineID: [:],
342 isExcluded: false,
343 isLocked: false)
344 var reterror: Error?
345 self.trustStatus { egoStatus, error in
346 retEgoStatus = egoStatus
347 reterror = error
348 expectation.fulfill()
349 }
350 test.wait(for: [expectation], timeout: 10.0)
351 return (retEgoStatus, reterror)
352 }
353
354 func fetchPolicyDocumentsSync(test: XCTestCase,
355 keys: [NSNumber: String]) -> ([NSNumber: [String]]?, Error?) {
356 let expectation = XCTestExpectation(description: "fetchPolicyDocuments replied")
357 var reta: [NSNumber: [String]]?, reterr: Error?
358 self.fetchPolicyDocuments(keys: keys) { a, err in
359 reta = a
360 reterr = err
361 expectation.fulfill()
362 }
363 test.wait(for: [expectation], timeout: 10.0)
364 return (reta, reterr)
365 }
366
367 func fetchEscrowContentsSync(test: XCTestCase) -> (Data?, String?, Data?, Error?) {
368 let expectation = XCTestExpectation(description: "fetchEscrowContents replied")
369 var retentropy: Data?
370 var retbottleID: String?
371 var retspki: Data?
372 var reterror: Error?
373
374 self.fetchEscrowContents { entropy, bottleID, spki, error in
375 retentropy = entropy
376 retbottleID = bottleID
377 retspki = spki
378 reterror = error
379
380 expectation.fulfill()
381 }
382 test.wait(for: [expectation], timeout: 10.0)
383 return (retentropy, retbottleID, retspki, reterror)
384 }
385
386 func requestHealthCheckSync(requiresEscrowCheck: Bool, test: XCTestCase) -> (Bool, Bool, Bool, Error?) {
387 let expectation = XCTestExpectation(description: "requestHealthCheck replied")
388 var retrepairaccount: Bool = false
389 var retrepairescrow: Bool = false
390 var retresetoctagon: Bool = false
391 var reterror: Error?
392
393 self.requestHealthCheck(requiresEscrowCheck: requiresEscrowCheck) { repairAccount, repairEscrow, resetOctagon, error in
394 retrepairaccount = repairAccount
395 retrepairescrow = repairEscrow
396 retresetoctagon = resetOctagon
397 reterror = error
398
399 expectation.fulfill()
400 }
401 test.wait(for: [expectation], timeout: 10.0)
402 return (retrepairaccount, retrepairescrow, retresetoctagon, reterror)
403 }
404 }