2 // FakeCuttlefish.swift
5 // Created by Ben Williamson on 5/23/18.
11 enum FakeCuttlefishError: Error {
13 case unknownChangeToken
17 enum FakeCuttlefishOpinion {
19 case trustsByPreapproval
23 struct FakeCuttlefishAssertion: CustomStringConvertible {
25 let opinion: FakeCuttlefishOpinion
28 func check(peer: Peer?, target: Peer?) -> Bool {
29 guard let peer = peer else {
33 guard peer.hasDynamicInfoAndSig else {
34 // No opinions? You've failed this assertion.
38 let dynamicInfo = TPPeerDynamicInfo(data: peer.dynamicInfoAndSig.peerDynamicInfo, sig: peer.dynamicInfoAndSig.sig)
39 guard let realDynamicInfo = dynamicInfo else {
43 let targetPermanentInfo: TPPeerPermanentInfo? =
44 target != nil ? TPPeerPermanentInfo(peerID: self.target,
45 data: target!.permanentInfoAndSig.peerPermanentInfo,
46 sig: target!.permanentInfoAndSig.sig,
47 keyFactory: TPECPublicKeyFactory())
52 return realDynamicInfo.includedPeerIDs.contains(self.target)
53 case .trustsByPreapproval:
54 guard let pubSignSPKI = targetPermanentInfo?.signingPubKey.spki() else {
57 let hash = TPHashBuilder.hash(with: .SHA256, of: pubSignSPKI)
58 return realDynamicInfo.preapprovals.contains(hash)
60 return realDynamicInfo.excludedPeerIDs.contains(self.target)
64 var description: String {
65 return "DCA:(\(self.peer)\(self.opinion)\(self.target))"
70 class FakeCuttlefishNotify: NSObject {
71 let pushes: (Data) -> Void
72 let containerName: String
74 init(_ containerName: String, pushes: @escaping (Data) -> Void) {
75 self.containerName = containerName
80 public func notify(_ function: String) throws {
81 let notification: [String: [String: Any]] = [
82 "aps": ["content-available": 1],
85 "c": self.containerName,
90 payload = try JSONSerialization.data(withJSONObject: notification)
99 func fakeRecord(zoneID: CKRecordZone.ID) -> CKRecord {
100 let recordID = CKRecord.ID(__recordName: self.uuid, zoneID: zoneID)
101 let record = CKRecord(recordType: SecCKRecordIntermediateKeyType, recordID: recordID)
103 record[SecCKRecordWrappedKeyKey] = self.wrappedkeyBase64
105 switch self.keyclass {
107 record[SecCKRecordKeyClassKey] = "tlk"
109 record[SecCKRecordKeyClassKey] = "classA"
111 record[SecCKRecordKeyClassKey] = "classC"
116 if !self.parentkeyUuid.isEmpty {
117 // TODO: no idea how to tell it about the 'verify' action
118 record[SecCKRecordParentKeyRefKey] = CKRecord.Reference(recordID: CKRecord.ID(__recordName: self.parentkeyUuid, zoneID: zoneID), action: .none)
124 func fakeKeyPointer(zoneID: CKRecordZone.ID) -> CKRecord {
125 let recordName: String
126 switch self.keyclass {
130 recordName = "classA"
132 recordName = "classC"
137 let recordID = CKRecord.ID(__recordName: recordName, zoneID: zoneID)
138 let record = CKRecord(recordType: SecCKRecordCurrentKeyType, recordID: recordID)
140 // TODO: no idea how to tell it about the 'verify' action
141 record[SecCKRecordParentKeyRefKey] = CKRecord.Reference(recordID: CKRecord.ID(__recordName: self.uuid, zoneID: zoneID), action: .none)
148 func fakeRecord(zoneID: CKRecordZone.ID) -> CKRecord {
149 let recordID = CKRecord.ID(__recordName: "tlkshare-\(self.keyUuid)::\(self.receiver)::\(self.sender)", zoneID: zoneID)
150 let record = CKRecord(recordType: SecCKRecordTLKShareType, recordID: recordID)
152 record[SecCKRecordSenderPeerID] = self.sender
153 record[SecCKRecordReceiverPeerID] = self.receiver
154 record[SecCKRecordReceiverPublicEncryptionKey] = self.receiverPublicEncryptionKey
155 record[SecCKRecordCurve] = self.curve
156 record[SecCKRecordVersion] = self.version
157 record[SecCKRecordEpoch] = self.epoch
158 record[SecCKRecordPoisoned] = self.poisoned
160 // TODO: no idea how to tell it about the 'verify' action
161 record[SecCKRecordParentKeyRefKey] = CKRecord.Reference(recordID: CKRecord.ID(__recordName: self.keyUuid, zoneID: zoneID), action: .none)
163 record[SecCKRecordWrappedKeyKey] = self.wrappedkey
164 record[SecCKRecordSignature] = self.signature
170 class FakeCuttlefishServer: CuttlefishAPIAsync {
173 var peersByID: [String: Peer] = [:]
174 var recoverySigningPubKey: Data?
175 var recoveryEncryptionPubKey: Data?
176 var bottles: [Bottle] = []
178 var viewKeys: [CKRecordZone.ID: ViewKeys] = [:]
179 var tlkShares: [CKRecordZone.ID: [TLKShare]] = [:]
186 var snapshotsByChangeToken: [String: State] = [:]
187 var currentChange: Int = 0
188 var currentChangeToken: String = ""
189 let notify: FakeCuttlefishNotify?
191 //var fakeCKZones: [CKRecordZone.ID: FakeCKZone]
192 var fakeCKZones: NSMutableDictionary
194 // @property (nullable) NSMutableDictionary<CKRecordZoneID*, ZoneKeys*>* keys;
195 var ckksZoneKeys: NSMutableDictionary
197 var nextFetchErrors: [Error] = []
198 var fetchViableBottlesError: [Error] = []
199 var nextJoinErrors: [Error] = []
200 var nextUpdateTrustErrors: [Error] = []
201 var returnNoActionResponse: Bool = false
202 var returnRepairAccountResponse: Bool = false
203 var returnRepairEscrowResponse: Bool = false
204 var returnResetOctagonResponse: Bool = false
205 var returnLeaveTrustResponse: Bool = false
206 var returnRepairErrorResponse: Error?
207 var fetchChangesCalledCount: Int = 0
209 var nextEstablishReturnsMoreChanges: Bool = false
211 var establishListener: ((EstablishRequest) -> NSError?)?
212 var updateListener: ((UpdateTrustRequest) -> NSError?)?
213 var fetchChangesListener: ((FetchChangesRequest) -> NSError?)?
214 var joinListener: ((JoinWithVoucherRequest) -> NSError?)?
215 var healthListener: ((GetRepairActionRequest) -> NSError?)?
216 var fetchViableBottlesListener: ((FetchViableBottlesRequest) -> NSError?)?
217 var resetListener: ((ResetRequest) -> NSError?)?
218 var setRecoveryKeyListener: ((SetRecoveryKeyRequest) -> NSError?)?
220 // Any policies in here will be returned by FetchPolicy before any inbuilt policies
221 var policyOverlay: [TPPolicyDocument] = []
223 var fetchViableBottlesDontReturnBottleWithID: String?
225 init(_ notify: FakeCuttlefishNotify?, ckZones: NSMutableDictionary, ckksZoneKeys: NSMutableDictionary) {
227 self.fakeCKZones = ckZones
228 self.ckksZoneKeys = ckksZoneKeys
231 func deleteAllPeers() {
232 self.state.peersByID.removeAll()
236 func pushNotify(_ function: String) {
237 if let notify = self.notify {
239 try notify.notify(function)
245 static func makeCloudKitCuttlefishError(code: CuttlefishErrorCode, retryAfter: TimeInterval = 5) -> NSError {
246 let cuttlefishError = CKPrettyError(domain: CuttlefishErrorDomain,
248 userInfo: [CuttlefishErrorRetryAfterKey: retryAfter])
249 let internalError = CKPrettyError(domain: CKInternalErrorDomain,
250 code: CKInternalErrorCode.errorInternalPluginError.rawValue,
251 userInfo: [NSUnderlyingErrorKey: cuttlefishError, ])
252 let ckError = CKPrettyError(domain: CKErrorDomain,
253 code: CKError.serverRejectedRequest.rawValue,
254 userInfo: [NSUnderlyingErrorKey: internalError,
255 CKErrorServerDescriptionKey: "Fake: FunctionError domain: CuttlefishError, code: \(code),\(code.rawValue)",
260 func makeSnapshot() {
261 self.currentChange += 1
262 self.currentChangeToken = "change\(self.currentChange)"
263 self.snapshotsByChangeToken[self.currentChangeToken] = self.state
266 func changesSince(snapshot: State) -> Changes {
267 return Changes.with { changes in
268 changes.changeToken = self.currentChangeToken
270 changes.differences = self.state.peersByID.compactMap { (key: String, value: Peer) -> PeerDifference? in
271 let old = snapshot.peersByID[key]
273 return PeerDifference.with {
276 } else if old != value {
277 return PeerDifference.with {
284 snapshot.peersByID.forEach { (key: String, _: Peer) in
285 if nil == self.state.peersByID[key] {
286 changes.differences.append(PeerDifference.with {
287 $0.remove = Peer.with {
294 if self.state.recoverySigningPubKey != snapshot.recoverySigningPubKey {
295 changes.recoverySigningPubKey = self.state.recoverySigningPubKey ?? Data()
297 if self.state.recoveryEncryptionPubKey != snapshot.recoveryEncryptionPubKey {
298 changes.recoveryEncryptionPubKey = self.state.recoveryEncryptionPubKey ?? Data()
304 func reset(_ request: ResetRequest, completion: @escaping (ResetResponse?, Error?) -> Void) {
305 print("FakeCuttlefish: reset called")
306 if let resetListener = self.resetListener {
307 let possibleError = resetListener(request)
308 guard possibleError == nil else {
309 completion(nil, possibleError)
315 completion(ResetResponse.with {
316 $0.changes = self.changesSince(snapshot: State())
318 self.pushNotify("reset")
321 func newKeysConflict(viewKeys: [ViewKeys]) -> Bool {
322 #if OCTAGON_TEST_FILL_ZONEKEYS
323 for keys in viewKeys {
324 let rzid = CKRecordZone.ID(zoneName: keys.view)
326 if let currentViewKeys = self.ckksZoneKeys[rzid] as? CKKSCurrentKeySet {
327 // Uploading the current view keys is okay. Fail only if they don't match
328 if keys.newTlk.uuid != currentViewKeys.tlk!.uuid ||
329 keys.newClassA.uuid != currentViewKeys.classA!.uuid ||
330 keys.newClassC.uuid != currentViewKeys.classC!.uuid {
340 func store(viewKeys: [ViewKeys]) -> [CKRecord] {
341 var allRecords: [CKRecord] = []
343 viewKeys.forEach { viewKeys in
344 let rzid = CKRecordZone.ID(zoneName: viewKeys.view)
345 self.state.viewKeys[rzid] = viewKeys
347 // Real cuttlefish makes these zones for you
348 if self.fakeCKZones[rzid] == nil {
349 self.fakeCKZones[rzid] = FakeCKZone(zone: rzid)
352 if let fakeZone = self.fakeCKZones[rzid] as? FakeCKZone {
353 fakeZone.queue.sync {
354 let tlkRecord = viewKeys.newTlk.fakeRecord(zoneID: rzid)
355 let classARecord = viewKeys.newClassA.fakeRecord(zoneID: rzid)
356 let classCRecord = viewKeys.newClassC.fakeRecord(zoneID: rzid)
358 let tlkPointerRecord = viewKeys.newTlk.fakeKeyPointer(zoneID: rzid)
359 let classAPointerRecord = viewKeys.newClassA.fakeKeyPointer(zoneID: rzid)
360 let classCPointerRecord = viewKeys.newClassC.fakeKeyPointer(zoneID: rzid)
362 // Some tests don't link everything needed to make zonekeys
363 // Those tests don't get this nice behavior
364 #if OCTAGON_TEST_FILL_ZONEKEYS
365 let zoneKeys = self.ckksZoneKeys[rzid] as? ZoneKeys ?? ZoneKeys(forZoneName: rzid.zoneName)
366 self.ckksZoneKeys[rzid] = zoneKeys
368 zoneKeys.tlk = CKKSKey(ckRecord: tlkRecord)
369 zoneKeys.classA = CKKSKey(ckRecord: classARecord)
370 zoneKeys.classC = CKKSKey(ckRecord: classCRecord)
372 zoneKeys.currentTLKPointer = CKKSCurrentKeyPointer(ckRecord: tlkPointerRecord)
373 zoneKeys.currentClassAPointer = CKKSCurrentKeyPointer(ckRecord: classAPointerRecord)
374 zoneKeys.currentClassCPointer = CKKSCurrentKeyPointer(ckRecord: classCPointerRecord)
377 let zoneRecords = [tlkRecord,
382 classCPointerRecord, ]
383 // TODO a rolled tlk too
385 zoneRecords.forEach { record in
386 fakeZone._onqueueAdd(toZone: record)
388 allRecords.append(contentsOf: zoneRecords)
391 // we made the zone above, shoudn't ever get here
392 print("Received an unexpected zone id: \(rzid)")
399 func store(tlkShares: [TLKShare]) -> [CKRecord] {
400 var allRecords: [CKRecord] = []
402 tlkShares.forEach { share in
403 let rzid = CKRecordZone.ID(zoneName: share.view)
405 var c = self.state.tlkShares[rzid] ?? []
407 self.state.tlkShares[rzid] = c
409 if let fakeZone = self.fakeCKZones[rzid] as? FakeCKZone {
410 let record = share.fakeRecord(zoneID: rzid)
411 fakeZone.add(toZone: record)
412 allRecords.append(record)
415 print("Received an unexpected zone id: \(rzid)")
422 func establish(_ request: EstablishRequest, completion: @escaping (EstablishResponse?, Error?) -> Void) {
423 print("FakeCuttlefish: establish called")
424 if !self.state.peersByID.isEmpty {
425 completion(nil, FakeCuttlefishError.notEmpty)
428 // Before performing write, check if we should error
429 if let establishListener = self.establishListener {
430 let possibleError = establishListener(request)
431 guard possibleError == nil else {
432 completion(nil, possibleError)
437 // Also check if we should bail due to conflicting viewKeys
438 if self.newKeysConflict(viewKeys: request.viewKeys) {
439 completion(nil, FakeCuttlefishServer.makeCloudKitCuttlefishError(code: .keyHierarchyAlreadyExists))
443 self.state.peersByID[request.peer.peerID] = request.peer
444 self.state.bottles.append(request.bottle)
446 var keyRecords: [CKRecord] = []
447 keyRecords.append(contentsOf: store(viewKeys: request.viewKeys))
448 keyRecords.append(contentsOf: store(tlkShares: request.tlkShares))
452 let response = EstablishResponse.with {
453 if self.nextEstablishReturnsMoreChanges {
454 $0.changes = Changes.with {
457 self.nextEstablishReturnsMoreChanges = false
459 $0.changes = self.changesSince(snapshot: State())
461 $0.zoneKeyHierarchyRecords = keyRecords.map { try! CloudKitCode.Ckcode_RecordTransport($0) }
464 completion(response, nil)
465 self.pushNotify("establish")
468 func joinWithVoucher(_ request: JoinWithVoucherRequest, completion: @escaping (JoinWithVoucherResponse?, Error?) -> Void) {
469 print("FakeCuttlefish: joinWithVoucher called")
471 if let joinListener = self.joinListener {
472 let possibleError = joinListener(request)
473 guard possibleError == nil else {
474 completion(nil, possibleError)
479 if let injectedError = self.nextJoinErrors.first {
480 print("FakeCuttlefish: erroring with injected error: ", String(describing: injectedError))
481 self.nextJoinErrors.removeFirst()
482 completion(nil, injectedError)
486 // Also check if we should bail due to conflicting viewKeys
487 if self.newKeysConflict(viewKeys: request.viewKeys) {
488 completion(nil, FakeCuttlefishServer.makeCloudKitCuttlefishError(code: .keyHierarchyAlreadyExists))
492 guard let snapshot = self.snapshotsByChangeToken[request.changeToken] else {
493 completion(nil, FakeCuttlefishError.unknownChangeToken)
496 self.state.peersByID[request.peer.peerID] = request.peer
497 self.state.bottles.append(request.bottle)
499 var keyRecords: [CKRecord] = []
500 keyRecords.append(contentsOf: store(viewKeys: request.viewKeys))
501 keyRecords.append(contentsOf: store(tlkShares: request.tlkShares))
505 completion(JoinWithVoucherResponse.with {
506 $0.changes = self.changesSince(snapshot: snapshot)
507 $0.zoneKeyHierarchyRecords = keyRecords.map { try! CloudKitCode.Ckcode_RecordTransport($0) }
509 self.pushNotify("joinWithVoucher")
512 func updateTrust(_ request: UpdateTrustRequest, completion: @escaping (UpdateTrustResponse?, Error?) -> Void) {
513 print("FakeCuttlefish: updateTrust called: changeToken: ", request.changeToken, "peerID: ", request.peerID)
515 if let injectedError = self.nextUpdateTrustErrors.first {
516 print("FakeCuttlefish: updateTrust erroring with injected error: ", String(describing: injectedError))
517 self.nextUpdateTrustErrors.removeFirst()
518 completion(nil, injectedError)
522 guard let snapshot = self.snapshotsByChangeToken[request.changeToken] else {
523 completion(nil, FakeCuttlefishError.unknownChangeToken)
526 guard var peer = self.state.peersByID[request.peerID] else {
527 completion(nil, FakeCuttlefishError.unknownPeerID)
530 if request.hasStableInfoAndSig {
531 peer.stableInfoAndSig = request.stableInfoAndSig
533 if request.hasDynamicInfoAndSig {
534 peer.dynamicInfoAndSig = request.dynamicInfoAndSig
536 self.state.peersByID[request.peerID] = peer
538 // Before performing write, check if we should error
539 if let updateListener = self.updateListener {
540 let possibleError = updateListener(request)
541 guard possibleError == nil else {
542 completion(nil, possibleError)
547 // Also check if we should bail due to conflicting viewKeys
548 if self.newKeysConflict(viewKeys: request.viewKeys) {
549 completion(nil, FakeCuttlefishServer.makeCloudKitCuttlefishError(code: .keyHierarchyAlreadyExists))
553 var keyRecords: [CKRecord] = []
554 keyRecords.append(contentsOf: store(viewKeys: request.viewKeys))
555 keyRecords.append(contentsOf: store(tlkShares: request.tlkShares))
557 let newDynamicInfo = TPPeerDynamicInfo(data: peer.dynamicInfoAndSig.peerDynamicInfo,
558 sig: peer.dynamicInfoAndSig.sig)
559 print("FakeCuttlefish: new peer dynamicInfo: ", request.peerID, String(describing: newDynamicInfo?.dictionaryRepresentation()))
562 let response = UpdateTrustResponse.with {
563 $0.changes = self.changesSince(snapshot: snapshot)
564 $0.zoneKeyHierarchyRecords = keyRecords.map { try! CloudKitCode.Ckcode_RecordTransport($0) }
567 completion(response, nil)
568 self.pushNotify("updateTrust")
571 func setRecoveryKey(_ request: SetRecoveryKeyRequest, completion: @escaping (SetRecoveryKeyResponse?, Error?) -> Void) {
572 print("FakeCuttlefish: setRecoveryKey called")
574 if let listener = self.setRecoveryKeyListener {
575 let operationError = listener(request)
576 guard operationError == nil else {
577 completion(nil, operationError)
582 guard let snapshot = self.snapshotsByChangeToken[request.changeToken] else {
583 completion(nil, FakeCuttlefishError.unknownChangeToken)
586 self.state.recoverySigningPubKey = request.recoverySigningPubKey
587 self.state.recoveryEncryptionPubKey = request.recoveryEncryptionPubKey
588 self.state.peersByID[request.peerID]?.stableInfoAndSig = request.stableInfoAndSig
590 completion(SetRecoveryKeyResponse.with {
591 $0.changes = self.changesSince(snapshot: snapshot)
593 self.pushNotify("setRecoveryKey")
596 func fetchChanges(_ request: FetchChangesRequest, completion: @escaping (FetchChangesResponse?, Error?) -> Void) {
597 print("FakeCuttlefish: fetchChanges called: ", request.changeToken)
599 self.fetchChangesCalledCount += 1
601 if let fetchChangesListener = self.fetchChangesListener {
602 let possibleError = fetchChangesListener(request)
603 guard possibleError == nil else {
604 completion(nil, possibleError)
609 if let injectedError = self.nextFetchErrors.first {
610 print("FakeCuttlefish: fetchChanges erroring with injected error: ", String(describing: injectedError))
611 self.nextFetchErrors.removeFirst()
612 completion(nil, injectedError)
617 if request.changeToken == "" {
620 guard let s = self.snapshotsByChangeToken[request.changeToken] else {
621 completion(nil, FakeCuttlefishError.unknownChangeToken)
626 let response = FetchChangesResponse.with {
627 $0.changes = self.changesSince(snapshot: snapshot)
630 completion(response, nil)
633 func fetchViableBottles(_ request: FetchViableBottlesRequest, completion: @escaping (FetchViableBottlesResponse?, Error?) -> Void) {
634 print("FakeCuttlefish: fetchViableBottles called")
636 if let fetchViableBottlesListener = self.fetchViableBottlesListener {
637 let possibleError = fetchViableBottlesListener(request)
638 guard possibleError == nil else {
639 completion(nil, possibleError)
644 if let injectedError = self.fetchViableBottlesError.first {
645 print("FakeCuttlefish: fetchViableBottles erroring with injected error: ", String(describing: injectedError))
646 self.fetchViableBottlesError.removeFirst()
647 completion(nil, injectedError)
651 let bottles = self.state.bottles.filter { $0.bottleID != fetchViableBottlesDontReturnBottleWithID }
652 completion(FetchViableBottlesResponse.with {
653 $0.viableBottles = bottles.compactMap { bottle in
655 $0.escrowRecordID = bottle.bottleID
662 func fetchPolicyDocuments(_ request: FetchPolicyDocumentsRequest,
663 completion: @escaping (FetchPolicyDocumentsResponse?, Error?) -> Void) {
664 print("FakeCuttlefish: fetchPolicyDocuments called")
665 var response = FetchPolicyDocumentsResponse()
667 let policies = builtInPolicyDocuments()
668 let dummyPolicies = Dictionary(uniqueKeysWithValues: policies.map { ($0.version.versionNumber, ($0.version.policyHash, $0.protobuf)) })
669 let overlayPolicies = Dictionary(uniqueKeysWithValues: self.policyOverlay.map { ($0.version.versionNumber, ($0.version.policyHash, $0.protobuf)) })
671 for key in request.keys {
672 if let (hash, data) = overlayPolicies[key.version], hash == key.hash {
673 response.entries.append(PolicyDocumentMapEntry.with { $0.key = key; $0.value = data })
677 guard let (hash, data) = dummyPolicies[key.version] else {
680 if hash == key.hash {
681 response.entries.append(PolicyDocumentMapEntry.with { $0.key = key; $0.value = data })
684 completion(response, nil)
687 func assertCuttlefishState(_ assertion: FakeCuttlefishAssertion) -> Bool {
688 return assertion.check(peer: self.state.peersByID[assertion.peer], target: self.state.peersByID[assertion.target])
691 func validatePeers(_: ValidatePeersRequest, completion: @escaping (ValidatePeersResponse?, Error?) -> Void) {
692 var response = ValidatePeersResponse()
693 response.validatorsHealth = 0.0
694 response.results = []
695 completion(response, nil)
697 func reportHealth(_: ReportHealthRequest, completion: @escaping (ReportHealthResponse?, Error?) -> Void) {
698 completion(ReportHealthResponse(), nil)
700 func pushHealthInquiry(_: PushHealthInquiryRequest, completion: @escaping (PushHealthInquiryResponse?, Error?) -> Void) {
701 completion(PushHealthInquiryResponse(), nil)
704 func getRepairAction(_ request: GetRepairActionRequest, completion: @escaping (GetRepairActionResponse?, Error?) -> Void) {
705 print("FakeCuttlefish: getRepairAction called")
707 if let healthListener = self.healthListener {
708 let possibleError = healthListener(request)
709 guard possibleError == nil else {
710 completion(nil, possibleError)
715 if self.returnRepairEscrowResponse {
716 let response = GetRepairActionResponse.with {
717 $0.repairAction = .postRepairEscrow
719 completion(response, nil)
720 } else if self.returnRepairAccountResponse {
721 let response = GetRepairActionResponse.with {
722 $0.repairAction = .postRepairAccount
724 completion(response, nil)
725 } else if self.returnResetOctagonResponse {
726 let response = GetRepairActionResponse.with {
727 $0.repairAction = .resetOctagon
729 completion(response, nil)
730 } else if returnLeaveTrustResponse {
731 let response = GetRepairActionResponse.with {
732 $0.repairAction = .leaveTrust
734 completion(response, nil)
735 } else if self.returnNoActionResponse {
736 let response = GetRepairActionResponse.with {
737 $0.repairAction = .noAction
739 completion(response, nil)
740 } else if self.returnRepairErrorResponse != nil {
741 let response = GetRepairActionResponse.with {
742 $0.repairAction = .noAction
744 completion(response, self.returnRepairErrorResponse)
746 completion(GetRepairActionResponse(), nil)
750 func getClubCertificates(_: GetClubCertificatesRequest, completion: @escaping (GetClubCertificatesResponse?, Error?) -> Void) {
751 completion(GetClubCertificatesResponse(), nil)
754 func getSupportAppInfo(_: GetSupportAppInfoRequest, completion: @escaping (GetSupportAppInfoResponse?, Error?) -> Void) {
755 completion(GetSupportAppInfoResponse(), nil)
759 extension FakeCuttlefishServer: CloudKitCode.Invocable {
760 func invoke<RequestType, ResponseType>(function: String,
761 request: RequestType,
762 completion: @escaping (ResponseType?, Error?) -> Void) {
763 // Ideally we'd pattern match on both request and completion, but that crashes the swift compiler at this time (<rdar://problem/54412402>)
765 case let request as ResetRequest:
766 self.reset(request, completion: completion as! (ResetResponse?, Error?) -> Void)
768 case let request as EstablishRequest:
769 self.establish(request, completion: completion as! (EstablishResponse?, Error?) -> Void)
771 case let request as JoinWithVoucherRequest:
772 self.joinWithVoucher(request, completion: completion as! (JoinWithVoucherResponse?, Error?) -> Void)
774 case let request as UpdateTrustRequest:
775 self.updateTrust(request, completion: completion as! (UpdateTrustResponse?, Error?) -> Void)
777 case let request as SetRecoveryKeyRequest:
778 self.setRecoveryKey(request, completion: completion as! (SetRecoveryKeyResponse?, Error?) -> Void)
780 case let request as FetchChangesRequest:
781 self.fetchChanges(request, completion: completion as! (FetchChangesResponse?, Error?) -> Void)
783 case let request as FetchViableBottlesRequest:
784 self.fetchViableBottles(request, completion: completion as! (FetchViableBottlesResponse?, Error?) -> Void)
786 case let request as FetchPolicyDocumentsRequest:
787 self.fetchPolicyDocuments(request, completion: completion as! (FetchPolicyDocumentsResponse?, Error?) -> Void)
789 case let request as ValidatePeersRequest:
790 self.validatePeers(request, completion: completion as! (ValidatePeersResponse?, Error?) -> Void)
792 case let request as ReportHealthRequest:
793 self.reportHealth(request, completion: completion as! (ReportHealthResponse?, Error?) -> Void)
795 case let request as PushHealthInquiryRequest:
796 self.pushHealthInquiry(request, completion: completion as! (PushHealthInquiryResponse?, Error?) -> Void)
798 case let request as GetRepairActionRequest:
799 self.getRepairAction(request, completion: completion as! (GetRepairActionResponse?, Error?) -> Void)