GIDFakeMainBundle.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "GoogleSignIn/Tests/Unit/GIDFakeMainBundle.h"
  15. #import <GoogleUtilities/GULSwizzler.h>
  16. #import <GoogleUtilities/GULSwizzler+Unswizzle.h>
  17. static NSString *const kCFBundleURLTypesKey = @"CFBundleURLTypes";
  18. static NSString *const kCFBundleURLSchemesKey = @"CFBundleURLSchemes";
  19. // Info.plist config keys
  20. static NSString *const kConfigClientIDKey = @"GIDClientID";
  21. static NSString *const kConfigServerClientIDKey = @"GIDServerClientID";
  22. static NSString *const kConfigHostedDomainKey = @"GIDHostedDomain";
  23. static NSString *const kConfigOpenIDRealmKey = @"GIDOpenIDRealm";
  24. @implementation GIDFakeMainBundle {
  25. NSString *_clientId;
  26. // Represents the Info.plist keys to fake.
  27. NSArray *_fakedKeys;
  28. // Represents the values for any Info.plist keys to be faked.
  29. NSMutableDictionary *_fakeConfig;
  30. }
  31. - (instancetype)initWithClientID:(nullable id)clientID
  32. serverClientID:(nullable id)serverClientID
  33. hostedDomain:(nullable id)hostedDomain
  34. openIDRealm:(nullable id)openIDRealm {
  35. self = [super init];
  36. if (self) {
  37. _clientId = clientID;
  38. _fakeConfig = clientID ? [@{ @"GIDClientID" : clientID } mutableCopy] : [@{} mutableCopy];
  39. [self fakeWithClientID:clientID
  40. serverClientID:serverClientID
  41. hostedDomain:hostedDomain
  42. openIDRealm:openIDRealm];
  43. }
  44. return self;
  45. }
  46. - (instancetype)init {
  47. return [self initWithClientID:nil
  48. serverClientID:nil
  49. hostedDomain:nil
  50. openIDRealm:nil];
  51. }
  52. - (void)startFaking {
  53. _fakedKeys = @[ kCFBundleURLTypesKey,
  54. kConfigClientIDKey,
  55. kConfigServerClientIDKey,
  56. kConfigHostedDomainKey,
  57. kConfigOpenIDRealmKey ];
  58. [GULSwizzler swizzleClass:[NSBundle class]
  59. selector:@selector(objectForInfoDictionaryKey:)
  60. isClassSelector:NO
  61. withBlock:^id(id _self, NSString *key) {
  62. if ([self->_fakedKeys containsObject:key]) {
  63. return self->_fakeConfig[key];
  64. } else {
  65. @throw [NSException exceptionWithName:@"Requested unexpected info.plist key."
  66. reason:nil
  67. userInfo:nil];
  68. }
  69. }];
  70. }
  71. - (void)startFakingWithClientID:(nullable NSString *)clientId {
  72. _clientId = clientId;
  73. _fakeConfig = clientId ? [@{ @"GIDClientID" : clientId } mutableCopy] : [@{} mutableCopy];
  74. [self startFaking];
  75. }
  76. - (void)stopFaking {
  77. [GULSwizzler unswizzleClass:[NSBundle class]
  78. selector:@selector(objectForInfoDictionaryKey:)
  79. isClassSelector:NO];
  80. _fakeConfig = nil;
  81. }
  82. #pragma mark - Utilities
  83. /**
  84. * @fn reversedClientId
  85. * @return The reversed version of _clientID.
  86. */
  87. - (NSString *)reversedClientId {
  88. NSArray *clientIdComponents = [_clientId.lowercaseString componentsSeparatedByString:@"."];
  89. NSArray *reversedClientIdComponents = [clientIdComponents reverseObjectEnumerator].allObjects;
  90. NSString *reversedClientId = [reversedClientIdComponents componentsJoinedByString:@"."];
  91. return reversedClientId;
  92. }
  93. /**
  94. * @fn stringByFlippingCasesInString:
  95. * @param original The string to flip cases for.
  96. * @return A string with A-Z replaced by a-z and a-z replaced by A-Z.
  97. */
  98. - (NSString *)stringByFlippingCasesInString:(NSString *)original {
  99. const unichar A = 'A';
  100. const unichar Z = 'Z';
  101. const unichar a = 'a';
  102. const unichar z = 'z';
  103. NSMutableString *flipped = [NSMutableString string];
  104. for (unsigned int i = 0; i < original.length; i++) {
  105. unichar c = [original characterAtIndex:i];
  106. if (A <= c && c <= Z) {
  107. c += a - A;
  108. } else if (a <= c && c <= z) {
  109. c -= a - A;
  110. }
  111. [flipped appendString:[NSString stringWithFormat:@"%c", c]];
  112. }
  113. return flipped;
  114. }
  115. #pragma mark - URL Schemes
  116. - (void)fakeAllSchemesSupported {
  117. _fakeConfig[kCFBundleURLTypesKey] = @[
  118. @{
  119. kCFBundleURLSchemesKey : @[ [self reversedClientId] ]
  120. }
  121. ];
  122. }
  123. - (void)fakeAllSchemesSupportedAndMerged {
  124. _fakeConfig[kCFBundleURLTypesKey] = @[
  125. @{
  126. kCFBundleURLSchemesKey : @[
  127. [self reversedClientId]
  128. ]
  129. },
  130. ];
  131. }
  132. - (void)fakeAllSchemesSupportedWithCasesMangled {
  133. NSString *caseFlippedReverseClientId =
  134. [self stringByFlippingCasesInString:[self reversedClientId]];
  135. _fakeConfig[kCFBundleURLTypesKey] = @[
  136. @{
  137. kCFBundleURLSchemesKey : @[ caseFlippedReverseClientId ]
  138. }
  139. ];
  140. }
  141. - (void)fakeMissingClientIdScheme {
  142. [self fakeMissingAllSchemes];
  143. }
  144. - (void)fakeMissingAllSchemes {
  145. _fakeConfig[kCFBundleURLTypesKey] = nil;
  146. }
  147. - (void)fakeOtherSchemes {
  148. _fakeConfig[kCFBundleURLTypesKey] = @[
  149. @{
  150. kCFBundleURLSchemesKey : @[ @"junk" ]
  151. }
  152. ];
  153. }
  154. - (void)fakeOtherSchemesAndAllSchemes {
  155. _fakeConfig[kCFBundleURLTypesKey] = @[
  156. @{
  157. kCFBundleURLSchemesKey : @[ @"junk" ]
  158. },
  159. @{
  160. kCFBundleURLSchemesKey : @[ [self reversedClientId] ]
  161. }
  162. ];
  163. }
  164. - (void)fakeWithClientID:(id)clientID
  165. serverClientID:(id)serverClientID
  166. hostedDomain:(id)hostedDomain
  167. openIDRealm:(id)openIDRealm {
  168. _fakeConfig[kConfigClientIDKey] = clientID;
  169. _fakeConfig[kConfigServerClientIDKey] = serverClientID;
  170. _fakeConfig[kConfigHostedDomainKey] = hostedDomain;
  171. _fakeConfig[kConfigOpenIDRealmKey] = openIDRealm;
  172. }
  173. @end