FIRAuthAppCredentialManagerTests.m 12 KB

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