Skip to content

Commit 5bc1a9e

Browse files
authored
Crashlytics cache key app version (#5132)
1 parent b8ed76d commit 5bc1a9e

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
lines changed

Crashlytics/Crashlytics/Models/FIRCLSSettings.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
NSString *const CreatedAtKey = @"created_at";
3030
NSString *const GoogleAppIDKey = @"google_app_id";
3131
NSString *const BuildInstanceID = @"build_instance_id";
32+
NSString *const AppVersion = @"app_version";
3233

3334
@interface FIRCLSSettings ()
3435

@@ -120,6 +121,15 @@ - (void)reloadFromCacheWithGoogleAppID:(NSString *)googleAppID
120121
self.isCacheKeyExpired = YES;
121122
}
122123
}
124+
125+
NSString *cacheAppVersion = cacheKey[AppVersion];
126+
if (![cacheAppVersion isEqualToString:self.appIDModel.synthesizedVersion]) {
127+
FIRCLSDebugLog(@"[Crashlytics:Settings] Settings expired because app version changed");
128+
129+
@synchronized(self) {
130+
self.isCacheKeyExpired = YES;
131+
}
132+
}
123133
}
124134

125135
- (void)cacheSettingsWithGoogleAppID:(NSString *)googleAppID
@@ -129,6 +139,7 @@ - (void)cacheSettingsWithGoogleAppID:(NSString *)googleAppID
129139
CreatedAtKey : createdAtTimestamp,
130140
GoogleAppIDKey : googleAppID,
131141
BuildInstanceID : self.appIDModel.buildInstanceID,
142+
AppVersion : self.appIDModel.synthesizedVersion,
132143
};
133144

134145
NSError *error = nil;

Crashlytics/Crashlytics/Settings/Models/FIRCLSApplicationIdentifierModel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ NS_ASSUME_NONNULL_BEGIN
3434
@property(nonatomic, readonly, nullable) NSString* platform;
3535
@property(nonatomic, readonly, nullable) NSString* buildVersion;
3636
@property(nonatomic, readonly, nullable) NSString* displayVersion;
37+
38+
/**
39+
* Returns the synthesized app version, similar to how the backend does it
40+
* <displayVersion> (<buildVersion>)
41+
*/
42+
@property(nonatomic, readonly, nullable) NSString* synthesizedVersion;
43+
3744
@property(nonatomic, readonly) FIRCLSApplicationInstallationSourceType installSource;
3845

3946
/**

Crashlytics/Crashlytics/Settings/Models/FIRCLSApplicationIdentifierModel.m

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ - (NSString *)displayVersion {
6666
return FIRCLSApplicationGetShortBundleVersion();
6767
}
6868

69+
- (NSString *)synthesizedVersion {
70+
return [NSString stringWithFormat:@"%@ (%@)", self.displayName, self.buildVersion];
71+
}
72+
6973
- (FIRCLSApplicationInstallationSourceType)installSource {
7074
return FIRCLSApplicationInstallationSource();
7175
}

Crashlytics/UnitTests/FIRCLSSettingsTests.m

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@
4646
NSString *FIRCLSDefaultMockBuildInstanceID = @"12345abcdef";
4747
NSString *FIRCLSDifferentMockBuildInstanceID = @"98765zyxwv";
4848

49+
NSString *FIRCLSDefaultMockAppDisplayVersion = @"1.2.3-beta.2";
50+
NSString *FIRCLSDifferentMockAppDisplayVersion = @"1.2.3-beta.3";
51+
52+
NSString *FIRCLSDefaultMockAppBuildVersion = @"1024";
53+
NSString *FIRCLSDifferentMockAppBuildVersion = @"2048";
54+
4955
NSString *const TestGoogleAppID = @"1:test:google:app:id";
5056
NSString *const TestChangedGoogleAppID = @"2:changed:google:app:id";
5157

@@ -77,6 +83,8 @@ - (void)setUp {
7783

7884
_appIDModel = [[FABMockApplicationIdentifierModel alloc] init];
7985
_appIDModel.buildInstanceID = FIRCLSDefaultMockBuildInstanceID;
86+
_appIDModel.displayVersion = FIRCLSDefaultMockAppDisplayVersion;
87+
_appIDModel.buildVersion = FIRCLSDefaultMockAppBuildVersion;
8088

8189
_settings = [[FIRCLSSettings alloc] initWithFileManager:_fileManager appIDModel:_appIDModel];
8290
}
@@ -291,6 +299,45 @@ - (void)testCacheExpiredFromBuildInstanceID {
291299
XCTAssertEqual(self.settings.errorLogBufferSize, 128000);
292300
}
293301

302+
- (void)testCacheExpiredFromAppVersion {
303+
NSError *error = nil;
304+
[self writeSettings:FIRCLSTestSettingsActivated error:&error];
305+
XCTAssertNil(error, "%@", error);
306+
307+
// 1 delete for clearing the cache key, plus 2 for the deletes from reloading and clearing the
308+
// cache and cache key
309+
self.fileManager.expectedRemoveCount = 3;
310+
311+
NSTimeInterval currentTimestamp = [NSDate timeIntervalSinceReferenceDate];
312+
[self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
313+
314+
// Change the App Version
315+
self.appIDModel.displayVersion = FIRCLSDifferentMockAppDisplayVersion;
316+
self.appIDModel.buildVersion = FIRCLSDifferentMockAppBuildVersion;
317+
318+
[self.settings reloadFromCacheWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
319+
320+
XCTAssertEqual(self.settings.isCacheExpired, YES);
321+
322+
// Since the TTL just expired, do not clear settings
323+
XCTAssertEqualObjects(self.settings.orgID, @"010101000000111111111111");
324+
XCTAssertEqualObjects(self.settings.fetchedBundleID, @"com.lets.test.crashlytics");
325+
XCTAssertEqual(self.settings.errorLogBufferSize, 64 * 1000);
326+
327+
// Pretend we fetched settings again, but they had different values
328+
[self writeSettings:FIRCLSTestSettingsInverse error:&error];
329+
XCTAssertNil(error, "%@", error);
330+
331+
// Cache the settings
332+
[self.settings cacheSettingsWithGoogleAppID:TestGoogleAppID currentTimestamp:currentTimestamp];
333+
334+
// We should have the updated values that were fetched, and should not be expired
335+
XCTAssertEqual(self.settings.isCacheExpired, NO);
336+
XCTAssertEqualObjects(self.settings.orgID, @"01e101a0000011b113115111");
337+
XCTAssertEqualObjects(self.settings.fetchedBundleID, @"im.from.the.server");
338+
XCTAssertEqual(self.settings.errorLogBufferSize, 128000);
339+
}
340+
294341
- (void)testGoogleAppIDChanged {
295342
NSError *error = nil;
296343
[self writeSettings:FIRCLSTestSettingsInverse error:&error];

Crashlytics/UnitTests/Mocks/FABMockApplicationIdentifierModel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ NS_ASSUME_NONNULL_BEGIN
2323
// Allows us to set this value in FIRCLSSettingsTests
2424
@property(nonatomic, copy) NSString* buildInstanceID;
2525

26+
// Allows us to set this value in FIRCLSSettingsTests
27+
@property(nonatomic, copy) NSString* displayVersion;
28+
29+
// Allows us to set this value in FIRCLSSettingsTests
30+
@property(nonatomic, copy) NSString* buildVersion;
31+
2632
// Allows us to set this value in FIRCLSReportManagerTests
2733
@property(nonatomic, copy) NSString* bundleID;
2834

Crashlytics/UnitTests/Mocks/FABMockApplicationIdentifierModel.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ @implementation FABMockApplicationIdentifierModel
1919
// Don't call the custom methods that compute this value, so we can
2020
// set it in FIRCLSSettingsTests
2121
@synthesize buildInstanceID;
22+
@synthesize displayVersion;
23+
@synthesize buildVersion;
2224

2325
// Set in FIRCLSReportManagerTests
2426
@synthesize bundleID;

0 commit comments

Comments
 (0)