FIRAppTest.m 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  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 "FirebaseCore/Tests/Unit/FIRTestCase.h"
  15. #import "FirebaseCore/Tests/Unit/FIRTestComponents.h"
  16. #import <GoogleUtilities/GULAppEnvironmentUtil.h>
  17. #import "FirebaseCore/Sources/FIRAnalyticsConfiguration.h"
  18. #import "FirebaseCore/Sources/Private/FIRAppInternal.h"
  19. #import "FirebaseCore/Sources/Private/FIRComponentType.h"
  20. #import "FirebaseCore/Sources/Private/FIRCoreDiagnosticsConnector.h"
  21. #import "FirebaseCore/Sources/Private/FIROptionsInternal.h"
  22. #import "SharedTestUtilities/FIROptionsMock.h"
  23. NSString *const kFIRTestAppName1 = @"test_app_name_1";
  24. NSString *const kFIRTestAppName2 = @"test-app-name-2";
  25. @interface FIRApp (TestInternal)
  26. + (void)resetApps;
  27. - (instancetype)initInstanceWithName:(NSString *)name options:(FIROptions *)options;
  28. - (BOOL)configureCore;
  29. + (NSError *)errorForInvalidAppID;
  30. - (BOOL)isAppIDValid;
  31. + (NSString *)actualBundleID;
  32. + (NSNumber *)mapFromServiceStringToTypeEnum:(NSString *)serviceString;
  33. + (NSString *)deviceModel;
  34. + (NSString *)installString;
  35. + (NSURL *)filePathURLWithName:(NSString *)fileName;
  36. + (NSString *)stringAtURL:(NSURL *)filePathURL;
  37. + (BOOL)writeString:(NSString *)string toURL:(NSURL *)filePathURL;
  38. + (void)logAppInfo:(NSNotification *)notification;
  39. + (BOOL)validateAppID:(NSString *)appID;
  40. + (BOOL)validateAppIDFormat:(NSString *)appID withVersion:(NSString *)version;
  41. + (BOOL)validateAppIDFingerprint:(NSString *)appID withVersion:(NSString *)version;
  42. + (nullable NSNumber *)readDataCollectionSwitchFromPlist;
  43. + (nullable NSNumber *)readDataCollectionSwitchFromUserDefaultsForApp:(FIRApp *)app;
  44. @end
  45. @interface FIRAppTest : FIRTestCase
  46. @property(nonatomic) id appClassMock;
  47. @property(nonatomic) id observerMock;
  48. @property(nonatomic) id mockCoreDiagnosticsConnector;
  49. @property(nonatomic) NSNotificationCenter *notificationCenter;
  50. @end
  51. @implementation FIRAppTest
  52. - (void)setUp {
  53. [super setUp];
  54. [FIROptions resetDefaultOptions];
  55. [FIRApp resetApps];
  56. _appClassMock = OCMClassMock([FIRApp class]);
  57. _observerMock = OCMObserverMock();
  58. _mockCoreDiagnosticsConnector = OCMClassMock([FIRCoreDiagnosticsConnector class]);
  59. [FIROptionsMock mockFIROptions];
  60. OCMStub(ClassMethod([self.mockCoreDiagnosticsConnector logCoreTelemetryWithOptions:[OCMArg any]]))
  61. .andDo(^(NSInvocation *invocation){
  62. });
  63. // TODO: Remove all usages of defaultCenter in Core, then we can instantiate an instance here to
  64. // inject instead of using defaultCenter.
  65. _notificationCenter = [NSNotificationCenter defaultCenter];
  66. }
  67. - (void)tearDown {
  68. [_appClassMock stopMocking];
  69. [_notificationCenter removeObserver:_observerMock];
  70. _observerMock = nil;
  71. _notificationCenter = nil;
  72. _mockCoreDiagnosticsConnector = nil;
  73. [super tearDown];
  74. }
  75. - (void)testConfigure {
  76. [self registerLibrariesWithClasses:@[
  77. [FIRTestClassCached class], [FIRTestClassEagerCached class]
  78. ]];
  79. NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName
  80. isDefaultApp:YES];
  81. [self expectNotificationForObserver:self.observerMock
  82. notificationName:kFIRAppReadyToConfigureSDKNotification
  83. object:[FIRApp class]
  84. userInfo:expectedUserInfo];
  85. XCTAssertNoThrow([FIRApp configure]);
  86. OCMVerifyAll(self.observerMock);
  87. FIRApp *app = [FIRApp defaultApp];
  88. XCTAssertNotNil(app);
  89. XCTAssertEqualObjects(app.name, kFIRDefaultAppName);
  90. XCTAssertEqualObjects(app.options.clientID, kClientID);
  91. XCTAssertTrue([FIRApp allApps].count == 1);
  92. // Check the registered libraries instances available.
  93. XCTAssertNotNil(FIR_COMPONENT(FIRTestProtocolCached, app.container));
  94. XCTAssertNotNil(FIR_COMPONENT(FIRTestProtocolEagerCached, app.container));
  95. XCTAssertNil(FIR_COMPONENT(FIRTestProtocol, app.container));
  96. }
  97. - (void)testConfigureWithNoDefaultOptions {
  98. id optionsClassMock = OCMClassMock([FIROptions class]);
  99. OCMStub([optionsClassMock defaultOptions]).andReturn(nil);
  100. XCTAssertThrows([FIRApp configure]);
  101. }
  102. - (void)testConfigureWithOptions {
  103. #pragma clang diagnostic push
  104. #pragma clang diagnostic ignored "-Wnonnull"
  105. // Test `nil` options.
  106. XCTAssertThrows([FIRApp configureWithOptions:nil]);
  107. #pragma clang diagnostic pop
  108. XCTAssertTrue([FIRApp allApps].count == 0);
  109. NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRDefaultAppName
  110. isDefaultApp:YES];
  111. [self expectNotificationForObserver:self.observerMock
  112. notificationName:kFIRAppReadyToConfigureSDKNotification
  113. object:[FIRApp class]
  114. userInfo:expectedUserInfo];
  115. // Use a valid instance of options.
  116. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  117. GCMSenderID:kGCMSenderID];
  118. options.clientID = kClientID;
  119. XCTAssertNoThrow([FIRApp configureWithOptions:options]);
  120. OCMVerifyAll(self.observerMock);
  121. // Verify the default app instance is created.
  122. FIRApp *app = [FIRApp defaultApp];
  123. XCTAssertNotNil(app);
  124. XCTAssertEqualObjects(app.name, kFIRDefaultAppName);
  125. XCTAssertEqualObjects(app.options.googleAppID, kGoogleAppID);
  126. XCTAssertEqualObjects(app.options.GCMSenderID, kGCMSenderID);
  127. XCTAssertEqualObjects(app.options.clientID, kClientID);
  128. XCTAssertTrue([FIRApp allApps].count == 1);
  129. }
  130. - (void)testConfigureWithNameAndOptions {
  131. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  132. GCMSenderID:kGCMSenderID];
  133. options.clientID = kClientID;
  134. #pragma clang diagnostic push
  135. #pragma clang diagnostic ignored "-Wnonnull"
  136. XCTAssertThrows([FIRApp configureWithName:nil options:options]);
  137. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:nil]);
  138. #pragma clang diagnostic pop
  139. XCTAssertThrows([FIRApp configureWithName:@"" options:options]);
  140. XCTAssertTrue([FIRApp allApps].count == 0);
  141. NSDictionary *expectedUserInfo = [self expectedUserInfoWithAppName:kFIRTestAppName1
  142. isDefaultApp:NO];
  143. [self expectNotificationForObserver:self.observerMock
  144. notificationName:kFIRAppReadyToConfigureSDKNotification
  145. object:[FIRApp class]
  146. userInfo:expectedUserInfo];
  147. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:options]);
  148. OCMVerifyAll(self.observerMock);
  149. XCTAssertTrue([FIRApp allApps].count == 1);
  150. FIRApp *app = [FIRApp appNamed:kFIRTestAppName1];
  151. XCTAssertNotNil(app);
  152. XCTAssertEqualObjects(app.name, kFIRTestAppName1);
  153. XCTAssertEqualObjects(app.options.clientID, kClientID);
  154. // Configure the same app again should throw an exception.
  155. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:options]);
  156. }
  157. - (void)testConfigureWithMultipleApps {
  158. FIROptions *options1 = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  159. GCMSenderID:kGCMSenderID];
  160. options1.deepLinkURLScheme = kDeepLinkURLScheme;
  161. // Set up notification center observer for verifying notifications.
  162. [self.notificationCenter addMockObserver:self.observerMock
  163. name:kFIRAppReadyToConfigureSDKNotification
  164. object:[FIRApp class]];
  165. NSDictionary *expectedUserInfo1 = [self expectedUserInfoWithAppName:kFIRTestAppName1
  166. isDefaultApp:NO];
  167. [[self.observerMock expect] notificationWithName:kFIRAppReadyToConfigureSDKNotification
  168. object:[FIRApp class]
  169. userInfo:expectedUserInfo1];
  170. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:options1]);
  171. XCTAssertTrue([FIRApp allApps].count == 1);
  172. // Configure a different app with valid customized options.
  173. FIROptions *options2 = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  174. GCMSenderID:kGCMSenderID];
  175. options2.bundleID = kBundleID;
  176. options2.APIKey = kCustomizedAPIKey;
  177. NSDictionary *expectedUserInfo2 = [self expectedUserInfoWithAppName:kFIRTestAppName2
  178. isDefaultApp:NO];
  179. [[self.observerMock expect] notificationWithName:kFIRAppReadyToConfigureSDKNotification
  180. object:[FIRApp class]
  181. userInfo:expectedUserInfo2];
  182. [self.observerMock setExpectationOrderMatters:YES];
  183. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName2 options:options2]);
  184. OCMVerifyAll(self.observerMock);
  185. XCTAssertTrue([FIRApp allApps].count == 2);
  186. FIRApp *app = [FIRApp appNamed:kFIRTestAppName2];
  187. XCTAssertNotNil(app);
  188. XCTAssertEqualObjects(app.name, kFIRTestAppName2);
  189. XCTAssertEqualObjects(app.options.googleAppID, kGoogleAppID);
  190. XCTAssertEqualObjects(app.options.APIKey, kCustomizedAPIKey);
  191. }
  192. - (void)testConfigureThrowsAfterConfigured {
  193. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  194. GCMSenderID:kGCMSenderID];
  195. [FIRApp configureWithOptions:options];
  196. XCTAssertNotNil([FIRApp defaultApp]);
  197. // A second configure call should throw, since Firebase is already configured.
  198. XCTAssertThrows([FIRApp configureWithOptions:options]);
  199. // Test the same with a custom named app.
  200. [FIRApp configureWithName:kFIRTestAppName1 options:options];
  201. XCTAssertNotNil([FIRApp appNamed:kFIRTestAppName1]);
  202. // A second configure call should throw, since Firebase is already configured.
  203. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:options]);
  204. }
  205. - (void)testConfigureDefaultAppInExtension {
  206. id environmentMock = OCMClassMock([GULAppEnvironmentUtil class]);
  207. OCMStub([environmentMock isAppExtension]).andReturn(YES);
  208. // Set up the default app like a standard app.
  209. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  210. GCMSenderID:kGCMSenderID];
  211. [FIRApp configureWithOptions:options];
  212. XCTAssertNotNil([FIRApp defaultApp]);
  213. XCTAssertEqual([FIRApp allApps].count, 1);
  214. // Configuring with the same set of options shouldn't throw.
  215. XCTAssertNoThrow([FIRApp configureWithOptions:options]);
  216. // Only 1 app should have been configured still, the default app.
  217. XCTAssertNotNil([FIRApp defaultApp]);
  218. XCTAssertEqual([FIRApp allApps].count, 1);
  219. // Use a set of a different options to call configure again, which should throw.
  220. FIROptions *differentOptions = [[FIROptions alloc] initWithGoogleAppID:@"1:789:ios:789XYZ"
  221. GCMSenderID:kGCMSenderID];
  222. XCTAssertThrows([FIRApp configureWithOptions:differentOptions]);
  223. XCTAssertEqual([FIRApp allApps].count, 1);
  224. // Explicily stop the environmentMock.
  225. [environmentMock stopMocking];
  226. environmentMock = nil;
  227. }
  228. - (void)testConfigureCustomAppInExtension {
  229. id environmentMock = OCMClassMock([GULAppEnvironmentUtil class]);
  230. OCMStub([environmentMock isAppExtension]).andReturn(YES);
  231. // Set up a custom named app like a standard app.
  232. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  233. GCMSenderID:kGCMSenderID];
  234. [FIRApp configureWithName:kFIRTestAppName1 options:options];
  235. XCTAssertNotNil([FIRApp appNamed:kFIRTestAppName1]);
  236. XCTAssertEqual([FIRApp allApps].count, 1);
  237. // Configuring with the same set of options shouldn't throw.
  238. XCTAssertNoThrow([FIRApp configureWithName:kFIRTestAppName1 options:options]);
  239. // Only 1 app should have been configured still.
  240. XCTAssertNotNil([FIRApp appNamed:kFIRTestAppName1]);
  241. XCTAssertEqual([FIRApp allApps].count, 1);
  242. // Use a set of a different options to call configure again, which should throw.
  243. FIROptions *differentOptions = [[FIROptions alloc] initWithGoogleAppID:@"1:789:ios:789XYZ"
  244. GCMSenderID:kGCMSenderID];
  245. XCTAssertThrows([FIRApp configureWithName:kFIRTestAppName1 options:differentOptions]);
  246. XCTAssertEqual([FIRApp allApps].count, 1);
  247. // Explicily stop the environmentMock.
  248. [environmentMock stopMocking];
  249. environmentMock = nil;
  250. }
  251. - (void)testValidName {
  252. XCTAssertNoThrow([FIRApp configureWithName:@"aA1_" options:[FIROptions defaultOptions]]);
  253. XCTAssertNoThrow([FIRApp configureWithName:@"aA1-" options:[FIROptions defaultOptions]]);
  254. XCTAssertNoThrow([FIRApp configureWithName:@"aAē1_" options:[FIROptions defaultOptions]]);
  255. XCTAssertThrows([FIRApp configureWithName:@"aA1%" options:[FIROptions defaultOptions]]);
  256. XCTAssertThrows([FIRApp configureWithName:@"aA1?" options:[FIROptions defaultOptions]]);
  257. XCTAssertThrows([FIRApp configureWithName:@"aA1!" options:[FIROptions defaultOptions]]);
  258. }
  259. - (void)testDefaultApp {
  260. FIRApp *app = [FIRApp defaultApp];
  261. XCTAssertNil(app);
  262. [FIRApp configure];
  263. app = [FIRApp defaultApp];
  264. XCTAssertEqualObjects(app.name, kFIRDefaultAppName);
  265. XCTAssertEqualObjects(app.options.clientID, kClientID);
  266. }
  267. - (void)testAppNamed {
  268. FIRApp *app = [FIRApp appNamed:kFIRTestAppName1];
  269. XCTAssertNil(app);
  270. [FIRApp configureWithName:kFIRTestAppName1 options:[FIROptions defaultOptions]];
  271. app = [FIRApp appNamed:kFIRTestAppName1];
  272. XCTAssertEqualObjects(app.name, kFIRTestAppName1);
  273. XCTAssertEqualObjects(app.options.clientID, kClientID);
  274. }
  275. - (void)testDeleteApp {
  276. [self registerLibrariesWithClasses:@[
  277. [FIRTestClassCached class], [FIRTestClassEagerCached class]
  278. ]];
  279. NSString *name = NSStringFromSelector(_cmd);
  280. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  281. GCMSenderID:kGCMSenderID];
  282. [FIRApp configureWithName:name options:options];
  283. FIRApp *app = [FIRApp appNamed:name];
  284. XCTAssertNotNil(app);
  285. XCTAssertTrue([FIRApp allApps].count == 1);
  286. // Check the registered libraries instances available.
  287. XCTAssertNotNil(FIR_COMPONENT(FIRTestProtocolCached, app.container));
  288. XCTAssertNotNil(FIR_COMPONENT(FIRTestProtocolEagerCached, app.container));
  289. XCTAssertNil(FIR_COMPONENT(FIRTestProtocol, app.container));
  290. [self expectNotificationForObserver:self.observerMock
  291. notificationName:kFIRAppDeleteNotification
  292. object:[FIRApp class]
  293. userInfo:[OCMArg any]];
  294. XCTestExpectation *expectation =
  295. [self expectationWithDescription:@"Deleting the app should succeed."];
  296. [app deleteApp:^(BOOL success) {
  297. XCTAssertTrue(success);
  298. [expectation fulfill];
  299. }];
  300. [self waitForExpectations:@[ expectation ] timeout:1];
  301. OCMVerifyAll(self.observerMock);
  302. XCTAssertTrue([FIRApp allApps].count == 0);
  303. // Check no new library instances created after the app delete.
  304. XCTAssertNil(FIR_COMPONENT(FIRTestProtocolCached, app.container));
  305. XCTAssertNil(FIR_COMPONENT(FIRTestProtocolEagerCached, app.container));
  306. }
  307. - (void)testOptionsLocking {
  308. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  309. GCMSenderID:kGCMSenderID];
  310. options.projectID = kProjectID;
  311. options.databaseURL = kDatabaseURL;
  312. // Options should not be locked before they are used to configure a `FIRApp`.
  313. XCTAssertFalse(options.isEditingLocked);
  314. // The options returned should be locked after configuring `FIRApp`.
  315. NSString *name = NSStringFromSelector(_cmd);
  316. [FIRApp configureWithName:name options:options];
  317. FIROptions *optionsCopy = [[FIRApp appNamed:name] options];
  318. XCTAssertTrue(optionsCopy.isEditingLocked);
  319. }
  320. #pragma mark - App ID v1
  321. - (void)testAppIDV1 {
  322. // Missing separator between platform:fingerprint.
  323. XCTAssertFalse([FIRApp validateAppID:@"1:1337:iosdeadbeef"]);
  324. // Wrong platform "android".
  325. XCTAssertFalse([FIRApp validateAppID:@"1:1337:android:deadbeef"]);
  326. // The fingerprint, aka 4th field, should only contain hex characters.
  327. XCTAssertFalse([FIRApp validateAppID:@"1:1337:ios:123abcxyz"]);
  328. // The fingerprint, aka 4th field, is not tested in V1, so a bad value shouldn't cause a failure.
  329. XCTAssertTrue([FIRApp validateAppID:@"1:1337:ios:deadbeef"]);
  330. }
  331. #pragma mark - App ID v2
  332. - (void)testAppIDV2 {
  333. // Missing separator between platform:fingerprint.
  334. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios5e18052ab54fbfec"]);
  335. // Unknown versions may contain anything.
  336. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:123abcxyz"]);
  337. XCTAssertTrue([FIRApp validateAppID:@"2:thisdoesn'teven_m:a:t:t:e:r_"]);
  338. // Known good fingerprint.
  339. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:5e18052ab54fbfec"]);
  340. // Unknown fingerprint, not tested so shouldn't cause a failure.
  341. XCTAssertTrue([FIRApp validateAppID:@"2:1337:ios:deadbeef"]);
  342. }
  343. #pragma mark - App ID other
  344. - (void)testAppIDV3 {
  345. // Currently there is no specification for v3, so we would not expect it to fail.
  346. XCTAssertTrue([FIRApp validateAppID:@"3:1337:ios:deadbeef"]);
  347. }
  348. - (void)testAppIDEmpty {
  349. XCTAssertFalse([FIRApp validateAppID:@""]);
  350. }
  351. - (void)testAppIDValidationTrue {
  352. // Ensure that isAppIDValid matches validateAppID.
  353. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"" GCMSenderID:@""];
  354. FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
  355. OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(YES);
  356. XCTAssertTrue([app isAppIDValid]);
  357. }
  358. - (void)testAppIDValidationFalse {
  359. // Ensure that isAppIDValid matches validateAppID.
  360. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:@"" GCMSenderID:@""];
  361. FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
  362. OCMStub([self.appClassMock validateAppID:[OCMArg any]]).andReturn(NO);
  363. XCTAssertFalse([app isAppIDValid]);
  364. }
  365. - (void)testAppIDPrefix {
  366. // Unknown numeric-character prefixes should pass.
  367. XCTAssertTrue([FIRApp validateAppID:@"0:"]);
  368. XCTAssertTrue([FIRApp validateAppID:@"01:"]);
  369. XCTAssertTrue([FIRApp validateAppID:@"10:"]);
  370. XCTAssertTrue([FIRApp validateAppID:@"010:"]);
  371. XCTAssertTrue([FIRApp validateAppID:@"3:"]);
  372. XCTAssertTrue([FIRApp validateAppID:@"123:"]);
  373. XCTAssertTrue([FIRApp validateAppID:@"999999999:"]);
  374. // Non-numeric prefixes should not pass.
  375. XCTAssertFalse([FIRApp validateAppID:@"a:"]);
  376. XCTAssertFalse([FIRApp validateAppID:@"abcsdf0:"]);
  377. XCTAssertFalse([FIRApp validateAppID:@"0aaaa:"]);
  378. XCTAssertFalse([FIRApp validateAppID:@"0aaaa0450:"]);
  379. XCTAssertFalse([FIRApp validateAppID:@"-1:"]);
  380. XCTAssertFalse([FIRApp validateAppID:@"abcsdf:"]);
  381. XCTAssertFalse([FIRApp validateAppID:@"ABDCF:"]);
  382. XCTAssertFalse([FIRApp validateAppID:@" :"]);
  383. XCTAssertFalse([FIRApp validateAppID:@"1 :"]);
  384. XCTAssertFalse([FIRApp validateAppID:@" 1:"]);
  385. XCTAssertFalse([FIRApp validateAppID:@" 123 :"]);
  386. XCTAssertFalse([FIRApp validateAppID:@"1 23:"]);
  387. XCTAssertFalse([FIRApp validateAppID:@"&($*&%(*$&:"]);
  388. XCTAssertFalse([FIRApp validateAppID:@"abCDSF$%%df:"]);
  389. // Known version prefixes should never pass without the rest of the app ID string present.
  390. XCTAssertFalse([FIRApp validateAppID:@"1:"]);
  391. // Version must include ":".
  392. XCTAssertFalse([FIRApp validateAppID:@"0"]);
  393. XCTAssertFalse([FIRApp validateAppID:@"01"]);
  394. XCTAssertFalse([FIRApp validateAppID:@"10"]);
  395. XCTAssertFalse([FIRApp validateAppID:@"010"]);
  396. XCTAssertFalse([FIRApp validateAppID:@"3"]);
  397. XCTAssertFalse([FIRApp validateAppID:@"123"]);
  398. XCTAssertFalse([FIRApp validateAppID:@"999999999"]);
  399. XCTAssertFalse([FIRApp validateAppID:@"com.google.bundleID"]);
  400. }
  401. - (void)testAppIDFormatInvalid {
  402. OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  403. // Some direct tests of the validateAppIDFormat:withVersion: method.
  404. // Sanity checks first.
  405. NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  406. NSString *const kGoodVersionV1 = @"1";
  407. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV1]);
  408. NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  409. NSString *const kGoodVersionV2 = @"2";
  410. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);
  411. // Version mismatch.
  412. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV1]);
  413. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodVersionV2]);
  414. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@"999:"]);
  415. // Nil or empty strings.
  416. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:nil]);
  417. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:@""]);
  418. XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:kGoodVersionV1]);
  419. XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:kGoodVersionV1]);
  420. XCTAssertFalse([FIRApp validateAppIDFormat:nil withVersion:nil]);
  421. XCTAssertFalse([FIRApp validateAppIDFormat:@"" withVersion:@""]);
  422. // App ID contains only the version prefix.
  423. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodVersionV1 withVersion:kGoodVersionV1]);
  424. // The version is the entire app ID.
  425. XCTAssertFalse([FIRApp validateAppIDFormat:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  426. // Versions digits that may make a partial match.
  427. XCTAssertFalse([FIRApp validateAppIDFormat:@"01:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  428. XCTAssertFalse([FIRApp validateAppIDFormat:@"10:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  429. XCTAssertFalse([FIRApp validateAppIDFormat:@"11:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  430. XCTAssertFalse([FIRApp validateAppIDFormat:@"21:1337:ios:5e18052ab54fbfec"
  431. withVersion:kGoodVersionV2]);
  432. XCTAssertFalse([FIRApp validateAppIDFormat:@"22:1337:ios:5e18052ab54fbfec"
  433. withVersion:kGoodVersionV2]);
  434. XCTAssertFalse([FIRApp validateAppIDFormat:@"02:1337:ios:5e18052ab54fbfec"
  435. withVersion:kGoodVersionV2]);
  436. XCTAssertFalse([FIRApp validateAppIDFormat:@"20:1337:ios:5e18052ab54fbfec"
  437. withVersion:kGoodVersionV2]);
  438. // Extra fields.
  439. XCTAssertFalse([FIRApp validateAppIDFormat:@"ab:1:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  440. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:ab:1337:ios:deadbeef" withVersion:kGoodVersionV1]);
  441. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ab:ios:deadbeef" withVersion:kGoodVersionV1]);
  442. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:ab:deadbeef" withVersion:kGoodVersionV1]);
  443. XCTAssertFalse([FIRApp validateAppIDFormat:@"1:1337:ios:deadbeef:ab" withVersion:kGoodVersionV1]);
  444. }
  445. - (void)testAppIDFingerprintInvalid {
  446. OCMStub([self.appClassMock actualBundleID]).andReturn(@"com.google.bundleID");
  447. // Some direct tests of the validateAppIDFingerprint:withVersion: method.
  448. // Sanity checks first.
  449. NSString *const kGoodAppIDV1 = @"1:1337:ios:deadbeef";
  450. NSString *const kGoodVersionV1 = @"1";
  451. XCTAssertTrue([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodVersionV1]);
  452. NSString *const kGoodAppIDV2 = @"2:1337:ios:5e18052ab54fbfec";
  453. NSString *const kGoodVersionV2 = @"2";
  454. XCTAssertTrue([FIRApp validateAppIDFormat:kGoodAppIDV2 withVersion:kGoodVersionV2]);
  455. // Nil or empty strings.
  456. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:nil]);
  457. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:@""]);
  458. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:kGoodVersionV1]);
  459. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:kGoodVersionV1]);
  460. XCTAssertFalse([FIRApp validateAppIDFingerprint:nil withVersion:nil]);
  461. XCTAssertFalse([FIRApp validateAppIDFingerprint:@"" withVersion:@""]);
  462. // App ID contains only the version prefix.
  463. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodVersionV1 withVersion:kGoodVersionV1]);
  464. // The version is the entire app ID.
  465. XCTAssertFalse([FIRApp validateAppIDFingerprint:kGoodAppIDV1 withVersion:kGoodAppIDV1]);
  466. }
  467. // Uncomment if you need to measure performance of [FIRApp validateAppID:].
  468. // It is commented because measures are heavily dependent on a build agent configuration,
  469. // so it cannot produce reliable resault on CI
  470. //- (void)testAppIDFingerprintPerfomance {
  471. // [self measureBlock:^{
  472. // for (NSInteger i = 0; i < 100; ++i) {
  473. // [self testAppIDPrefix];
  474. // }
  475. // }];
  476. //}
  477. #pragma mark - Automatic Data Collection Tests
  478. - (void)testGlobalDataCollectionNoFlags {
  479. // Test: No flags set.
  480. NSString *name = NSStringFromSelector(_cmd);
  481. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  482. GCMSenderID:kGCMSenderID];
  483. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  484. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  485. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  486. .andReturn(nil);
  487. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  488. }
  489. - (void)testGlobalDataCollectionPlistSetEnabled {
  490. // Test: Plist set to enabled, no override.
  491. NSString *name = NSStringFromSelector(_cmd);
  492. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  493. GCMSenderID:kGCMSenderID];
  494. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  495. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  496. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  497. .andReturn(nil);
  498. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  499. }
  500. - (void)testGlobalDataCollectionPlistSetDisabled {
  501. // Test: Plist set to disabled, no override.
  502. NSString *name = NSStringFromSelector(_cmd);
  503. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  504. GCMSenderID:kGCMSenderID];
  505. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  506. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  507. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  508. .andReturn(nil);
  509. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  510. }
  511. - (void)testGlobalDataCollectionUserSpecifiedEnabled {
  512. // Test: User specified as enabled, no plist value.
  513. NSString *name = NSStringFromSelector(_cmd);
  514. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  515. GCMSenderID:kGCMSenderID];
  516. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  517. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  518. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  519. .andReturn(@YES);
  520. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  521. }
  522. - (void)testGlobalDataCollectionUserSpecifiedDisabled {
  523. // Test: User specified as disabled, no plist value.
  524. NSString *name = NSStringFromSelector(_cmd);
  525. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  526. GCMSenderID:kGCMSenderID];
  527. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  528. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(nil);
  529. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  530. .andReturn(@NO);
  531. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  532. }
  533. - (void)testGlobalDataCollectionUserOverriddenEnabled {
  534. // Test: User specified as enabled, with plist set as disabled.
  535. NSString *name = NSStringFromSelector(_cmd);
  536. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  537. GCMSenderID:kGCMSenderID];
  538. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  539. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@NO);
  540. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  541. .andReturn(@YES);
  542. XCTAssertTrue(app.isDataCollectionDefaultEnabled);
  543. }
  544. - (void)testGlobalDataCollectionUserOverriddenDisabled {
  545. // Test: User specified as disabled, with plist set as enabled.
  546. NSString *name = NSStringFromSelector(_cmd);
  547. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  548. GCMSenderID:kGCMSenderID];
  549. FIRApp *app = [[FIRApp alloc] initInstanceWithName:name options:options];
  550. OCMStub([self.appClassMock readDataCollectionSwitchFromPlist]).andReturn(@YES);
  551. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  552. .andReturn(@NO);
  553. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  554. }
  555. - (void)testGlobalDataCollectionWriteToDefaults {
  556. id defaultsMock = OCMPartialMock([NSUserDefaults standardUserDefaults]);
  557. NSString *name = NSStringFromSelector(_cmd);
  558. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  559. GCMSenderID:kGCMSenderID];
  560. [FIRApp configureWithName:name options:options];
  561. FIRApp *app = [FIRApp appNamed:name];
  562. app.dataCollectionDefaultEnabled = YES;
  563. NSString *key =
  564. [NSString stringWithFormat:kFIRGlobalAppDataCollectionEnabledDefaultsKeyFormat, app.name];
  565. OCMVerify([defaultsMock setObject:@YES forKey:key]);
  566. app.dataCollectionDefaultEnabled = NO;
  567. OCMVerify([defaultsMock setObject:@NO forKey:key]);
  568. [defaultsMock stopMocking];
  569. }
  570. - (void)testGlobalDataCollectionClearedAfterDelete {
  571. // Configure and disable data collection for the default FIRApp.
  572. NSString *name = NSStringFromSelector(_cmd);
  573. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  574. GCMSenderID:kGCMSenderID];
  575. [FIRApp configureWithName:name options:options];
  576. FIRApp *app = [FIRApp appNamed:name];
  577. app.dataCollectionDefaultEnabled = NO;
  578. XCTAssertFalse(app.isDataCollectionDefaultEnabled);
  579. // Delete the app, and verify that the switch was reset.
  580. XCTestExpectation *deleteFinished =
  581. [self expectationWithDescription:@"The app should successfully delete."];
  582. [app deleteApp:^(BOOL success) {
  583. XCTAssertTrue(success);
  584. [deleteFinished fulfill];
  585. }];
  586. // Wait for the delete to complete.
  587. [self waitForExpectations:@[ deleteFinished ] timeout:1];
  588. // Set up an app with the same name again, and check the data collection flag.
  589. [FIRApp configureWithName:name options:options];
  590. XCTAssertTrue([FIRApp appNamed:name].isDataCollectionDefaultEnabled);
  591. }
  592. - (void)testGlobalDataCollectionNoDiagnosticsSent {
  593. FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID
  594. GCMSenderID:kGCMSenderID];
  595. FIRApp *app = [[FIRApp alloc] initInstanceWithName:NSStringFromSelector(_cmd) options:options];
  596. app.dataCollectionDefaultEnabled = NO;
  597. // Add an observer for the diagnostics notification. Currently no object is sent, but in the
  598. // future that could change so leave it as OCMOCK_ANY.
  599. [self.notificationCenter addMockObserver:self.observerMock
  600. name:kFIRAppDiagnosticsNotification
  601. object:OCMOCK_ANY];
  602. // Stub out reading from user defaults since stubbing out the BOOL has issues. If the data
  603. // collection switch is disabled, the `sendLogs` call should return immediately and not fire a
  604. // notification.
  605. OCMStub([self.appClassMock readDataCollectionSwitchFromUserDefaultsForApp:OCMOCK_ANY])
  606. .andReturn(@NO);
  607. // Ensure configure doesn't fire a notification.
  608. [FIRApp configure];
  609. // The observer mock is strict and will raise an exception when an unexpected notification is
  610. // received.
  611. OCMVerifyAll(self.observerMock);
  612. }
  613. #pragma mark - Analytics Flag Tests
  614. - (void)testAnalyticsSetByGlobalDataCollectionSwitch {
  615. // Test that the global data collection switch triggers setting Analytics when no explicit flag is
  616. // set.
  617. id optionsMock = OCMClassMock([FIROptions class]);
  618. OCMStub([optionsMock isAnalyticsCollectionExplicitlySet]).andReturn(NO);
  619. // We need to use the default app name since Analytics only associates with the default app.
  620. FIRApp *defaultApp = [[FIRApp alloc] initInstanceWithName:kFIRDefaultAppName options:optionsMock];
  621. id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  622. OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  623. // Ensure Analytics is set after the global flag is set. It needs to
  624. [defaultApp setDataCollectionDefaultEnabled:YES];
  625. OCMVerify([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:NO]);
  626. [defaultApp setDataCollectionDefaultEnabled:NO];
  627. OCMVerify([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:NO]);
  628. }
  629. - (void)testAnalyticsNotSetByGlobalDataCollectionSwitch {
  630. // Test that the global data collection switch doesn't override an explicitly set Analytics flag.
  631. id optionsMock = OCMClassMock([FIROptions class]);
  632. OCMStub([optionsMock isAnalyticsCollectionExplicitlySet]).andReturn(YES);
  633. FIRApp *app = [[FIRApp alloc] initInstanceWithName:@"testAnalyticsNotSet" options:optionsMock];
  634. id configurationMock = OCMClassMock([FIRAnalyticsConfiguration class]);
  635. OCMStub([configurationMock sharedInstance]).andReturn(configurationMock);
  636. // Reject any changes to Analytics when the data collection changes.
  637. OCMReject([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:YES]);
  638. OCMReject([configurationMock setAnalyticsCollectionEnabled:YES persistSetting:NO]);
  639. [app setDataCollectionDefaultEnabled:YES];
  640. OCMReject([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:YES]);
  641. OCMReject([configurationMock setAnalyticsCollectionEnabled:NO persistSetting:NO]);
  642. [app setDataCollectionDefaultEnabled:NO];
  643. }
  644. #pragma mark - Internal Methods
  645. - (void)testIsDefaultAppConfigured {
  646. // Ensure it's false before anything is configured.
  647. XCTAssertFalse([FIRApp isDefaultAppConfigured]);
  648. // Configure it and ensure it's configured.
  649. [FIRApp configure];
  650. XCTAssertTrue([FIRApp isDefaultAppConfigured]);
  651. // Reset the apps and ensure it's not configured anymore.
  652. [FIRApp resetApps];
  653. XCTAssertFalse([FIRApp isDefaultAppConfigured]);
  654. }
  655. - (void)testRegisterLibrary_InvalidLibraryName {
  656. NSString *originalFirebaseUserAgent = [FIRApp firebaseUserAgent];
  657. [FIRApp registerLibrary:@"Oops>" withVersion:@"1.0.0"];
  658. XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:originalFirebaseUserAgent]);
  659. }
  660. - (void)testRegisterLibrary_InvalidLibraryVersion {
  661. NSString *originalFirebaseUserAgent = [FIRApp firebaseUserAgent];
  662. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0+"];
  663. XCTAssertTrue([[FIRApp firebaseUserAgent] isEqualToString:originalFirebaseUserAgent]);
  664. }
  665. - (void)testRegisterLibrary_SingleLibrary {
  666. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0"];
  667. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0"]);
  668. }
  669. - (void)testRegisterLibrary_MultipleLibraries {
  670. [FIRApp registerLibrary:@"ValidName" withVersion:@"1.0.0"];
  671. [FIRApp registerLibrary:@"ValidName2" withVersion:@"2.0.0"];
  672. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0 ValidName2/2.0.0"]);
  673. }
  674. - (void)testRegisterLibrary_RegisteringConformingLibrary {
  675. Class testClass = [FIRTestClass class];
  676. [FIRApp registerInternalLibrary:testClass withName:@"ValidName" withVersion:@"1.0.0"];
  677. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"ValidName/1.0.0"]);
  678. }
  679. - (void)testRegisterLibrary_RegisteringNonConformingLibrary {
  680. XCTAssertThrows([FIRApp registerInternalLibrary:[NSString class]
  681. withName:@"InvalidLibrary"
  682. withVersion:@"1.0.0"]);
  683. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"InvalidLibrary`/1.0.0"]);
  684. }
  685. - (void)testFirebaseUserAgent_SwiftFlagWithNoSwift {
  686. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"swift/false"]);
  687. }
  688. - (void)testFirebaseUserAgent_ApplePlatformFlag {
  689. // When a Catalyst app is run on macOS then both `TARGET_OS_MACCATALYST` and `TARGET_OS_IOS` are
  690. // `true`.
  691. #if TARGET_OS_MACCATALYST
  692. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/ios"]);
  693. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/tvos"]);
  694. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/macos"]);
  695. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/watchos"]);
  696. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"apple-platform/maccatalyst"]);
  697. #elif TARGET_OS_IOS
  698. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"apple-platform/ios"]);
  699. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/tvos"]);
  700. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/macos"]);
  701. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/watchos"]);
  702. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/maccatalyst"]);
  703. #endif // TARGET_OS_MACCATALYST
  704. #if TARGET_OS_TV
  705. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/ios"]);
  706. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"apple-platform/tvos"]);
  707. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/macos"]);
  708. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/watchos"]);
  709. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/maccatalyst"]);
  710. #endif // TARGET_OS_TV
  711. #if TARGET_OS_OSX
  712. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/ios"]);
  713. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/tvos"]);
  714. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"apple-platform/macos"]);
  715. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/watchos"]);
  716. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/maccatalyst"]);
  717. #endif // TARGET_OS_OSX
  718. #if TARGET_OS_WATCH
  719. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/ios"]);
  720. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/tvos"]);
  721. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/macos"]);
  722. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:@"apple-platform/watchos"]);
  723. XCTAssertFalse([[FIRApp firebaseUserAgent] containsString:@"apple-platform/maccatalyst"]);
  724. #endif // TARGET_OS_WATCH
  725. }
  726. - (void)testFirebaseUserAgent_DeploymentType {
  727. #if SWIFT_PACKAGE
  728. NSString *deploymentType = @"swiftpm";
  729. #elif FIREBASE_BUILD_CARTHAGE
  730. NSString *deploymentType = @"carthage";
  731. #elif FIREBASE_BUILD_ZIP_FILE
  732. NSString *deploymentType = @"zip";
  733. #else
  734. NSString *deploymentType = @"cocoapods";
  735. #endif
  736. NSString *expectedComponent = [NSString stringWithFormat:@"deploy/%@", deploymentType];
  737. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:expectedComponent]);
  738. }
  739. - (void)testFirebaseUserAgent_DeviceModel {
  740. NSString *expectedComponent =
  741. [NSString stringWithFormat:@"device/%@", [GULAppEnvironmentUtil deviceModel]];
  742. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:expectedComponent]);
  743. }
  744. - (void)testFirebaseUserAgent_OSVersion {
  745. NSString *expectedComponent =
  746. [NSString stringWithFormat:@"os-version/%@", [GULAppEnvironmentUtil systemVersion]];
  747. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:expectedComponent]);
  748. }
  749. - (void)testFirebaseUserAgent_IsFromAppStore {
  750. NSString *appStoreValue = [GULAppEnvironmentUtil isFromAppStore] ? @"true" : @"false";
  751. NSString *expectedComponent = [NSString stringWithFormat:@"appstore/%@", appStoreValue];
  752. XCTAssertTrue([[FIRApp firebaseUserAgent] containsString:expectedComponent]);
  753. }
  754. #pragma mark - Core Diagnostics
  755. - (void)testCoreDiagnosticsLoggedWhenFIRAppIsConfigured {
  756. [self expectCoreDiagnosticsDataLogWithOptions:[self appOptions]];
  757. [self createConfiguredAppWithName:NSStringFromSelector(_cmd)];
  758. OCMVerifyAll(self.mockCoreDiagnosticsConnector);
  759. }
  760. - (void)testCoreDiagnosticsLoggedWhenAppDidBecomeActive {
  761. FIRApp *app = [self createConfiguredAppWithName:NSStringFromSelector(_cmd)];
  762. [self expectCoreDiagnosticsDataLogWithOptions:app.options];
  763. [self.notificationCenter postNotificationName:[self appDidBecomeActiveNotificationName]
  764. object:nil];
  765. OCMVerifyAll(self.mockCoreDiagnosticsConnector);
  766. }
  767. #pragma mark - private
  768. - (void)expectNotificationForObserver:(id)observer
  769. notificationName:(NSNotificationName)name
  770. object:(nullable id)object
  771. userInfo:(nullable NSDictionary *)userInfo {
  772. [self.notificationCenter addMockObserver:observer name:name object:object];
  773. [[observer expect] notificationWithName:name object:object userInfo:userInfo];
  774. }
  775. - (NSDictionary<NSString *, NSObject *> *)expectedUserInfoWithAppName:(NSString *)name
  776. isDefaultApp:(BOOL)isDefaultApp {
  777. return @{
  778. kFIRAppNameKey : name,
  779. kFIRAppIsDefaultAppKey : [NSNumber numberWithBool:isDefaultApp],
  780. kFIRGoogleAppIDKey : kGoogleAppID
  781. };
  782. }
  783. - (void)expectCoreDiagnosticsDataLogWithOptions:(nullable FIROptions *)expectedOptions {
  784. [self.mockCoreDiagnosticsConnector stopMocking];
  785. self.mockCoreDiagnosticsConnector = nil;
  786. self.mockCoreDiagnosticsConnector = OCMClassMock([FIRCoreDiagnosticsConnector class]);
  787. OCMExpect(ClassMethod([self.mockCoreDiagnosticsConnector
  788. logCoreTelemetryWithOptions:[OCMArg checkWithBlock:^BOOL(FIROptions *options) {
  789. if (!expectedOptions) {
  790. return YES;
  791. }
  792. return [options.googleAppID isEqualToString:expectedOptions.googleAppID] &&
  793. [options.GCMSenderID isEqualToString:expectedOptions.GCMSenderID];
  794. }]]));
  795. }
  796. - (NSNotificationName)appDidBecomeActiveNotificationName {
  797. #if TARGET_OS_IOS || TARGET_OS_TV
  798. return UIApplicationDidBecomeActiveNotification;
  799. #endif
  800. #if TARGET_OS_OSX
  801. return NSApplicationDidBecomeActiveNotification;
  802. #endif
  803. }
  804. - (FIRApp *)createConfiguredAppWithName:(NSString *)name {
  805. FIROptions *options = [self appOptions];
  806. [FIRApp configureWithName:name options:options];
  807. return [FIRApp appNamed:name];
  808. }
  809. - (FIROptions *)appOptions {
  810. return [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  811. }
  812. - (void)registerLibrariesWithClasses:(NSArray<Class> *)classes {
  813. for (Class klass in classes) {
  814. [FIRApp registerInternalLibrary:klass withName:NSStringFromClass(klass) withVersion:@"1.0"];
  815. }
  816. }
  817. @end