GIDFakeMainBundle.m 4.4 KB

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