FIRAppTest.m 34 KB

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