FIRAppTest.m 34 KB

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