FIRAppTest.m 32 KB

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