FIRAppTest.m 45 KB

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