FIRCLSSettingsTests.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. // Copyright 2019 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 "FIRCLSSettings.h"
  15. #import <Foundation/Foundation.h>
  16. #import <XCTest/XCTest.h>
  17. #if __has_include(<FBLPromises/FBLPromises.h>)
  18. #import <FBLPromises/FBLPromises.h>
  19. #else
  20. #import "FBLPromises.h"
  21. #endif
  22. #import "FABMockApplicationIdentifierModel.h"
  23. #import "FIRCLSFileManager.h"
  24. #import "FIRCLSMockFileManager.h"
  25. const NSString *FIRCLSTestSettingsActivated =
  26. @"{\"settings_version\":3,\"cache_duration\":60,\"features\":{\"collect_logged_exceptions\":"
  27. @"true,\"collect_reports\":true},\"app\":{\"status\":\"activated\",\"update_required\":false},"
  28. @"\"fabric\":{\"org_id\":\"010101000000111111111111\",\"bundle_id\":\"com.lets.test."
  29. @"crashlytics\"}}";
  30. const NSString *FIRCLSTestSettingsInverse =
  31. @"{\"settings_version\":3,\"cache_duration\":12345,\"features\":{\"collect_logged_exceptions\":"
  32. @"false,\"collect_reports\":false},\"app\":{\"status\":\"new\",\"update_required\":true},"
  33. @"\"fabric\":{\"org_id\":\"01e101a0000011b113115111\",\"bundle_id\":\"im.from.the.server\"},"
  34. @"\"session\":{\"log_buffer_size\":128000,\"max_chained_exception_depth\":32,\"max_complete_"
  35. @"sessions_count\":4,\"max_custom_exception_events\":1000,\"max_custom_key_value_pairs\":2000,"
  36. @"\"identifier_mask\":255}}";
  37. const NSString *FIRCLSTestSettingsCorrupted = @"{{{{ non_key: non\"value {}";
  38. NSString *FIRCLSDefaultMockBuildInstanceID = @"12345abcdef";
  39. NSString *FIRCLSDifferentMockBuildInstanceID = @"98765zyxwv";
  40. NSString *FIRCLSDefaultMockAppDisplayVersion = @"1.2.3-beta.2";
  41. NSString *FIRCLSDifferentMockAppDisplayVersion = @"1.2.3-beta.3";
  42. NSString *FIRCLSDefaultMockAppBuildVersion = @"1024";
  43. NSString *FIRCLSDifferentMockAppBuildVersion = @"2048";
  44. NSString *const TestGoogleAppID = @"1:test:google:app:id";
  45. NSString *const TestChangedGoogleAppID = @"2:changed:google:app:id";
  46. @interface FIRCLSSettings (Testing)
  47. @property(nonatomic, strong) NSDictionary<NSString *, id> *settingsDictionary;
  48. @end
  49. @interface FIRCLSSettingsTests : XCTestCase
  50. @property(nonatomic, retain) FIRCLSMockFileManager *fileManager;
  51. @property(nonatomic, retain) FABMockApplicationIdentifierModel *appIDModel;
  52. @property(nonatomic, retain) FIRCLSSettings *settings;
  53. @end
  54. @implementation FIRCLSSettingsTests
  55. - (void)setUp {
  56. [super setUp];
  57. _fileManager = [[FIRCLSMockFileManager alloc] init];
  58. _appIDModel = [[FABMockApplicationIdentifierModel alloc] init];
  59. _appIDModel.buildInstanceID = FIRCLSDefaultMockBuildInstanceID;
  60. _appIDModel.displayVersion = FIRCLSDefaultMockAppDisplayVersion;
  61. _appIDModel.buildVersion = FIRCLSDefaultMockAppBuildVersion;
  62. _settings = [[FIRCLSSettings alloc] initWithFileManager:_fileManager appIDModel:_appIDModel];
  63. }
  64. - (void)testDefaultSettings {
  65. XCTAssertEqual(self.settings.isCacheExpired, YES);
  66. // Default to an hour
  67. XCTAssertEqual(self.settings.cacheDurationSeconds, 60 * 60);
  68. XCTAssertEqualObjects(self.settings.orgID, nil);
  69. XCTAssertEqualObjects(self.settings.fetchedBundleID, nil);
  70. XCTAssertFalse(self.settings.appNeedsOnboarding);
  71. XCTAssertFalse(self.settings.appUpdateRequired);
  72. XCTAssertTrue(self.settings.collectReportsEnabled);
  73. XCTAssertTrue(self.settings.errorReportingEnabled);
  74. XCTAssertTrue(self.settings.customExceptionsEnabled);
  75. XCTAssertEqual(self.settings.errorLogBufferSize, 64 * 1000);
  76. XCTAssertEqual(self.settings.logBufferSize, 64 * 1000);
  77. XCTAssertEqual(self.settings.maxCustomExceptions, 8);
  78. XCTAssertEqual(self.settings.maxCustomKeys, 64);
  79. XCTAssertTrue(self.settings.shouldUseNewReportEndpoint);
  80. }
  81. - (BOOL)writeSettings:(const NSString *)settings error:(NSError **)error {
  82. return [self writeSettings:settings error:error isCacheKey:NO];
  83. }
  84. - (BOOL)writeSettings:(const NSString *)settings
  85. error:(NSError **)error
  86. isCacheKey:(BOOL)isCacheKey {
  87. NSString *path = _fileManager.settingsFilePath;
  88. if (isCacheKey) {
  89. path = _fileManager.settingsCacheKeyPath;
  90. }
  91. return [self.fileManager createFileAtPath:path
  92. contents:[settings dataUsingEncoding:NSUTF8StringEncoding]
  93. attributes:nil];
  94. }
  95. - (void)cacheSettingsWithGoogleAppID:(NSString *)googleAppID
  96. currentTimestamp:(NSTimeInterval)currentTimestamp
  97. expectedRemoveCount:(NSInteger)expectedRemoveCount {
  98. self.fileManager.removeExpectation = [[XCTestExpectation alloc]
  99. initWithDescription:@"FIRCLSMockFileManager.removeExpectation.cache"];
  100. self.fileManager.removeCount = 0;
  101. self.fileManager.expectedRemoveCount = expectedRemoveCount;
  102. [self.settings cacheSettingsWithGoogleAppID:googleAppID currentTimestamp:currentTimestamp];
  103. [self waitForExpectations:@[ self.fileManager.removeExpectation ] timeout:1];
  104. }
  105. - (void)reloadFromCacheWithGoogleAppID:(NSString *)googleAppID
  106. currentTimestamp:(NSTimeInterval)currentTimestamp
  107. expectedRemoveCount:(NSInteger)expectedRemoveCount {
  108. self.fileManager.removeExpectation = [[XCTestExpectation alloc]
  109. initWithDescription:@"FIRCLSMockFileManager.removeExpectation.reload"];
  110. self.fileManager.removeCount = 0;
  111. self.fileManager.expectedRemoveCount = expectedRemoveCount;
  112. [self.settings reloadFromCacheWithGoogleAppID:googleAppID currentTimestamp:currentTimestamp];
  113. [self waitForExpectations:@[ self.fileManager.removeExpectation ] timeout:1];
  114. }
  115. - (void)testActivatedSettingsCached {
  116. NSError *error = nil;
  117. [self writeSettings:FIRCLSTestSettingsActivated error:&error];
  118. XCTAssertNil(error, "%@", error);
  119. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  120. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  121. XCTAssertEqual(self.settings.isCacheExpired, NO);
  122. XCTAssertEqual(self.settings.cacheDurationSeconds, 60);
  123. XCTAssertEqualObjects(self.settings.orgID, @"010101000000111111111111");
  124. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"com.lets.test.crashlytics");
  125. XCTAssertFalse(self.settings.appNeedsOnboarding);
  126. XCTAssertFalse(self.settings.appUpdateRequired);
  127. XCTAssertTrue(self.settings.collectReportsEnabled);
  128. XCTAssertTrue(self.settings.errorReportingEnabled);
  129. XCTAssertTrue(self.settings.customExceptionsEnabled);
  130. XCTAssertEqual(self.settings.errorLogBufferSize, 64 * 1000);
  131. XCTAssertEqual(self.settings.logBufferSize, 64 * 1000);
  132. XCTAssertEqual(self.settings.maxCustomExceptions, 8);
  133. XCTAssertEqual(self.settings.maxCustomKeys, 64);
  134. }
  135. - (void)testInverseDefaultSettingsCached {
  136. NSError *error = nil;
  137. [self writeSettings:FIRCLSTestSettingsInverse error:&error];
  138. XCTAssertNil(error, "%@", error);
  139. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  140. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  141. XCTAssertEqual(self.settings.isCacheExpired, NO);
  142. XCTAssertEqual(self.settings.cacheDurationSeconds, 12345);
  143. XCTAssertEqualObjects(self.settings.orgID, @"01e101a0000011b113115111");
  144. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"im.from.the.server");
  145. XCTAssertTrue(self.settings.appNeedsOnboarding);
  146. XCTAssertTrue(self.settings.appUpdateRequired);
  147. XCTAssertFalse(self.settings.collectReportsEnabled);
  148. XCTAssertFalse(self.settings.errorReportingEnabled);
  149. XCTAssertFalse(self.settings.customExceptionsEnabled);
  150. XCTAssertEqual(self.settings.errorLogBufferSize, 128000);
  151. XCTAssertEqual(self.settings.logBufferSize, 128000);
  152. XCTAssertEqual(self.settings.maxCustomExceptions, 1000);
  153. XCTAssertEqual(self.settings.maxCustomKeys, 2000);
  154. }
  155. - (void)testCacheExpiredFromTTL {
  156. NSError *error = nil;
  157. [self writeSettings:FIRCLSTestSettingsActivated error:&error];
  158. XCTAssertNil(error, "%@", error);
  159. // 1 delete for clearing the cache key, plus 2 for the deletes from reloading and clearing the
  160. // cache and cache key
  161. self.fileManager.expectedRemoveCount = 3;
  162. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  163. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  164. // Go forward in time by 2x the cache duration
  165. NSTimeInterval futureTimestamp = currentTimestamp + (2 * self.settings.cacheDurationSeconds);
  166. [self.settings reloadFromCacheWithGoogleAppID:TestGoogleAppID currentTimestamp:futureTimestamp];
  167. XCTAssertEqual(self.settings.isCacheExpired, YES);
  168. // Since the TTL just expired, do not clear settings
  169. XCTAssertEqualObjects(self.settings.orgID, @"010101000000111111111111");
  170. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"com.lets.test.crashlytics");
  171. XCTAssertEqual(self.settings.errorLogBufferSize, 64 * 1000);
  172. // Pretend we fetched settings again, but they had different values
  173. [self writeSettings:FIRCLSTestSettingsInverse error:&error];
  174. XCTAssertNil(error, "%@", error);
  175. // Cache the settings
  176. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  177. // We should have the updated values that were fetched, and should not be expired
  178. XCTAssertEqual(self.settings.isCacheExpired, NO);
  179. XCTAssertEqualObjects(self.settings.orgID, @"01e101a0000011b113115111");
  180. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"im.from.the.server");
  181. XCTAssertEqual(self.settings.errorLogBufferSize, 128000);
  182. }
  183. - (void)testCacheExpiredFromBuildInstanceID {
  184. NSError *error = nil;
  185. [self writeSettings:FIRCLSTestSettingsActivated error:&error];
  186. XCTAssertNil(error, "%@", error);
  187. // 1 delete for clearing the cache key, plus 2 for the deletes from reloading and clearing the
  188. // cache and cache key
  189. self.fileManager.expectedRemoveCount = 3;
  190. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  191. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  192. // Change the Build Instance ID
  193. self.appIDModel.buildInstanceID = FIRCLSDifferentMockBuildInstanceID;
  194. [self.settings reloadFromCacheWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  195. XCTAssertEqual(self.settings.isCacheExpired, YES);
  196. // Since the TTL just expired, do not clear settings
  197. XCTAssertEqualObjects(self.settings.orgID, @"010101000000111111111111");
  198. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"com.lets.test.crashlytics");
  199. XCTAssertEqual(self.settings.errorLogBufferSize, 64 * 1000);
  200. // Pretend we fetched settings again, but they had different values
  201. [self writeSettings:FIRCLSTestSettingsInverse error:&error];
  202. XCTAssertNil(error, "%@", error);
  203. // Cache the settings
  204. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  205. // We should have the updated values that were fetched, and should not be expired
  206. XCTAssertEqual(self.settings.isCacheExpired, NO);
  207. XCTAssertEqualObjects(self.settings.orgID, @"01e101a0000011b113115111");
  208. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"im.from.the.server");
  209. XCTAssertEqual(self.settings.errorLogBufferSize, 128000);
  210. }
  211. - (void)testCacheExpiredFromAppVersion {
  212. NSError *error = nil;
  213. [self writeSettings:FIRCLSTestSettingsActivated error:&error];
  214. XCTAssertNil(error, "%@", error);
  215. // 1 delete for clearing the cache key, plus 2 for the deletes from reloading and clearing the
  216. // cache and cache key
  217. self.fileManager.expectedRemoveCount = 3;
  218. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  219. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  220. // Change the App Version
  221. self.appIDModel.displayVersion = FIRCLSDifferentMockAppDisplayVersion;
  222. self.appIDModel.buildVersion = FIRCLSDifferentMockAppBuildVersion;
  223. [self.settings reloadFromCacheWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  224. XCTAssertEqual(self.settings.isCacheExpired, YES);
  225. // Since the TTL just expired, do not clear settings
  226. XCTAssertEqualObjects(self.settings.orgID, @"010101000000111111111111");
  227. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"com.lets.test.crashlytics");
  228. XCTAssertEqual(self.settings.errorLogBufferSize, 64 * 1000);
  229. // Pretend we fetched settings again, but they had different values
  230. [self writeSettings:FIRCLSTestSettingsInverse error:&error];
  231. XCTAssertNil(error, "%@", error);
  232. // Cache the settings
  233. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  234. // We should have the updated values that were fetched, and should not be expired
  235. XCTAssertEqual(self.settings.isCacheExpired, NO);
  236. XCTAssertEqualObjects(self.settings.orgID, @"01e101a0000011b113115111");
  237. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"im.from.the.server");
  238. XCTAssertEqual(self.settings.errorLogBufferSize, 128000);
  239. }
  240. - (void)testGoogleAppIDChanged {
  241. NSError *error = nil;
  242. [self writeSettings:FIRCLSTestSettingsInverse error:&error];
  243. XCTAssertNil(error, "%@", error);
  244. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  245. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  246. // Different Google App ID
  247. [self reloadFromCacheWithGoogleAppID:TestChangedGoogleAppID
  248. currentTimestamp:currentTimestamp
  249. expectedRemoveCount:2];
  250. XCTAssertEqual(self.settings.isCacheExpired, YES);
  251. // Clear the settings because they were for a different Google App ID
  252. XCTAssertEqualObjects(self.settings.orgID, nil);
  253. XCTAssertEqualObjects(self.settings.fetchedBundleID, nil);
  254. // Pretend we fetched settings again, but they had different values
  255. [self writeSettings:FIRCLSTestSettingsActivated error:&error];
  256. XCTAssertNil(error, "%@", error);
  257. // Cache the settings with the new Google App ID
  258. [self.settings cacheSettingsWithGoogleAppID:TestChangedGoogleAppID
  259. currentTimestamp:currentTimestamp];
  260. // Should have new values and not expired
  261. XCTAssertEqual(self.settings.isCacheExpired, NO);
  262. XCTAssertEqualObjects(self.settings.orgID, @"010101000000111111111111");
  263. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"com.lets.test.crashlytics");
  264. XCTAssertEqual(self.settings.errorLogBufferSize, 64 * 1000);
  265. }
  266. // This is a weird case where we got settings, but never created a cache key for it. We are
  267. // treating this as if the cache was invalid and re-fetching in this case.
  268. - (void)testActivatedSettingsMissingCacheKey {
  269. NSError *error = nil;
  270. [self writeSettings:FIRCLSTestSettingsActivated error:&error];
  271. XCTAssertNil(error, "%@", error);
  272. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  273. // We only expect 1 removal because the cache key doesn't exist,
  274. // and deleteCachedSettings deletes the cache and the cache key
  275. [self reloadFromCacheWithGoogleAppID:TestGoogleAppID
  276. currentTimestamp:currentTimestamp
  277. expectedRemoveCount:1];
  278. XCTAssertEqual(self.settings.isCacheExpired, YES);
  279. XCTAssertEqual(self.settings.cacheDurationSeconds, 3600);
  280. XCTAssertEqualObjects(self.settings.orgID, nil);
  281. XCTAssertEqualObjects(self.settings.fetchedBundleID, nil);
  282. XCTAssertFalse(self.settings.appNeedsOnboarding);
  283. XCTAssertFalse(self.settings.appUpdateRequired);
  284. XCTAssertTrue(self.settings.collectReportsEnabled);
  285. XCTAssertTrue(self.settings.errorReportingEnabled);
  286. XCTAssertTrue(self.settings.customExceptionsEnabled);
  287. XCTAssertEqual(self.settings.errorLogBufferSize, 64 * 1000);
  288. XCTAssertEqual(self.settings.logBufferSize, 64 * 1000);
  289. XCTAssertEqual(self.settings.maxCustomExceptions, 8);
  290. XCTAssertEqual(self.settings.maxCustomKeys, 64);
  291. }
  292. // These tests are partially to make sure the SDK doesn't crash when it
  293. // has corrupted settings.
  294. - (void)testCorruptCache {
  295. // First write and load a good settings file
  296. NSError *error = nil;
  297. [self writeSettings:FIRCLSTestSettingsInverse error:&error];
  298. XCTAssertNil(error, "%@", error);
  299. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  300. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  301. // Should have "Inverse" values
  302. XCTAssertEqual(self.settings.isCacheExpired, NO);
  303. XCTAssertEqual(self.settings.cacheDurationSeconds, 12345);
  304. XCTAssertEqualObjects(self.settings.orgID, @"01e101a0000011b113115111");
  305. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"im.from.the.server");
  306. XCTAssertTrue(self.settings.appNeedsOnboarding);
  307. XCTAssertEqual(self.settings.errorLogBufferSize, 128000);
  308. // Then write a corrupted one and cache + reload it
  309. [self writeSettings:FIRCLSTestSettingsCorrupted error:&error];
  310. XCTAssertNil(error, "%@", error);
  311. // Cache them, and reload. Since it's corrupted we should delete it all
  312. [self cacheSettingsWithGoogleAppID:TestGoogleAppID
  313. currentTimestamp:currentTimestamp
  314. expectedRemoveCount:2];
  315. // Should have default values because we deleted the cache and settingsDictionary
  316. XCTAssertEqual(self.settings.isCacheExpired, YES);
  317. XCTAssertEqual(self.settings.cacheDurationSeconds, 3600);
  318. XCTAssertEqualObjects(self.settings.orgID, nil);
  319. XCTAssertEqualObjects(self.settings.fetchedBundleID, nil);
  320. XCTAssertFalse(self.settings.appNeedsOnboarding);
  321. XCTAssertEqual(self.settings.errorLogBufferSize, 64 * 1000);
  322. XCTAssertTrue(self.settings.shouldUseNewReportEndpoint);
  323. }
  324. - (void)testCorruptCacheKey {
  325. // First write and load a good settings file
  326. NSError *error = nil;
  327. [self writeSettings:FIRCLSTestSettingsInverse error:&error];
  328. XCTAssertNil(error, "%@", error);
  329. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  330. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  331. // Should have "Inverse" values
  332. XCTAssertEqual(self.settings.isCacheExpired, NO);
  333. XCTAssertEqual(self.settings.cacheDurationSeconds, 12345);
  334. XCTAssertEqualObjects(self.settings.orgID, @"01e101a0000011b113115111");
  335. XCTAssertEqualObjects(self.settings.fetchedBundleID, @"im.from.the.server");
  336. XCTAssertTrue(self.settings.appNeedsOnboarding);
  337. XCTAssertEqual(self.settings.errorLogBufferSize, 128000);
  338. // Then pretend we wrote a corrupted cache key and just reload it
  339. [self writeSettings:FIRCLSTestSettingsCorrupted error:&error isCacheKey:YES];
  340. XCTAssertNil(error, "%@", error);
  341. // Since settings themselves are corrupted, delete it all
  342. [self reloadFromCacheWithGoogleAppID:TestGoogleAppID
  343. currentTimestamp:currentTimestamp
  344. expectedRemoveCount:2];
  345. // Should have default values because we deleted the cache and settingsDictionary
  346. XCTAssertEqual(self.settings.isCacheExpired, YES);
  347. XCTAssertEqual(self.settings.cacheDurationSeconds, 3600);
  348. XCTAssertEqualObjects(self.settings.orgID, nil);
  349. XCTAssertEqualObjects(self.settings.fetchedBundleID, nil);
  350. XCTAssertFalse(self.settings.appNeedsOnboarding);
  351. XCTAssertEqual(self.settings.errorLogBufferSize, 64 * 1000);
  352. }
  353. - (void)testNewReportEndpointSettings {
  354. NSString *settingsJSON =
  355. @"{\"settings_version\":3,\"cache_duration\":60,\"app\":{\"status\":\"activated\",\"update_"
  356. @"required\":false,\"report_upload_variant\":2}}";
  357. NSError *error = nil;
  358. [self writeSettings:settingsJSON error:&error];
  359. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  360. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  361. XCTAssertNil(error, "%@", error);
  362. XCTAssertNotNil(self.settings.settingsDictionary);
  363. NSLog(@"[Debug Log] %@", self.settings.settingsDictionary);
  364. XCTAssertTrue(self.settings.shouldUseNewReportEndpoint);
  365. }
  366. - (void)testLegacyReportEndpointSettings {
  367. NSString *settingsJSON =
  368. @"{\"settings_version\":3,\"cache_duration\":60,\"app\":{\"status\":\"activated\",\"update_"
  369. @"required\":false,\"report_upload_variant\":1}}";
  370. NSError *error = nil;
  371. [self writeSettings:settingsJSON error:&error];
  372. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  373. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  374. XCTAssertNil(error, "%@", error);
  375. XCTAssertFalse(self.settings.shouldUseNewReportEndpoint);
  376. }
  377. - (void)testLegacyReportEndpointSettingsWithNonExistentKey {
  378. NSString *settingsJSON = @"{\"settings_version\":3,\"cache_duration\":60,\"app\":{\"status\":"
  379. @"\"activated\",\"update_required\":false}}";
  380. NSError *error = nil;
  381. [self writeSettings:settingsJSON error:&error];
  382. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  383. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  384. XCTAssertNil(error, "%@", error);
  385. XCTAssertTrue(self.settings.shouldUseNewReportEndpoint);
  386. }
  387. - (void)testLegacyReportEndpointSettingsWithUnknownValue {
  388. NSString *newEndpointJSON =
  389. @"{\"settings_version\":3,\"cache_duration\":60,\"app\":{\"status\":\"activated\",\"update_"
  390. @"required\":false,\"report_upload_variant\":xyz}}";
  391. NSError *error = nil;
  392. [self writeSettings:newEndpointJSON error:&error];
  393. NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
  394. [self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
  395. XCTAssertNil(error, "%@", error);
  396. XCTAssertTrue(self.settings.shouldUseNewReportEndpoint);
  397. }
  398. - (void)testShouldUseNewReportEndpointWithEmptyDictionary {
  399. NSError *error = nil;
  400. [self writeSettings:nil error:&error];
  401. XCTAssertNil(error, "%@", error);
  402. XCTAssertNotNil(self.settings);
  403. XCTAssertTrue(self.settings.shouldUseNewReportEndpoint);
  404. }
  405. @end