FIRAppTest.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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 "FIRTestCase.h"
  15. #import <FirebaseCore/FIRAnalyticsConfiguration+Internal.h>
  16. #import <FirebaseCore/FIRAppInternal.h>
  17. #import <FirebaseCore/FIROptionsInternal.h>
  18. NSString *const kFIRTestAppName1 = @"test_app_name_1";
  19. NSString *const kFIRTestAppName2 = @"test-app-name-2";
  20. @interface FIRApp (TestInternal)
  21. @property(nonatomic) BOOL alreadySentConfigureNotification;
  22. @property(nonatomic) BOOL alreadySentDeleteNotification;
  23. + (void)resetApps;
  24. - (instancetype)initInstanceWithName:(NSString *)name options:(FIROptions *)options;
  25. - (BOOL)configureCore;
  26. + (NSError *)errorForInvalidAppID;
  27. - (BOOL)isAppIDValid;
  28. + (NSString *)actualBundleID;
  29. + (NSNumber *)mapFromServiceStringToTypeEnum:(NSString *)serviceString;
  30. + (NSString *)deviceModel;
  31. + (NSString *)installString;
  32. + (NSURL *)filePathURLWithName:(NSString *)fileName;
  33. + (NSString *)stringAtURL:(NSURL *)filePathURL;
  34. + (BOOL)writeString:(NSString *)string toURL:(NSURL *)filePathURL;
  35. + (void)logAppInfo:(NSNotification *)notification;
  36. + (BOOL)validateAppID:(NSString *)appID;
  37. + (BOOL)validateAppIDFormat:(NSString *)appID withVersion:(NSString *)version;
  38. + (BOOL)validateAppIDFingerprint:(NSString *)appID withVersion:(NSString *)version;
  39. + (nullable NSNumber *)readDataCollectionSwitchFromPlist;
  40. + (nullable NSNumber *)readDataCollectionSwitchFromUserDefaultsForApp:(FIRApp *)app;
  41. @end
  42. @interface FIRAppTest : FIRTestCase
  43. @property(nonatomic) id appClassMock;
  44. @property(nonatomic) id optionsInstanceMock;
  45. @property(nonatomic) id observerMock;
  46. @property(nonatomic) FIRApp *app;
  47. @property(nonatomic) NSNotificationCenter *notificationCenter;
  48. @end
  49. @implementation FIRAppTest
  50. - (void)setUp {
  51. [super setUp];
  52. [FIROptions resetDefaultOptions];
  53. [FIRApp resetApps];
  54. _appClassMock = OCMClassMock([FIRApp class]);
  55. _optionsInstanceMock = OCMPartialMock([FIROptions defaultOptions]);
  56. _observerMock = OCMObserverMock();
  57. // TODO: Remove all usages of defaultCenter in Core, then we can instantiate an instance here to
  58. // inject instead of using defaultCenter.
  59. _notificationCenter = [NSNotificationCenter defaultCenter];
  60. }
  61. - (void)tearDown {
  62. [_appClassMock stopMocking];
  63. [_optionsInstanceMock stopMocking];
  64. [_notificationCenter removeObserver:_observerMock];
  65. _observerMock = nil;
  66. _notificationCenter = nil;
  67. [super tearDown];
  68. }
  69. - (void)testConfigure {
  70. NSDictionary *expectedUserInfo =
  71. [self expectedUserInfoWithAppName:kFIRDefaultAppName isDefaultApp:YES];
  72. [self expectNotificationForObserver:self.observerMock
  73. notificationName:kFIRAppReadyToConfigureSDKNotification
  74. object:[FIRApp class]
  75. userInfo:expectedUserInfo];
  76. XCTAssertNoThrow([FIRApp configure]);
  77. OCMVerifyAll(self.observerMock);
  78. self.app = [FIRApp defaultApp];
  79. XCTAssertNotNil(self.app);
  80. XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  81. XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  82. XCTAssertTrue([FIRApp allApps].count == 1);
  83. XCTAssertTrue(self.app.alreadySentConfigureNotification);
  84. // Test if options is nil
  85. id optionsClassMock = OCMClassMock([FIROptions class]);
  86. OCMStub([optionsClassMock defaultOptions]).andReturn(nil);
  87. XCTAssertThrows([FIRApp configure]);
  88. }
  89. - (void)testConfigureWithOptions {
  90. // nil options
  91. #pragma clang diagnostic push
  92. #pragma clang diagnostic ignored "-Wnonnull"
  93. XCTAssertThrows([FIRApp configureWithOptions:nil]);
  94. #pragma clang diagnostic pop
  95. XCTAssertTrue([FIRApp allApps].count == 0);
  96. NSDictionary *expectedUserInfo =
  97. [self expectedUserInfoWithAppName:kFIRDefaultAppName isDefaultApp:YES];
  98. [self expectNotificationForObserver:self.observerMock
  99. notificationName:kFIRAppReadyToConfigureSDKNotification
  100. object:[FIRApp class]
  101. userInfo:expectedUserInfo];
  102. // default options
  103. XCTAssertNoThrow([FIRApp configureWithOptions:[FIROptions defaultOptions]]);
  104. OCMVerifyAll(self.observerMock);
  105. self.app = [FIRApp defaultApp];
  106. XCTAssertNotNil(self.app);
  107. XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  108. XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  109. XCTAssertTrue([FIRApp allApps].count == 1);
  110. }
  111. - (void)testConfigureWithCustomizedOptions {
  112. // valid customized options
  113. FIROptions *options =
  114. [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  115. options.bundleID = kBundleID;
  116. options.APIKey = kCustomizedAPIKey;
  117. NSDictionary *expectedUserInfo =
  118. [self expectedUserInfoWithAppName:kFIRDefaultAppName isDefaultApp:YES];
  119. [self expectNotificationForObserver:self.observerMock
  120. notificationName:kFIRAppReadyToConfigureSDKNotification
  121. object:[FIRApp class]
  122. userInfo:expectedUserInfo];
  123. XCTAssertNoThrow([FIRApp configureWithOptions:options]);
  124. OCMVerifyAll(self.observerMock);
  125. self.app = [FIRApp defaultApp];
  126. XCTAssertNotNil(self.app);
  127. XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  128. XCTAssertEqualObjects(self.app.options.googleAppID, kGoogleAppID);
  129. XCTAssertEqualObjects(self.app.options.APIKey, kCustomizedAPIKey);
  130. XCTAssertTrue([FIRApp allApps].count == 1);
  131. }
  132. - (void)testConfigureWithNameAndOptions {
  133. #pragma clang diagnostic push
  134. #pragma clang diagnostic ignored "-Wnonnull"
  135. XCTAssertThrows([FIRApp configureWithName:nil options:[FIROptions defaultOptions]]);
  136. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:nil]);
  137. #pragma clang diagnostic pop
  138. XCTAssertThrows([FIRApp configureWithName:@"" options:[FIROptions defaultOptions]]);
  139. XCTAssertThrows(
  140. [FIRApp configureWithName:kFIRDefaultAppName options:[FIROptions defaultOptions]]);
  141. XCTAssertTrue([FIRApp allApps].count == 0);
  142. NSDictionary *expectedUserInfo =
  143. [self expectedUserInfoWithAppName:kFIRTestAppName1 isDefaultApp:NO];
  144. [self expectNotificationForObserver:self.observerMock
  145. notificationName:kFIRAppReadyToConfigureSDKNotification
  146. object:[FIRApp class]
  147. userInfo:expectedUserInfo];
  148. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]]);
  149. OCMVerifyAll(self.observerMock);
  150. XCTAssertTrue([FIRApp allApps].count == 1);
  151. self.app = [FIRApp appNamed:kFIRTestAppName1];
  152. XCTAssertNotNil(self.app);
  153. XCTAssertEqualObjects(self.app.name, kFIRTestAppName1);
  154. XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  155. // Configure the same app again should throw an exception.
  156. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]]);
  157. }
  158. - (void)testConfigureWithNameAndCustomizedOptions {
  159. FIROptions *options = [FIROptions defaultOptions];
  160. FIROptions *newOptions = [options copy];
  161. newOptions.deepLinkURLScheme = kDeepLinkURLScheme;
  162. // Set up notification center observer for verifying notifications.
  163. [self.notificationCenter addMockObserver:self.observerMock
  164. name:kFIRAppReadyToConfigureSDKNotification
  165. object:[FIRApp class]];
  166. NSDictionary *expectedUserInfo1 =
  167. [self expectedUserInfoWithAppName:kFIRTestAppName1 isDefaultApp:NO];
  168. [[self.observerMock expect] notificationWithName:kFIRAppReadyToConfigureSDKNotification
  169. object:[FIRApp class]
  170. userInfo:expectedUserInfo1];
  171. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:newOptions]);
  172. XCTAssertTrue([FIRApp allApps].count == 1);
  173. self.app = [FIRApp appNamed:kFIRTestAppName1];
  174. // Configure a different app with valid customized options
  175. FIROptions *customizedOptions =
  176. [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  177. customizedOptions.bundleID = kBundleID;
  178. customizedOptions.APIKey = kCustomizedAPIKey;
  179. NSDictionary *expectedUserInfo2 =
  180. [self expectedUserInfoWithAppName:kFIRTestAppName2 isDefaultApp:NO];
  181. [[self.observerMock expect] notificationWithName:kFIRAppReadyToConfigureSDKNotification
  182. object:[FIRApp class]
  183. userInfo:expectedUserInfo2];
  184. [self.observerMock setExpectationOrderMatters:YES];
  185. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName2 options:customizedOptions]);
  186. OCMVerifyAll(self.observerMock);
  187. XCTAssertTrue([FIRApp allApps].count == 2);
  188. self.app = [FIRApp appNamed:kFIRTestAppName2];
  189. XCTAssertNotNil(self.app);
  190. XCTAssertEqualObjects(self.app.name, kFIRTestAppName2);
  191. XCTAssertEqualObjects(self.app.options.googleAppID, kGoogleAppID);
  192. XCTAssertEqualObjects(self.app.options.APIKey, kCustomizedAPIKey);
  193. }
  194. - (void)testValidName {
  195. XCTAssertNoThrow([FIRApp configureWithName:@"aA1_" options:[FIROptions defaultOptions]]);
  196. XCTAssertThrows([FIRApp configureWithName:@"aA1%" options:[FIROptions defaultOptions]]);
  197. XCTAssertThrows([FIRApp configureWithName:@"aA1?" options:[FIROptions defaultOptions]]);
  198. XCTAssertThrows([FIRApp configureWithName:@"aA1!" options:[FIROptions defaultOptions]]);
  199. }
  200. - (void)testDefaultApp {
  201. self.app = [FIRApp defaultApp];
  202. XCTAssertNil(self.app);
  203. [FIRApp configure];
  204. self.app = [FIRApp defaultApp];
  205. XCTAssertEqualObjects(self.app.name, kFIRDefaultAppName);
  206. XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  207. }
  208. - (void)testAppNamed {
  209. self.app = [FIRApp appNamed:kFIRTestAppName1];
  210. XCTAssertNil(self.app);
  211. [FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]];
  212. self.app = [FIRApp appNamed:kFIRTestAppName1];
  213. XCTAssertEqualObjects(self.app.name, kFIRTestAppName1);
  214. XCTAssertEqualObjects(self.app.options.clientID, kClientID);
  215. }
  216. - (void)testDeleteApp {
  217. [FIRApp configure];
  218. self.app = [FIRApp defaultApp];
  219. XCTAssertTrue([FIRApp allApps].count == 1);
  220. [self expectNotificationForObserver:self.observerMock
  221. notificationName:kFIRAppDeleteNotification
  222. object:[FIRApp class]
  223. userInfo:[OCMArg any]];
  224. [self.app deleteApp:^(BOOL success) {
  225. XCTAssertTrue(success);
  226. }];
  227. OCMVerifyAll(self.observerMock);
  228. XCTAssertTrue(self.app.alreadySentDeleteNotification);
  229. XCTAssertTrue([FIRApp allApps].count == 0);
  230. }
  231. - (void)testErrorForSubspecConfigurationFailure {
  232. NSError *error = [FIRApp errorForSubspecConfigurationFailureWithDomain:kFirebaseAdMobErrorDomain
  233. errorCode:FIRErrorCodeAdMobFailed
  234. service:kFIRServiceAdMob
  235. reason:@"some reason"];
  236. XCTAssertNotNil(error);
  237. XCTAssert([error.domain isEqualToString:kFirebaseAdMobErrorDomain]);
  238. XCTAssert(error.code == FIRErrorCodeAdMobFailed);
  239. XCTAssert([error.description containsString:@"Configuration failed for"]);
  240. }
  241. - (void)testGetTokenWithCallback {
  242. [FIRApp configure];
  243. FIRApp *app = [FIRApp defaultApp];
  244. __block BOOL getTokenImplementationWasCalled = NO;
  245. __block BOOL getTokenCallbackWasCalled = NO;
  246. __block BOOL passedRefreshValue = NO;
  247. [app getTokenForcingRefresh:YES
  248. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  249. getTokenCallbackWasCalled = YES;
  250. }];
  251. XCTAssert(getTokenCallbackWasCalled,
  252. @"The callback should be invoked by the base implementation when no block for "
  253. "'getTokenImplementation' has been specified.");
  254. getTokenCallbackWasCalled = NO;
  255. app.getTokenImplementation = ^(BOOL refresh, FIRTokenCallback callback) {
  256. getTokenImplementationWasCalled = YES;
  257. passedRefreshValue = refresh;
  258. callback(nil, nil);
  259. };
  260. [app getTokenForcingRefresh:YES
  261. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  262. getTokenCallbackWasCalled = YES;
  263. }];
  264. XCTAssert(getTokenImplementationWasCalled,
  265. @"The 'getTokenImplementation' block was never called.");
  266. XCTAssert(passedRefreshValue,
  267. @"The value for the 'refresh' parameter wasn't passed to the 'getTokenImplementation' "
  268. "block correctly.");
  269. XCTAssert(getTokenCallbackWasCalled,
  270. @"The 'getTokenImplementation' should have invoked the callback. This could be an "
  271. "error in this test, or the callback parameter may not have been passed to the "
  272. "implementation correctly.");
  273. getTokenImplementationWasCalled = NO;
  274. getTokenCallbackWasCalled = NO;
  275. passedRefreshValue = NO;
  276. [app getTokenForcingRefresh:NO
  277. withCallback:^(NSString *_Nullable token, NSError *_Nullable error) {
  278. getTokenCallbackWasCalled = YES;
  279. }];
  280. XCTAssertFalse(passedRefreshValue,
  281. @"The value for the 'refresh' parameter wasn't passed to the "
  282. "'getTokenImplementation' block correctly.");
  283. }
  284. - (void)testModifyingOptionsThrows {
  285. [FIRApp configure];
  286. FIROptions *options = [[FIRApp defaultApp] options];
  287. XCTAssertTrue(options.isEditingLocked);
  288. // Modification to every property should result in an exception.
  289. XCTAssertThrows(options.androidClientID = @"should_throw");
  290. XCTAssertThrows(options.APIKey = @"should_throw");
  291. XCTAssertThrows(options.bundleID = @"should_throw");
  292. XCTAssertThrows(options.clientID = @"should_throw");
  293. XCTAssertThrows(options.databaseURL = @"should_throw");
  294. XCTAssertThrows(options.deepLinkURLScheme = @"should_throw");
  295. XCTAssertThrows(options.GCMSenderID = @"should_throw");
  296. XCTAssertThrows(options.googleAppID = @"should_throw");
  297. XCTAssertThrows(options.projectID = @"should_throw");
  298. XCTAssertThrows(options.storageBucket = @"should_throw");
  299. XCTAssertThrows(options.trackingID = @"should_throw");
  300. }
  301. - (void)testOptionsLocking {
  302. FIROptions *options =
  303. [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  304. options.projectID = kProjectID;
  305. options.databaseURL = kDatabaseURL;
  306. // Options should not be locked before they are used to configure a `FIRApp`.
  307. XCTAssertFalse(options.isEditingLocked);
  308. // The options returned should be locked after configuring `FIRApp`.
  309. [FIRApp configureWithOptions:options];
  310. FIROptions *optionsCopy = [[FIRApp defaultApp] options];
  311. XCTAssertTrue(optionsCopy.isEditingLocked);
  312. }
  313. #pragma mark - App ID v1
  314. - (void)testAppIDV1 {
  315. // Missing separator between platform:fingerprint.
  316. XCTAssertFalse([FIRApp validateAppID:@"1:1337:iosdeadbeef"]);
  317. // Wrong platform "android".
  318. XCTAssertFalse([FIRApp validateAppID:@"1:1337:android:deadbeef"]);
  319. // The fingerprint, aka 4th field, should only contain hex characters.
  320. XCTAssertFalse([FIRApp validateAppID:@"1:1337:ios:123abcxyz"]);
  321. // The fingerprint, aka 4th field, is not tested in V1, so a bad value shouldn't cause a failure.
  322. XCTAssertTrue([FIRApp validateAppID:@"1:1337:ios:deadbeef"]);
  323. }
  324. #pragma mark - App ID v2
  325. - (void)testAppIDV2 {
  326. // Missing separator between platform:fingerprint.
  327. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios5e18052ab54fbfec"]);
  328. // Unknown versions may contain anything.
  329. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:123abcxyz"]);
  330. XCTAssertTrue([FIRApp validateAppID:@"2:thisdoesn'teven_m:a:t:t:e:r_"]);
  331. // Known good fingerprint.
  332. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:5e18052ab54fbfec"]);
  333. // Unknown fingerprint, not tested so shouldn't cause a failure.
  334. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:deadbeef"]);
  335. }
  336. #pragma mark - App ID other
  337. - (void)testAppIDV3 {
  338. // Currently there is no specification for v3, so we would not expect it to fail.
  339. XCTAssertTrue([FIRApp validateAppID:@"3:1337:ios:deadbeef"]);
  340. }
  341. - (void)testAppIDEmpty {
  342. XCTAssertFalse([FIRApp validateAppID:@""]);
  343. }
  344. - (void)testAppIDValidationTrue {
  345. // Ensure that isAppIDValid matches validateAppID.
  346. [FIRApp configure];
  347. OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(YES);
  348. XCTAssertTrue([[FIRApp defaultApp] isAppIDValid]);
  349. }
  350. - (void)testAppIDValidationFalse {
  351. // Ensure that isAppIDValid matches validateAppID.
  352. [FIRApp configure];
  353. OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(NO);
  354. XCTAssertFalse([[FIRApp defaultApp] isAppIDValid]);
  355. }
  356. - (void)testAppIDPrefix {
  357. // Unknown numeric-character prefixes should pass.
  358. XCTAssertTrue([FIRApp validateAppID:@"0:"]);
  359. XCTAssertTrue([FIRApp validateAppID:@"01:"]);
  360. XCTAssertTrue([FIRApp validateAppID:@"10:"]);
  361. XCTAssertTrue([FIRApp validateAppID:@"010:"]);
  362. XCTAssertTrue([FIRApp validateAppID:@"3:"]);
  363. XCTAssertTrue([FIRApp validateAppID:@"123:"]);
  364. XCTAssertTrue([FIRApp validateAppID:@"999999999:"]);
  365. // Non-numeric prefixes should not pass.
  366. XCTAssertFalse([FIRApp validateAppID:@"a:"]);
  367. XCTAssertFalse([FIRApp validateAppID:@"abcsdf0:"]);
  368. XCTAssertFalse([FIRApp validateAppID:@"0aaaa:"]);
  369. XCTAssertFalse([FIRApp validateAppID:@"0aaaa0450:"]);
  370. XCTAssertFalse([FIRApp validateAppID:@"-1:"]);
  371. XCTAssertFalse([FIRApp validateAppID:@"abcsdf:"]);
  372. XCTAssertFalse([FIRApp validateAppID:@"ABDCF:"]);
  373. XCTAssertFalse([FIRApp validateAppID:@" :"]);
  374. XCTAssertFalse([FIRApp validateAppID:@"1 :"]);
  375. XCTAssertFalse([FIRApp validateAppID:@" 1:"]);
  376. XCTAssertFalse([FIRApp validateAppID:@" 123 :"]);
  377. XCTAssertFalse([FIRApp validateAppID:@"1 23:"]);
  378. XCTAssertFalse([FIRApp validateAppID:@"&($*&%(*$&:"]);
  379. XCTAssertFalse([FIRApp validateAppID:@"abCDSF$%%df:"]);
  380. // Known version prefixes should never pass without the rest of the app ID string present.
  381. XCTAssertFalse([FIRApp validateAppID:@"1:"]);
  382. // Version must include ":".
  383. XCTAssertFalse([FIRApp validateAppID:@"0"]);
  384. XCTAssertFalse([FIRApp validateAppID:@"01"]);
  385. XCTAssertFalse([FIRApp validateAppID:@"10"]);
  386. XCTAssertFalse([FIRApp validateAppID:@"010"]);
  387. XCTAssertFalse([FIRApp validateAppID:@"3"]);
  388. XCTAssertFalse([FIRApp validateAppID:@"123"]);
  389. XCTAssertFalse([FIRApp validateAppID:@"999999999"]);
  390. XCTAssertFalse([FIRApp validateAppID:@"com.google.bundleID"]);
  391. }
  392. - (void)testAppIDFormatInvalid {
  393. OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  394. // Some direct tests of the validateAppIDFormat:withVersion: method.
  395. // Sanity checks first.
  396. NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  397. NSString *const kGoodVersionV1 = @"1:";
  398. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV1]);
  399. NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  400. NSString *const kGoodVersionV2 = @"2:";
  401. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);
  402. // Version mismatch.
  403. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV1]);
  404. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV2]);
  405. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@"999:"]);
  406. // Nil or empty strings.
  407. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:nil]);
  408. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@""]);
  409. XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:kGoodVersionV1]);
  410. XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:kGoodVersionV1]);
  411. XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:nil]);
  412. XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:@""]);
  413. // App ID contains only the version prefix.
  414. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodVersionV1 withVersion:kGoodVersionV1]);
  415. // The version is the entire app ID.
  416. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  417. // Versions digits that may make a partial match.
  418. XCTAssertFalse([FIRApp validateAppIDFormat:@"01:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  419. XCTAssertFalse([FIRApp validateAppIDFormat:@"10:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  420. XCTAssertFalse([FIRApp validateAppIDFormat:@"11:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  421. XCTAssertFalse(
  422. [FIRApp validateAppIDFormat:@"21:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  423. XCTAssertFalse(
  424. [FIRApp validateAppIDFormat:@"22:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  425. XCTAssertFalse(
  426. [FIRApp validateAppIDFormat:@"02:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  427. XCTAssertFalse(
  428. [FIRApp validateAppIDFormat:@"20:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  429. // Extra fields.
  430. XCTAssertFalse([FIRApp validateAppIDFormat:@"ab:1:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  431. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:ab:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  432. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ab:ios:deadbeef" withVersion:kGoodVersionV1]);
  433. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:ab:deadbeef" withVersion:kGoodVersionV1]);
  434. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:deadbeef:ab" withVersion:kGoodVersionV1]);
  435. }
  436. - (void)testAppIDFingerprintInvalid {
  437. OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  438. // Some direct tests of the validateAppIDFingerprint:withVersion: method.
  439. // Sanity checks first.
  440. NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  441. NSString *const kGoodVersionV1 = @"1:";
  442. XCTAssertTrue([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodVersionV1]);
  443. NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  444. NSString *const kGoodVersionV2 = @"2:";
  445. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);
  446. // Version mismatch.
  447. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV2 withVersion:kGoodVersionV1]);
  448. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodVersionV2]);
  449. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:@"999:"]);
  450. // Nil or empty strings.
  451. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:nil]);
  452. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:@""]);
  453. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:kGoodVersionV1]);
  454. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:kGoodVersionV1]);
  455. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:nil]);
  456. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:@""]);
  457. // App ID contains only the version prefix.
  458. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodVersionV1 withVersion:kGoodVersionV1]);
  459. // The version is the entire app ID.
  460. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  461. // Versions digits that may make a partial match.
  462. XCTAssertFalse(
  463. [FIRApp validateAppIDFingerprint:@"01:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  464. XCTAssertFalse(
  465. [FIRApp validateAppIDFingerprint:@"10:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  466. XCTAssertFalse(
  467. [FIRApp validateAppIDFingerprint:@"11:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  468. XCTAssertFalse(
  469. [FIRApp validateAppIDFingerprint:@"21:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  470. XCTAssertFalse(
  471. [FIRApp validateAppIDFingerprint:@"22:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  472. XCTAssertFalse(
  473. [FIRApp validateAppIDFingerprint:@"02:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  474. XCTAssertFalse(
  475. [FIRApp validateAppIDFingerprint:@"20:1337:ios:5e18052ab54fbfec" withVersion:kGoodVersionV2]);
  476. // Extra fields.
  477. XCTAssertFalse(
  478. [FIRApp validateAppIDFingerprint:@"ab:1:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  479. XCTAssertFalse(
  480. [FIRApp validateAppIDFingerprint:@"1:ab:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  481. XCTAssertFalse(
  482. [FIRApp validateAppIDFingerprint:@"1:1337:ab:ios:deadbeef" withVersion:kGoodVersionV1]);
  483. XCTAssertFalse(
  484. [FIRApp validateAppIDFingerprint:@"1:1337:ios:ab:deadbeef" withVersion:kGoodVersionV1]);
  485. XCTAssertFalse(
  486. [FIRApp validateAppIDFingerprint:@"1:1337:ios:deadbeef:ab" withVersion:kGoodVersionV1]);
  487. }
  488. #pragma mark - Automatic Data Collection Tests
  489. - (void)testGlobalDataCollectionNoFlags {
  490. // Test: No flags set.
  491. [FIRApp configure];
  492. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  493. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  494. .andReturn(nil);
  495. XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
  496. }
  497. - (void)testGlobalDataCollectionPlistSetEnabled {
  498. // Test: Plist set to enabled, no override.
  499. [FIRApp configure];
  500. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  501. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  502. .andReturn(nil);
  503. XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
  504. }
  505. - (void)testGlobalDataCollectionPlistSetDisabled {
  506. // Test: Plist set to disabled, no override.
  507. [FIRApp configure];
  508. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  509. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  510. .andReturn(nil);
  511. XCTAssertFalse([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
  512. }
  513. - (void)testGlobalDataCollectionUserSpecifiedEnabled {
  514. // Test: User specified as enabled, no plist value.
  515. [FIRApp configure];
  516. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  517. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  518. .andReturn(@YES);
  519. XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
  520. }
  521. - (void)testGlobalDataCollectionUserSpecifiedDisabled {
  522. // Test: User specified as disabled, no plist value.
  523. [FIRApp configure];
  524. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  525. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  526. .andReturn(@NO);
  527. XCTAssertFalse([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
  528. }
  529. - (void)testGlobalDataCollectionUserOverriddenEnabled {
  530. // Test: User specified as enabled, with plist set as disabled.
  531. [FIRApp configure];
  532. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  533. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  534. .andReturn(@YES);
  535. XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
  536. }
  537. - (void)testGlobalDataCollectionUserOverriddenDisabled {
  538. // Test: User specified as disabled, with plist set as enabled.
  539. [FIRApp configure];
  540. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  541. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  542. .andReturn(@NO);
  543. XCTAssertFalse([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
  544. }
  545. - (void)testGlobalDataCollectionWriteToDefaults {
  546. id defaultsMock = OCMPartialMock([NSUserDefaults standardUserDefaults]);
  547. [FIRApp configure];
  548. FIRApp *app = [FIRApp defaultApp];
  549. app.automaticDataCollectionEnabled = YES;
  550. NSString *key =
  551. [NSString stringWithFormat:kFIRGlobalAppDataCollectionEnabledDefaultsKeyFormat, app.name];
  552. OCMVerify([defaultsMock setObject:@YES forKey:key]);
  553. [FIRApp defaultApp].automaticDataCollectionEnabled = NO;
  554. OCMVerify([defaultsMock setObject:@NO forKey:key]);
  555. [defaultsMock stopMocking];
  556. }
  557. - (void)testGlobalDataCollectionClearedAfterDelete {
  558. // Configure and disable data collection for the default FIRApp.
  559. [FIRApp configure];
  560. FIRApp *app = [FIRApp defaultApp];
  561. app.automaticDataCollectionEnabled = NO;
  562. XCTAssertFalse(app.isAutomaticDataCollectionEnabled);
  563. // Delete the app, and verify that the switch was reset.
  564. XCTestExpectation *deleteFinished =
  565. [self expectationWithDescription:@"The app should successfully delete."];
  566. [app deleteApp:^(BOOL success) {
  567. if (success) {
  568. [deleteFinished fulfill];
  569. }
  570. }];
  571. // Wait for the delete to complete.
  572. [self waitForExpectations:@[ deleteFinished ] timeout:1];
  573. // Set up the default app again, and check the data collection flag.
  574. [FIRApp configure];
  575. XCTAssertTrue([FIRApp defaultApp].isAutomaticDataCollectionEnabled);
  576. }
  577. - (void)testGlobalDataCollectionNoDiagnosticsSent {
  578. [FIRApp configure];
  579. // Add an observer for the diagnostics notification - both with and without an object to ensure it
  580. // catches it either way. Currently no object is sent, but in the future that could change.
  581. [self.notificationCenter addMockObserver:self.observerMock
  582. name:kFIRAppDiagnosticsNotification
  583. object:nil];
  584. [self.notificationCenter addMockObserver:self.observerMock
  585. name:kFIRAppDiagnosticsNotification
  586. object:OCMOCK_ANY];
  587. // Stub out reading from user defaults since stubbing out the BOOL has issues. If the data
  588. // collection switch is disabled, the `sendLogs` call should return immediately and not fire a
  589. // notification.
  590. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  591. .andReturn(@NO);
  592. NSError *error = [NSError errorWithDomain:@"com.firebase" code:42 userInfo:nil];
  593. [[FIRApp defaultApp] sendLogsWithServiceName:@"Service" version:@"Version" error:error];
  594. // The observer mock is strict and will raise an exception when an unexpected notification is
  595. // received.
  596. OCMVerifyAll(self.observerMock);
  597. }
  598. #pragma mark - Analytics Flag Tests
  599. - (void)testAnalyticsSetByGlobalDataCollectionSwitch {
  600. // Test that the global data collection switch triggers setting Analytics when no explicit flag is
  601. // set.
  602. [FIRApp configure];
  603. id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  604. OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  605. OCMStub([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
  606. OCMStub([self.optionsInstanceMock isAnalyticsCollectionExpicitlySet]).andReturn(NO);
  607. // Ensure Analytics is set after the global flag is set.
  608. [[FIRApp defaultApp] setAutomaticDataCollectionEnabled:YES];
  609. OCMVerify([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:NO]);
  610. [[FIRApp defaultApp] setAutomaticDataCollectionEnabled:NO];
  611. OCMVerify([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:NO]);
  612. }
  613. - (void)testAnalyticsNotSetByGlobalDataCollectionSwitch {
  614. // Test that the global data collection switch doesn't override an explicitly set Analytics flag.
  615. [FIRApp configure];
  616. id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  617. OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  618. OCMStub([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
  619. OCMStub([self.optionsInstanceMock isAnalyticsCollectionExpicitlySet]).andReturn(YES);
  620. // Reject any changes to Analytics when the data collection changes.
  621. [[FIRApp defaultApp] setAutomaticDataCollectionEnabled:YES];
  622. OCMReject([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
  623. [[FIRApp defaultApp] setAutomaticDataCollectionEnabled:NO];
  624. OCMReject([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
  625. }
  626. #pragma mark - Internal Methods
  627. - (void)testAuthGetUID {
  628. [FIRApp configure];
  629. [FIRApp defaultApp].getUIDImplementation = ^NSString * {
  630. return @"highlander";
  631. };
  632. XCTAssertEqual([[FIRApp defaultApp] getUID], @"highlander");
  633. }
  634. - (void)testIsAppConfigured {
  635. // Ensure it's false before anything is configured.
  636. XCTAssertFalse([FIRApp isDefaultAppConfigured]);
  637. // Configure it and ensure it's configured.
  638. [FIRApp configure];
  639. XCTAssertTrue([FIRApp isDefaultAppConfigured]);
  640. // Reset the apps and ensure it's not configured anymore.
  641. [FIRApp resetApps];
  642. XCTAssertFalse([FIRApp isDefaultAppConfigured]);
  643. }
  644. - (void)testIllegalLibraryName {
  645. [FIRApp registerLibrary:@"Oops>" withVersion:@"1.0.0"];
  646. XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:@""]);
  647. }
  648. - (void)testIllegalLibraryVersion {
  649. [FIRApp registerLibrary:@"LegalName" withVersion:@"1.0.0+"];
  650. XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:@""]);
  651. }
  652. - (void)testSingleLibrary {
  653. [FIRApp registerLibrary:@"LegalName" withVersion:@"1.0.0"];
  654. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"LegalName/1.0.0"]);
  655. }
  656. - (void)testMultipleLibraries {
  657. [FIRApp registerLibrary:@"LegalName" withVersion:@"1.0.0"];
  658. [FIRApp registerLibrary:@"LegalName2" withVersion:@"2.0.0"];
  659. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"LegalName/1.0.0 LegalName2/2.0.0"]);
  660. }
  661. #pragma mark - private
  662. - (void)expectNotificationForObserver:(id)observer
  663. notificationName:(NSNotificationName)name
  664. object:(nullable id)object
  665. userInfo:(nullable NSDictionary *)userInfo {
  666. [self.notificationCenter addMockObserver:observer name:name object:object];
  667. [[observer expect] notificationWithName:name object:object userInfo:userInfo];
  668. }
  669. - (NSDictionary<NSString *, NSObject *> *)expectedUserInfoWithAppName:(NSString *)name
  670. isDefaultApp:(BOOL)isDefaultApp {
  671. return @{
  672. kFIRAppNameKey : name,
  673. kFIRAppIsDefaultAppKey : [NSNumber numberWithBool:isDefaultApp],
  674. kFIRGoogleAppIDKey : kGoogleAppID
  675. };
  676. }
  677. @end