Skip to content

Commit eba4532

Browse files
committed
Android: test binary-diff package install codepaths
2 parents 316a439 + 1794c83 commit eba4532

14 files changed

Lines changed: 670 additions & 101 deletions

File tree

android/app/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ dependencies {
9797

9898
testImplementation 'junit:junit:4.13.2'
9999
testImplementation 'org.json:json:20231013'
100+
testImplementation 'org.mockito:mockito-core:5.14.2'
100101

101102
androidTestImplementation 'junit:junit:4.13.2'
102103
androidTestImplementation 'androidx.test.ext:junit:1.2.1'

android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateManager.java

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
185185
}
186186

187187
connection.setRequestProperty("Accept-Encoding", "identity");
188+
189+
int responseCode = connection.getResponseCode();
190+
if (responseCode < 200 || responseCode >= 300) {
191+
throw new CodePushUnknownException("Error downloading update package. Response code: "
192+
+ responseCode + ". Response body: " + NetworkUtils.readStreamToString(connection.getErrorStream()));
193+
}
194+
188195
bin = new BufferedInputStream(connection.getInputStream());
189196

190197
long totalBytes = connection.getContentLength();
@@ -234,6 +241,15 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
234241
}
235242
}
236243

244+
installDownloadedUpdate(updatePackage, expectedBundleFileName, stringPublicKey,
245+
downloadFile, isZip, newUpdateFolderPath, newUpdateMetadataPath);
246+
}
247+
248+
void installDownloadedUpdate(JSONObject updatePackage, String expectedBundleFileName,
249+
String stringPublicKey, File downloadFile, boolean isZip,
250+
String newUpdateFolderPath, String newUpdateMetadataPath) throws IOException {
251+
String newUpdateHash = updatePackage.optString(CodePushConstants.PACKAGE_HASH_KEY, null);
252+
237253
if (isZip) {
238254
// Unzip the downloaded file and then delete the zip
239255
String unzippedFolderPath = getUnzippedFolderPath();
@@ -301,30 +317,26 @@ public void downloadPackage(JSONObject updatePackage, String expectedBundleFileN
301317
String signaturePath = CodePushUpdateUtils.getSignatureFilePath(newUpdateFolderPath);
302318
boolean isSignatureAppearedInBundle = FileUtils.fileAtPathExists(signaturePath);
303319

320+
if (isSignatureVerificationEnabled && !isSignatureAppearedInBundle) {
321+
throw new CodePushInvalidUpdateException(
322+
"Error! Public key was provided but there is no JWT signature within app bundle to verify. " +
323+
"Possible reasons, why that might happen: \n" +
324+
"1. You've been released CodePush bundle update using version of CodePush CLI that is not support code signing.\n" +
325+
"2. You've been released CodePush bundle update without providing --privateKeyPath option."
326+
);
327+
}
328+
329+
if (!isSignatureVerificationEnabled && isSignatureAppearedInBundle) {
330+
CodePushUtils.log(
331+
"Warning! JWT signature exists in codepush update but code integrity check couldn't be performed because there is no public key configured. " +
332+
"Please ensure that public key is properly configured within your application."
333+
);
334+
}
335+
336+
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
337+
304338
if (isSignatureVerificationEnabled) {
305-
if (isSignatureAppearedInBundle) {
306-
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
307-
CodePushUpdateUtils.verifyUpdateSignature(newUpdateFolderPath, newUpdateHash, stringPublicKey);
308-
} else {
309-
throw new CodePushInvalidUpdateException(
310-
"Error! Public key was provided but there is no JWT signature within app bundle to verify. " +
311-
"Possible reasons, why that might happen: \n" +
312-
"1. You've been released CodePush bundle update using version of CodePush CLI that is not support code signing.\n" +
313-
"2. You've been released CodePush bundle update without providing --privateKeyPath option."
314-
);
315-
}
316-
} else {
317-
if (isSignatureAppearedInBundle) {
318-
CodePushUtils.log(
319-
"Warning! JWT signature exists in codepush update but code integrity check couldn't be performed because there is no public key configured. " +
320-
"Please ensure that public key is properly configured within your application."
321-
);
322-
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
323-
} else {
324-
if (isDiffUpdate) {
325-
CodePushUpdateUtils.verifyFolderHash(newUpdateFolderPath, newUpdateHash);
326-
}
327-
}
339+
CodePushUpdateUtils.verifyUpdateSignature(newUpdateFolderPath, newUpdateHash, stringPublicKey);
328340
}
329341

330342
CodePushUtils.setJSONValueForKey(updatePackage, CodePushConstants.RELATIVE_BUNDLE_PATH_KEY, relativeBundlePath);
@@ -384,6 +396,13 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl, String bundl
384396
try {
385397
downloadUrl = new URL(remoteBundleUrl);
386398
connection = (HttpURLConnection) (downloadUrl.openConnection());
399+
400+
int responseCode = connection.getResponseCode();
401+
if (responseCode < 200 || responseCode >= 300) {
402+
throw new CodePushUnknownException("Error downloading update package. Response code: "
403+
+ responseCode + ". Response body: " + NetworkUtils.readStreamToString(connection.getErrorStream()));
404+
}
405+
387406
bin = new BufferedInputStream(connection.getInputStream());
388407
File downloadFile = new File(getCurrentPackageBundlePath(bundleFileName));
389408
downloadFile.delete();
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@file:JvmName("NetworkUtils")
2+
3+
package com.microsoft.codepush.react
4+
5+
import java.io.InputStream
6+
7+
fun readStreamToString(inputStream: InputStream?): String {
8+
return inputStream?.bufferedReader()?.use { it.readText() } ?: ""
9+
}

0 commit comments

Comments
 (0)