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