GIDFakeMainBundle.m 4.4 KB

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