GIDEMMSupportTest.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2022 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 <TargetConditionals.h>
  17. #if TARGET_OS_IOS && !TARGET_OS_MACCATALYST
  18. #import <XCTest/XCTest.h>
  19. #import "GoogleSignIn/Sources/GIDEMMSupport.h"
  20. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignIn.h"
  21. #import "GoogleSignIn/Sources/GIDEMMErrorHandler.h"
  22. #import "GoogleSignIn/Sources/GIDMDMPasscodeState.h"
  23. #ifdef SWIFT_PACKAGE
  24. @import AppAuth;
  25. @import GoogleUtilities_MethodSwizzler;
  26. @import GoogleUtilities_SwizzlerTestHelpers;
  27. @import OCMock;
  28. #else
  29. #import <GoogleUtilities/GULSwizzler.h>
  30. #import <GoogleUtilities/GULSwizzler+Unswizzle.h>
  31. #import <AppAuth/OIDError.h>
  32. #import <OCMock/OCMock.h>
  33. #endif
  34. NS_ASSUME_NONNULL_BEGIN
  35. // The system name in old iOS versions.
  36. static NSString *const kOldIOSName = @"iPhone OS";
  37. // The system name in new iOS versions.
  38. static NSString *const kNewIOSName = @"iOS";
  39. // They keys in EMM dictionary.
  40. static NSString *const kEMMKey = @"emm_support";
  41. static NSString *const kDeviceOSKey = @"device_os";
  42. static NSString *const kEMMPasscodeInfoKey = @"emm_passcode_info";
  43. @interface GIDEMMSupportTest : XCTestCase
  44. @end
  45. @implementation GIDEMMSupportTest
  46. - (void)testUpdatedEMMParametersWithParameters_NoEMMKey {
  47. NSDictionary *originalParameters = @{
  48. @"not_emm_support_key" : @"xyz",
  49. };
  50. NSDictionary *updatedEMMParameters =
  51. [GIDEMMSupport updatedEMMParametersWithParameters:originalParameters];
  52. XCTAssertEqualObjects(updatedEMMParameters, originalParameters);
  53. }
  54. - (void)testUpdateEMMParametersWithParameters_systemName {
  55. [GULSwizzler swizzleClass:[UIDevice class]
  56. selector:@selector(systemName)
  57. isClassSelector:NO
  58. withBlock:^(id sender) { return kNewIOSName; }];
  59. NSDictionary *originalParameters = @{
  60. kEMMKey : @"xyz",
  61. };
  62. NSDictionary *updatedEMMParameters =
  63. [GIDEMMSupport updatedEMMParametersWithParameters:originalParameters];
  64. NSDictionary *expectedParameters = @{
  65. kEMMKey : @"xyz",
  66. kDeviceOSKey : [NSString stringWithFormat:@"%@ %@", kNewIOSName, [self systemVersion]]
  67. };
  68. XCTAssertEqualObjects(updatedEMMParameters, expectedParameters);
  69. [self addTeardownBlock:^{
  70. [GULSwizzler unswizzleClass:[UIDevice class]
  71. selector:@selector(systemName)
  72. isClassSelector:NO];
  73. }];
  74. }
  75. // When the systemName is @"iPhone OS" we still get "iOS".
  76. - (void)testUpdateEMMParametersWithParameters_systemNameNormalization {
  77. [GULSwizzler swizzleClass:[UIDevice class]
  78. selector:@selector(systemName)
  79. isClassSelector:NO
  80. withBlock:^(id sender) { return kOldIOSName; }];
  81. NSDictionary *originalParameters = @{
  82. kEMMKey : @"xyz",
  83. };
  84. NSDictionary *updatedEMMParameters =
  85. [GIDEMMSupport updatedEMMParametersWithParameters:originalParameters];
  86. NSDictionary *expectedParameters = @{
  87. kEMMKey : @"xyz",
  88. kDeviceOSKey : [NSString stringWithFormat:@"%@ %@", kNewIOSName, [self systemVersion]]
  89. };
  90. XCTAssertEqualObjects(updatedEMMParameters, expectedParameters);
  91. [self addTeardownBlock:^{
  92. [GULSwizzler unswizzleClass:[UIDevice class]
  93. selector:@selector(systemName)
  94. isClassSelector:NO];
  95. }];
  96. }
  97. - (void)testUpdateEMMParametersWithParameters_passcodInfo {
  98. [GULSwizzler swizzleClass:[UIDevice class]
  99. selector:@selector(systemName)
  100. isClassSelector:NO
  101. withBlock:^(id sender) { return kOldIOSName; }];
  102. NSDictionary *originalParameters = @{
  103. kEMMKey : @"xyz",
  104. kDeviceOSKey : @"old one",
  105. kEMMPasscodeInfoKey : @"something",
  106. };
  107. NSDictionary *updatedEMMParameters =
  108. [GIDEMMSupport updatedEMMParametersWithParameters:originalParameters];
  109. NSDictionary *expectedParameters = @{
  110. kEMMKey : @"xyz",
  111. kDeviceOSKey : [NSString stringWithFormat:@"%@ %@", kNewIOSName, [self systemVersion]],
  112. kEMMPasscodeInfoKey : [GIDMDMPasscodeState passcodeState].info,
  113. };
  114. XCTAssertEqualObjects(updatedEMMParameters, expectedParameters);
  115. [self addTeardownBlock:^{
  116. [GULSwizzler unswizzleClass:[UIDevice class]
  117. selector:@selector(systemName)
  118. isClassSelector:NO];
  119. }];
  120. }
  121. - (void)testHandleTokenFetchEMMError_errorIsEMM {
  122. // Set expectations.
  123. NSDictionary *errorJSON = @{ @"error" : @"EMM Specific Error" };
  124. NSError *emmError = [NSError errorWithDomain:@"anydomain"
  125. code:12345
  126. userInfo:@{ OIDOAuthErrorResponseErrorKey : errorJSON }];
  127. id mockEMMErrorHandler = OCMStrictClassMock([GIDEMMErrorHandler class]);
  128. [[[mockEMMErrorHandler stub] andReturn:mockEMMErrorHandler] sharedInstance];
  129. __block void (^savedCompletion)(void);
  130. [[[mockEMMErrorHandler stub] andReturnValue:@YES]
  131. handleErrorFromResponse:errorJSON completion:[OCMArg checkWithBlock:^(id arg) {
  132. savedCompletion = arg;
  133. return YES;
  134. }]];
  135. XCTestExpectation *notCalled = [self expectationWithDescription:@"Callback is not called"];
  136. notCalled.inverted = YES;
  137. XCTestExpectation *called = [self expectationWithDescription:@"Callback is called"];
  138. [GIDEMMSupport handleTokenFetchEMMError:emmError completion:^(NSError *error) {
  139. [notCalled fulfill];
  140. [called fulfill];
  141. XCTAssertEqualObjects(error.domain, kGIDSignInErrorDomain);
  142. XCTAssertEqual(error.code, kGIDSignInErrorCodeEMM);
  143. }];
  144. [self waitForExpectations:@[ notCalled ] timeout:1];
  145. savedCompletion();
  146. [self waitForExpectations:@[ called ] timeout:1];
  147. }
  148. - (void)testHandleTokenFetchEMMError_errorIsNotEMM {
  149. // Set expectations.
  150. NSDictionary *errorJSON = @{ @"error" : @"Not EMM Specific Error" };
  151. NSError *emmError = [NSError errorWithDomain:@"anydomain"
  152. code:12345
  153. userInfo:@{ OIDOAuthErrorResponseErrorKey : errorJSON }];
  154. id mockEMMErrorHandler = OCMStrictClassMock([GIDEMMErrorHandler class]);
  155. [[[mockEMMErrorHandler stub] andReturn:mockEMMErrorHandler] sharedInstance];
  156. __block void (^savedCompletion)(void);
  157. [[[mockEMMErrorHandler stub] andReturnValue:@NO]
  158. handleErrorFromResponse:errorJSON completion:[OCMArg checkWithBlock:^(id arg) {
  159. savedCompletion = arg;
  160. return YES;
  161. }]];
  162. XCTestExpectation *notCalled = [self expectationWithDescription:@"Callback is not called"];
  163. notCalled.inverted = YES;
  164. XCTestExpectation *called = [self expectationWithDescription:@"Callback is called"];
  165. [GIDEMMSupport handleTokenFetchEMMError:emmError completion:^(NSError *error) {
  166. [notCalled fulfill];
  167. [called fulfill];
  168. XCTAssertEqualObjects(error.domain, @"anydomain");
  169. XCTAssertEqual(error.code, 12345);
  170. }];
  171. [self waitForExpectations:@[ notCalled ] timeout:1];
  172. savedCompletion();
  173. [self waitForExpectations:@[ called ] timeout:1];
  174. }
  175. # pragma mark - Helpers
  176. - (NSString *)systemVersion {
  177. return [UIDevice currentDevice].systemVersion;
  178. }
  179. @end
  180. NS_ASSUME_NONNULL_END
  181. #endif // TARGET_OS_IOS && !TARGET_OS_MACCATALYST