FIRAppTest.m 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. // Copyright 2017 Google
  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 "FIRAppInternal.h"
  15. #import "FIROptionsInternal.h"
  16. #import "FIRTestCase.h"
  17. NSString *const kFIRTestAppName1 = @"test_app_name_1";
  18. NSString *const kFIRTestAppName2 = @"test-app-name-2";
  19. @interface FIRApp (TestInternal)
  20. @property(nonatomic) BOOL alreadySentConfigureNotification;
  21. @property(nonatomic) BOOL alreadySentDeleteNotification;
  22. + (void)resetApps;
  23. - (instancetype)initInstanceWithName:(NSString *)name options:(FIROptions *)options;
  24. - (BOOL)configureCore;
  25. + (NSError *)errorForInvalidAppID;
  26. - (BOOL)isAppIDValid;
  27. + (NSString *)actualBundleID;
  28. + (NSNumber *)mapFromServiceStringToTypeEnum:(NSString *)serviceString;
  29. + (NSString *)deviceModel;
  30. + (NSString *)installString;
  31. + (NSURL *)filePathURLWithName:(NSString *)fileName;
  32. + (NSString *)stringAtURL:(NSURL *)filePathURL;
  33. + (BOOL)writeString:(NSString *)string toURL:(NSURL *)filePathURL;
  34. + (void)logAppInfo:(NSNotification *)notification;
  35. + (BOOL)validateAppID:(NSString *)appID;
  36. + (BOOL)validateAppIDFormat:(NSString *)appID withVersion:(NSString *)version;
  37. + (BOOL)validateAppIDFingerprint:(NSString *)appID withVersion:(NSString *)version;
  38. @end
  39. @interface FIRAppTest : FIRTestCase
  40. @property(nonatomic) id appClassMock;
  41. @property(nonatomic) id optionsInstanceMock;
  42. @property(nonatomic) id notificationCenterMock;
  43. @property(nonatomic) FIRApp *app;
  44. @end
  45. @implementation FIRAppTest
  46. - (void)setUp {
  47. [super setUp];
  48. [FIROptions resetDefaultOptions];
  49. [FIRApp resetApps];
  50. _appClassMock = OCMClassMock([FIRApp class]);
  51. _optionsInstanceMock = OCMPartialMock([FIROptions defaultOptions]);
  52. _notificationCenterMock = OCMPartialMock([NSNotificationCenter defaultCenter]);
  53. }
  54. - (void)tearDown {
  55. [_appClassMock stopMocking];
  56. [_optionsInstanceMock stopMocking];
  57. [_notificationCenterMock stopMocking];
  58. [super tearDown];
  59. }
  60. - (void)testConfigure {
  61. NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName
  62. isDefaultApp:YES];
  63. OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
  64. object:[FIRApp class]
  65. userInfo:expectedUserInfo]);
  66. XCTAssertNoThrow([FIRApp configure]);
  67. OCMVerifyAll(self.notificationCenterMock);
  68. self.app = [FIRApp defaultApp];
  69. XCTAssertNotNil(self.app);
  70. XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  71. XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  72. XCTAssertTrue([FIRApp allApps].count == 1);
  73. XCTAssertTrue(self.app.alreadySentConfigureNotification);
  74. // Test if options is nil
  75. id optionsClassMock = OCMClassMock([FIROptions class]);
  76. OCMStub([optionsClassMock defaultOptions]).andReturn(nil);
  77. XCTAssertThrows([FIRApp configure]);
  78. }
  79. - (void)testConfigureWithOptions {
  80. // nil options
  81. XCTAssertThrows([FIRApp configureWithOptions:nil]);
  82. XCTAssertTrue([FIRApp allApps].count == 0);
  83. NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName
  84. isDefaultApp:YES];
  85. OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
  86. object:[FIRApp class]
  87. userInfo:expectedUserInfo]);
  88. // default options
  89. XCTAssertNoThrow([FIRApp configureWithOptions:[FIROptions defaultOptions]]);
  90. OCMVerifyAll(self.notificationCenterMock);
  91. self.app = [FIRApp defaultApp];
  92. XCTAssertNotNil(self.app);
  93. XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  94. XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  95. XCTAssertTrue([FIRApp allApps].count == 1);
  96. }
  97. - (void)testConfigureWithCustomizedOptions {
  98. // valid customized options
  99. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  100. bundleID:kBundleID
  101. GCMSenderID:kGCMSenderID
  102. APIKey:kCustomizedAPIKey
  103. clientID:nil
  104. trackingID:nil
  105. androidClientID:nil
  106. databaseURL:nil
  107. storageBucket:nil
  108. deepLinkURLScheme:nil];
  109. NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName
  110. isDefaultApp:YES];
  111. OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
  112. object:[FIRApp class]
  113. userInfo:expectedUserInfo]);
  114. XCTAssertNoThrow([FIRApp configureWithOptions:options]);
  115. OCMVerifyAll(self.notificationCenterMock);
  116. self.app = [FIRApp defaultApp];
  117. XCTAssertNotNil(self.app);
  118. XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  119. XCTAssertEqualObjects(self.app.options.googleAppID, kGoogleAppID);
  120. XCTAssertEqualObjects(self.app.options.APIKey, kCustomizedAPIKey);
  121. XCTAssertTrue([FIRApp allApps].count == 1);
  122. }
  123. - (void)testConfigureWithNameAndOptions {
  124. XCTAssertThrows([FIRApp configureWithName:nil options:[FIROptions defaultOptions]]);
  125. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:nil]);
  126. XCTAssertThrows([FIRApp configureWithName:@"" options:[FIROptions defaultOptions]]);
  127. XCTAssertThrows([FIRApp configureWithName:kFIRDefaultAppName
  128. options:[FIROptions defaultOptions]]);
  129. XCTAssertTrue([FIRApp allApps].count == 0);
  130. NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRTestAppName1
  131. isDefaultApp:NO];
  132. OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
  133. object:[FIRApp class]
  134. userInfo:expectedUserInfo]);
  135. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]]);
  136. OCMVerifyAll(self.notificationCenterMock);
  137. XCTAssertTrue([FIRApp allApps].count == 1);
  138. self.app = [FIRApp appNamed:kFIRTestAppName1];
  139. XCTAssertNotNil(self.app);
  140. XCTAssertEqualObjects(self.app.name, kFIRTestAppName1);
  141. XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  142. // Configure the same app again should throw an exception.
  143. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]]);
  144. }
  145. - (void)testConfigureWithNameAndCustomizedOptions {
  146. FIROptions *options = [FIROptions defaultOptions];
  147. FIROptions *newOptions = [options copy];
  148. newOptions.deepLinkURLScheme = kDeepLinkURLScheme;
  149. NSDictionary *expectedUserInfo1 = [self expectedUserInfoWithAppName:kFIRTestAppName1
  150. isDefaultApp:NO];
  151. OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
  152. object:[FIRApp class]
  153. userInfo:expectedUserInfo1]);
  154. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:newOptions]);
  155. XCTAssertTrue([FIRApp allApps].count == 1);
  156. self.app = [FIRApp appNamed:kFIRTestAppName1];
  157. // Configure a different app with valid customized options
  158. FIROptions *customizedOptions = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  159. bundleID:kBundleID
  160. GCMSenderID:kGCMSenderID
  161. APIKey:kCustomizedAPIKey
  162. clientID:nil
  163. trackingID:nil
  164. androidClientID:nil
  165. databaseURL:nil
  166. storageBucket:nil
  167. deepLinkURLScheme:nil];
  168. NSDictionary *expectedUserInfo2 = [self expectedUserInfoWithAppName:kFIRTestAppName2
  169. isDefaultApp:NO];
  170. OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
  171. object:[FIRApp class]
  172. userInfo:expectedUserInfo2]);
  173. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName2 options:customizedOptions]);
  174. OCMVerifyAll(self.notificationCenterMock);
  175. XCTAssertTrue([FIRApp allApps].count == 2);
  176. self.app = [FIRApp appNamed:kFIRTestAppName2];
  177. XCTAssertNotNil(self.app);
  178. XCTAssertEqualObjects(self.app.name, kFIRTestAppName2);
  179. XCTAssertEqualObjects(self.app.options.googleAppID, kGoogleAppID);
  180. XCTAssertEqualObjects(self.app.options.APIKey, kCustomizedAPIKey);
  181. }
  182. - (void)testValidName {
  183. XCTAssertNoThrow([FIRApp configureWithName:@"aA1_" options:[FIROptions defaultOptions]]);
  184. XCTAssertThrows([FIRApp configureWithName:@"aA1%" options:[FIROptions defaultOptions]]);
  185. XCTAssertThrows([FIRApp configureWithName:@"aA1?" options:[FIROptions defaultOptions]]);
  186. XCTAssertThrows([FIRApp configureWithName:@"aA1!" options:[FIROptions defaultOptions]]);
  187. }
  188. - (void)testDefaultApp {
  189. self.app = [FIRApp defaultApp];
  190. XCTAssertNil(self.app);
  191. [FIRApp configure];
  192. self.app = [FIRApp defaultApp];
  193. XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  194. XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  195. }
  196. - (void)testAppNamed {
  197. self.app = [FIRApp appNamed:kFIRTestAppName1];
  198. XCTAssertNil(self.app);
  199. [FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]];
  200. self.app = [FIRApp appNamed:kFIRTestAppName1];
  201. XCTAssertEqualObjects(self.app.name, kFIRTestAppName1);
  202. XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  203. }
  204. - (void)testDeleteApp {
  205. [FIRApp configure];
  206. self.app = [FIRApp defaultApp];
  207. XCTAssertTrue([FIRApp allApps].count == 1);
  208. [self.app deleteApp:^(BOOL success) {
  209. XCTAssertTrue(success);
  210. }];
  211. OCMVerify([self.notificationCenterMock postNotificationName:kFIRAppDeleteNotification
  212. object:[FIRApp class]
  213. userInfo:[OCMArg any]]);
  214. XCTAssertTrue(self.app.alreadySentDeleteNotification);
  215. XCTAssertTrue([FIRApp allApps].count == 0);
  216. }
  217. - (void)testErrorForSubspecConfigurationFailure {
  218. NSError *error = [FIRApp errorForSubspecConfigurationFailureWithDomain:kFirebaseAdMobErrorDomain
  219. errorCode:FIRErrorCodeAdMobFailed
  220. service:kFIRServiceAdMob
  221. reason:@"some reason"];
  222. XCTAssertNotNil(error);
  223. XCTAssert([error.domain isEqualToString:kFirebaseAdMobErrorDomain]);
  224. XCTAssert(error.code == FIRErrorCodeAdMobFailed);
  225. XCTAssert([error.description containsString:@"Configuration failed for"]);
  226. }
  227. - (void)testGetTokenWithCallback {
  228. [FIRApp configure];
  229. FIRApp *app = [FIRApp defaultApp];
  230. __block BOOL getTokenImplementationWasCalled = NO;
  231. __block BOOL getTokenCallbackWasCalled = NO;
  232. __block BOOL passedRefreshValue = NO;
  233. [app getTokenForcingRefresh:YES
  234. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  235. getTokenCallbackWasCalled = YES;
  236. }];
  237. XCTAssert(getTokenCallbackWasCalled,
  238. @"The callback should be invoked by the base implementation when no block for "
  239. "'getTokenImplementation' has been specified.");
  240. getTokenCallbackWasCalled = NO;
  241. app.getTokenImplementation = ^(BOOL refresh, FIRTokenCallback callback) {
  242. getTokenImplementationWasCalled = YES;
  243. passedRefreshValue = refresh;
  244. callback(nil, nil);
  245. };
  246. [app getTokenForcingRefresh:YES
  247. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  248. getTokenCallbackWasCalled = YES;
  249. }];
  250. XCTAssert(getTokenImplementationWasCalled,
  251. @"The 'getTokenImplementation' block was never called.");
  252. XCTAssert(passedRefreshValue,
  253. @"The value for the 'refresh' parameter wasn't passed to the 'getTokenImplementation' "
  254. "block correctly.");
  255. XCTAssert(getTokenCallbackWasCalled,
  256. @"The 'getTokenImplementation' should have invoked the callback. This could be an "
  257. "error in this test, or the callback parameter may not have been passed to the "
  258. "implementation correctly.");
  259. getTokenImplementationWasCalled = NO;
  260. getTokenCallbackWasCalled = NO;
  261. passedRefreshValue = NO;
  262. [app getTokenForcingRefresh:NO
  263. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  264. getTokenCallbackWasCalled = YES;
  265. }];
  266. XCTAssertFalse(passedRefreshValue,
  267. @"The value for the 'refresh' parameter wasn't passed to the "
  268. "'getTokenImplementation' block correctly.");
  269. }
  270. - (void)testModifyingOptionsThrows {
  271. [FIRApp configure];
  272. FIROptions *options = [[FIRApp defaultApp] options];
  273. XCTAssertTrue(options.isEditingLocked);
  274. // Modification to every property should result in an exception.
  275. XCTAssertThrows(options.androidClientID = @"should_throw");
  276. XCTAssertThrows(options.APIKey = @"should_throw");
  277. XCTAssertThrows(options.bundleID = @"should_throw");
  278. XCTAssertThrows(options.clientID = @"should_throw");
  279. XCTAssertThrows(options.databaseURL = @"should_throw");
  280. XCTAssertThrows(options.deepLinkURLScheme = @"should_throw");
  281. XCTAssertThrows(options.GCMSenderID = @"should_throw");
  282. XCTAssertThrows(options.googleAppID = @"should_throw");
  283. XCTAssertThrows(options.projectID = @"should_throw");
  284. XCTAssertThrows(options.storageBucket = @"should_throw");
  285. XCTAssertThrows(options.trackingID = @"should_throw");
  286. }
  287. - (void)testOptionsLocking {
  288. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  289. GCMSenderID:kGCMSenderID];
  290. options.projectID = kProjectID;
  291. options.databaseURL = kDatabaseURL;
  292. // Options should not be locked before they are used to configure a `FIRApp`.
  293. XCTAssertFalse(options.isEditingLocked);
  294. // The options returned should be locked after configuring `FIRApp`.
  295. [FIRApp configureWithOptions:options];
  296. FIROptions *optionsCopy = [[FIRApp defaultApp] options];
  297. XCTAssertTrue(optionsCopy.isEditingLocked);
  298. }
  299. #pragma mark - App ID v1
  300. - (void)testAppIDV1 {
  301. // Missing separator between platform:fingerprint.
  302. XCTAssertFalse([FIRApp validateAppID:@"1:1337:iosdeadbeef"]);
  303. // Wrong platform "android".
  304. XCTAssertFalse([FIRApp validateAppID:@"1:1337:android:deadbeef"]);
  305. // The fingerprint, aka 4th field, should only contain hex characters.
  306. XCTAssertFalse([FIRApp validateAppID:@"1:1337:ios:123abcxyz"]);
  307. // The fingerprint, aka 4th field, is not tested in V1, so a bad value shouldn't cause a failure.
  308. XCTAssertTrue([FIRApp validateAppID:@"1:1337:ios:deadbeef"]);
  309. }
  310. #pragma mark - App ID v2
  311. - (void)testAppIDV2 {
  312. // Missing separator between platform:fingerprint.
  313. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios5e18052ab54fbfec"]);
  314. // Unknown versions may contain anything.
  315. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:123abcxyz"]);
  316. XCTAssertTrue([FIRApp validateAppID:@"2:thisdoesn'teven_m:a:t:t:e:r_"]);
  317. // Known good fingerprint.
  318. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:5e18052ab54fbfec"]);
  319. // Unknown fingerprint, not tested so shouldn't cause a failure.
  320. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:deadbeef"]);
  321. }
  322. #pragma mark - App ID other
  323. - (void)testAppIDV3 {
  324. // Currently there is no specification for v3, so we would not expect it to fail.
  325. XCTAssertTrue([FIRApp validateAppID:@"3:1337:ios:deadbeef"]);
  326. }
  327. - (void)testAppIDEmpty {
  328. XCTAssertFalse([FIRApp validateAppID:@""]);
  329. }
  330. - (void)testAppIDValidationTrue {
  331. // Ensure that isAppIDValid matches validateAppID.
  332. [FIRApp configure];
  333. OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(YES);
  334. XCTAssertTrue([[FIRApp defaultApp] isAppIDValid]);
  335. }
  336. - (void)testAppIDValidationFalse {
  337. // Ensure that isAppIDValid matches validateAppID.
  338. [FIRApp configure];
  339. OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(NO);
  340. XCTAssertFalse([[FIRApp defaultApp] isAppIDValid]);
  341. }
  342. - (void)testAppIDPrefix {
  343. // Unknown numeric-character prefixes should pass.
  344. XCTAssertTrue([FIRApp validateAppID:@"0:"]);
  345. XCTAssertTrue([FIRApp validateAppID:@"01:"]);
  346. XCTAssertTrue([FIRApp validateAppID:@"10:"]);
  347. XCTAssertTrue([FIRApp validateAppID:@"010:"]);
  348. XCTAssertTrue([FIRApp validateAppID:@"3:"]);
  349. XCTAssertTrue([FIRApp validateAppID:@"123:"]);
  350. XCTAssertTrue([FIRApp validateAppID:@"999999999:"]);
  351. // Non-numeric prefixes should not pass.
  352. XCTAssertFalse([FIRApp validateAppID:@"a:"]);
  353. XCTAssertFalse([FIRApp validateAppID:@"abcsdf0:"]);
  354. XCTAssertFalse([FIRApp validateAppID:@"0aaaa:"]);
  355. XCTAssertFalse([FIRApp validateAppID:@"0aaaa0450:"]);
  356. XCTAssertFalse([FIRApp validateAppID:@"-1:"]);
  357. XCTAssertFalse([FIRApp validateAppID:@"abcsdf:"]);
  358. XCTAssertFalse([FIRApp validateAppID:@"ABDCF:"]);
  359. XCTAssertFalse([FIRApp validateAppID:@" :"]);
  360. XCTAssertFalse([FIRApp validateAppID:@"1 :"]);
  361. XCTAssertFalse([FIRApp validateAppID:@" 1:"]);
  362. XCTAssertFalse([FIRApp validateAppID:@" 123 :"]);
  363. XCTAssertFalse([FIRApp validateAppID:@"1 23:"]);
  364. XCTAssertFalse([FIRApp validateAppID:@"&($*&%(*$&:"]);
  365. XCTAssertFalse([FIRApp validateAppID:@"abCDSF$%%df:"]);
  366. // Known version prefixes should never pass without the rest of the app ID string present.
  367. XCTAssertFalse([FIRApp validateAppID:@"1:"]);
  368. // Version must include ":".
  369. XCTAssertFalse([FIRApp validateAppID:@"0"]);
  370. XCTAssertFalse([FIRApp validateAppID:@"01"]);
  371. XCTAssertFalse([FIRApp validateAppID:@"10"]);
  372. XCTAssertFalse([FIRApp validateAppID:@"010"]);
  373. XCTAssertFalse([FIRApp validateAppID:@"3"]);
  374. XCTAssertFalse([FIRApp validateAppID:@"123"]);
  375. XCTAssertFalse([FIRApp validateAppID:@"999999999"]);
  376. XCTAssertFalse([FIRApp validateAppID:@"com.google.bundleID"]);
  377. }
  378. - (void)testAppIDFormatInvalid {
  379. OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  380. // Some direct tests of the validateAppIDFormat:withVersion: method.
  381. // Sanity checks first.
  382. NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  383. NSString *const kGoodVersionV1 = @"1:";
  384. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV1]);
  385. NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  386. NSString *const kGoodVersionV2 = @"2:";
  387. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);
  388. // Version mismatch.
  389. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV1]);
  390. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV2]);
  391. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@"999:"]);
  392. // Nil or empty strings.
  393. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:nil]);
  394. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@""]);
  395. XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:kGoodVersionV1]);
  396. XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:kGoodVersionV1]);
  397. XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:nil]);
  398. XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:@""]);
  399. // App ID contains only the version prefix.
  400. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodVersionV1 withVersion:kGoodVersionV1]);
  401. // The version is the entire app ID.
  402. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  403. // Versions digits that may make a partial match.
  404. XCTAssertFalse([FIRApp validateAppIDFormat:@"01:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  405. XCTAssertFalse([FIRApp validateAppIDFormat:@"10:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  406. XCTAssertFalse([FIRApp validateAppIDFormat:@"11:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  407. XCTAssertFalse([FIRApp validateAppIDFormat:@"21:1337:ios:5e18052ab54fbfec"
  408. withVersion:kGoodVersionV2]);
  409. XCTAssertFalse([FIRApp validateAppIDFormat:@"22:1337:ios:5e18052ab54fbfec"
  410. withVersion:kGoodVersionV2]);
  411. XCTAssertFalse([FIRApp validateAppIDFormat:@"02:1337:ios:5e18052ab54fbfec"
  412. withVersion:kGoodVersionV2]);
  413. XCTAssertFalse([FIRApp validateAppIDFormat:@"20:1337:ios:5e18052ab54fbfec"
  414. withVersion:kGoodVersionV2]);
  415. // Extra fields.
  416. XCTAssertFalse([FIRApp validateAppIDFormat:@"ab:1:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  417. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:ab:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  418. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ab:ios:deadbeef" withVersion:kGoodVersionV1]);
  419. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:ab:deadbeef" withVersion:kGoodVersionV1]);
  420. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:deadbeef:ab" withVersion:kGoodVersionV1]);
  421. }
  422. - (void)testAppIDFingerprintInvalid {
  423. OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  424. // Some direct tests of the validateAppIDFingerprint:withVersion: method.
  425. // Sanity checks first.
  426. NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  427. NSString *const kGoodVersionV1 = @"1:";
  428. XCTAssertTrue([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodVersionV1]);
  429. NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  430. NSString *const kGoodVersionV2 = @"2:";
  431. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);
  432. // Version mismatch.
  433. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV2 withVersion:kGoodVersionV1]);
  434. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodVersionV2]);
  435. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:@"999:"]);
  436. // Nil or empty strings.
  437. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:nil]);
  438. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:@""]);
  439. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:kGoodVersionV1]);
  440. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:kGoodVersionV1]);
  441. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:nil]);
  442. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:@""]);
  443. // App ID contains only the version prefix.
  444. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodVersionV1 withVersion:kGoodVersionV1]);
  445. // The version is the entire app ID.
  446. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  447. // Versions digits that may make a partial match.
  448. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"01:1337:ios:deadbeef"
  449. withVersion:kGoodVersionV1]);
  450. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"10:1337:ios:deadbeef"
  451. withVersion:kGoodVersionV1]);
  452. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"11:1337:ios:deadbeef"
  453. withVersion:kGoodVersionV1]);
  454. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"21:1337:ios:5e18052ab54fbfec"
  455. withVersion:kGoodVersionV2]);
  456. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"22:1337:ios:5e18052ab54fbfec"
  457. withVersion:kGoodVersionV2]);
  458. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"02:1337:ios:5e18052ab54fbfec"
  459. withVersion:kGoodVersionV2]);
  460. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"20:1337:ios:5e18052ab54fbfec"
  461. withVersion:kGoodVersionV2]);
  462. // Extra fields.
  463. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"ab:1:1337:ios:deadbeef"
  464. withVersion:kGoodVersionV1]);
  465. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"1:ab:1337:ios:deadbeef"
  466. withVersion:kGoodVersionV1]);
  467. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"1:1337:ab:ios:deadbeef"
  468. withVersion:kGoodVersionV1]);
  469. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"1:1337:ios:ab:deadbeef"
  470. withVersion:kGoodVersionV1]);
  471. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"1:1337:ios:deadbeef:ab"
  472. withVersion:kGoodVersionV1]);
  473. }
  474. #pragma mark - Internal Methods
  475. - (void)testAuthGetUID {
  476. [FIRApp configure];
  477. [FIRApp defaultApp].getUIDImplementation = ^NSString *{ return @"highlander"; };
  478. XCTAssertEqual([[FIRApp defaultApp] getUID], @"highlander");
  479. }
  480. #pragma mark - private
  481. - (NSDictionary <NSString *, NSObject *> *)expectedUserInfoWithAppName:(NSString *)name
  482. isDefaultApp:(BOOL)isDefaultApp {
  483. return @{
  484. kFIRAppNameKey : name,
  485. kFIRAppIsDefaultAppKey : [NSNumber numberWithBool:isDefaultApp],
  486. kFIRGoogleAppIDKey : kGoogleAppID
  487. };
  488. }
  489. @end