FIROptionsTest.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FIRTestCase.h"
  15. #import <FirebaseCore/FIRAppInternal.h>
  16. #import <FirebaseCore/FIRBundleUtil.h>
  17. #import <FirebaseCore/FIROptionsInternal.h>
  18. extern NSString *const kFIRIsMeasurementEnabled;
  19. extern NSString *const kFIRIsAnalyticsCollectionEnabled;
  20. extern NSString *const kFIRIsAnalyticsCollectionDeactivated;
  21. extern NSString *const kFIRLibraryVersionID;
  22. @interface FIROptions (Test)
  23. - (nullable NSDictionary *)analyticsOptionsDictionaryWithInfoDictionary:
  24. (nullable NSDictionary *)infoDictionary;
  25. @end
  26. @interface FIROptionsTest : FIRTestCase
  27. @end
  28. @implementation FIROptionsTest
  29. - (void)setUp {
  30. [super setUp];
  31. [FIROptions resetDefaultOptions];
  32. }
  33. - (void)testInit {
  34. NSDictionary *optionsDictionary = [FIROptions defaultOptionsDictionary];
  35. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  36. [self assertOptionsMatchDefaults:options andProjectID:YES];
  37. XCTAssertNil(options.deepLinkURLScheme);
  38. XCTAssertTrue(options.usingOptionsFromDefaultPlist);
  39. options.deepLinkURLScheme = kDeepLinkURLScheme;
  40. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  41. }
  42. - (void)testDefaultOptionsDictionaryWithNilFilePath {
  43. id mockBundleUtil = OCMClassMock([FIRBundleUtil class]);
  44. [OCMStub([mockBundleUtil optionsDictionaryPathWithResourceName:kServiceInfoFileName
  45. andFileType:kServiceInfoFileType
  46. inBundles:[FIRBundleUtil relevantBundles]])
  47. andReturn:nil];
  48. XCTAssertNil([FIROptions defaultOptionsDictionary]);
  49. }
  50. - (void)testDefaultOptionsDictionaryWithInvalidSourceFile {
  51. id mockBundleUtil = OCMClassMock([FIRBundleUtil class]);
  52. [OCMStub([mockBundleUtil optionsDictionaryPathWithResourceName:kServiceInfoFileName
  53. andFileType:kServiceInfoFileType
  54. inBundles:[FIRBundleUtil relevantBundles]])
  55. andReturn:@"invalid.plist"];
  56. XCTAssertNil([FIROptions defaultOptionsDictionary]);
  57. }
  58. - (void)testDefaultOptions {
  59. FIROptions *options = [FIROptions defaultOptions];
  60. [self assertOptionsMatchDefaults:options andProjectID:YES];
  61. XCTAssertNil(options.deepLinkURLScheme);
  62. XCTAssertTrue(options.usingOptionsFromDefaultPlist);
  63. options.deepLinkURLScheme = kDeepLinkURLScheme;
  64. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  65. }
  66. - (void)testInitCustomizedOptions {
  67. FIROptions *options =
  68. [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  69. options.APIKey = kAPIKey;
  70. options.bundleID = kBundleID;
  71. options.clientID = kClientID;
  72. options.databaseURL = kDatabaseURL;
  73. options.deepLinkURLScheme = kDeepLinkURLScheme;
  74. options.projectID = kProjectID;
  75. options.storageBucket = kStorageBucket;
  76. options.trackingID = kTrackingID;
  77. [self assertOptionsMatchDefaults:options andProjectID:YES];
  78. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  79. XCTAssertFalse(options.usingOptionsFromDefaultPlist);
  80. }
  81. - (void)testInitWithContentsOfFile {
  82. NSString *filePath =
  83. [[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"];
  84. FIROptions *options = [[FIROptions alloc] initWithContentsOfFile:filePath];
  85. [self assertOptionsMatchDefaults:options andProjectID:YES];
  86. XCTAssertNil(options.deepLinkURLScheme);
  87. XCTAssertFalse(options.usingOptionsFromDefaultPlist);
  88. #pragma clang diagnostic push
  89. #pragma clang diagnostic ignored "-Wnonnull"
  90. FIROptions *emptyOptions = [[FIROptions alloc] initWithContentsOfFile:nil];
  91. #pragma clang diagnostic pop
  92. XCTAssertNil(emptyOptions);
  93. FIROptions *invalidOptions = [[FIROptions alloc] initWithContentsOfFile:@"invalid.plist"];
  94. XCTAssertNil(invalidOptions);
  95. }
  96. - (void)assertOptionsMatchDefaults:(FIROptions *)options andProjectID:(BOOL)matchProjectID {
  97. XCTAssertEqualObjects(options.googleAppID, kGoogleAppID);
  98. XCTAssertEqualObjects(options.APIKey, kAPIKey);
  99. XCTAssertEqualObjects(options.clientID, kClientID);
  100. XCTAssertEqualObjects(options.trackingID, kTrackingID);
  101. XCTAssertEqualObjects(options.GCMSenderID, kGCMSenderID);
  102. XCTAssertNil(options.androidClientID);
  103. XCTAssertEqualObjects(options.libraryVersionID, kFIRLibraryVersionID);
  104. XCTAssertEqualObjects(options.databaseURL, kDatabaseURL);
  105. XCTAssertEqualObjects(options.storageBucket, kStorageBucket);
  106. XCTAssertEqualObjects(options.bundleID, kBundleID);
  107. // Custom `matchProjectID` parameter to be removed once the deprecated `FIROptions` constructor is
  108. // removed.
  109. if (matchProjectID) {
  110. XCTAssertEqualObjects(options.projectID, kProjectID);
  111. }
  112. }
  113. - (void)testCopyingProperties {
  114. NSMutableString *mutableString;
  115. FIROptions *options =
  116. [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  117. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  118. options.APIKey = mutableString;
  119. [mutableString appendString:@"2"];
  120. XCTAssertEqualObjects(options.APIKey, @"1");
  121. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  122. options.bundleID = mutableString;
  123. [mutableString appendString:@"2"];
  124. XCTAssertEqualObjects(options.bundleID, @"1");
  125. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  126. options.clientID = mutableString;
  127. [mutableString appendString:@"2"];
  128. XCTAssertEqualObjects(options.clientID, @"1");
  129. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  130. options.trackingID = mutableString;
  131. [mutableString appendString:@"2"];
  132. XCTAssertEqualObjects(options.trackingID, @"1");
  133. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  134. options.GCMSenderID = mutableString;
  135. [mutableString appendString:@"2"];
  136. XCTAssertEqualObjects(options.GCMSenderID, @"1");
  137. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  138. options.projectID = mutableString;
  139. [mutableString appendString:@"2"];
  140. XCTAssertEqualObjects(options.projectID, @"1");
  141. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  142. options.androidClientID = mutableString;
  143. [mutableString appendString:@"2"];
  144. XCTAssertEqualObjects(options.androidClientID, @"1");
  145. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  146. options.googleAppID = mutableString;
  147. [mutableString appendString:@"2"];
  148. XCTAssertEqualObjects(options.googleAppID, @"1");
  149. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  150. options.databaseURL = mutableString;
  151. [mutableString appendString:@"2"];
  152. XCTAssertEqualObjects(options.databaseURL, @"1");
  153. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  154. options.deepLinkURLScheme = mutableString;
  155. [mutableString appendString:@"2"];
  156. XCTAssertEqualObjects(options.deepLinkURLScheme, @"1");
  157. mutableString = [[NSMutableString alloc] initWithString:@"1"];
  158. options.storageBucket = mutableString;
  159. [mutableString appendString:@"2"];
  160. XCTAssertEqualObjects(options.storageBucket, @"1");
  161. }
  162. - (void)testCopyWithZone {
  163. // default options
  164. FIROptions *options = [FIROptions defaultOptions];
  165. options.deepLinkURLScheme = kDeepLinkURLScheme;
  166. XCTAssertEqualObjects(options.deepLinkURLScheme, kDeepLinkURLScheme);
  167. FIROptions *newOptions = [options copy];
  168. XCTAssertEqualObjects(newOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  169. [options setDeepLinkURLScheme:kNewDeepLinkURLScheme];
  170. XCTAssertEqualObjects(options.deepLinkURLScheme, kNewDeepLinkURLScheme);
  171. XCTAssertEqualObjects(newOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  172. // customized options
  173. FIROptions *customizedOptions =
  174. [[FIROptions alloc] initWithGoogleAppID:kGoogleAppID GCMSenderID:kGCMSenderID];
  175. customizedOptions.deepLinkURLScheme = kDeepLinkURLScheme;
  176. FIROptions *copyCustomizedOptions = [customizedOptions copy];
  177. [copyCustomizedOptions setDeepLinkURLScheme:kNewDeepLinkURLScheme];
  178. XCTAssertEqualObjects(customizedOptions.deepLinkURLScheme, kDeepLinkURLScheme);
  179. XCTAssertEqualObjects(copyCustomizedOptions.deepLinkURLScheme, kNewDeepLinkURLScheme);
  180. }
  181. - (void)testAnalyticsConstants {
  182. // The keys are public values and should never change.
  183. XCTAssertEqualObjects(kFIRIsMeasurementEnabled, @"IS_MEASUREMENT_ENABLED");
  184. XCTAssertEqualObjects(kFIRIsAnalyticsCollectionEnabled, @"FIREBASE_ANALYTICS_COLLECTION_ENABLED");
  185. XCTAssertEqualObjects(kFIRIsAnalyticsCollectionDeactivated,
  186. @"FIREBASE_ANALYTICS_COLLECTION_DEACTIVATED");
  187. }
  188. - (void)testAnalyticsOptions {
  189. // No keys anywhere.
  190. NSDictionary *optionsDictionary = nil;
  191. FIROptions *options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  192. NSDictionary *mainDictionary = nil;
  193. NSDictionary *expectedAnalyticsOptions = @{};
  194. NSDictionary *analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:nil];
  195. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  196. optionsDictionary = @{};
  197. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  198. mainDictionary = @{};
  199. expectedAnalyticsOptions = @{};
  200. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  201. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  202. // Main has no keys.
  203. optionsDictionary = @{
  204. kFIRIsAnalyticsCollectionDeactivated : @YES,
  205. kFIRIsAnalyticsCollectionEnabled : @YES,
  206. kFIRIsMeasurementEnabled : @YES
  207. };
  208. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  209. mainDictionary = @{};
  210. expectedAnalyticsOptions = optionsDictionary;
  211. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  212. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  213. // Main overrides all the keys.
  214. optionsDictionary = @{
  215. kFIRIsAnalyticsCollectionDeactivated : @YES,
  216. kFIRIsAnalyticsCollectionEnabled : @YES,
  217. kFIRIsMeasurementEnabled : @YES
  218. };
  219. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  220. mainDictionary = @{
  221. kFIRIsAnalyticsCollectionDeactivated : @NO,
  222. kFIRIsAnalyticsCollectionEnabled : @NO,
  223. kFIRIsMeasurementEnabled : @NO
  224. };
  225. expectedAnalyticsOptions = mainDictionary;
  226. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  227. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  228. // Keys exist only in main.
  229. optionsDictionary = @{};
  230. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  231. mainDictionary = @{
  232. kFIRIsAnalyticsCollectionDeactivated : @YES,
  233. kFIRIsAnalyticsCollectionEnabled : @YES,
  234. kFIRIsMeasurementEnabled : @YES
  235. };
  236. expectedAnalyticsOptions = mainDictionary;
  237. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  238. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  239. // Main overrides single keys.
  240. optionsDictionary = @{
  241. kFIRIsAnalyticsCollectionDeactivated : @YES,
  242. kFIRIsAnalyticsCollectionEnabled : @YES,
  243. kFIRIsMeasurementEnabled : @YES
  244. };
  245. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  246. mainDictionary = @{ kFIRIsAnalyticsCollectionDeactivated : @NO };
  247. expectedAnalyticsOptions = @{
  248. kFIRIsAnalyticsCollectionDeactivated : @NO, // override
  249. kFIRIsAnalyticsCollectionEnabled : @YES,
  250. kFIRIsMeasurementEnabled : @YES
  251. };
  252. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  253. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  254. optionsDictionary = @{
  255. kFIRIsAnalyticsCollectionDeactivated : @YES,
  256. kFIRIsAnalyticsCollectionEnabled : @YES,
  257. kFIRIsMeasurementEnabled : @YES
  258. };
  259. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  260. mainDictionary = @{ kFIRIsAnalyticsCollectionEnabled : @NO };
  261. expectedAnalyticsOptions = @{
  262. kFIRIsAnalyticsCollectionDeactivated : @YES,
  263. kFIRIsAnalyticsCollectionEnabled : @NO, // override
  264. kFIRIsMeasurementEnabled : @YES
  265. };
  266. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  267. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  268. optionsDictionary = @{
  269. kFIRIsAnalyticsCollectionDeactivated : @YES,
  270. kFIRIsAnalyticsCollectionEnabled : @YES,
  271. kFIRIsMeasurementEnabled : @YES
  272. };
  273. options = [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  274. mainDictionary = @{ kFIRIsMeasurementEnabled : @NO };
  275. expectedAnalyticsOptions = @{
  276. kFIRIsAnalyticsCollectionDeactivated : @YES,
  277. kFIRIsAnalyticsCollectionEnabled : @YES,
  278. kFIRIsMeasurementEnabled : @NO // override
  279. };
  280. analyticsOptions = [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  281. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  282. }
  283. - (void)testAnalyticsOptions_combinatorial {
  284. // Complete combinatorial test.
  285. // Possible values for the flags in the plist, where NSNull means the flag is not present.
  286. NSArray *values = @[ [NSNull null], @NO, @YES ];
  287. // Sanity checks for the combination generation.
  288. int combinationCount = 0;
  289. NSMutableArray *uniqueMainCombinations = [[NSMutableArray alloc] init];
  290. NSMutableArray *uniqueOptionsCombinations = [[NSMutableArray alloc] init];
  291. // Generate all optout flag combinations for { main plist X GoogleService-info options plist }.
  292. // Options present in the main plist should override options of the same key in the service plist.
  293. for (id mainDeactivated in values) {
  294. for (id mainAnalyticsEnabled in values) {
  295. for (id mainMeasurementEnabled in values) {
  296. for (id optionsDeactivated in values) {
  297. for (id optionsAnalyticsEnabled in values) {
  298. for (id optionsMeasurementEnabled in values) {
  299. @autoreleasepool {
  300. // Fill the GoogleService-info options plist dictionary.
  301. NSMutableDictionary *optionsDictionary = [[NSMutableDictionary alloc] init];
  302. if (![optionsDeactivated isEqual:[NSNull null]]) {
  303. optionsDictionary[kFIRIsAnalyticsCollectionDeactivated] = optionsDeactivated;
  304. }
  305. if (![optionsAnalyticsEnabled isEqual:[NSNull null]]) {
  306. optionsDictionary[kFIRIsAnalyticsCollectionEnabled] = optionsAnalyticsEnabled;
  307. }
  308. if (![optionsMeasurementEnabled isEqual:[NSNull null]]) {
  309. optionsDictionary[kFIRIsMeasurementEnabled] = optionsMeasurementEnabled;
  310. }
  311. FIROptions *options =
  312. [[FIROptions alloc] initInternalWithOptionsDictionary:optionsDictionary];
  313. if (![uniqueOptionsCombinations containsObject:optionsDictionary]) {
  314. [uniqueOptionsCombinations addObject:optionsDictionary];
  315. }
  316. // Fill the main plist dictionary.
  317. NSMutableDictionary *mainDictionary = [[NSMutableDictionary alloc] init];
  318. if (![mainDeactivated isEqual:[NSNull null]]) {
  319. mainDictionary[kFIRIsAnalyticsCollectionDeactivated] = mainDeactivated;
  320. }
  321. if (![mainAnalyticsEnabled isEqual:[NSNull null]]) {
  322. mainDictionary[kFIRIsAnalyticsCollectionEnabled] = mainAnalyticsEnabled;
  323. }
  324. if (![mainMeasurementEnabled isEqual:[NSNull null]]) {
  325. mainDictionary[kFIRIsMeasurementEnabled] = mainMeasurementEnabled;
  326. }
  327. // Add mainDictionary to uniqueMainCombinations if it isn't included yet.
  328. if (![uniqueMainCombinations containsObject:mainDictionary]) {
  329. [uniqueMainCombinations addObject:mainDictionary];
  330. }
  331. // Generate the expected options by adding main values on top of the service options
  332. // values. The main values will replace any existing options values with the same
  333. // key. This is a different way of combining the two sets of flags from the actual
  334. // implementation in FIROptions, with equivalent output.
  335. NSMutableDictionary *expectedAnalyticsOptions =
  336. [[NSMutableDictionary alloc] initWithDictionary:optionsDictionary];
  337. [expectedAnalyticsOptions addEntriesFromDictionary:mainDictionary];
  338. NSDictionary *analyticsOptions =
  339. [options analyticsOptionsDictionaryWithInfoDictionary:mainDictionary];
  340. XCTAssertEqualObjects(analyticsOptions, expectedAnalyticsOptions);
  341. combinationCount++;
  342. }
  343. }
  344. }
  345. }
  346. }
  347. }
  348. }
  349. // Verify the sanity checks.
  350. XCTAssertEqual(combinationCount, 729); // = 3^6.
  351. XCTAssertEqual(uniqueOptionsCombinations.count, 27);
  352. int optionsSizeCount[4] = {0};
  353. for (NSDictionary *dictionary in uniqueOptionsCombinations) {
  354. optionsSizeCount[dictionary.count]++;
  355. }
  356. XCTAssertEqual(optionsSizeCount[0], 1);
  357. XCTAssertEqual(optionsSizeCount[1], 6);
  358. XCTAssertEqual(optionsSizeCount[2], 12);
  359. XCTAssertEqual(optionsSizeCount[3], 8);
  360. XCTAssertEqual(uniqueMainCombinations.count, 27);
  361. int mainSizeCount[4] = {0};
  362. for (NSDictionary *dictionary in uniqueMainCombinations) {
  363. mainSizeCount[dictionary.count]++;
  364. }
  365. XCTAssertEqual(mainSizeCount[0], 1);
  366. XCTAssertEqual(mainSizeCount[1], 6);
  367. XCTAssertEqual(mainSizeCount[2], 12);
  368. XCTAssertEqual(mainSizeCount[3], 8);
  369. }
  370. - (void)testVersionFormat {
  371. NSRegularExpression *sLibraryVersionRegex =
  372. [NSRegularExpression regularExpressionWithPattern:@"^[0-9]{8,}$" options:0 error:NULL];
  373. NSUInteger numberOfMatches =
  374. [sLibraryVersionRegex numberOfMatchesInString:kFIRLibraryVersionID
  375. options:0
  376. range:NSMakeRange(0, kFIRLibraryVersionID.length)];
  377. XCTAssertEqual(numberOfMatches, 1, @"Incorrect library version format.");
  378. }
  379. @end