FIRAuthAppCredentialManagerTests.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <XCTest/XCTest.h>
  17. #import <OCMock/OCMock.h>
  18. #import "FIRAuthAppCredential.h"
  19. #import "FIRAuthAppCredentialManager.h"
  20. #import "FIRAuthKeychainServices.h"
  21. #define ANY_ERROR_POINTER ((NSError * __autoreleasing * _Nullable)[OCMArg anyPointer])
  22. #define SAVE_TO(var) \
  23. [OCMArg checkWithBlock:^BOOL(id arg) { \
  24. var = arg; \
  25. return YES; \
  26. }]
  27. /** @var kReceipt
  28. @brief A fake receipt used for testing.
  29. */
  30. static NSString *const kReceipt = @"FAKE_RECEIPT";
  31. /** @var kAnotherReceipt
  32. @brief Another fake receipt used for testing.
  33. */
  34. static NSString *const kAnotherReceipt = @"OTHER_RECEIPT";
  35. /** @var kSecret
  36. @brief A fake secret used for testing.
  37. */
  38. static NSString *const kSecret = @"FAKE_SECRET";
  39. /** @var kAnotherSecret
  40. @brief Another fake secret used for testing.
  41. */
  42. static NSString *const kAnotherSecret = @"OTHER_SECRET";
  43. /** @var kVerificationTimeout
  44. @brief The verification timeout used for testing.
  45. */
  46. static const NSTimeInterval kVerificationTimeout = 1;
  47. /** @var kExpectationTimeout
  48. @brief The test expectation timeout.
  49. @remarks This must be considerably greater than @c kVerificationTimeout .
  50. */
  51. static const NSTimeInterval kExpectationTimeout = 2;
  52. NS_ASSUME_NONNULL_BEGIN
  53. /** @class FIRAuthAppCredentialManagerTests
  54. @brief Unit tests for @c FIRAuthAppCredentialManager .
  55. */
  56. @interface FIRAuthAppCredentialManagerTests : XCTestCase
  57. @end
  58. @implementation FIRAuthAppCredentialManagerTests {
  59. /** @var _mockKeychain
  60. @brief The mock keychain for testing.
  61. */
  62. id _mockKeychain;
  63. }
  64. - (void)setUp {
  65. _mockKeychain = OCMClassMock([FIRAuthKeychainServices class]);
  66. }
  67. /** @fn testCompletion
  68. @brief Tests a successfully completed verification flow.
  69. */
  70. - (void)testCompletion {
  71. // Initial empty state.
  72. OCMExpect([_mockKeychain dataForKey:OCMOCK_ANY error:ANY_ERROR_POINTER]).andReturn(nil);
  73. FIRAuthAppCredentialManager *manager =
  74. [[FIRAuthAppCredentialManager alloc] initWithKeychain:_mockKeychain];
  75. XCTAssertNil(manager.credential);
  76. OCMVerifyAll(_mockKeychain);
  77. // Start verification.
  78. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  79. OCMExpect([_mockKeychain setData:OCMOCK_ANY forKey:OCMOCK_ANY error:ANY_ERROR_POINTER])
  80. .andReturn(YES);
  81. [manager didStartVerificationWithReceipt:kReceipt
  82. timeout:kVerificationTimeout
  83. callback:^(FIRAuthAppCredential *credential) {
  84. XCTAssertEqualObjects(credential.receipt, kReceipt);
  85. XCTAssertEqualObjects(credential.secret, kSecret);
  86. [expectation fulfill];
  87. }];
  88. XCTAssertNil(manager.credential);
  89. OCMVerifyAll(_mockKeychain);
  90. // Mismatched receipt shouldn't finish verification.
  91. XCTAssertFalse([manager canFinishVerificationWithReceipt:kAnotherReceipt secret:kAnotherSecret]);
  92. XCTAssertNil(manager.credential);
  93. // Finish verification.
  94. OCMExpect([_mockKeychain setData:OCMOCK_ANY forKey:OCMOCK_ANY error:ANY_ERROR_POINTER])
  95. .andReturn(YES);
  96. XCTAssertTrue([manager canFinishVerificationWithReceipt:kReceipt secret:kSecret]);
  97. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  98. XCTAssertNotNil(manager.credential);
  99. XCTAssertEqualObjects(manager.credential.receipt, kReceipt);
  100. XCTAssertEqualObjects(manager.credential.secret, kSecret);
  101. OCMVerifyAll(_mockKeychain);
  102. // Repeated receipt should have no effect.
  103. XCTAssertFalse([manager canFinishVerificationWithReceipt:kReceipt secret:kAnotherSecret]);
  104. XCTAssertEqualObjects(manager.credential.secret, kSecret);
  105. }
  106. /** @fn testTimeout
  107. @brief Tests a verification flow that times out.
  108. */
  109. - (void)testTimeout {
  110. // Initial empty state.
  111. OCMExpect([_mockKeychain dataForKey:OCMOCK_ANY error:ANY_ERROR_POINTER]).andReturn(nil);
  112. FIRAuthAppCredentialManager *manager =
  113. [[FIRAuthAppCredentialManager alloc] initWithKeychain:_mockKeychain];
  114. XCTAssertNil(manager.credential);
  115. OCMVerifyAll(_mockKeychain);
  116. // Start verification.
  117. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  118. OCMExpect([_mockKeychain setData:OCMOCK_ANY forKey:OCMOCK_ANY error:ANY_ERROR_POINTER])
  119. .andReturn(YES);
  120. [manager didStartVerificationWithReceipt:kReceipt
  121. timeout:kVerificationTimeout
  122. callback:^(FIRAuthAppCredential *credential) {
  123. XCTAssertEqualObjects(credential.receipt, kReceipt);
  124. XCTAssertNil(credential.secret);
  125. [expectation fulfill];
  126. }];
  127. XCTAssertNil(manager.credential);
  128. OCMVerifyAll(_mockKeychain);
  129. // Time-out.
  130. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  131. XCTAssertNil(manager.credential);
  132. // Completion after timeout.
  133. OCMExpect([_mockKeychain setData:OCMOCK_ANY forKey:OCMOCK_ANY error:ANY_ERROR_POINTER])
  134. .andReturn(YES);
  135. XCTAssertTrue([manager canFinishVerificationWithReceipt:kReceipt secret:kSecret]);
  136. XCTAssertNotNil(manager.credential);
  137. XCTAssertEqualObjects(manager.credential.receipt, kReceipt);
  138. XCTAssertEqualObjects(manager.credential.secret, kSecret);
  139. OCMVerifyAll(_mockKeychain);
  140. }
  141. /** @fn testMaximumPendingReceipt
  142. @brief Tests the maximum allowed number of pending receipt.
  143. */
  144. - (void)testMaximumPendingReceipt {
  145. // Initial empty state.
  146. OCMExpect([_mockKeychain dataForKey:OCMOCK_ANY error:ANY_ERROR_POINTER]).andReturn(nil);
  147. FIRAuthAppCredentialManager *manager =
  148. [[FIRAuthAppCredentialManager alloc] initWithKeychain:_mockKeychain];
  149. XCTAssertNil(manager.credential);
  150. OCMVerifyAll(_mockKeychain);
  151. // Start verification of the target receipt.
  152. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  153. OCMExpect([_mockKeychain setData:OCMOCK_ANY forKey:OCMOCK_ANY error:ANY_ERROR_POINTER])
  154. .andReturn(YES);
  155. [manager didStartVerificationWithReceipt:kReceipt
  156. timeout:kVerificationTimeout
  157. callback:^(FIRAuthAppCredential *credential) {
  158. XCTAssertEqualObjects(credential.receipt, kReceipt);
  159. XCTAssertEqualObjects(credential.secret, kSecret);
  160. [expectation fulfill];
  161. }];
  162. XCTAssertNil(manager.credential);
  163. OCMVerifyAll(_mockKeychain);
  164. // Start verification of a number of random receipts without overflowing.
  165. for (NSUInteger i = 1; i < manager.maximumNumberOfPendingReceipts; i++) {
  166. OCMExpect([_mockKeychain setData:OCMOCK_ANY forKey:OCMOCK_ANY error:ANY_ERROR_POINTER])
  167. .andReturn(YES);
  168. NSString *randomReceipt = [NSString stringWithFormat:@"RANDOM_%lu", (unsigned long)i];
  169. XCTestExpectation *randomExpectation = [self expectationWithDescription:randomReceipt];
  170. [manager didStartVerificationWithReceipt:randomReceipt
  171. timeout:kVerificationTimeout
  172. callback:^(FIRAuthAppCredential *credential) {
  173. // They all should get full credential because one is
  174. // available at this point.
  175. XCTAssertEqualObjects(credential.receipt, kReceipt);
  176. XCTAssertEqualObjects(credential.secret, kSecret);
  177. [randomExpectation fulfill];
  178. }];
  179. }
  180. // Finish verification of target receipt.
  181. OCMExpect([_mockKeychain setData:OCMOCK_ANY forKey:OCMOCK_ANY error:ANY_ERROR_POINTER])
  182. .andReturn(YES);
  183. XCTAssertTrue([manager canFinishVerificationWithReceipt:kReceipt secret:kSecret]);
  184. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  185. XCTAssertNotNil(manager.credential);
  186. XCTAssertEqualObjects(manager.credential.receipt, kReceipt);
  187. XCTAssertEqualObjects(manager.credential.secret, kSecret);
  188. OCMVerifyAll(_mockKeychain);
  189. // Clear credential to prepare for next round.
  190. [manager clearCredential];
  191. XCTAssertNil(manager.credential);
  192. // Start verification of another target receipt.
  193. expectation = [self expectationWithDescription:@"another callback"];
  194. OCMExpect([_mockKeychain setData:OCMOCK_ANY forKey:OCMOCK_ANY error:ANY_ERROR_POINTER])
  195. .andReturn(YES);
  196. [manager didStartVerificationWithReceipt:kAnotherReceipt
  197. timeout:kVerificationTimeout
  198. callback:^(FIRAuthAppCredential *credential) {
  199. XCTAssertEqualObjects(credential.receipt, kAnotherReceipt);
  200. XCTAssertNil(credential.secret);
  201. [expectation fulfill];
  202. }];
  203. XCTAssertNil(manager.credential);
  204. OCMVerifyAll(_mockKeychain);
  205. // Start verification of a number of random receipts to overflow.
  206. for (NSUInteger i = 0; i < manager.maximumNumberOfPendingReceipts; i++) {
  207. OCMExpect([_mockKeychain setData:OCMOCK_ANY forKey:OCMOCK_ANY error:ANY_ERROR_POINTER])
  208. .andReturn(YES);
  209. NSString *randomReceipt = [NSString stringWithFormat:@"RANDOM_%lu", (unsigned long)i];
  210. XCTestExpectation *randomExpectation = [self expectationWithDescription:randomReceipt];
  211. [manager didStartVerificationWithReceipt:randomReceipt
  212. timeout:kVerificationTimeout
  213. callback:^(FIRAuthAppCredential *credential) {
  214. // They all should get partial credential because verification
  215. // has never completed.
  216. XCTAssertEqualObjects(credential.receipt, randomReceipt);
  217. XCTAssertNil(credential.secret);
  218. [randomExpectation fulfill];
  219. }];
  220. }
  221. // Finish verification of the other target receipt.
  222. XCTAssertFalse([manager canFinishVerificationWithReceipt:kAnotherReceipt secret:kAnotherSecret]);
  223. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  224. XCTAssertNil(manager.credential);
  225. }
  226. /** @fn testKeychain
  227. @brief Tests state preservation in the keychain.
  228. */
  229. - (void)testKeychain {
  230. // Initial empty state.
  231. OCMExpect([_mockKeychain dataForKey:OCMOCK_ANY error:ANY_ERROR_POINTER]).andReturn(nil);
  232. FIRAuthAppCredentialManager *manager =
  233. [[FIRAuthAppCredentialManager alloc] initWithKeychain:_mockKeychain];
  234. XCTAssertNil(manager.credential);
  235. OCMVerifyAll(_mockKeychain);
  236. // Start verification.
  237. XCTestExpectation *expectation = [self expectationWithDescription:@"callback"];
  238. __block NSString *key;
  239. __block NSString *data;
  240. OCMExpect([_mockKeychain setData:SAVE_TO(data) forKey:SAVE_TO(key) error:ANY_ERROR_POINTER])
  241. .andReturn(YES);
  242. [manager didStartVerificationWithReceipt:kReceipt
  243. timeout:kVerificationTimeout
  244. callback:^(FIRAuthAppCredential *credential) {
  245. XCTAssertEqualObjects(credential.receipt, kReceipt);
  246. XCTAssertNil(credential.secret);
  247. [expectation fulfill];
  248. }];
  249. XCTAssertNil(manager.credential);
  250. OCMVerifyAll(_mockKeychain);
  251. // Time-out.
  252. [self waitForExpectationsWithTimeout:kExpectationTimeout handler:nil];
  253. XCTAssertNil(manager.credential);
  254. // Start a new manager with saved data in keychain.
  255. OCMExpect([_mockKeychain dataForKey:key error:ANY_ERROR_POINTER]).andReturn(data);
  256. manager = [[FIRAuthAppCredentialManager alloc] initWithKeychain:_mockKeychain];
  257. XCTAssertNil(manager.credential);
  258. OCMVerifyAll(_mockKeychain);
  259. // Finish verification.
  260. OCMExpect([_mockKeychain setData:SAVE_TO(data) forKey:SAVE_TO(key) error:ANY_ERROR_POINTER])
  261. .andReturn(YES);
  262. XCTAssertTrue([manager canFinishVerificationWithReceipt:kReceipt secret:kSecret]);
  263. XCTAssertNotNil(manager.credential);
  264. XCTAssertEqualObjects(manager.credential.receipt, kReceipt);
  265. XCTAssertEqualObjects(manager.credential.secret, kSecret);
  266. OCMVerifyAll(_mockKeychain);
  267. // Start yet another new manager with saved data in keychain.
  268. OCMExpect([_mockKeychain dataForKey:key error:ANY_ERROR_POINTER]).andReturn(data);
  269. manager = [[FIRAuthAppCredentialManager alloc] initWithKeychain:_mockKeychain];
  270. XCTAssertNotNil(manager.credential);
  271. XCTAssertEqualObjects(manager.credential.receipt, kReceipt);
  272. XCTAssertEqualObjects(manager.credential.secret, kSecret);
  273. OCMVerifyAll(_mockKeychain);
  274. }
  275. @end
  276. NS_ASSUME_NONNULL_END