GIDFakeMainBundle.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. - (void)startFakingWithClientID:(NSString *)clientId {
  32. _clientId = clientId;
  33. _fakedKeys = @[ kCFBundleURLTypesKey,
  34. kConfigClientIDKey,
  35. kConfigServerClientIDKey,
  36. kConfigHostedDomainKey,
  37. kConfigOpenIDRealmKey ];
  38. _fakeConfig = [@{ @"GIDClientID" : clientId } mutableCopy];
  39. [GULSwizzler swizzleClass:[NSBundle class]
  40. selector:@selector(objectForInfoDictionaryKey:)
  41. isClassSelector:NO
  42. withBlock:^id(id _self, NSString *key) {
  43. if ([self->_fakedKeys containsObject:key]) {
  44. return self->_fakeConfig[key];
  45. } else {
  46. @throw [NSException exceptionWithName:@"Requested unexpected info.plist key."
  47. reason:nil
  48. userInfo:nil];
  49. }
  50. }];
  51. }
  52. - (void)stopFaking {
  53. [GULSwizzler unswizzleClass:[NSBundle class]
  54. selector:@selector(objectForInfoDictionaryKey:)
  55. isClassSelector:NO];
  56. _fakeConfig = nil;
  57. }
  58. #pragma mark - Utilities
  59. /**
  60. * @fn reversedClientId
  61. * @return The reversed version of _clientID.
  62. */
  63. - (NSString *)reversedClientId {
  64. NSArray *clientIdComponents = [_clientId.lowercaseString componentsSeparatedByString:@"."];
  65. NSArray *reversedClientIdComponents = [clientIdComponents reverseObjectEnumerator].allObjects;
  66. NSString *reversedClientId = [reversedClientIdComponents componentsJoinedByString:@"."];
  67. return reversedClientId;
  68. }
  69. /**
  70. * @fn stringByFlippingCasesInString:
  71. * @param original The string to flip cases for.
  72. * @return A string with A-Z replaced by a-z and a-z replaced by A-Z.
  73. */
  74. - (NSString *)stringByFlippingCasesInString:(NSString *)original {
  75. const unichar A = 'A';
  76. const unichar Z = 'Z';
  77. const unichar a = 'a';
  78. const unichar z = 'z';
  79. NSMutableString *flipped = [NSMutableString string];
  80. for (unsigned int i = 0; i < original.length; i++) {
  81. unichar c = [original characterAtIndex:i];
  82. if (A <= c && c <= Z) {
  83. c += a - A;
  84. } else if (a <= c && c <= z) {
  85. c -= a - A;
  86. }
  87. [flipped appendString:[NSString stringWithFormat:@"%c", c]];
  88. }
  89. return flipped;
  90. }
  91. #pragma mark - URL Schemes
  92. - (void)fakeAllSchemesSupported {
  93. _fakeConfig[kCFBundleURLTypesKey] = @[
  94. @{
  95. kCFBundleURLSchemesKey : @[ [self reversedClientId] ]
  96. }
  97. ];
  98. }
  99. - (void)fakeAllSchemesSupportedAndMerged {
  100. _fakeConfig[kCFBundleURLTypesKey] = @[
  101. @{
  102. kCFBundleURLSchemesKey : @[
  103. [self reversedClientId]
  104. ]
  105. },
  106. ];
  107. }
  108. - (void)fakeAllSchemesSupportedWithCasesMangled {
  109. NSString *caseFlippedReverseClientId =
  110. [self stringByFlippingCasesInString:[self reversedClientId]];
  111. _fakeConfig[kCFBundleURLTypesKey] = @[
  112. @{
  113. kCFBundleURLSchemesKey : @[ caseFlippedReverseClientId ]
  114. }
  115. ];
  116. }
  117. - (void)fakeMissingClientIdScheme {
  118. [self fakeMissingAllSchemes];
  119. }
  120. - (void)fakeMissingAllSchemes {
  121. _fakeConfig[kCFBundleURLTypesKey] = nil;
  122. }
  123. - (void)fakeOtherSchemes {
  124. _fakeConfig[kCFBundleURLTypesKey] = @[
  125. @{
  126. kCFBundleURLSchemesKey : @[ @"junk" ]
  127. }
  128. ];
  129. }
  130. - (void)fakeOtherSchemesAndAllSchemes {
  131. _fakeConfig[kCFBundleURLTypesKey] = @[
  132. @{
  133. kCFBundleURLSchemesKey : @[ @"junk" ]
  134. },
  135. @{
  136. kCFBundleURLSchemesKey : @[ [self reversedClientId] ]
  137. }
  138. ];
  139. }
  140. - (void)fakeWithClientID:(id)clientID
  141. serverClientID:(id)serverClientID
  142. hostedDomain:(id)hostedDomain
  143. openIDRealm:(id)openIDRealm {
  144. _fakeConfig[kConfigClientIDKey] = clientID;
  145. _fakeConfig[kConfigServerClientIDKey] = serverClientID;
  146. _fakeConfig[kConfigHostedDomainKey] = hostedDomain;
  147. _fakeConfig[kConfigOpenIDRealmKey] = openIDRealm;
  148. }
  149. @end