GIDAuthStateMigrationTest.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // Copyright 2021 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 <XCTest/XCTest.h>
  15. #import "GoogleSignIn/Sources/GIDAuthStateMigration.h"
  16. #import "GoogleSignIn/Sources/GIDSignInCallbackSchemes.h"
  17. #import <AppAuth/AppAuth.h>
  18. #import <GTMAppAuth/GTMAppAuthFetcherAuthorization+Keychain.h>
  19. #import <GTMAppAuth/GTMKeychain.h>
  20. #import <GTMAppAuth/GTMOAuth2KeychainCompatibility.h>
  21. #import <OCMock/OCMock.h>
  22. static NSString *const kTokenURL = @"https://host.com/example/token/url";
  23. static NSString *const kCallbackPath = @"/callback/path";
  24. static NSString *const kKeychainName = @"keychain_name";
  25. static NSString *const kBundleID = @"com.google.GoogleSignInInternalSample.dev";
  26. static NSString *const kClientID =
  27. @"223520599684-kg64hfn0h950oureqacja2fltg00msv3.apps.googleusercontent.com";
  28. static NSString *const kDotReversedClientID =
  29. @"com.googleusercontent.apps.223520599684-kg64hfn0h950oureqacja2fltg00msv3";
  30. static NSString *const kSavedFingerprint = @"com.google.GoogleSignInInternalSample.dev-"
  31. "223520599684-kg64hfn0h950oureqacja2fltg00msv3.apps.googleusercontent.com-email profile";
  32. static NSString *const kSavedFingerprint_HostedDomain =
  33. @"com.google.GoogleSignInInternalSample.dev-"
  34. "223520599684-kg64hfn0h950oureqacja2fltg00msv3.apps.googleusercontent.com-email profile-"
  35. "hd=test.com";
  36. static NSString *const kGTMOAuth2PersistenceString = @"param1=value1&param2=value2";
  37. static NSString *const kAdditionalTokenRequestParametersPostfix = @"~~atrp";
  38. static NSString *const kAdditionalTokenRequestParameters = @"param3=value3&param4=value4";
  39. static NSString *const kFinalPersistenceString =
  40. @"param1=value1&param2=value2&param3=value3&param4=value4";
  41. static NSString *const kRedirectURI =
  42. @"com.googleusercontent.apps.223520599684-kg64hfn0h950oureqacja2fltg00msv3:/callback/path";
  43. static NSString *const kMigrationCheckPerformedKey = @"GID_MigrationCheckPerformed";
  44. static NSString *const kFingerprintService = @"fingerprint";
  45. NS_ASSUME_NONNULL_BEGIN
  46. @interface GIDAuthStateMigration ()
  47. + (nullable GTMAppAuthFetcherAuthorization *)
  48. extractAuthorizationWithTokenURL:(NSURL *)tokenURL callbackPath:(NSString *)callbackPath;
  49. + (nullable NSString *)passwordForService:(NSString *)service;
  50. @end
  51. @interface GIDAuthStateMigrationTest : XCTestCase
  52. @end
  53. @implementation GIDAuthStateMigrationTest {
  54. id _mockUserDefaults;
  55. id _mockGTMAppAuthFetcherAuthorization;
  56. id _mockGIDAuthStateMigration;
  57. id _mockGTMKeychain;
  58. id _mockNSBundle;
  59. id _mockGIDSignInCallbackSchemes;
  60. id _mockGTMOAuth2KeychainCompatibility;
  61. }
  62. - (void)setUp {
  63. [super setUp];
  64. _mockUserDefaults = OCMStrictClassMock([NSUserDefaults class]);
  65. _mockGTMAppAuthFetcherAuthorization = OCMStrictClassMock([GTMAppAuthFetcherAuthorization class]);
  66. _mockGIDAuthStateMigration = OCMStrictClassMock([GIDAuthStateMigration class]);
  67. _mockGTMKeychain = OCMStrictClassMock([GTMKeychain class]);
  68. _mockNSBundle = OCMStrictClassMock([NSBundle class]);
  69. _mockGIDSignInCallbackSchemes = OCMStrictClassMock([GIDSignInCallbackSchemes class]);
  70. _mockGTMOAuth2KeychainCompatibility = OCMStrictClassMock([GTMOAuth2KeychainCompatibility class]);
  71. }
  72. - (void)tearDown {
  73. [_mockUserDefaults verify];
  74. [_mockUserDefaults stopMocking];
  75. [_mockGTMAppAuthFetcherAuthorization verify];
  76. [_mockGTMAppAuthFetcherAuthorization stopMocking];
  77. [_mockGIDAuthStateMigration verify];
  78. [_mockGIDAuthStateMigration stopMocking];
  79. [_mockGTMKeychain verify];
  80. [_mockGTMKeychain stopMocking];
  81. [_mockNSBundle verify];
  82. [_mockNSBundle stopMocking];
  83. [_mockGIDSignInCallbackSchemes verify];
  84. [_mockGIDSignInCallbackSchemes stopMocking];
  85. [_mockGTMOAuth2KeychainCompatibility verify];
  86. [_mockGTMOAuth2KeychainCompatibility stopMocking];
  87. [super tearDown];
  88. }
  89. #pragma mark - Tests
  90. - (void)testMigrateIfNeeded_NoPreviousMigration {
  91. [[[_mockUserDefaults stub] andReturn:_mockUserDefaults] standardUserDefaults];
  92. [[[_mockUserDefaults expect] andReturnValue:@NO]
  93. boolForKey:kMigrationCheckPerformedKey];
  94. [[[_mockGIDAuthStateMigration expect] andReturn:_mockGTMAppAuthFetcherAuthorization]
  95. extractAuthorizationWithTokenURL:[NSURL URLWithString:kTokenURL] callbackPath:kCallbackPath];
  96. [[[_mockGTMAppAuthFetcherAuthorization expect] andReturnValue:@YES]
  97. saveAuthorization:_mockGTMAppAuthFetcherAuthorization toKeychainForName:kKeychainName];
  98. [[_mockUserDefaults expect] setBool:YES forKey:kMigrationCheckPerformedKey];
  99. [GIDAuthStateMigration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
  100. callbackPath:kCallbackPath
  101. keychainName:kKeychainName
  102. isFreshInstall:NO];
  103. }
  104. - (void)testMigrateIfNeeded_HasPreviousMigration {
  105. [[[_mockUserDefaults stub] andReturn:_mockUserDefaults] standardUserDefaults];
  106. [[[_mockUserDefaults expect] andReturnValue:@YES]
  107. boolForKey:kMigrationCheckPerformedKey];
  108. [GIDAuthStateMigration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
  109. callbackPath:kCallbackPath
  110. keychainName:kKeychainName
  111. isFreshInstall:NO];
  112. }
  113. - (void)testMigrateIfNeeded_KeychainFailure {
  114. [[[_mockUserDefaults stub] andReturn:_mockUserDefaults] standardUserDefaults];
  115. [[[_mockUserDefaults expect] andReturnValue:@NO]
  116. boolForKey:kMigrationCheckPerformedKey];
  117. [[[_mockGIDAuthStateMigration expect] andReturn:_mockGTMAppAuthFetcherAuthorization]
  118. extractAuthorizationWithTokenURL:[NSURL URLWithString:kTokenURL] callbackPath:kCallbackPath];
  119. [[[_mockGTMAppAuthFetcherAuthorization expect] andReturnValue:[NSNumber numberWithBool:NO]]
  120. saveAuthorization:_mockGTMAppAuthFetcherAuthorization toKeychainForName:kKeychainName];
  121. [GIDAuthStateMigration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
  122. callbackPath:kCallbackPath
  123. keychainName:kKeychainName
  124. isFreshInstall:NO];
  125. }
  126. - (void)testMigrateIfNeeded_isFreshInstall {
  127. [[[_mockUserDefaults stub] andReturn:_mockUserDefaults] standardUserDefaults];
  128. [[[_mockUserDefaults expect] andReturnValue:@NO]
  129. boolForKey:kMigrationCheckPerformedKey];
  130. [[_mockUserDefaults expect] setBool:YES forKey:kMigrationCheckPerformedKey];
  131. [GIDAuthStateMigration migrateIfNeededWithTokenURL:[NSURL URLWithString:kTokenURL]
  132. callbackPath:kCallbackPath
  133. keychainName:kKeychainName
  134. isFreshInstall:YES];
  135. }
  136. - (void)testExtractAuthorization {
  137. [self extractAuthorizationWithFingerprint:kSavedFingerprint];
  138. }
  139. - (void)testExtractAuthorization_HostedDomain {
  140. [self extractAuthorizationWithFingerprint:kSavedFingerprint_HostedDomain];
  141. }
  142. #pragma mark - Helpers
  143. // Generate the service name for the stored additional token request parameters string.
  144. - (NSString *)additionalTokenRequestParametersKeyFromFingerprint:(NSString *)fingerprint {
  145. return [NSString stringWithFormat:@"%@%@", fingerprint, kAdditionalTokenRequestParametersPostfix];
  146. }
  147. // The parameterized extractAuthorization test.
  148. - (void)extractAuthorizationWithFingerprint:(NSString *)fingerprint {
  149. [[[_mockGIDAuthStateMigration expect] andReturn:fingerprint]
  150. passwordForService:kFingerprintService];
  151. [[[_mockGTMKeychain expect] andReturn:kGTMOAuth2PersistenceString]
  152. passwordFromKeychainForName:fingerprint];
  153. [[[_mockNSBundle expect] andReturn:_mockNSBundle] mainBundle];
  154. [[[_mockNSBundle expect] andReturn:kBundleID] bundleIdentifier];
  155. [[[_mockGIDSignInCallbackSchemes expect] andReturn:_mockGIDSignInCallbackSchemes] alloc];
  156. (void)[[[_mockGIDSignInCallbackSchemes expect] andReturn:_mockGIDSignInCallbackSchemes]
  157. initWithClientIdentifier:kClientID];
  158. [[[_mockGIDSignInCallbackSchemes expect] andReturn:kDotReversedClientID] clientIdentifierScheme];
  159. [[[_mockGIDAuthStateMigration expect] andReturn:kAdditionalTokenRequestParameters]
  160. passwordForService:[self additionalTokenRequestParametersKeyFromFingerprint:fingerprint]];
  161. [[[_mockGTMOAuth2KeychainCompatibility expect] andReturn:_mockGTMAppAuthFetcherAuthorization]
  162. authorizeFromPersistenceString:kFinalPersistenceString
  163. tokenURL:[NSURL URLWithString:kTokenURL]
  164. redirectURI:kRedirectURI
  165. clientID:kClientID
  166. clientSecret:nil];
  167. GTMAppAuthFetcherAuthorization *authorization =
  168. [GIDAuthStateMigration extractAuthorizationWithTokenURL:[NSURL URLWithString:kTokenURL]
  169. callbackPath:kCallbackPath];
  170. XCTAssertNotNil(authorization);
  171. }
  172. @end
  173. NS_ASSUME_NONNULL_END