GACAppCheckDebugProviderTests.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2020 Google LLC
  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 "FBLPromise+Testing.h"
  19. #import "AppCheckCore/Sources/DebugProvider/API/GACAppCheckDebugProviderAPIService.h"
  20. #import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckDebugProvider.h"
  21. #import "AppCheckCore/Sources/Public/AppCheckCore/GACAppCheckToken.h"
  22. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  23. static NSString *const kDebugTokenEnvKey = @"FIRAAppCheckDebugToken";
  24. static NSString *const kDebugTokenUserDefaultsKey = @"FIRAAppCheckDebugToken";
  25. @interface GACAppCheckDebugProvider (Tests)
  26. - (instancetype)initWithAPIService:(id<GACAppCheckDebugProviderAPIServiceProtocol>)APIService;
  27. @end
  28. @interface GACAppCheckDebugProviderTests : XCTestCase
  29. @property(nonatomic) GACAppCheckDebugProvider *provider;
  30. @property(nonatomic) id processInfoMock;
  31. @property(nonatomic) id fakeAPIService;
  32. @end
  33. typedef void (^GACAppCheckTokenValidationBlock)(GACAppCheckToken *_Nullable token,
  34. NSError *_Nullable error);
  35. @implementation GACAppCheckDebugProviderTests
  36. - (void)setUp {
  37. self.processInfoMock = OCMPartialMock([NSProcessInfo processInfo]);
  38. self.fakeAPIService = OCMProtocolMock(@protocol(GACAppCheckDebugProviderAPIServiceProtocol));
  39. self.provider = [[GACAppCheckDebugProvider alloc] initWithAPIService:self.fakeAPIService];
  40. }
  41. - (void)tearDown {
  42. self.provider = nil;
  43. [self.processInfoMock stopMocking];
  44. self.processInfoMock = nil;
  45. [[NSUserDefaults standardUserDefaults] removeObjectForKey:kDebugTokenUserDefaultsKey];
  46. }
  47. #pragma mark - Initialization
  48. - (void)testInitWithValidApp {
  49. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"app_id" GCMSenderID:@"sender_id"];
  50. options.APIKey = @"api_key";
  51. options.projectID = @"project_id";
  52. FIRApp *app = [[FIRApp alloc] initInstanceWithName:@"testInitWithValidApp" options:options];
  53. XCTAssertNotNil([[GACAppCheckDebugProvider alloc]
  54. initWithServiceName:options.googleAppID
  55. resourceName:[GACAppCheckDebugProviderTests resourceNameFromApp:app]
  56. APIKey:app.options.APIKey
  57. requestHooks:nil]);
  58. }
  59. #pragma mark - Debug token generating/storing
  60. - (void)testCurrentTokenWhenEnvironmentVariableSetAndTokenStored {
  61. [[NSUserDefaults standardUserDefaults] setObject:@"stored token"
  62. forKey:kDebugTokenUserDefaultsKey];
  63. NSString *envToken = @"env token";
  64. OCMStub([self.processInfoMock processInfo]).andReturn(self.processInfoMock);
  65. OCMExpect([self.processInfoMock environment]).andReturn(@{kDebugTokenEnvKey : envToken});
  66. XCTAssertEqualObjects([self.provider currentDebugToken], envToken);
  67. }
  68. - (void)testCurrentTokenWhenNoEnvironmentVariableAndTokenStored {
  69. NSString *storedToken = @"stored token";
  70. [[NSUserDefaults standardUserDefaults] setObject:storedToken forKey:kDebugTokenUserDefaultsKey];
  71. XCTAssertNil(NSProcessInfo.processInfo.environment[kDebugTokenEnvKey]);
  72. XCTAssertEqualObjects([self.provider currentDebugToken], storedToken);
  73. }
  74. - (void)testCurrentTokenWhenNoEnvironmentVariableAndNoTokenStored {
  75. XCTAssertNil(NSProcessInfo.processInfo.environment[kDebugTokenEnvKey]);
  76. XCTAssertNil([[NSUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey]);
  77. NSString *generatedToken = [self.provider currentDebugToken];
  78. XCTAssertNotNil(generatedToken);
  79. // Check if the generated token is stored to the user defaults.
  80. XCTAssertEqualObjects(
  81. [[NSUserDefaults standardUserDefaults] stringForKey:kDebugTokenUserDefaultsKey],
  82. generatedToken);
  83. // Check if the same token is used once generated.
  84. XCTAssertEqualObjects([self.provider currentDebugToken], generatedToken);
  85. }
  86. #pragma mark - Debug token to FAC token exchange
  87. - (void)testGetTokenSuccess {
  88. // 1. Stub API service.
  89. NSString *expectedDebugToken = [self.provider currentDebugToken];
  90. GACAppCheckToken *validToken = [[GACAppCheckToken alloc] initWithToken:@"valid_token"
  91. expirationDate:[NSDate date]
  92. receivedAtDate:[NSDate date]];
  93. OCMExpect([self.fakeAPIService appCheckTokenWithDebugToken:expectedDebugToken])
  94. .andReturn([FBLPromise resolvedWith:validToken]);
  95. // 2. Validate get token.
  96. [self validateGetToken:^(GACAppCheckToken *_Nullable token, NSError *_Nullable error) {
  97. XCTAssertNil(error);
  98. XCTAssertEqualObjects(token.token, validToken.token);
  99. XCTAssertEqualObjects(token.expirationDate, validToken.expirationDate);
  100. XCTAssertEqualObjects(token.receivedAtDate, validToken.receivedAtDate);
  101. }];
  102. // 3. Verify fakes.
  103. OCMVerifyAll(self.fakeAPIService);
  104. }
  105. - (void)testGetTokenAPIError {
  106. // 1. Stub API service.
  107. NSString *expectedDebugToken = [self.provider currentDebugToken];
  108. NSError *APIError = [NSError errorWithDomain:@"testGetTokenAPIError" code:-1 userInfo:nil];
  109. FBLPromise *rejectedPromise = [FBLPromise pendingPromise];
  110. [rejectedPromise reject:APIError];
  111. OCMExpect([self.fakeAPIService appCheckTokenWithDebugToken:expectedDebugToken])
  112. .andReturn(rejectedPromise);
  113. // 2. Validate get token.
  114. [self validateGetToken:^(GACAppCheckToken *_Nullable token, NSError *_Nullable error) {
  115. XCTAssertEqualObjects(error, APIError);
  116. XCTAssertNil(token);
  117. }];
  118. // 3. Verify fakes.
  119. OCMVerifyAll(self.fakeAPIService);
  120. }
  121. #pragma mark - Helpers
  122. - (void)validateGetToken:(GACAppCheckTokenValidationBlock)validationBlock {
  123. XCTestExpectation *expectation = [self expectationWithDescription:@"getToken"];
  124. [self.provider
  125. getTokenWithCompletion:^(GACAppCheckToken *_Nullable token, NSError *_Nullable error) {
  126. validationBlock(token, error);
  127. [expectation fulfill];
  128. }];
  129. [self waitForExpectations:@[ expectation ] timeout:0.5];
  130. }
  131. // TODO(andrewheard): Remove from generic App Check SDK.
  132. // FIREBASE_APP_CHECK_ONLY_BEGIN
  133. + (NSString *)resourceNameFromApp:(FIRApp *)app {
  134. return [NSString
  135. stringWithFormat:@"projects/%@/apps/%@", app.options.projectID, app.options.googleAppID];
  136. }
  137. // FIREBASE_APP_CHECK_ONLY_END
  138. @end