GIDKeychainHandlerTest.m 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright 2022 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 "GoogleSignIn/Sources/GIDKeychainHandler/Implementations/GIDKeychainHandler.h"
  15. #import <XCTest/XCTest.h>
  16. #import "GoogleSignIn/Tests/Unit/OIDAuthState+Testing.h"
  17. #import "GoogleSignIn/Tests/Unit/OIDTokenResponse+Testing.h"
  18. #ifdef SWIFT_PACKAGE
  19. @import AppAuth;
  20. @import GTMAppAuth;
  21. @import OCMock;
  22. #else
  23. #import <AppAuth/AppAuth.h>
  24. #import <GTMAppAuth/GTMAppAuth.h>
  25. #import <OCMock/OCMock.h>
  26. #endif
  27. static NSTimeInterval const kIDTokenExpiresIn = 100;
  28. @interface GIDKeychainHandlerTest : XCTestCase {
  29. GIDKeychainHandler *_keychainHandler;
  30. id _authorization;
  31. }
  32. @end
  33. @implementation GIDKeychainHandlerTest
  34. - (void)setUp {
  35. _keychainHandler = [[GIDKeychainHandler alloc] init];
  36. _authorization = OCMStrictClassMock([GTMAppAuthFetcherAuthorization class]);
  37. }
  38. - (void)testLoadAuthState {
  39. [_keychainHandler loadAuthState];
  40. [[_authorization verify] authorizationFromKeychainForName:[OCMArg any]
  41. useDataProtectionKeychain:YES];
  42. }
  43. - (void)testSaveAuthState {
  44. NSString *idToken = [self idTokenWithExpiresIn:kIDTokenExpiresIn];
  45. OIDAuthState *authState = [OIDAuthState testInstanceWithIDToken:idToken
  46. accessToken:kAccessToken
  47. accessTokenExpiresIn:kAccessTokenExpiresIn
  48. refreshToken:kRefreshToken];
  49. [_keychainHandler saveAuthState:authState];
  50. [[_authorization verify] saveAuthorization:[OCMArg any]
  51. toKeychainForName:[OCMArg any]
  52. useDataProtectionKeychain:YES];
  53. }
  54. - (void)testRemoveAllKeychainEntries {
  55. [_keychainHandler removeAllKeychainEntries];
  56. [[_authorization verify] removeAuthorizationFromKeychainForName:[OCMArg any]
  57. useDataProtectionKeychain:YES];
  58. }
  59. #pragma mark - Helpers
  60. - (NSString *)idTokenWithExpiresIn:(NSTimeInterval)expiresIn {
  61. // The expireTime should be based on 1970.
  62. NSTimeInterval expireTime = [[NSDate date] timeIntervalSince1970] + expiresIn;
  63. return [OIDTokenResponse idTokenWithSub:kUserID exp:@(expireTime)];
  64. }
  65. @end