5 // Created by Ben Williamson on 6/8/18.
11 func dumpSync(test: XCTestCase) -> ([AnyHashable: Any]?, Error?) {
12 let expectation = XCTestExpectation(description: "dump replied")
13 var reta: [AnyHashable: Any]?, reterr: Error?
19 test.wait(for: [expectation], timeout: 10.0)
23 func resetSync(resetReason: CuttlefishResetReason, test: XCTestCase) -> Error? {
24 let expectation = XCTestExpectation(description: "reset replied")
26 self.reset(resetReason: resetReason) { error in
30 test.wait(for: [expectation], timeout: 10.0)
34 func localResetSync(test: XCTestCase) -> Error? {
35 let expectation = XCTestExpectation(description: "reset replied")
37 self.localReset { error in
41 test.wait(for: [expectation], timeout: 10.0)
45 func prepareSync(test: XCTestCase,
51 deviceName: String = "test device name",
52 serialNumber: String = "456",
53 osVersion: String = "123",
54 policyVersion: TPPolicyVersion? = nil,
55 policySecrets: [String: Data]? = nil,
56 syncUserControllableViews: TPPBPeerStableInfo_UserControllableViewStatus = .UNKNOWN,
57 signingPrivateKeyPersistentRef: Data? = nil,
58 encryptionPrivateKeyPersistentRef: Data? = nil
59 ) -> (String?, Data?, Data?, Data?, Data?, TPSyncingPolicy?, Error?) {
60 let expectation = XCTestExpectation(description: "prepare replied")
61 var reta: String?, retb: Data?, retc: Data?, retd: Data?, rete: Data?, reterr: Error?
62 var retpolicy: TPSyncingPolicy?
63 self.prepare(epoch: epoch,
65 bottleSalt: bottleSalt,
68 deviceName: deviceName,
69 serialNumber: serialNumber,
71 policyVersion: policyVersion,
72 policySecrets: policySecrets,
73 syncUserControllableViews: syncUserControllableViews,
74 signingPrivateKeyPersistentRef: signingPrivateKeyPersistentRef,
75 encryptionPrivateKeyPersistentRef: encryptionPrivateKeyPersistentRef
76 ) { a, b, c, d, e, f, err in
86 test.wait(for: [expectation], timeout: 10.0)
87 return (reta, retb, retc, retd, rete, retpolicy, reterr)
90 func establishSync(test: XCTestCase,
91 ckksKeys: [CKKSKeychainBackedKeySet],
92 tlkShares: [CKKSTLKShare],
93 preapprovedKeys: [Data]?) -> (String?, [CKRecord], TPSyncingPolicy?, Error?) {
94 let expectation = XCTestExpectation(description: "prepare replied")
95 var reta: String?, retkhr: [CKRecord]?, reterr: Error?
96 var retpolicy: TPSyncingPolicy?
97 self.establish(ckksKeys: ckksKeys,
99 preapprovedKeys: preapprovedKeys) { a, khr, policy, err in
104 expectation.fulfill()
106 test.wait(for: [expectation], timeout: 10.0)
107 return (reta, retkhr!, retpolicy, reterr)
110 func vouchSync(test: XCTestCase,
113 permanentInfoSig: Data,
116 ckksKeys: [CKKSKeychainBackedKeySet]) -> (Data?, Data?, Error?) {
117 let expectation = XCTestExpectation(description: "vouch replied")
118 var reta: Data?, retb: Data?, reterr: Error?
119 self.vouch(peerID: peerID,
120 permanentInfo: permanentInfo,
121 permanentInfoSig: permanentInfoSig,
122 stableInfo: stableInfo,
123 stableInfoSig: stableInfoSig,
124 ckksKeys: ckksKeys) { a, b, err in
128 expectation.fulfill()
130 test.wait(for: [expectation], timeout: 10.0)
131 return (reta, retb, reterr)
134 func preflightVouchWithBottleSync(test: XCTestCase, bottleID: String) -> (String?, TPSyncingPolicy?, Bool, Error?) {
135 let expectation = XCTestExpectation(description: "preflightVouchWithBottle replied")
136 var reta: String?, reterr: Error?
137 var retrefetched: Bool = false
138 var retpolicy: TPSyncingPolicy?
139 self.preflightVouchWithBottle(bottleID: bottleID) { a, policy, refetched, err in
142 retrefetched = refetched
144 expectation.fulfill()
146 test.wait(for: [expectation], timeout: 10.0)
147 return (reta, retpolicy, retrefetched, reterr)
150 func vouchWithBottleSync(test: XCTestCase, b: String, entropy: Data, bottleSalt: String, tlkShares: [CKKSTLKShare]) -> (Data?, Data?, Int64, Int64, Error?) {
151 let expectation = XCTestExpectation(description: "vouchWithBottle replied")
152 var reta: Data?, retb: Data?, retc: Int64 = 0, retd: Int64 = 0, reterr: Error?
153 self.vouchWithBottle(bottleID: b, entropy: entropy, bottleSalt: bottleSalt, tlkShares: tlkShares) { a, b, c, d, err in
159 expectation.fulfill()
161 test.wait(for: [expectation], timeout: 10.0)
162 return (reta, retb, retc, retd, reterr)
165 func joinSync(test: XCTestCase,
168 ckksKeys: [CKKSKeychainBackedKeySet],
169 tlkShares: [CKKSTLKShare],
170 preapprovedKeys: [Data]? = nil) -> (String?, [CKRecord]?, TPSyncingPolicy?, Error?) {
171 let expectation = XCTestExpectation(description: "join replied")
172 var reta: String?, retkhr: [CKRecord]?, reterr: Error?
173 var retpolicy: TPSyncingPolicy?
174 self.join(voucherData: voucherData,
175 voucherSig: voucherSig,
177 tlkShares: tlkShares,
178 preapprovedKeys: preapprovedKeys) { a, khr, policy, err in
183 expectation.fulfill()
185 test.wait(for: [expectation], timeout: 10.0)
186 return (reta, retkhr, retpolicy, reterr)
189 func preapprovedJoinSync(test: XCTestCase,
190 ckksKeys: [CKKSKeychainBackedKeySet],
191 tlkShares: [CKKSTLKShare],
192 preapprovedKeys: [Data]? = nil) -> (String?, [CKRecord]?, TPSyncingPolicy?, Error?) {
193 let expectation = XCTestExpectation(description: "preapprovedjoin replied")
195 var retkhr: [CKRecord]?
196 var retpolicy: TPSyncingPolicy?
198 self.preapprovedJoin(ckksKeys: ckksKeys,
199 tlkShares: tlkShares,
200 preapprovedKeys: preapprovedKeys) { a, khr, policy, err in
205 expectation.fulfill()
207 test.wait(for: [expectation], timeout: 10.0)
208 return (reta, retkhr, retpolicy, reterr)
211 func updateSync(test: XCTestCase,
212 deviceName: String? = nil,
213 serialNumber: String? = nil,
214 osVersion: String? = nil,
215 policyVersion: UInt64? = nil,
216 policySecrets: [String: Data]? = nil,
217 syncUserControllableViews: TPPBPeerStableInfo_UserControllableViewStatus? = nil) -> (TrustedPeersHelperPeerState?, TPSyncingPolicy?, Error?) {
218 let expectation = XCTestExpectation(description: "update replied")
220 var retstate: TrustedPeersHelperPeerState?
221 var retpolicy: TPSyncingPolicy?
222 self.update(deviceName: deviceName,
223 serialNumber: serialNumber,
224 osVersion: osVersion,
225 policyVersion: policyVersion,
226 policySecrets: policySecrets,
227 syncUserControllableViews: syncUserControllableViews) { state, policy, err in
231 expectation.fulfill()
233 test.wait(for: [expectation], timeout: 10.0)
234 return (retstate, retpolicy, reterr)
237 func setAllowedMachineIDsSync(test: XCTestCase, allowedMachineIDs: Set<String>, accountIsDemo: Bool, listDifference: Bool = true) -> (Error?) {
238 let expectation = XCTestExpectation(description: "setAllowedMachineIDs replied")
240 let honorIDMSListChanges = accountIsDemo ? false : true
241 self.setAllowedMachineIDs(allowedMachineIDs, honorIDMSListChanges: honorIDMSListChanges) { differences, err in
242 XCTAssertEqual(differences, listDifference, "Reported list difference should match expectation")
244 expectation.fulfill()
246 test.wait(for: [expectation], timeout: 10.0)
250 func addAllowedMachineIDsSync(test: XCTestCase, machineIDs: [String]) -> Error? {
251 let expectation = XCTestExpectation(description: "addAllow replied")
253 self.addAllow(machineIDs) { err in
255 expectation.fulfill()
257 test.wait(for: [expectation], timeout: 10.0)
261 func removeAllowedMachineIDsSync(test: XCTestCase, machineIDs: [String]) -> Error? {
262 let expectation = XCTestExpectation(description: "removeAllow replied")
264 self.removeAllow(machineIDs) { err in
266 expectation.fulfill()
268 test.wait(for: [expectation], timeout: 10.0)
272 func fetchAllowedMachineIDsSync(test: XCTestCase) -> (Set<String>?, Error?) {
273 let expectation = XCTestExpectation(description: "fetchMIDList replied")
274 var retlist: Set<String>?
276 self.fetchAllowedMachineIDs { list, err in
279 expectation.fulfill()
281 test.wait(for: [expectation], timeout: 10.0)
282 return (retlist, reterr)
285 func departByDistrustingSelfSync(test: XCTestCase) -> Error? {
286 let expectation = XCTestExpectation(description: "departByDistrustingSelf replied")
288 self.departByDistrustingSelf { error in
290 expectation.fulfill()
292 test.wait(for: [expectation], timeout: 10.0)
296 func distrustSync(test: XCTestCase, peerIDs: Set<String>) -> Error? {
297 let expectation = XCTestExpectation(description: "distrustSync replied")
299 self.distrust(peerIDs: peerIDs) { error in
301 expectation.fulfill()
303 test.wait(for: [expectation], timeout: 10.0)
307 func getStateSync(test: XCTestCase) -> ContainerState {
308 let expectation = XCTestExpectation(description: "getState replied")
309 var retstate: ContainerState?
310 self.getState { state in
312 expectation.fulfill()
314 test.wait(for: [expectation], timeout: 10.0)
318 func loadSecretSync(test: XCTestCase,
319 label: String) -> (Data?) {
322 secret = try loadSecret(label: label)
328 func setRecoveryKeySync(test: XCTestCase, recoveryKey: String, recoverySalt: String, ckksKeys: [CKKSKeychainBackedKeySet]) -> ([CKRecord]?, Error?) {
329 let expectation = XCTestExpectation(description: "setRecoveryKey replied")
330 var retrecords: [CKRecord]?
333 self.setRecoveryKey(recoveryKey: recoveryKey, salt: recoverySalt, ckksKeys: ckksKeys) { records, error in
336 expectation.fulfill()
338 test.wait(for: [expectation], timeout: 10.0)
339 return (retrecords, reterr)
342 func fetchViableBottlesSync(test: XCTestCase) -> ([String]?, [String]?, Error?) {
343 let expectation = XCTestExpectation(description: "fetchViableBottles replied")
344 var retescrowRecordIDs: [String]?
345 var retpartialEscrowRecordIDs: [String]?
347 self.fetchViableBottles { escrowRecordIDs, partialEscrowRecordIDs, error in
348 retescrowRecordIDs = escrowRecordIDs
349 retpartialEscrowRecordIDs = partialEscrowRecordIDs
351 expectation.fulfill()
353 test.wait(for: [expectation], timeout: 10.0)
354 return (retescrowRecordIDs, retpartialEscrowRecordIDs, reterror)
357 func trustStatusSync(test: XCTestCase) -> (TrustedPeersHelperEgoPeerStatus, Error?) {
358 let expectation = XCTestExpectation(description: "trustStatus replied")
359 var retEgoStatus = TrustedPeersHelperEgoPeerStatus(egoPeerID: nil,
361 viablePeerCountsByModelID: [:],
362 peerCountsByMachineID: [:],
366 self.trustStatus { egoStatus, error in
367 retEgoStatus = egoStatus
369 expectation.fulfill()
371 test.wait(for: [expectation], timeout: 10.0)
372 return (retEgoStatus, reterror)
375 func fetchPolicyDocumentsSync(test: XCTestCase,
376 versions: Set<TPPolicyVersion>) -> ([TPPolicyVersion: Data]?, Error?) {
377 let expectation = XCTestExpectation(description: "fetchPolicyDocuments replied")
378 var reta: [TPPolicyVersion: Data]?, reterr: Error?
379 self.fetchPolicyDocuments(versions: versions) { a, err in
382 expectation.fulfill()
384 test.wait(for: [expectation], timeout: 10.0)
385 return (reta, reterr)
388 func fetchCurrentPolicySync(test: XCTestCase) -> (TPSyncingPolicy?, TPPBPeerStableInfo_UserControllableViewStatus, Error?) {
389 let expectation = XCTestExpectation(description: "fetchCurrentPolicy replied")
390 var reta: TPSyncingPolicy?, reterr: Error?
391 var retOp: TPPBPeerStableInfo_UserControllableViewStatus = .UNKNOWN
392 self.fetchCurrentPolicy(modelIDOverride: nil) { a, peerOpinion, err in
396 expectation.fulfill()
398 test.wait(for: [expectation], timeout: 10.0)
399 return (reta, retOp, reterr)
402 func fetchEscrowContentsSync(test: XCTestCase) -> (Data?, String?, Data?, Error?) {
403 let expectation = XCTestExpectation(description: "fetchEscrowContents replied")
404 var retentropy: Data?
405 var retbottleID: String?
409 self.fetchEscrowContents { entropy, bottleID, spki, error in
411 retbottleID = bottleID
415 expectation.fulfill()
417 test.wait(for: [expectation], timeout: 10.0)
418 return (retentropy, retbottleID, retspki, reterror)
421 func requestHealthCheckSync(requiresEscrowCheck: Bool, test: XCTestCase) -> (Bool, Bool, Bool, Bool, Error?) {
422 let expectation = XCTestExpectation(description: "requestHealthCheck replied")
423 var retrepairaccount: Bool = false
424 var retrepairescrow: Bool = false
425 var retresetoctagon: Bool = false
426 var retleavetrust: Bool = false
429 self.requestHealthCheck(requiresEscrowCheck: requiresEscrowCheck) { repairAccount, repairEscrow, resetOctagon, leaveTrust, error in
430 retrepairaccount = repairAccount
431 retrepairescrow = repairEscrow
432 retresetoctagon = resetOctagon
433 retleavetrust = leaveTrust
436 expectation.fulfill()
438 test.wait(for: [expectation], timeout: 10.0)
439 return (retrepairaccount, retrepairescrow, retresetoctagon, retleavetrust, reterror)