FIRAppTest.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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.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:kFirebaseCoreErrorDomain
  220. errorCode:-38
  221. service:kFIRServiceAuth
  222. reason:@"some reason"];
  223. XCTAssertNotNil(error);
  224. XCTAssert([error.domain isEqualToString:kFirebaseCoreErrorDomain]);
  225. XCTAssert(error.code == -38);
  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. // Nil or empty strings.
  377. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:nil]);
  378. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:@""]);
  379. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:kGoodVersionV1]);
  380. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:kGoodVersionV1]);
  381. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:nil]);
  382. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:@""]);
  383. // App ID contains only the version prefix.
  384. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodVersionV1 withVersion:kGoodVersionV1]);
  385. // The version is the entire app ID.
  386. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  387. }
  388. // Uncomment if you need to measure performance of [FIRApp validateAppID:].
  389. // It is commented because measures are heavily dependent on a build agent configuration,
  390. // so it cannot produce reliable resault on CI
  391. //- (void)testAppIDFingerprintPerfomance {
  392. // [self measureBlock:^{
  393. // for (NSInteger i = 0; i < 100; ++i) {
  394. // [self testAppIDPrefix];
  395. // }
  396. // }];
  397. //}
  398. #pragma mark - Automatic Data Collection Tests
  399. - (void)testGlobalDataCollectionNoFlags {
  400. // Test: No flags set.
  401. NSString *name = NSStringFromSelector(_cmd);
  402. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  403. GCMSenderID:kGCMSenderID];
  404. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  405. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  406. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  407. .andReturn(nil);
  408. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  409. }
  410. - (void)testGlobalDataCollectionPlistSetEnabled {
  411. // Test: Plist set to enabled, no override.
  412. NSString *name = NSStringFromSelector(_cmd);
  413. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  414. GCMSenderID:kGCMSenderID];
  415. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  416. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  417. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  418. .andReturn(nil);
  419. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  420. }
  421. - (void)testGlobalDataCollectionPlistSetDisabled {
  422. // Test: Plist set to disabled, no override.
  423. NSString *name = NSStringFromSelector(_cmd);
  424. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  425. GCMSenderID:kGCMSenderID];
  426. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  427. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  428. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  429. .andReturn(nil);
  430. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  431. }
  432. - (void)testGlobalDataCollectionUserSpecifiedEnabled {
  433. // Test: User specified as enabled, no plist value.
  434. NSString *name = NSStringFromSelector(_cmd);
  435. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  436. GCMSenderID:kGCMSenderID];
  437. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  438. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  439. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  440. .andReturn(@YES);
  441. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  442. }
  443. - (void)testGlobalDataCollectionUserSpecifiedDisabled {
  444. // Test: User specified as disabled, no plist value.
  445. NSString *name = NSStringFromSelector(_cmd);
  446. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  447. GCMSenderID:kGCMSenderID];
  448. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  449. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  450. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  451. .andReturn(@NO);
  452. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  453. }
  454. - (void)testGlobalDataCollectionUserOverriddenEnabled {
  455. // Test: User specified as enabled, with plist set as disabled.
  456. NSString *name = NSStringFromSelector(_cmd);
  457. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  458. GCMSenderID:kGCMSenderID];
  459. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  460. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  461. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  462. .andReturn(@YES);
  463. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  464. }
  465. - (void)testGlobalDataCollectionUserOverriddenDisabled {
  466. // Test: User specified as disabled, with plist set as enabled.
  467. NSString *name = NSStringFromSelector(_cmd);
  468. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  469. GCMSenderID:kGCMSenderID];
  470. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  471. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  472. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  473. .andReturn(@NO);
  474. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  475. }
  476. - (void)testGlobalDataCollectionWriteToDefaults {
  477. id defaultsMock = OCMPartialMock([NSUserDefaults standardUserDefaults]);
  478. NSString *name = NSStringFromSelector(_cmd);
  479. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  480. GCMSenderID:kGCMSenderID];
  481. [FIRApp configureWithName:name options:options];
  482. FIRApp *app = [FIRApp appNamed:name];
  483. app.dataCollectionDefaultEnabled = YES;
  484. NSString *key =
  485. [NSString stringWithFormat:kFIRGlobalAppDataCollectionEnabledDefaultsKeyFormat, app.name];
  486. OCMVerify([defaultsMock setObject:@YES forKey:key]);
  487. app.dataCollectionDefaultEnabled = NO;
  488. OCMVerify([defaultsMock setObject:@NO forKey:key]);
  489. [defaultsMock stopMocking];
  490. }
  491. - (void)testGlobalDataCollectionClearedAfterDelete {
  492. // Configure and disable data collection for the default FIRApp.
  493. NSString *name = NSStringFromSelector(_cmd);
  494. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  495. GCMSenderID:kGCMSenderID];
  496. [FIRApp configureWithName:name options:options];
  497. FIRApp *app = [FIRApp appNamed:name];
  498. app.dataCollectionDefaultEnabled = NO;
  499. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  500. // Delete the app, and verify that the switch was reset.
  501. XCTestExpectation *deleteFinished =
  502. [self expectationWithDescription:@"The app should successfully delete."];
  503. [app deleteApp:^(BOOL success) {
  504. XCTAssertTrue(success);
  505. [deleteFinished fulfill];
  506. }];
  507. // Wait for the delete to complete.
  508. [self waitForExpectations:@[ deleteFinished ] timeout:1];
  509. // Set up an app with the same name again, and check the data collection flag.
  510. [FIRApp configureWithName:name options:options];
  511. XCTAssertTrue([FIRApp appNamed:name].isDataCollectionDefaultEnabled);
  512. }
  513. - (void)testGlobalDataCollectionNoDiagnosticsSent {
  514. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  515. GCMSenderID:kGCMSenderID];
  516. FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
  517. app.dataCollectionDefaultEnabled = NO;
  518. // Add an observer for the diagnostics notification. Currently no object is sent, but in the
  519. // future that could change so leave it as OCMOCK_ANY.
  520. [self.notificationCenter addMockObserver:self.observerMock
  521. name:kFIRAppDiagnosticsNotification
  522. object:OCMOCK_ANY];
  523. // Stub out reading from user defaults since stubbing out the BOOL has issues. If the data
  524. // collection switch is disabled, the `sendLogs` call should return immediately and not fire a
  525. // notification.
  526. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  527. .andReturn(@NO);
  528. // Ensure configure doesn't fire a notification.
  529. [FIRApp configure];
  530. NSError *error = [NSError errorWithDomain:@"com.firebase" code:42 userInfo:nil];
  531. [app sendLogsWithServiceName:@"Service" version:@"Version" error:error];
  532. // The observer mock is strict and will raise an exception when an unexpected notification is
  533. // received.
  534. OCMVerifyAll(self.observerMock);
  535. }
  536. #pragma mark - Analytics Flag Tests
  537. - (void)testAnalyticsSetByGlobalDataCollectionSwitch {
  538. // Test that the global data collection switch triggers setting Analytics when no explicit flag is
  539. // set.
  540. id optionsMock = OCMClassMock([FIROptions class]);
  541. OCMStub([optionsMock isAnalyticsCollectionExpicitlySet]).andReturn(NO);
  542. // We need to use the default app name since Analytics only associates with the default app.
  543. FIRApp *defaultApp = [[FIRApp alloc] initInstanceWithName:kFIRDefaultAppName options:optionsMock];
  544. id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  545. OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  546. OCMStub([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
  547. // Ensure Analytics is set after the global flag is set. It needs to
  548. [defaultApp setDataCollectionDefaultEnabled:YES];
  549. OCMVerify([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:NO]);
  550. [defaultApp setDataCollectionDefaultEnabled:NO];
  551. OCMVerify([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:NO]);
  552. }
  553. - (void)testAnalyticsNotSetByGlobalDataCollectionSwitch {
  554. // Test that the global data collection switch doesn't override an explicitly set Analytics flag.
  555. id optionsMock = OCMClassMock([FIROptions class]);
  556. OCMStub([optionsMock isAnalyticsCollectionExpicitlySet]).andReturn(YES);
  557. FIRApp *app = [[FIRApp alloc] initInstanceWithName:@"testAnalyticsNotSet" options:optionsMock];
  558. id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  559. OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  560. OCMStub([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
  561. // Reject any changes to Analytics when the data collection changes.
  562. [app setDataCollectionDefaultEnabled:YES];
  563. OCMReject([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
  564. [app setDataCollectionDefaultEnabled:NO];
  565. OCMReject([configurationMock setAnalyticsCollectionEnabled:OCMOCK_ANY persistSetting:OCMOCK_ANY]);
  566. }
  567. #pragma mark - Internal Methods
  568. - (void)testIsDefaultAppConfigured {
  569. // Ensure it's false before anything is configured.
  570. XCTAssertFalse([FIRApp isDefaultAppConfigured]);
  571. // Configure it and ensure it's configured.
  572. [FIRApp configure];
  573. XCTAssertTrue([FIRApp isDefaultAppConfigured]);
  574. // Reset the apps and ensure it's not configured anymore.
  575. [FIRApp resetApps];
  576. XCTAssertFalse([FIRApp isDefaultAppConfigured]);
  577. }
  578. - (void)testInvalidLibraryName {
  579. [FIRApp registerLibrary:@"Oops>" withVersion:@"1.0.0"];
  580. XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:@""]);
  581. }
  582. - (void)testInvalidLibraryVersion {
  583. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0+"];
  584. XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:@""]);
  585. }
  586. - (void)testSingleLibrary {
  587. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0"];
  588. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0"]);
  589. }
  590. - (void)testMultipleLibraries {
  591. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0"];
  592. [FIRApp registerLibrary:@"ValidName2" withVersion:@"2.0.0"];
  593. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0 ValidName2/2.0.0"]);
  594. }
  595. - (void)testRegisteringConformingLibrary {
  596. Class testClass = [FIRTestClass class];
  597. [FIRApp registerInternalLibrary:testClass withName:@"ValidName" withVersion:@"1.0.0"];
  598. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0"]);
  599. }
  600. - (void)testRegisteringNonConformingLibrary {
  601. XCTAssertThrows([FIRApp registerInternalLibrary:[NSString class]
  602. withName:@"InvalidLibrary"
  603. withVersion:@"1.0.0"]);
  604. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"InvalidLibrary`/1.0.0"]);
  605. }
  606. #pragma mark - private
  607. - (void)expectNotificationForObserver:(id)observer
  608. notificationName:(NSNotificationName)name
  609. object:(nullable id)object
  610. userInfo:(nullable NSDictionary *)userInfo {
  611. [self.notificationCenter addMockObserver:observer name:name object:object];
  612. [[observer expect] notificationWithName:name object:object userInfo:userInfo];
  613. }
  614. - (NSDictionary<NSString *, NSObject *> *)expectedUserInfoWithAppName:(NSString *)name
  615. isDefaultApp:(BOOL)isDefaultApp {
  616. return @{
  617. kFIRAppNameKey : name,
  618. kFIRAppIsDefaultAppKey : [NSNumber numberWithBool:isDefaultApp],
  619. kFIRGoogleAppIDKey : kGoogleAppID
  620. };
  621. }
  622. @end