aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Core/Tests/FIRAppTest.m
blob: 549c1ab1cf724ad955b64ff4e8c4f8bd5eedcd16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
// Copyright 2017 Google
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#import "FIRTestCase.h"

#import <FirebaseCore/FIRAnalyticsConfiguration+Internal.h>
#import <FirebaseCore/FIRAppInternal.h>
#import <FirebaseCore/FIROptionsInternal.h>

NSString *const kFIRTestAppName1 = @"test_app_name_1";
NSString *const kFIRTestAppName2 = @"test-app-name-2";

@interface FIRApp (TestInternal)

@property(nonatomic) BOOL alreadySentConfigureNotification;
@property(nonatomic) BOOL alreadySentDeleteNotification;

+ (void)resetApps;
- (instancetype)initInstanceWithName:(NSString *)name options:(FIROptions *)options;
- (BOOL)configureCore;
+ (NSError *)errorForInvalidAppID;
- (BOOL)isAppIDValid;
+ (NSString *)actualBundleID;
+ (NSNumber *)mapFromServiceStringToTypeEnum:(NSString *)serviceString;
+ (NSString *)deviceModel;
+ (NSString *)installString;
+ (NSURL *)filePathURLWithName:(NSString *)fileName;
+ (NSString *)stringAtURL:(NSURL *)filePathURL;
+ (BOOL)writeString:(NSString *)string toURL:(NSURL *)filePathURL;
+ (void)logAppInfo:(NSNotification *)notification;
+ (BOOL)validateAppID:(NSString *)appID;
+ (BOOL)validateAppIDFormat:(NSString *)appID withVersion:(NSString *)version;
+ (BOOL)validateAppIDFingerprint:(NSString *)appID withVersion:(NSString *)version;

+ (nullable NSNumber *)readDataCollectionSwitchFromPlist;
+ (nullable NSNumber *)readDataCollectionSwitchFromUserDefaultsForApp:(FIRApp *)app;

@end

@interface FIRAppTest : FIRTestCase

@property(nonatomic) id appClassMock;
@property(nonatomic) id optionsInstanceMock;
@property(nonatomic) id notificationCenterMock;
@property(nonatomic) FIRApp *app;

@end

@implementation FIRAppTest

- (void)setUp {
  [super setUp];
  [FIROptions resetDefaultOptions];
  [FIRApp resetApps];
  _appClassMock = OCMClassMock([FIRApp class]);
  _optionsInstanceMock = OCMPartialMock([FIROptions defaultOptions]);
  _notificationCenterMock = OCMPartialMock([NSNotificationCenter defaultCenter]);
}

- (void)tearDown {
  [_appClassMock stopMocking];
  [_optionsInstanceMock stopMocking];
  [_notificationCenterMock stopMocking];

  [super tearDown];
}

- (void)testConfigure {
  NSDictionary *expectedUserInfo =
      [self expectedUserInfoWithAppName:kFIRDefaultAppName isDefaultApp:YES];
  OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
                                                       object:[FIRApp class]
                                                     userInfo:expectedUserInfo]);
  XCTAssertNoThrow([FIRApp configure]);
  OCMVerifyAll(self.notificationCenterMock);

  self.app = [FIRApp defaultApp];
  XCTAssertNotNil(self.app);
  XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  XCTAssertTrue([FIRApp allApps].count == 1);
  XCTAssertTrue(self.app.alreadySentConfigureNotification);

  // Test if options is nil
  id optionsClassMock = OCMClassMock([FIROptions class]);
  OCMStub([optionsClassMock defaultOptions]).andReturn(nil);
  XCTAssertThrows([FIRApp configure]);
}

- (void)testConfigureWithOptions {
// nil options
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
  XCTAssertThrows([FIRApp configureWithOptions:nil]);
#pragma clang diagnostic pop
  XCTAssertTrue([FIRApp allApps].count == 0);

  NSDictionary *expectedUserInfo =
      [self expectedUserInfoWithAppName:kFIRDefaultAppName isDefaultApp:YES];
  OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
                                                       object:[FIRApp class]
                                                     userInfo:expectedUserInfo]);
  // default options
  XCTAssertNoThrow([FIRApp configureWithOptions:[FIROptions defaultOptions]]);
  OCMVerifyAll(self.notificationCenterMock);

  self.app = [FIRApp defaultApp];
  XCTAssertNotNil(self.app);
  XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  XCTAssertTrue([FIRApp allApps].count == 1);
}

- (void)testConfigureWithCustomizedOptions {
  // valid customized options
  FIROptions *options =
      [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  options.bundleID = kBundleID;
  options.APIKey = kCustomizedAPIKey;
  NSDictionary *expectedUserInfo =
      [self expectedUserInfoWithAppName:kFIRDefaultAppName isDefaultApp:YES];
  OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
                                                       object:[FIRApp class]
                                                     userInfo:expectedUserInfo]);

  XCTAssertNoThrow([FIRApp configureWithOptions:options]);
  OCMVerifyAll(self.notificationCenterMock);

  self.app = [FIRApp defaultApp];
  XCTAssertNotNil(self.app);
  XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  XCTAssertEqualObjects(self.app.options.googleAppID, kGoogleAppID);
  XCTAssertEqualObjects(self.app.options.APIKey, kCustomizedAPIKey);
  XCTAssertTrue([FIRApp allApps].count == 1);
}

- (void)testConfigureWithNameAndOptions {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wnonnull"
  XCTAssertThrows([FIRApp configureWithName:nil options:[FIROptions defaultOptions]]);
  XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:nil]);
#pragma clang diagnostic pop
  XCTAssertThrows([FIRApp configureWithName:@"" options:[FIROptions defaultOptions]]);
  XCTAssertThrows(
      [FIRApp configureWithName:kFIRDefaultAppName options:[FIROptions defaultOptions]]);
  XCTAssertTrue([FIRApp allApps].count == 0);

  NSDictionary *expectedUserInfo =
      [self expectedUserInfoWithAppName:kFIRTestAppName1 isDefaultApp:NO];
  OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
                                                       object:[FIRApp class]
                                                     userInfo:expectedUserInfo]);
  XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]]);
  OCMVerifyAll(self.notificationCenterMock);

  XCTAssertTrue([FIRApp allApps].count == 1);
  self.app = [FIRApp appNamed:kFIRTestAppName1];
  XCTAssertNotNil(self.app);
  XCTAssertEqualObjects(self.app.name, kFIRTestAppName1);
  XCTAssertEqualObjects(self.app.options.clientID, kClientID);

  // Configure the same app again should throw an exception.
  XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]]);
}

- (void)testConfigureWithNameAndCustomizedOptions {
  FIROptions *options = [FIROptions defaultOptions];
  FIROptions *newOptions = [options copy];
  newOptions.deepLinkURLScheme = kDeepLinkURLScheme;

  NSDictionary *expectedUserInfo1 =
      [self expectedUserInfoWithAppName:kFIRTestAppName1 isDefaultApp:NO];
  OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
                                                       object:[FIRApp class]
                                                     userInfo:expectedUserInfo1]);
  XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:newOptions]);
  XCTAssertTrue([FIRApp allApps].count == 1);
  self.app = [FIRApp appNamed:kFIRTestAppName1];

  // Configure a different app with valid customized options
  FIROptions *customizedOptions =
      [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  customizedOptions.bundleID = kBundleID;
  customizedOptions.APIKey = kCustomizedAPIKey;

  NSDictionary *expectedUserInfo2 =
      [self expectedUserInfoWithAppName:kFIRTestAppName2 isDefaultApp:NO];
  OCMExpect([self.notificationCenterMock postNotificationName:kFIRAppReadyToConfigureSDKNotification
                                                       object:[FIRApp class]
                                                     userInfo:expectedUserInfo2]);
  XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName2 options:customizedOptions]);
  OCMVerifyAll(self.notificationCenterMock);

  XCTAssertTrue([FIRApp allApps].count == 2);
  self.app = [FIRApp appNamed:kFIRTestAppName2];
  XCTAssertNotNil(self.app);
  XCTAssertEqualObjects(self.app.name, kFIRTestAppName2);
  XCTAssertEqualObjects(self.app.options.googleAppID, kGoogleAppID);
  XCTAssertEqualObjects(self.app.options.APIKey, kCustomizedAPIKey);
}

- (void)testValidName {
  XCTAssertNoThrow([FIRApp configureWithName:@"aA1_" options:[FIROptions defaultOptions]]);
  XCTAssertThrows([FIRApp configureWithName:@"aA1%" options:[FIROptions defaultOptions]]);
  XCTAssertThrows([FIRApp configureWithName:@"aA1?" options:[FIROptions defaultOptions]]);
  XCTAssertThrows([FIRApp configureWithName:@"aA1!" options:[FIROptions defaultOptions]]);
}

- (void)testDefaultApp {
  self.app = [FIRApp defaultApp];
  XCTAssertNil(self.app);

  [FIRApp configure];
  self.app = [FIRApp defaultApp];
  XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  XCTAssertEqualObjects(self.app.options.clientID, kClientID);
}

- (void)testAppNamed {
  self.app = [FIRApp appNamed:kFIRTestAppName1];
  XCTAssertNil(self.app);

  [FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]];
  self.app = [FIRApp appNamed:kFIRTestAppName1];
  XCTAssertEqualObjects(self.app.name, kFIRTestAppName1);
  XCTAssertEqualObjects(self.app.options.clientID, kClientID);
}

- (void)testDeleteApp {
  [FIRApp configure];
  self.app = [FIRApp defaultApp];
  XCTAssertTrue([FIRApp allApps].count == 1);
  [self.app deleteApp:^(BOOL success) {
    XCTAssertTrue(success);
  }];
  OCMVerify([self.notificationCenterMock postNotificationName:kFIRAppDeleteNotification
                                                       object:[FIRApp class]
                                                     userInfo:[OCMArg any]]);
  XCTAssertTrue(self.app.alreadySentDeleteNotification);
  XCTAssertTrue([FIRApp allApps].count == 0);
}

- (void)testErrorForSubspecConfigurationFailure {
  NSError *error = [FIRApp errorForSubspecConfigurationFailureWithDomain:kFirebaseAdMobErrorDomain
                                                               errorCode:FIRErrorCodeAdMobFailed
                                                                 service:kFIRServiceAdMob
                                                                  reason:@"some reason"];
  XCTAssertNotNil(error);
  XCTAssert([error.domain isEqualToString:kFirebaseAdMobErrorDomain]);
  XCTAssert(error.code == FIRErrorCodeAdMobFailed);
  XCTAssert([error.description containsString:@"Configuration failed for"]);
}

- (void)testGetTokenWithCallback {
  [FIRApp configure];
  FIRApp *app = [FIRApp defaultApp];

  __block BOOL getTokenImplementationWasCalled = NO;
  __block BOOL getTokenCallbackWasCalled = NO;
  __block BOOL passedRefreshValue = NO;

  [app getTokenForcingRefresh:YES
                 withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
                   getTokenCallbackWasCalled = YES;
                 }];

  XCTAssert(getTokenCallbackWasCalled,
            @"The callback should be invoked by the base implementation when no block for "
             "'getTokenImplementation' has been specified.");

  getTokenCallbackWasCalled = NO;

  app.getTokenImplementation = ^(BOOL refresh, FIRTokenCallback callback) {
    getTokenImplementationWasCalled = YES;
    passedRefreshValue = refresh;
    callback(nil, nil);
  };
  [app getTokenForcingRefresh:YES
                 withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
                   getTokenCallbackWasCalled = YES;
                 }];

  XCTAssert(getTokenImplementationWasCalled,
            @"The 'getTokenImplementation' block was never called.");
  XCTAssert(passedRefreshValue,
            @"The value for the 'refresh' parameter wasn't passed to the 'getTokenImplementation' "
             "block correctly.");
  XCTAssert(getTokenCallbackWasCalled,
            @"The 'getTokenImplementation' should have invoked the callback. This could be an "
             "error in this test, or the callback parameter may not have been passed to the "
             "implementation correctly.");

  getTokenImplementationWasCalled = NO;
  getTokenCallbackWasCalled = NO;
  passedRefreshValue = NO;

  [app getTokenForcingRefresh:NO
                 withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
                   getTokenCallbackWasCalled = YES;
                 }];

  XCTAssertFalse(passedRefreshValue,
                 @"The value for the 'refresh' parameter wasn't passed to the "
                  "'getTokenImplementation' block correctly.");
}

- (void)testModifyingOptionsThrows {
  [FIRApp configure];
  FIROptions *options = [[FIRApp defaultApp] options];
  XCTAssertTrue(options.isEditingLocked);

  // Modification to every property should result in an exception.
  XCTAssertThrows(options.androidClientID = @"should_throw");
  XCTAssertThrows(options.APIKey = @"should_throw");
  XCTAssertThrows(options.bundleID = @"should_throw");
  XCTAssertThrows(options.clientID = @"should_throw");
  XCTAssertThrows(options.databaseURL = @"should_throw");
  XCTAssertThrows(options.deepLinkURLScheme = @"should_throw");
  XCTAssertThrows(options.GCMSenderID = @"should_throw");
  XCTAssertThrows(options.googleAppID = @"should_throw");
  XCTAssertThrows(options.projectID = @"should_throw");
  XCTAssertThrows(options.storageBucket = @"should_throw");
  XCTAssertThrows(options.trackingID = @"should_throw");
}

- (void)testOptionsLocking {
  FIROptions *options =
      [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  options.projectID = kProjectID;
  options.databaseURL = kDatabaseURL;

  // Options should not be locked before they are used to configure a `FIRApp`.
  XCTAssertFalse(options.isEditingLocked);

  // The options returned should be locked after configuring `FIRApp`.
  [FIRApp configureWithOptions:options];
  FIROptions *optionsCopy = [[FIRApp defaultApp] options];
  XCTAssertTrue(optionsCopy.isEditingLocked);
}

#pragma mark - App ID v1

- (void)testAppIDV1 {
  // Missing separator between platform:fingerprint.
  XCTAssertFalse([FIRApp validateAppID:@"1:1337:iosdeadbeef"]);

  // Wrong platform "android".
  XCTAssertFalse([FIRApp validateAppID:@"1:1337:android:deadbeef"]);

  // The fingerprint, aka 4th field, should only contain hex characters.
  XCTAssertFalse([FIRApp validateAppID:@"1:1337:ios:123abcxyz"]);

  // The fingerprint, aka 4th field, is not tested in V1, so a bad value shouldn't cause a failure.
  XCTAssertTrue([FIRApp validateAppID:@"1:1337:ios:deadbeef"]);
}

#pragma mark - App ID v2

- (void)testAppIDV2 {
  // Missing separator between platform:fingerprint.
  XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios5e18052ab54fbfec"]);

  // Unknown versions may contain anything.
  XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:123abcxyz"]);
  XCTAssertTrue([FIRApp validateAppID:@"2:thisdoesn'teven_m:a:t:t:e:r_"]);

  // Known good fingerprint.
  XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:5e18052ab54fbfec"]);

  // Unknown fingerprint, not tested so shouldn't cause a failure.
  XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:deadbeef"]);
}

#pragma mark - App ID other

- (void)testAppIDV3 {
  // Currently there is no specification for v3, so we would not expect it to fail.
  XCTAssertTrue([FIRApp validateAppID:@"3:1337:ios:deadbeef"]);
}

- (void)testAppIDEmpty {
  XCTAssertFalse([FIRApp validateAppID:@""]);
}

- (void)testAppIDValidationTrue {
  // Ensure that isAppIDValid matches validateAppID.
  [FIRApp configure];
  OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(YES);
  XCTAssertTrue([[FIRApp defaultApp] isAppIDValid]);
}

- (void)testAppIDValidationFalse {
  // Ensure that isAppIDValid matches validateAppID.
  [FIRApp configure];
  OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(NO);
  XCTAssertFalse([[FIRApp defaultApp] isAppIDValid]);
}

- (void)testAppIDPrefix {
  // Unknown numeric-character prefixes should pass.
  XCTAssertTrue([FIRApp validateAppID:@"0:"]);
  XCTAssertTrue([FIRApp validateAppID:@"01:"]);
  XCTAssertTrue([FIRApp validateAppID:@"10:"]);
  XCTAssertTrue([FIRApp validateAppID:@"010:"]);
  XCTAssertTrue([FIRApp validateAppID:@"3:"]);
  XCTAssertTrue([FIRApp validateAppID:@"123:"]);
  XCTAssertTrue([FIRApp validateAppID:@"999999999:"]);

  // Non-numeric prefixes should not pass.
  XCTAssertFalse([FIRApp validateAppID:@"a:"]);
  XCTAssertFalse([FIRApp validateAppID:@"abcsdf0:"]);
  XCTAssertFalse([FIRApp validateAppID:@"0aaaa:"]);
  XCTAssertFalse([FIRApp validateAppID:@"0aaaa0450:"]);
  XCTAssertFalse([FIRApp validateAppID:@"-1:"]);
  XCTAssertFalse([FIRApp validateAppID:@"abcsdf:"]);
  XCTAssertFalse([FIRApp validateAppID:@"ABDCF:"]);
  XCTAssertFalse([FIRApp validateAppID:@" :"]);
  XCTAssertFalse([FIRApp validateAppID:@"1 :"]);
  XCTAssertFalse([FIRApp validateAppID:@" 1:"]);
  XCTAssertFalse([FIRApp validateAppID:@" 123 :"]);
  XCTAssertFalse([FIRApp validateAppID:@"1 23:"]);
  XCTAssertFalse([FIRApp validateAppID:@"&($*&%(*$&:"]);
  XCTAssertFalse([FIRApp validateAppID:@"abCDSF$%%df:"]);

  // Known version prefixes should never pass without the rest of the app ID string present.
  XCTAssertFalse([FIRApp validateAppID:@"1:"]);

  // Version must include ":".
  XCTAssertFalse([FIRApp validateAppID:@"0"]);
  XCTAssertFalse([FIRApp validateAppID:@"01"]);
  XCTAssertFalse([FIRApp validateAppID:@"10"]);
  XCTAssertFalse([FIRApp validateAppID:@"010"]);
  XCTAssertFalse([FIRApp validateAppID:@"3"]);
  XCTAssertFalse([FIRApp validateAppID:@"123"]);
  XCTAssertFalse([FIRApp validateAppID:@"999999999"]);
  XCTAssertFalse([FIRApp validateAppID:@"com.google.bundleID"]);
}

- (void)testAppIDFormatInvalid {
  OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  // Some direct tests of the validateAppIDFormat:withVersion: method.
  // Sanity checks first.
  NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  NSString *const kGoodVersionV1 = @"1:";
  XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV1]);

  NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  NSString *const kGoodVersionV2 = @"2:";
  XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);

  // Version mismatch.
  XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV2]);
  XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@"999:"]);

  // Nil or empty strings.
  XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:nil]);
  XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@""]);
  XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:nil]);
  XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:@""]);

  // App ID contains only the version prefix.
  XCTAssertFalse([FIRApp validateAppIDFormat:kGoodVersionV1 withVersion:kGoodVersionV1]);
  // The version is the entire app ID.
  XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodAppIDV1]);

  // Versions digits that may make a partial match.
  XCTAssertFalse([FIRApp validateAppIDFormat:@"01:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFormat:@"10:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFormat:@"11:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse(
      [FIRApp validateAppIDFormat:@"21:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  XCTAssertFalse(
      [FIRApp validateAppIDFormat:@"22:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  XCTAssertFalse(
      [FIRApp validateAppIDFormat:@"02:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  XCTAssertFalse(
      [FIRApp validateAppIDFormat:@"20:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);

  // Extra fields.
  XCTAssertFalse([FIRApp validateAppIDFormat:@"ab:1:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFormat:@"1:ab:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ab:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:ab:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:deadbeef:ab" withVersion:kGoodVersionV1]);
}

- (void)testAppIDFingerprintInvalid {
  OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  // Some direct tests of the validateAppIDFingerprint:withVersion: method.
  // Sanity checks first.
  NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  NSString *const kGoodVersionV1 = @"1:";
  XCTAssertTrue([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodVersionV1]);

  NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  NSString *const kGoodVersionV2 = @"2:";
  XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);

  // Version mismatch.
  XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV2 withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodVersionV2]);
  XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:@"999:"]);

  // Nil or empty strings.
  XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:nil]);
  XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:@""]);
  XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:kGoodVersionV1]);
  XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:nil]);
  XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:@""]);

  // App ID contains only the version prefix.
  XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodVersionV1 withVersion:kGoodVersionV1]);
  // The version is the entire app ID.
  XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodAppIDV1]);

  // Versions digits that may make a partial match.
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"01:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"10:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"11:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"21:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"22:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"02:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"20:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  // Extra fields.
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"ab:1:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"1:ab:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"1:1337:ab:ios:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"1:1337:ios:ab:deadbeef" withVersion:kGoodVersionV1]);
  XCTAssertFalse(
      [FIRApp validateAppIDFingerprint:@"1:1337:ios:deadbeef:ab" withVersion:kGoodVersionV1]);
}

#pragma mark - Automatic Data Collection Tests

- (void)testGlobalDataCollectionNoFlags {
  // Test: No flags set.
  [FIRApp configure];
  OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
      .andReturn(nil);

  XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
}

- (void)testGlobalDataCollectionPlistSetEnabled {
  // Test: Plist set to enabled, no override.
  [FIRApp configure];
  OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
      .andReturn(nil);

  XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
}

- (void)testGlobalDataCollectionPlistSetDisabled {
  // Test: Plist set to disabled, no override.
  [FIRApp configure];
  OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
      .andReturn(nil);

  XCTAssertFalse([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
}

- (void)testGlobalDataCollectionUserSpecifiedEnabled {
  // Test: User specified as enabled, no plist value.
  [FIRApp configure];
  OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
      .andReturn(@YES);

  XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
}

- (void)testGlobalDataCollectionUserSpecifiedDisabled {
  // Test: User specified as disabled, no plist value.
  [FIRApp configure];
  OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
      .andReturn(@NO);

  XCTAssertFalse([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
}

- (void)testGlobalDataCollectionUserOverriddenEnabled {
  // Test: User specified as enabled, with plist set as disabled.
  [FIRApp configure];
  OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
      .andReturn(@YES);

  XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
}

- (void)testGlobalDataCollectionUserOverriddenDisabled {
  // Test: User specified as disabled, with plist set as enabled.
  [FIRApp configure];
  OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
      .andReturn(@NO);

  XCTAssertFalse([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
}

- (void)testGlobalDataCollectionWriteToDefaults {
  id defaultsMock = OCMPartialMock([NSUserDefaults standardUserDefaults]);
  [FIRApp configure];

  FIRApp *app = [FIRApp defaultApp];
  app.automaticDataCollectionEnabled = YES;
  NSString *key =
      [NSString stringWithFormat:kFIRGlobalAppDataCollectionEnabledDefaultsKeyFormat, app.name];
  OCMVerify([defaultsMock setObject:@YES forKey:key]);

  [FIRApp defaultApp].automaticDataCollectionEnabled = NO;
  OCMVerify([defaultsMock setObject:@NO forKey:key]);

  [defaultsMock stopMocking];
}

- (void)testGlobalDataCollectionClearedAfterDelete {
  // Configure and disable data collection for the default FIRApp.
  [FIRApp configure];
  FIRApp *app = [FIRApp defaultApp];
  app.automaticDataCollectionEnabled = NO;
  XCTAssertFalse(app.isAutomaticDataCollectionEnabled);

  // Delete the app, and verify that the switch was reset.
  XCTestExpectation *deleteFinished =
      [self expectationWithDescription:@"The app should successfully delete."];
  [app deleteApp:^(BOOL success) {
    if (success) {
      [deleteFinished fulfill];
    }
  }];

  // Wait for the delete to complete.
  [self waitForExpectations:@[ deleteFinished ] timeout:1];

  // Set up the default app again, and check the data collection flag.
  [FIRApp configure];
  XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
}

- (void)testGlobalDataCollectionNoDiagnosticsSent {
  [FIRApp configure];

  // Stub out reading from user defaults since stubbing out the BOOL has issues. If the data
  // collection switch is disabled, the `sendLogs` call should return immediately and not fire a
  // notification.
  OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
      .andReturn(@NO);
  OCMReject([self.notificationCenterMock postNotificationName:kFIRAppDiagnosticsNotification
                                                       object:OCMOCK_ANY
                                                     userInfo:OCMOCK_ANY]);
  NSError *error = [NSError errorWithDomain:@"com.firebase" code:42 userInfo:nil];
  [[FIRApp defaultApp] sendLogsWithServiceName:@"Service" version:@"Version" error:error];
}

#pragma mark - Analytics Flag Tests

- (void)testAnalyticsSetByGlobalDataCollectionSwitch {
  // Test that the global data collection switch triggers setting Analytics when no explicit flag is
  // set.
  [FIRApp configure];

  id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  OCMStub([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
  OCMStub([self.optionsInstanceMock isAnalyticsCollectionExpicitlySet]).andReturn(NO);

  // Ensure Analytics is set after the global flag is set.
  [[FIRApp defaultApp] setAutomaticDataCollectionEnabled:YES];
  OCMVerify([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:NO]);

  [[FIRApp defaultApp] setAutomaticDataCollectionEnabled:NO];
  OCMVerify([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:NO]);
}

- (void)testAnalyticsNotSetByGlobalDataCollectionSwitch {
  // Test that the global data collection switch doesn't override an explicitly set Analytics flag.
  [FIRApp configure];

  id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  OCMStub([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
  OCMStub([self.optionsInstanceMock isAnalyticsCollectionExpicitlySet]).andReturn(YES);

  // Reject any changes to Analytics when the data collection changes.
  [[FIRApp defaultApp] setAutomaticDataCollectionEnabled:YES];
  OCMReject([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);

  [[FIRApp defaultApp] setAutomaticDataCollectionEnabled:NO];
  OCMReject([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
}

#pragma mark - Internal Methods

- (void)testAuthGetUID {
  [FIRApp configure];

  [FIRApp defaultApp].getUIDImplementation = ^NSString * {
    return @"highlander";
  };
  XCTAssertEqual([[FIRApp defaultApp] getUID], @"highlander");
}

- (void)testIsAppConfigured {
  // Ensure it's false before anything is configured.
  XCTAssertFalse([FIRApp isDefaultAppConfigured]);

  // Configure it and ensure it's configured.
  [FIRApp configure];
  XCTAssertTrue([FIRApp isDefaultAppConfigured]);

  // Reset the apps and ensure it's not configured anymore.
  [FIRApp resetApps];
  XCTAssertFalse([FIRApp isDefaultAppConfigured]);
}

- (void)testIllegalLibraryName {
  [FIRApp registerLibrary:@"Oops>" withVersion:@"1.0.0"];
  XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:@""]);
}

- (void)testIllegalLibraryVersion {
  [FIRApp registerLibrary:@"LegalName" withVersion:@"1.0.0+"];
  XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:@""]);
}

- (void)testSingleLibrary {
  [FIRApp registerLibrary:@"LegalName" withVersion:@"1.0.0"];
  XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"LegalName/1.0.0"]);
}

- (void)testMultipleLibraries {
  [FIRApp registerLibrary:@"LegalName" withVersion:@"1.0.0"];
  [FIRApp registerLibrary:@"LegalName2" withVersion:@"2.0.0"];
  XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"LegalName/1.0.0 LegalName2/2.0.0"]);
}

#pragma mark - private

- (NSDictionary<NSString *, NSObject *> *)expectedUserInfoWithAppName:(NSString *)name
                                                         isDefaultApp:(BOOL)isDefaultApp {
  return @{
    kFIRAppNameKey : name,
    kFIRAppIsDefaultAppKey : [NSNumber numberWithBool:isDefaultApp],
    kFIRGoogleAppIDKey : kGoogleAppID
  };
}

@end