FIRAppTest.m 44 KB

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