FIRAppTest.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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. XCTAssertNoThrow([FIRApp configureWithName:@"aA1-" options:[FIROptions defaultOptions]]);
  177. XCTAssertNoThrow([FIRApp configureWithName:@"aAē1_" options:[FIROptions defaultOptions]]);
  178. XCTAssertThrows([FIRApp configureWithName:@"aA1%" options:[FIROptions defaultOptions]]);
  179. XCTAssertThrows([FIRApp configureWithName:@"aA1?" options:[FIROptions defaultOptions]]);
  180. XCTAssertThrows([FIRApp configureWithName:@"aA1!" options:[FIROptions defaultOptions]]);
  181. }
  182. - (void)testDefaultApp {
  183. FIRApp *app = [FIRApp defaultApp];
  184. XCTAssertNil(app);
  185. [FIRApp configure];
  186. app = [FIRApp defaultApp];
  187. XCTAssertEqualObjects(app.name, kFIRDefaultAppName);
  188. XCTAssertEqualObjects(app.options.clientID, kClientID);
  189. }
  190. - (void)testAppNamed {
  191. FIRApp *app = [FIRApp appNamed:kFIRTestAppName1];
  192. XCTAssertNil(app);
  193. [FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]];
  194. app = [FIRApp appNamed:kFIRTestAppName1];
  195. XCTAssertEqualObjects(app.name, kFIRTestAppName1);
  196. XCTAssertEqualObjects(app.options.clientID, kClientID);
  197. }
  198. - (void)testDeleteApp {
  199. NSString *name = NSStringFromSelector(_cmd);
  200. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  201. GCMSenderID:kGCMSenderID];
  202. [FIRApp configureWithName:name options:options];
  203. FIRApp *app = [FIRApp appNamed:name];
  204. XCTAssertNotNil(app);
  205. XCTAssertTrue([FIRApp allApps].count == 1);
  206. [self expectNotificationForObserver:self.observerMock
  207. notificationName:kFIRAppDeleteNotification
  208. object:[FIRApp class]
  209. userInfo:[OCMArg any]];
  210. XCTestExpectation *expectation =
  211. [self expectationWithDescription:@"Deleting the app should succeed."];
  212. [app deleteApp:^(BOOL success) {
  213. XCTAssertTrue(success);
  214. [expectation fulfill];
  215. }];
  216. [self waitForExpectations:@[ expectation ] timeout:1];
  217. OCMVerifyAll(self.observerMock);
  218. XCTAssertTrue([FIRApp allApps].count == 0);
  219. }
  220. - (void)testErrorForSubspecConfigurationFailure {
  221. NSError *error = [FIRApp errorForSubspecConfigurationFailureWithDomain:kFirebaseCoreErrorDomain
  222. errorCode:-38
  223. service:kFIRServiceAuth
  224. reason:@"some reason"];
  225. XCTAssertNotNil(error);
  226. XCTAssert([error.domain isEqualToString:kFirebaseCoreErrorDomain]);
  227. XCTAssert(error.code == -38);
  228. XCTAssert([error.description containsString:@"Configuration failed for"]);
  229. }
  230. - (void)testOptionsLocking {
  231. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  232. GCMSenderID:kGCMSenderID];
  233. options.projectID = kProjectID;
  234. options.databaseURL = kDatabaseURL;
  235. // Options should not be locked before they are used to configure a `FIRApp`.
  236. XCTAssertFalse(options.isEditingLocked);
  237. // The options returned should be locked after configuring `FIRApp`.
  238. NSString *name = NSStringFromSelector(_cmd);
  239. [FIRApp configureWithName:name options:options];
  240. FIROptions *optionsCopy = [[FIRApp appNamed:name] options];
  241. XCTAssertTrue(optionsCopy.isEditingLocked);
  242. }
  243. #pragma mark - App ID v1
  244. - (void)testAppIDV1 {
  245. // Missing separator between platform:fingerprint.
  246. XCTAssertFalse([FIRApp validateAppID:@"1:1337:iosdeadbeef"]);
  247. // Wrong platform "android".
  248. XCTAssertFalse([FIRApp validateAppID:@"1:1337:android:deadbeef"]);
  249. // The fingerprint, aka 4th field, should only contain hex characters.
  250. XCTAssertFalse([FIRApp validateAppID:@"1:1337:ios:123abcxyz"]);
  251. // The fingerprint, aka 4th field, is not tested in V1, so a bad value shouldn't cause a failure.
  252. XCTAssertTrue([FIRApp validateAppID:@"1:1337:ios:deadbeef"]);
  253. }
  254. #pragma mark - App ID v2
  255. - (void)testAppIDV2 {
  256. // Missing separator between platform:fingerprint.
  257. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios5e18052ab54fbfec"]);
  258. // Unknown versions may contain anything.
  259. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:123abcxyz"]);
  260. XCTAssertTrue([FIRApp validateAppID:@"2:thisdoesn'teven_m:a:t:t:e:r_"]);
  261. // Known good fingerprint.
  262. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:5e18052ab54fbfec"]);
  263. // Unknown fingerprint, not tested so shouldn't cause a failure.
  264. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:deadbeef"]);
  265. }
  266. #pragma mark - App ID other
  267. - (void)testAppIDV3 {
  268. // Currently there is no specification for v3, so we would not expect it to fail.
  269. XCTAssertTrue([FIRApp validateAppID:@"3:1337:ios:deadbeef"]);
  270. }
  271. - (void)testAppIDEmpty {
  272. XCTAssertFalse([FIRApp validateAppID:@""]);
  273. }
  274. - (void)testAppIDValidationTrue {
  275. // Ensure that isAppIDValid matches validateAppID.
  276. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"" GCMSenderID:@""];
  277. FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
  278. OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(YES);
  279. XCTAssertTrue([app isAppIDValid]);
  280. }
  281. - (void)testAppIDValidationFalse {
  282. // Ensure that isAppIDValid matches validateAppID.
  283. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"" GCMSenderID:@""];
  284. FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
  285. OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(NO);
  286. XCTAssertFalse([app isAppIDValid]);
  287. }
  288. - (void)testAppIDPrefix {
  289. // Unknown numeric-character prefixes should pass.
  290. XCTAssertTrue([FIRApp validateAppID:@"0:"]);
  291. XCTAssertTrue([FIRApp validateAppID:@"01:"]);
  292. XCTAssertTrue([FIRApp validateAppID:@"10:"]);
  293. XCTAssertTrue([FIRApp validateAppID:@"010:"]);
  294. XCTAssertTrue([FIRApp validateAppID:@"3:"]);
  295. XCTAssertTrue([FIRApp validateAppID:@"123:"]);
  296. XCTAssertTrue([FIRApp validateAppID:@"999999999:"]);
  297. // Non-numeric prefixes should not pass.
  298. XCTAssertFalse([FIRApp validateAppID:@"a:"]);
  299. XCTAssertFalse([FIRApp validateAppID:@"abcsdf0:"]);
  300. XCTAssertFalse([FIRApp validateAppID:@"0aaaa:"]);
  301. XCTAssertFalse([FIRApp validateAppID:@"0aaaa0450:"]);
  302. XCTAssertFalse([FIRApp validateAppID:@"-1:"]);
  303. XCTAssertFalse([FIRApp validateAppID:@"abcsdf:"]);
  304. XCTAssertFalse([FIRApp validateAppID:@"ABDCF:"]);
  305. XCTAssertFalse([FIRApp validateAppID:@" :"]);
  306. XCTAssertFalse([FIRApp validateAppID:@"1 :"]);
  307. XCTAssertFalse([FIRApp validateAppID:@" 1:"]);
  308. XCTAssertFalse([FIRApp validateAppID:@" 123 :"]);
  309. XCTAssertFalse([FIRApp validateAppID:@"1 23:"]);
  310. XCTAssertFalse([FIRApp validateAppID:@"&($*&%(*$&:"]);
  311. XCTAssertFalse([FIRApp validateAppID:@"abCDSF$%%df:"]);
  312. // Known version prefixes should never pass without the rest of the app ID string present.
  313. XCTAssertFalse([FIRApp validateAppID:@"1:"]);
  314. // Version must include ":".
  315. XCTAssertFalse([FIRApp validateAppID:@"0"]);
  316. XCTAssertFalse([FIRApp validateAppID:@"01"]);
  317. XCTAssertFalse([FIRApp validateAppID:@"10"]);
  318. XCTAssertFalse([FIRApp validateAppID:@"010"]);
  319. XCTAssertFalse([FIRApp validateAppID:@"3"]);
  320. XCTAssertFalse([FIRApp validateAppID:@"123"]);
  321. XCTAssertFalse([FIRApp validateAppID:@"999999999"]);
  322. XCTAssertFalse([FIRApp validateAppID:@"com.google.bundleID"]);
  323. }
  324. - (void)testAppIDFormatInvalid {
  325. OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  326. // Some direct tests of the validateAppIDFormat:withVersion: method.
  327. // Sanity checks first.
  328. NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  329. NSString *const kGoodVersionV1 = @"1";
  330. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV1]);
  331. NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  332. NSString *const kGoodVersionV2 = @"2";
  333. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);
  334. // Version mismatch.
  335. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV1]);
  336. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV2]);
  337. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@"999:"]);
  338. // Nil or empty strings.
  339. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:nil]);
  340. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@""]);
  341. XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:kGoodVersionV1]);
  342. XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:kGoodVersionV1]);
  343. XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:nil]);
  344. XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:@""]);
  345. // App ID contains only the version prefix.
  346. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodVersionV1 withVersion:kGoodVersionV1]);
  347. // The version is the entire app ID.
  348. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  349. // Versions digits that may make a partial match.
  350. XCTAssertFalse([FIRApp validateAppIDFormat:@"01:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  351. XCTAssertFalse([FIRApp validateAppIDFormat:@"10:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  352. XCTAssertFalse([FIRApp validateAppIDFormat:@"11:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  353. XCTAssertFalse([FIRApp validateAppIDFormat:@"21:1337:ios:5e18052ab54fbfec"
  354. withVersion:kGoodVersionV2]);
  355. XCTAssertFalse([FIRApp validateAppIDFormat:@"22:1337:ios:5e18052ab54fbfec"
  356. withVersion:kGoodVersionV2]);
  357. XCTAssertFalse([FIRApp validateAppIDFormat:@"02:1337:ios:5e18052ab54fbfec"
  358. withVersion:kGoodVersionV2]);
  359. XCTAssertFalse([FIRApp validateAppIDFormat:@"20:1337:ios:5e18052ab54fbfec"
  360. withVersion:kGoodVersionV2]);
  361. // Extra fields.
  362. XCTAssertFalse([FIRApp validateAppIDFormat:@"ab:1:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  363. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:ab:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  364. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ab:ios:deadbeef" withVersion:kGoodVersionV1]);
  365. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:ab:deadbeef" withVersion:kGoodVersionV1]);
  366. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:deadbeef:ab" withVersion:kGoodVersionV1]);
  367. }
  368. - (void)testAppIDFingerprintInvalid {
  369. OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  370. // Some direct tests of the validateAppIDFingerprint:withVersion: method.
  371. // Sanity checks first.
  372. NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  373. NSString *const kGoodVersionV1 = @"1";
  374. XCTAssertTrue([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodVersionV1]);
  375. NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  376. NSString *const kGoodVersionV2 = @"2";
  377. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);
  378. // Nil or empty strings.
  379. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:nil]);
  380. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:@""]);
  381. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:kGoodVersionV1]);
  382. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:kGoodVersionV1]);
  383. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:nil]);
  384. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:@""]);
  385. // App ID contains only the version prefix.
  386. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodVersionV1 withVersion:kGoodVersionV1]);
  387. // The version is the entire app ID.
  388. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  389. }
  390. // Uncomment if you need to measure performance of [FIRApp validateAppID:].
  391. // It is commented because measures are heavily dependent on a build agent configuration,
  392. // so it cannot produce reliable resault on CI
  393. //- (void)testAppIDFingerprintPerfomance {
  394. // [self measureBlock:^{
  395. // for (NSInteger i = 0; i < 100; ++i) {
  396. // [self testAppIDPrefix];
  397. // }
  398. // }];
  399. //}
  400. #pragma mark - Automatic Data Collection Tests
  401. - (void)testGlobalDataCollectionNoFlags {
  402. // Test: No flags set.
  403. NSString *name = NSStringFromSelector(_cmd);
  404. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  405. GCMSenderID:kGCMSenderID];
  406. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  407. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  408. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  409. .andReturn(nil);
  410. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  411. }
  412. - (void)testGlobalDataCollectionPlistSetEnabled {
  413. // Test: Plist set to enabled, no override.
  414. NSString *name = NSStringFromSelector(_cmd);
  415. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  416. GCMSenderID:kGCMSenderID];
  417. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  418. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  419. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  420. .andReturn(nil);
  421. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  422. }
  423. - (void)testGlobalDataCollectionPlistSetDisabled {
  424. // Test: Plist set to disabled, no override.
  425. NSString *name = NSStringFromSelector(_cmd);
  426. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  427. GCMSenderID:kGCMSenderID];
  428. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  429. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  430. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  431. .andReturn(nil);
  432. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  433. }
  434. - (void)testGlobalDataCollectionUserSpecifiedEnabled {
  435. // Test: User specified as enabled, no plist value.
  436. NSString *name = NSStringFromSelector(_cmd);
  437. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  438. GCMSenderID:kGCMSenderID];
  439. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  440. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  441. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  442. .andReturn(@YES);
  443. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  444. }
  445. - (void)testGlobalDataCollectionUserSpecifiedDisabled {
  446. // Test: User specified as disabled, no plist value.
  447. NSString *name = NSStringFromSelector(_cmd);
  448. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  449. GCMSenderID:kGCMSenderID];
  450. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  451. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  452. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  453. .andReturn(@NO);
  454. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  455. }
  456. - (void)testGlobalDataCollectionUserOverriddenEnabled {
  457. // Test: User specified as enabled, with plist set as disabled.
  458. NSString *name = NSStringFromSelector(_cmd);
  459. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  460. GCMSenderID:kGCMSenderID];
  461. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  462. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  463. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  464. .andReturn(@YES);
  465. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  466. }
  467. - (void)testGlobalDataCollectionUserOverriddenDisabled {
  468. // Test: User specified as disabled, with plist set as enabled.
  469. NSString *name = NSStringFromSelector(_cmd);
  470. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  471. GCMSenderID:kGCMSenderID];
  472. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  473. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  474. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  475. .andReturn(@NO);
  476. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  477. }
  478. - (void)testGlobalDataCollectionWriteToDefaults {
  479. id defaultsMock = OCMPartialMock([NSUserDefaults standardUserDefaults]);
  480. NSString *name = NSStringFromSelector(_cmd);
  481. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  482. GCMSenderID:kGCMSenderID];
  483. [FIRApp configureWithName:name options:options];
  484. FIRApp *app = [FIRApp appNamed:name];
  485. app.dataCollectionDefaultEnabled = YES;
  486. NSString *key =
  487. [NSString stringWithFormat:kFIRGlobalAppDataCollectionEnabledDefaultsKeyFormat, app.name];
  488. OCMVerify([defaultsMock setObject:@YES forKey:key]);
  489. app.dataCollectionDefaultEnabled = NO;
  490. OCMVerify([defaultsMock setObject:@NO forKey:key]);
  491. [defaultsMock stopMocking];
  492. }
  493. - (void)testGlobalDataCollectionClearedAfterDelete {
  494. // Configure and disable data collection for the default FIRApp.
  495. NSString *name = NSStringFromSelector(_cmd);
  496. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  497. GCMSenderID:kGCMSenderID];
  498. [FIRApp configureWithName:name options:options];
  499. FIRApp *app = [FIRApp appNamed:name];
  500. app.dataCollectionDefaultEnabled = NO;
  501. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  502. // Delete the app, and verify that the switch was reset.
  503. XCTestExpectation *deleteFinished =
  504. [self expectationWithDescription:@"The app should successfully delete."];
  505. [app deleteApp:^(BOOL success) {
  506. XCTAssertTrue(success);
  507. [deleteFinished fulfill];
  508. }];
  509. // Wait for the delete to complete.
  510. [self waitForExpectations:@[ deleteFinished ] timeout:1];
  511. // Set up an app with the same name again, and check the data collection flag.
  512. [FIRApp configureWithName:name options:options];
  513. XCTAssertTrue([FIRApp appNamed:name].isDataCollectionDefaultEnabled);
  514. }
  515. - (void)testGlobalDataCollectionNoDiagnosticsSent {
  516. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  517. GCMSenderID:kGCMSenderID];
  518. FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
  519. app.dataCollectionDefaultEnabled = NO;
  520. // Add an observer for the diagnostics notification. Currently no object is sent, but in the
  521. // future that could change so leave it as OCMOCK_ANY.
  522. [self.notificationCenter addMockObserver:self.observerMock
  523. name:kFIRAppDiagnosticsNotification
  524. object:OCMOCK_ANY];
  525. // Stub out reading from user defaults since stubbing out the BOOL has issues. If the data
  526. // collection switch is disabled, the `sendLogs` call should return immediately and not fire a
  527. // notification.
  528. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  529. .andReturn(@NO);
  530. // Ensure configure doesn't fire a notification.
  531. [FIRApp configure];
  532. NSError *error = [NSError errorWithDomain:@"com.firebase" code:42 userInfo:nil];
  533. [app sendLogsWithServiceName:@"Service" version:@"Version" error:error];
  534. // The observer mock is strict and will raise an exception when an unexpected notification is
  535. // received.
  536. OCMVerifyAll(self.observerMock);
  537. }
  538. #pragma mark - Analytics Flag Tests
  539. - (void)testAnalyticsSetByGlobalDataCollectionSwitch {
  540. // Test that the global data collection switch triggers setting Analytics when no explicit flag is
  541. // set.
  542. id optionsMock = OCMClassMock([FIROptions class]);
  543. OCMStub([optionsMock isAnalyticsCollectionExpicitlySet]).andReturn(NO);
  544. // We need to use the default app name since Analytics only associates with the default app.
  545. FIRApp *defaultApp = [[FIRApp alloc] initInstanceWithName:kFIRDefaultAppName options:optionsMock];
  546. id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  547. OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  548. // Ensure Analytics is set after the global flag is set. It needs to
  549. [defaultApp setDataCollectionDefaultEnabled:YES];
  550. OCMVerify([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:NO]);
  551. [defaultApp setDataCollectionDefaultEnabled:NO];
  552. OCMVerify([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:NO]);
  553. }
  554. - (void)testAnalyticsNotSetByGlobalDataCollectionSwitch {
  555. // Test that the global data collection switch doesn't override an explicitly set Analytics flag.
  556. id optionsMock = OCMClassMock([FIROptions class]);
  557. OCMStub([optionsMock isAnalyticsCollectionExpicitlySet]).andReturn(YES);
  558. FIRApp *app = [[FIRApp alloc] initInstanceWithName:@"testAnalyticsNotSet" options:optionsMock];
  559. id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  560. OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  561. // Reject any changes to Analytics when the data collection changes.
  562. OCMReject([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:YES]);
  563. OCMReject([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:NO]);
  564. [app setDataCollectionDefaultEnabled:YES];
  565. OCMReject([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:YES]);
  566. OCMReject([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:NO]);
  567. [app setDataCollectionDefaultEnabled:NO];
  568. }
  569. #pragma mark - Internal Methods
  570. - (void)testIsDefaultAppConfigured {
  571. // Ensure it's false before anything is configured.
  572. XCTAssertFalse([FIRApp isDefaultAppConfigured]);
  573. // Configure it and ensure it's configured.
  574. [FIRApp configure];
  575. XCTAssertTrue([FIRApp isDefaultAppConfigured]);
  576. // Reset the apps and ensure it's not configured anymore.
  577. [FIRApp resetApps];
  578. XCTAssertFalse([FIRApp isDefaultAppConfigured]);
  579. }
  580. - (void)testInvalidLibraryName {
  581. [FIRApp registerLibrary:@"Oops>" withVersion:@"1.0.0"];
  582. XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:@""]);
  583. }
  584. - (void)testInvalidLibraryVersion {
  585. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0+"];
  586. XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:@""]);
  587. }
  588. - (void)testSingleLibrary {
  589. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0"];
  590. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0"]);
  591. }
  592. - (void)testMultipleLibraries {
  593. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0"];
  594. [FIRApp registerLibrary:@"ValidName2" withVersion:@"2.0.0"];
  595. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0 ValidName2/2.0.0"]);
  596. }
  597. - (void)testRegisteringConformingLibrary {
  598. Class testClass = [FIRTestClass class];
  599. [FIRApp registerInternalLibrary:testClass withName:@"ValidName" withVersion:@"1.0.0"];
  600. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0"]);
  601. }
  602. - (void)testRegisteringNonConformingLibrary {
  603. XCTAssertThrows([FIRApp registerInternalLibrary:[NSString class]
  604. withName:@"InvalidLibrary"
  605. withVersion:@"1.0.0"]);
  606. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"InvalidLibrary`/1.0.0"]);
  607. }
  608. #pragma mark - private
  609. - (void)expectNotificationForObserver:(id)observer
  610. notificationName:(NSNotificationName)name
  611. object:(nullable id)object
  612. userInfo:(nullable NSDictionary *)userInfo {
  613. [self.notificationCenter addMockObserver:observer name:name object:object];
  614. [[observer expect] notificationWithName:name object:object userInfo:userInfo];
  615. }
  616. - (NSDictionary<NSString *, NSObject *> *)expectedUserInfoWithAppName:(NSString *)name
  617. isDefaultApp:(BOOL)isDefaultApp {
  618. return @{
  619. kFIRAppNameKey : name,
  620. kFIRAppIsDefaultAppKey : [NSNumber numberWithBool:isDefaultApp],
  621. kFIRGoogleAppIDKey : kGoogleAppID
  622. };
  623. }
  624. @end