GIDAppCheckTest.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // Copyright 2023 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <TargetConditionals.h>
  15. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  16. #import <XCTest/XCTest.h>
  17. #import <AppCheckCore/GACAppCheckToken.h>
  18. #import "GoogleSignIn/Sources/GIDAppCheck/Implementations/GIDAppCheck.h"
  19. #import "GoogleSignIn/Sources/GIDAppCheck/Implementations/Fake/GIDAppCheckProviderFake.h"
  20. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDAppCheckError.h"
  21. static NSUInteger const timeout = 1;
  22. static NSString *const kUserDefaultsTestSuiteName = @"GIDAppCheckTestKeySuiteName";
  23. NS_CLASS_AVAILABLE_IOS(14)
  24. @interface GIDAppCheckTest : XCTestCase
  25. @property(nonatomic, strong) NSUserDefaults *userDefaults;
  26. @end
  27. @implementation GIDAppCheckTest
  28. - (void)setUp {
  29. [super setUp];
  30. _userDefaults = [[NSUserDefaults alloc] initWithSuiteName:kUserDefaultsTestSuiteName];
  31. }
  32. - (void)tearDown {
  33. [super tearDown];
  34. [self.userDefaults removeObjectForKey:kGIDAppCheckPreparedKey];
  35. [self.userDefaults removeSuiteNamed:kUserDefaultsTestSuiteName];
  36. }
  37. - (void)testGetLimitedUseTokenFailureReturnsPlaceholder {
  38. XCTestExpectation *tokenFailExpectation =
  39. [self expectationWithDescription:@"App check token fail"];
  40. NSError *expectedError = [NSError errorWithDomain:kGIDAppCheckErrorDomain
  41. code:kGIDAppCheckProviderFakeError
  42. userInfo:nil];
  43. GIDAppCheckProviderFake *fakeProvider =
  44. [[GIDAppCheckProviderFake alloc] initWithAppCheckToken:nil error:expectedError];
  45. GIDAppCheck *appCheck = [[GIDAppCheck alloc] initWithAppCheckProvider:fakeProvider
  46. userDefaults:self.userDefaults];
  47. [appCheck getLimitedUseTokenWithCompletion:^(GACAppCheckToken *token,
  48. NSError * _Nullable error) {
  49. XCTAssertEqualObjects(expectedError, error);
  50. XCTAssertNotNil(token); // If there is an error, we expect a placeholder token
  51. [tokenFailExpectation fulfill];
  52. }];
  53. [self waitForExpectations:@[tokenFailExpectation] timeout:timeout];
  54. }
  55. - (void)testIsPreparedError {
  56. XCTestExpectation *notAlreadyPreparedExpectation =
  57. [self expectationWithDescription:@"App check not already prepared error"];
  58. GACAppCheckToken *expectedToken = [[GACAppCheckToken alloc] initWithToken:@"foo"
  59. expirationDate:[NSDate distantFuture]];
  60. // It doesn't matter what we pass for the error since we will check `isPrepared` and make one
  61. GIDAppCheckProviderFake *fakeProvider =
  62. [[GIDAppCheckProviderFake alloc] initWithAppCheckToken:expectedToken error:nil];
  63. GIDAppCheck *appCheck = [[GIDAppCheck alloc] initWithAppCheckProvider:fakeProvider
  64. userDefaults:self.userDefaults];
  65. [appCheck prepareForAppCheckWithCompletion:^(NSError * _Nullable error) {
  66. XCTAssertNil(error);
  67. [notAlreadyPreparedExpectation fulfill];
  68. }];
  69. [self waitForExpectations:@[notAlreadyPreparedExpectation] timeout:timeout];
  70. XCTestExpectation *alreadyPreparedExpectation =
  71. [self expectationWithDescription:@"App check already prepared error"];
  72. // Should be no error since multiple calls to prepare should be fine.
  73. [appCheck prepareForAppCheckWithCompletion:^(NSError * _Nullable error) {
  74. XCTAssertNil(error);
  75. [alreadyPreparedExpectation fulfill];
  76. }];
  77. [self waitForExpectations:@[alreadyPreparedExpectation] timeout:timeout];
  78. XCTAssertTrue([appCheck isPrepared]);
  79. }
  80. - (void)testGetLimitedUseTokenSucceeds {
  81. XCTestExpectation *prepareExpectation =
  82. [self expectationWithDescription:@"Prepare for App Check expectation"];
  83. GACAppCheckToken *expectedToken = [[GACAppCheckToken alloc] initWithToken:@"foo"
  84. expirationDate:[NSDate distantFuture]];
  85. GIDAppCheckProviderFake *fakeProvider =
  86. [[GIDAppCheckProviderFake alloc] initWithAppCheckToken:expectedToken error:nil];
  87. GIDAppCheck *appCheck = [[GIDAppCheck alloc] initWithAppCheckProvider:fakeProvider
  88. userDefaults:self.userDefaults];
  89. [appCheck prepareForAppCheckWithCompletion:^(NSError * _Nullable error) {
  90. XCTAssertNil(error);
  91. [prepareExpectation fulfill];
  92. }];
  93. // Wait for preparation to complete to test `isPrepared`
  94. [self waitForExpectations:@[prepareExpectation] timeout:timeout];
  95. XCTAssertTrue(appCheck.isPrepared);
  96. XCTestExpectation *getLimitedUseTokenSucceedsExpectation =
  97. [self expectationWithDescription:@"getLimitedUseToken should succeed"];
  98. [appCheck getLimitedUseTokenWithCompletion:^(GACAppCheckToken *token,
  99. NSError * _Nullable error) {
  100. XCTAssertNil(error);
  101. XCTAssertNotNil(token);
  102. XCTAssertEqualObjects(token, expectedToken);
  103. [getLimitedUseTokenSucceedsExpectation fulfill];
  104. }];
  105. [self waitForExpectations:@[getLimitedUseTokenSucceedsExpectation] timeout:timeout];
  106. XCTAssertTrue([appCheck isPrepared]);
  107. }
  108. - (void)testAsyncCompletions {
  109. XCTestExpectation *firstPrepareExpectation =
  110. [self expectationWithDescription:@"First async prepare for App Check expectation"];
  111. XCTestExpectation *secondPrepareExpectation =
  112. [self expectationWithDescription:@"Second async prepare for App Check expectation"];
  113. GACAppCheckToken *expectedToken = [[GACAppCheckToken alloc] initWithToken:@"foo"
  114. expirationDate:[NSDate distantFuture]];
  115. GIDAppCheckProviderFake *fakeProvider =
  116. [[GIDAppCheckProviderFake alloc] initWithAppCheckToken:expectedToken error:nil];
  117. GIDAppCheck *appCheck = [[GIDAppCheck alloc] initWithAppCheckProvider:fakeProvider
  118. userDefaults:self.userDefaults];
  119. dispatch_async(dispatch_get_main_queue(), ^{
  120. [appCheck prepareForAppCheckWithCompletion:^(NSError * _Nullable error) {
  121. XCTAssertNil(error);
  122. [firstPrepareExpectation fulfill];
  123. }];
  124. });
  125. dispatch_async(dispatch_get_main_queue(), ^{
  126. [appCheck prepareForAppCheckWithCompletion:^(NSError * _Nullable error) {
  127. XCTAssertNil(error);
  128. [secondPrepareExpectation fulfill];
  129. }];
  130. });
  131. [self waitForExpectations:@[firstPrepareExpectation, secondPrepareExpectation] timeout:timeout];
  132. XCTAssertTrue([appCheck isPrepared]);
  133. // Simulate requesting later on after `appCheck` is prepared
  134. XCTestExpectation *preparedExpectation =
  135. [self expectationWithDescription:@"Prepared expectation"];
  136. [appCheck prepareForAppCheckWithCompletion:^(NSError * _Nullable error) {
  137. XCTAssertNil(error);
  138. [preparedExpectation fulfill];
  139. }];
  140. [self waitForExpectations:@[preparedExpectation] timeout:timeout];
  141. }
  142. @end
  143. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST