Skip to content

Commit ba0e6c4

Browse files
author
rachaprince
authored
remove the ReleaseIdentifierStorage, rely on apk hash (#3188)
* remove the ReleaseIdentifierStorage, rely on apk hash * Fix failing test'
1 parent c012ba8 commit ba0e6c4

File tree

5 files changed

+22
-82
lines changed

5 files changed

+22
-82
lines changed

firebase-app-distribution/src/main/java/com/google/firebase/app/distribution/CheckForNewReleaseClient.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ class CheckForNewReleaseClient {
4040
private final FirebaseApp firebaseApp;
4141
private final FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient;
4242
private final FirebaseInstallationsApi firebaseInstallationsApi;
43+
// Maintain an in-memory mapping from source file to APK hash to avoid re-calculating the hash
4344
private static final ConcurrentMap<String, String> cachedApkHashes = new ConcurrentHashMap<>();
44-
private final ReleaseIdentifierStorage releaseIdentifierStorage;
4545

4646
Task<AppDistributionReleaseInternal> cachedCheckForNewRelease = null;
4747
private final Executor checkForNewReleaseExecutor;
@@ -54,20 +54,16 @@ class CheckForNewReleaseClient {
5454
this.firebaseAppDistributionTesterApiClient = firebaseAppDistributionTesterApiClient;
5555
this.firebaseInstallationsApi = firebaseInstallationsApi;
5656
this.checkForNewReleaseExecutor = Executors.newSingleThreadExecutor();
57-
this.releaseIdentifierStorage =
58-
new ReleaseIdentifierStorage(firebaseApp.getApplicationContext());
5957
}
6058

6159
CheckForNewReleaseClient(
6260
@NonNull FirebaseApp firebaseApp,
6361
@NonNull FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient,
6462
@NonNull FirebaseInstallationsApi firebaseInstallationsApi,
65-
@NonNull ReleaseIdentifierStorage releaseIdentifierStorage,
6663
@NonNull Executor executor) {
6764
this.firebaseApp = firebaseApp;
6865
this.firebaseAppDistributionTesterApiClient = firebaseAppDistributionTesterApiClient;
6966
this.firebaseInstallationsApi = firebaseInstallationsApi;
70-
this.releaseIdentifierStorage = releaseIdentifierStorage;
7167
this.checkForNewReleaseExecutor = executor;
7268
}
7369

@@ -158,7 +154,8 @@ private boolean isNewerBuildVersion(AppDistributionReleaseInternal newRelease)
158154
}
159155

160156
@VisibleForTesting
161-
boolean isSameAsInstalledRelease(AppDistributionReleaseInternal newRelease) {
157+
boolean isSameAsInstalledRelease(AppDistributionReleaseInternal newRelease)
158+
throws FirebaseAppDistributionException {
162159
if (newRelease.getBinaryType().equals(BinaryType.APK)) {
163160
return hasSameHashAsInstalledRelease(newRelease);
164161
}
@@ -201,7 +198,8 @@ String extractApkHash(PackageInfo packageInfo) {
201198
return cachedApkHashes.get(key);
202199
}
203200

204-
private boolean hasSameHashAsInstalledRelease(AppDistributionReleaseInternal newRelease) {
201+
private boolean hasSameHashAsInstalledRelease(AppDistributionReleaseInternal newRelease)
202+
throws FirebaseAppDistributionException {
205203
try {
206204
Context context = firebaseApp.getApplicationContext();
207205
PackageInfo metadataPackageInfo =
@@ -211,12 +209,9 @@ private boolean hasSameHashAsInstalledRelease(AppDistributionReleaseInternal new
211209
String installedReleaseApkHash = extractApkHash(metadataPackageInfo);
212210

213211
if (installedReleaseApkHash.isEmpty() || newRelease.getApkHash().isEmpty()) {
214-
// We don't have enough information about the APK hashes. Fallback to the external codehash.
215-
// TODO: Consider removing this when all returned releases have the efficient ApkHash
216-
String externalCodeHash =
217-
releaseIdentifierStorage.getExternalCodeHash(installedReleaseApkHash);
218-
LogWrapper.getInstance().v(TAG + "Defaulting to external codehash " + externalCodeHash);
219-
return externalCodeHash != null && externalCodeHash.equals(newRelease.getCodeHash());
212+
LogWrapper.getInstance().e(TAG + "Missing APK hash.");
213+
throw new FirebaseAppDistributionException(
214+
Constants.ErrorMessages.UNKNOWN_ERROR, FirebaseAppDistributionException.Status.UNKNOWN);
220215
}
221216
// If the hash of the zipped APK for the retrieved newRelease is equal to the stored hash
222217
// of the installed release, then they are the same release.

firebase-app-distribution/src/main/java/com/google/firebase/app/distribution/ReleaseIdentifierStorage.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

firebase-app-distribution/src/main/java/com/google/firebase/app/distribution/UpdateApkClient.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.google.firebase.app.distribution;
1616

1717
import static com.google.firebase.app.distribution.FirebaseAppDistributionException.Status.NETWORK_FAILURE;
18-
import static com.google.firebase.app.distribution.ReleaseIdentificationUtils.calculateApkHash;
1918
import static com.google.firebase.app.distribution.TaskUtils.safeSetTaskException;
2019
import static com.google.firebase.app.distribution.TaskUtils.safeSetTaskResult;
2120

@@ -51,8 +50,6 @@ class UpdateApkClient {
5150
@GuardedBy("updateTaskLock")
5251
private UpdateTaskImpl cachedUpdateTask;
5352

54-
private final ReleaseIdentifierStorage releaseIdentifierStorage;
55-
5653
private final Object updateTaskLock = new Object();
5754

5855
public UpdateApkClient(
@@ -64,8 +61,6 @@ public UpdateApkClient(
6461
@NonNull Executor downloadExecutor,
6562
@NonNull FirebaseApp firebaseApp,
6663
@NonNull InstallApkClient installApkClient) {
67-
this.releaseIdentifierStorage =
68-
new ReleaseIdentifierStorage(firebaseApp.getApplicationContext());
6964
this.appDistributionNotificationsManager =
7065
new FirebaseAppDistributionNotificationsManager(firebaseApp);
7166
this.downloadExecutor = downloadExecutor;
@@ -250,14 +245,6 @@ private void downloadToDisk(
250245
FirebaseAppDistributionException.Status.DOWNLOAD_FAILURE));
251246
}
252247

253-
File downloadedFile = new File(firebaseApp.getApplicationContext().getFilesDir(), fileName);
254-
255-
String internalCodeHash = calculateApkHash(downloadedFile);
256-
257-
if (internalCodeHash != null) {
258-
releaseIdentifierStorage.setCodeHashMap(internalCodeHash, newRelease);
259-
}
260-
261248
// completion
262249
postUpdateProgress(
263250
totalSize, totalSize, UpdateStatus.DOWNLOADED, showDownloadNotificationManager);

firebase-app-distribution/src/test/java/com/google/firebase/app/distribution/CheckForNewReleaseClientTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ public class CheckForNewReleaseClientTest {
7777
@Mock private FirebaseInstallationsApi mockFirebaseInstallations;
7878
@Mock private FirebaseAppDistributionTesterApiClient mockFirebaseAppDistributionTesterApiClient;
7979
@Mock private InstallationTokenResult mockInstallationTokenResult;
80-
@Mock private ReleaseIdentifierStorage mockReleaseIdentifierStorage;
8180

8281
Executor testExecutor = Executors.newSingleThreadExecutor();
8382

@@ -127,7 +126,6 @@ public void setup() {
127126
firebaseApp,
128127
mockFirebaseAppDistributionTesterApiClient,
129128
mockFirebaseInstallations,
130-
mockReleaseIdentifierStorage,
131129
testExecutor));
132130
}
133131

@@ -307,27 +305,29 @@ public void handleNewReleaseFromClient_whenNewReleaseIsSameAsInstalledAab_return
307305
}
308306

309307
@Test
310-
public void iisSameAsInstalledRelease_whenApkHashesEqual_returnsTrue() {
308+
public void iisSameAsInstalledRelease_whenApkHashesEqual_returnsTrue()
309+
throws FirebaseAppDistributionException {
311310
doReturn(CURRENT_APK_HASH).when(checkForNewReleaseClient).extractApkHash(any());
312311
assertTrue(
313312
checkForNewReleaseClient.isSameAsInstalledRelease(getTestInstalledRelease().build()));
314313
}
315314

316315
@Test
317-
public void isSameAsInstalledRelease_whenApkHashesNotEqual_returnsFalse() {
316+
public void isSameAsInstalledRelease_whenApkHashesNotEqual_returnsFalse()
317+
throws FirebaseAppDistributionException {
318318
doReturn(CURRENT_APK_HASH).when(checkForNewReleaseClient).extractApkHash(any());
319319
assertFalse(checkForNewReleaseClient.isSameAsInstalledRelease(getTestNewRelease().build()));
320320
}
321321

322322
@Test
323-
public void isSameAsInstalledRelease_ifApkHashNotPresent_fallsBackToExternalCodeHash() {
323+
public void isSameAsInstalledRelease_ifApkHashNotPresent_throwsError() {
324324
doReturn(CURRENT_APK_HASH).when(checkForNewReleaseClient).extractApkHash(any());
325-
when(mockReleaseIdentifierStorage.getExternalCodeHash(any())).thenReturn(CURRENT_CODEHASH);
326325

327-
assertFalse(
328-
checkForNewReleaseClient.isSameAsInstalledRelease(
329-
getTestNewRelease().setApkHash("").build()));
330-
verify(mockReleaseIdentifierStorage).getExternalCodeHash(CURRENT_APK_HASH);
326+
assertThrows(
327+
FirebaseAppDistributionException.class,
328+
() ->
329+
checkForNewReleaseClient.isSameAsInstalledRelease(
330+
getTestNewRelease().setApkHash("").build()));
331331
}
332332

333333
@Test

firebase-app-distribution/src/test/java/com/google/firebase/app/distribution/UpdateApkClientTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public class UpdateApkClientTest {
7676
Executor testExecutor = Executors.newSingleThreadExecutor();
7777

7878
@Before
79-
public void setup() {
79+
public void setup() throws FirebaseAppDistributionException {
8080

8181
MockitoAnnotations.initMocks(this);
8282

@@ -96,12 +96,11 @@ public void setup() {
9696

9797
this.updateApkClient =
9898
Mockito.spy(new UpdateApkClient(testExecutor, firebaseApp, mockInstallApkClient));
99+
doReturn(mockHttpsUrlConnection).when(updateApkClient).openHttpsUrlConnection(TEST_URL);
99100
}
100101

101102
@Test
102103
public void updateApk_whenDownloadFails_setsNetworkError() throws Exception {
103-
104-
doReturn(mockHttpsUrlConnection).when(updateApkClient).openHttpsUrlConnection(TEST_URL);
105104
// null inputStream causes download failure
106105
when(mockHttpsUrlConnection.getInputStream()).thenReturn(null);
107106
UpdateTaskImpl updateTask = updateApkClient.updateApk(TEST_RELEASE, false);
@@ -211,6 +210,8 @@ public void postProgressUpdate_whenErrorStatus_updatesNotificationsManagerWithEr
211210

212211
@Test
213212
public void updateApp_whenCalledMultipleTimesWithApk_returnsSameUpdateTask() {
213+
doReturn(Tasks.forResult(mockFile)).when(updateApkClient).downloadApk(TEST_RELEASE, false);
214+
214215
UpdateTask updateTask1 = updateApkClient.updateApk(TEST_RELEASE, false);
215216
UpdateTask updateTask2 = updateApkClient.updateApk(TEST_RELEASE, false);
216217

0 commit comments

Comments
 (0)