GIDMDMPasscodeStateTests.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <Foundation/Foundation.h>
  15. #import <LocalAuthentication/LocalAuthentication.h>
  16. #import <UIKit/UIKit.h>
  17. #import <XCTest/XCTest.h>
  18. #import "GoogleSignIn/Sources/GIDMDMPasscodeState.h"
  19. #import <GoogleUtilities/GULSwizzler.h>
  20. #import <GoogleUtilities/GULSwizzler+Unswizzle.h>
  21. @interface GIDMDMPasscodeStateTests : XCTestCase
  22. @end
  23. @implementation GIDMDMPasscodeStateTests {
  24. /** Whether or not the iOS version is equal or greater than 9.0. */
  25. BOOL _isIOS9orAbove;
  26. /** Whether or not `canEvaluatePolicy:error:` method has been called. */
  27. BOOL _canEvaluatePolicyCalled;
  28. /** The next result to be returned from the `canEvaluatePolicy:error:` method. */
  29. BOOL _nextCanEvaluatePolicyResult;
  30. /** The next error to be returned from the `canEvaluatePolicy:error:` method. */
  31. NSError *_nextCanEvaluatePolicyError;
  32. }
  33. - (void)setUp {
  34. [super setUp];
  35. _isIOS9orAbove = [[NSProcessInfo processInfo]
  36. isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){.majorVersion = 9}];
  37. if (!_isIOS9orAbove) {
  38. return;
  39. }
  40. _canEvaluatePolicyCalled = NO;
  41. id canEvaluatePolicyError = ^BOOL(id context, LAPolicy policy, NSError * _Nullable *error) {
  42. _canEvaluatePolicyCalled = YES;
  43. XCTAssertEqual(policy, LAPolicyDeviceOwnerAuthentication);
  44. if (error) {
  45. *error = _nextCanEvaluatePolicyError;
  46. }
  47. return _nextCanEvaluatePolicyResult;
  48. };
  49. [GULSwizzler swizzleClass:[LAContext class]
  50. selector:@selector(canEvaluatePolicy:error:)
  51. isClassSelector:NO
  52. withBlock:canEvaluatePolicyError];
  53. [self postApplicationDidEnterBackgroundNotification];
  54. }
  55. - (void)tearDown {
  56. if (!_isIOS9orAbove) {
  57. return;
  58. }
  59. [GULSwizzler unswizzleClass:[LAContext class]
  60. selector:@selector(canEvaluatePolicy:error:)
  61. isClassSelector:NO];
  62. }
  63. /**
  64. * Verifies the correct response when LocalAuthentication API returns without an error.
  65. */
  66. - (void)testLocalAuthenticationNoError {
  67. if (!_isIOS9orAbove) {
  68. return;
  69. }
  70. _nextCanEvaluatePolicyResult = YES;
  71. _nextCanEvaluatePolicyError = nil;
  72. GIDMDMPasscodeState *passcodeState = [GIDMDMPasscodeState passcodeState];
  73. XCTAssertTrue(_canEvaluatePolicyCalled);
  74. XCTAssertEqualObjects(passcodeState.status, @"YES");
  75. NSDictionary *dict = [self dictWithEncodedString:passcodeState.info];
  76. [self assertJSONNumber:dict[@"LocalAuthentication"][@"result"] isInteger:1];
  77. XCTAssertNil(dict[@"LocalAuthentication"][@"error_domain"]);
  78. XCTAssertNil(dict[@"LocalAuthentication"][@"error_code"]);
  79. }
  80. /**
  81. * Verifies the correct response when LocalAuthentication API returns with an error.
  82. */
  83. - (void)testLocalAuthenticationHasError {
  84. if (!_isIOS9orAbove) {
  85. return;
  86. }
  87. NSString *fakeErrorDomain = @"asdf.hjkl";
  88. NSInteger fakeErrorCode = -12345;
  89. _nextCanEvaluatePolicyResult = NO;
  90. _nextCanEvaluatePolicyError = [NSError errorWithDomain:fakeErrorDomain
  91. code:fakeErrorCode
  92. userInfo:nil];
  93. GIDMDMPasscodeState *passcodeState = [GIDMDMPasscodeState passcodeState];
  94. XCTAssertTrue(_canEvaluatePolicyCalled);
  95. XCTAssertEqualObjects(passcodeState.status, @"NO");
  96. NSDictionary *dict = [self dictWithEncodedString:passcodeState.info];
  97. [self assertJSONNumber:dict[@"LocalAuthentication"][@"result"] isInteger:0];
  98. XCTAssertEqualObjects(dict[@"LocalAuthentication"][@"error_domain"], fakeErrorDomain);
  99. XCTAssertEqualObjects(dict[@"LocalAuthentication"][@"error_code"], @(fakeErrorCode));
  100. }
  101. /**
  102. * Verifies caching behavior regarding to calling LocalAuthentication API.
  103. */
  104. - (void)testLocalAuthenticationCache {
  105. if (!_isIOS9orAbove) {
  106. return;
  107. }
  108. GIDMDMPasscodeState *oldPasscodeState = [GIDMDMPasscodeState passcodeState];
  109. _canEvaluatePolicyCalled = false;
  110. GIDMDMPasscodeState *newPasscodeState = [GIDMDMPasscodeState passcodeState];
  111. XCTAssertFalse(_canEvaluatePolicyCalled);
  112. XCTAssertEqualObjects(oldPasscodeState.status, newPasscodeState.status);
  113. XCTAssertEqualObjects(oldPasscodeState.info, newPasscodeState.info);
  114. // Verify that the cache is cleared after background notification.
  115. [self postApplicationDidEnterBackgroundNotification];
  116. [GIDMDMPasscodeState passcodeState];
  117. XCTAssertTrue(_canEvaluatePolicyCalled);
  118. }
  119. /**
  120. * Verifies the presence of the result from Keychain API.
  121. * Keychain API is in C thus there is no easy way to swizzler them.
  122. */
  123. - (void)testKeychain {
  124. GIDMDMPasscodeState *passcodeState = [GIDMDMPasscodeState passcodeState];
  125. NSDictionary *dict = [self dictWithEncodedString:passcodeState.info];
  126. XCTAssertTrue([dict[@"Keychain"][@"result"] isKindOfClass:[NSNumber class]]);
  127. }
  128. #pragma mark - Helpers
  129. /**
  130. * Posts `UIApplicationDidEnterBackgroundNotification` notification.
  131. */
  132. - (void)postApplicationDidEnterBackgroundNotification {
  133. [[NSNotificationCenter defaultCenter]
  134. postNotificationName:UIApplicationDidEnterBackgroundNotification object:nil];
  135. }
  136. - (NSDictionary *)dictWithEncodedString:(NSString *)string {
  137. string = [string stringByReplacingOccurrencesOfString:@"_" withString:@"/"];
  138. string = [string stringByReplacingOccurrencesOfString:@"-" withString:@"+"];
  139. NSData *data = [[NSData alloc] initWithBase64EncodedString:string options:0];
  140. XCTAssertNotNil(data);
  141. id dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
  142. XCTAssertTrue([dictionary isKindOfClass:[NSDictionary class]]);
  143. return (NSDictionary *)dictionary;
  144. }
  145. /**
  146. * Asserts that the given number is the integer by both value and type.
  147. */
  148. - (void)assertJSONNumber:(NSNumber *)number isInteger:(int)integer {
  149. XCTAssertTrue([number isKindOfClass:[NSNumber class]]);
  150. XCTAssertEqual([number intValue], integer);
  151. NSString *objcType = [NSString stringWithUTF8String:[number objCType]];
  152. // Depends on iOS version, numbers from JSON can be either "int" or "long long".
  153. XCTAssertTrue([objcType isEqualToString:@"i"] || [objcType isEqualToString:@"q"],
  154. @"unrecognized objcType: %@", objcType);
  155. }
  156. @end