GIDMDMPasscodeStateTests.m 6.6 KB

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