]> git.saurik.com Git - apple/security.git/blob - keychain/TrustedPeersHelperUnitTests/MockCuttlefish.swift
Security-59306.11.20.tar.gz
[apple/security.git] / keychain / TrustedPeersHelperUnitTests / MockCuttlefish.swift
1 //
2 // MockCuttlefish.swift
3 // TrustedPeersHelperUnitTests
4 //
5 // Created by Ben Williamson on 5/1/18.
6 //
7
8 import Foundation
9 import XCTest
10
11 enum Handler {
12 case reset((ResetRequest, @escaping (ResetResponse?, Error?) -> Void) -> Void)
13 case establish((EstablishRequest, @escaping (EstablishResponse?, Error?) -> Void) -> Void)
14 case joinWithVoucher((JoinWithVoucherRequest, @escaping (JoinWithVoucherResponse?, Error?) -> Void) -> Void)
15 case updateTrust((UpdateTrustRequest, @escaping (UpdateTrustResponse?, Error?) -> Void) -> Void)
16 case setRecoveryKey((SetRecoveryKeyRequest, @escaping (SetRecoveryKeyResponse?, Error?) -> Void) -> Void)
17 case fetchChanges((FetchChangesRequest, @escaping (FetchChangesResponse?, Error?) -> Void) -> Void)
18 case fetchViableBottles((FetchViableBottlesRequest, @escaping (FetchViableBottlesResponse?, Error?) -> Void) ->Void)
19 case fetchPolicyDocuments((FetchPolicyDocumentsRequest,
20 @escaping (FetchPolicyDocumentsResponse?, Error?) -> Void) -> Void)
21 }
22
23 class MockCuttlefishAPIAsyncClient: CuttlefishAPIAsync {
24
25 var handlers: [Handler] = []
26 var index: Int = 0
27
28 func expect(_ handler: Handler) {
29 self.handlers.append(handler)
30 }
31
32 func next() -> Handler {
33 if index >= handlers.count {
34 XCTFail("MockCuttlefish: Not expecting any more calls.")
35 self.dump()
36 abort()
37 }
38 return handlers[index]
39 }
40
41 func dump() {
42 print("---")
43 for i in 0 ..< handlers.count {
44 print("\(i) \(i == index ? "->" : " ") \(handlers[i])")
45 }
46 print("---")
47 }
48
49 func reset(_ request: ResetRequest, completion: @escaping (ResetResponse?, Error?) -> Void) {
50 print("MockCuttlefish: reset called")
51 if case .reset(let f) = next() {
52 index += 1
53 f(request, completion)
54 } else {
55 XCTFail("Unexpected: reset")
56 print("Unexpected: reset")
57 self.dump()
58 abort()
59 }
60 }
61
62 func establish(_ request: EstablishRequest, completion: @escaping (EstablishResponse?, Error?) -> Void) {
63 print("MockCuttlefish: establish called")
64 if case .establish(let f) = next() {
65 index += 1
66 f(request, completion)
67 } else {
68 XCTFail("Unexpected: establish")
69 print("Unexpected: establish")
70 self.dump()
71 abort()
72 }
73 }
74
75 func joinWithVoucher(_ request: JoinWithVoucherRequest,
76 completion: @escaping (JoinWithVoucherResponse?, Error?) -> Void) {
77 print("MockCuttlefish: joinWithVoucher called")
78 if case .joinWithVoucher(let f) = next() {
79 index += 1
80 f(request, completion)
81 } else {
82 XCTFail("Unexpected: joinWithVoucher")
83 print("Unexpected: joinWithVoucher")
84 self.dump()
85 abort()
86 }
87 }
88
89 func updateTrust(_ request: UpdateTrustRequest, completion: @escaping (UpdateTrustResponse?, Error?) -> Void) {
90 print("MockCuttlefish: updateTrust called")
91 if case .updateTrust(let f) = next() {
92 index += 1
93 f(request, completion)
94 } else {
95 XCTFail("Unexpected: updateTrust")
96 print("Unexpected: updateTrust")
97 self.dump()
98 abort()
99 }
100 }
101
102 func setRecoveryKey(_ request: SetRecoveryKeyRequest,
103 completion: @escaping (SetRecoveryKeyResponse?, Error?) -> Void) {
104 print("MockCuttlefish: setRecoveryKey called")
105 if case .setRecoveryKey(let f) = next() {
106 index += 1
107 f(request, completion)
108 } else {
109 XCTFail("Unexpected: setRecoveryKey")
110 print("Unexpected: setRecoveryKey")
111 self.dump()
112 abort()
113 }
114 }
115
116 func fetchChanges(_ request: FetchChangesRequest, completion: @escaping (FetchChangesResponse?, Error?) -> Void) {
117 print("MockCuttlefish: fetchChanges called")
118 if case .fetchChanges(let f) = next() {
119 index += 1
120 f(request, completion)
121 } else {
122 XCTFail("Unexpected: fetchChanges")
123 print("Unexpected: fetchChanges")
124 self.dump()
125 abort()
126 }
127 }
128
129 func fetchViableBottles(_ request: FetchViableBottlesRequest, completion: @escaping (FetchViableBottlesResponse?, Error?) -> Void) {
130 print("MockCuttlefish: fetchViableBottles called")
131 if case .fetchViableBottles(let f) = next() {
132 index += 1
133 f(request, completion)
134 } else {
135 XCTFail("Unexpected: fetchViableBottles")
136 self.dump()
137 abort()
138 }
139 }
140
141 func fetchPolicyDocuments(_ request: FetchPolicyDocumentsRequest,
142 completion: @escaping (FetchPolicyDocumentsResponse?, Error?) -> Void) {
143 print("MockCuttlefish: fetchPolicyDocuments called")
144 if case .fetchPolicyDocuments(let f) = next() {
145 index += 1
146 f(request, completion)
147 } else {
148 XCTFail("Unexpected: fetchPolicyDocuments")
149 print("Unexpected: fetchPolicyDocuments")
150 self.dump()
151 abort()
152 }
153 }
154
155 func validatePeers(_: ValidatePeersRequest, completion: @escaping (ValidatePeersResponse?, Error?) -> Void) {
156 completion(ValidatePeersResponse(), nil)
157 }
158 func reportHealth(_: ReportHealthRequest, completion: @escaping (ReportHealthResponse?, Error?) -> Void) {
159 print("MockCuttlefish: reportHealth called")
160 completion(ReportHealthResponse(), nil)
161 }
162 func pushHealthInquiry(_: HealthInquiryRequest, completion: @escaping (HealthInquiryResponse?, Error?) -> Void) {
163 completion(HealthInquiryResponse(), nil)
164 }
165 func getRepairAction(_: GetRepairActionRequest, completion: @escaping (GetRepairActionResponse?, Error?) -> Void) {
166 completion(GetRepairActionResponse(), nil)
167
168 }
169 }