Check a commit-pinned manifest on a configurable schedule, notify about newer versions, verify downloaded APKs, and hand off installation to Android. Generate the feed with each signed build. Include the wake artwork recovery fix and on-device lifecycle and updater regression tests.
133 lines
5.5 KiB
Java
133 lines
5.5 KiB
Java
package tv.luma.launcher;
|
|
|
|
import android.app.*;
|
|
import android.app.job.*;
|
|
import android.content.*;
|
|
import android.net.Uri;
|
|
import android.os.*;
|
|
import java.io.*;
|
|
import java.util.*;
|
|
|
|
/** Real Android archive/signature parsing, HTTP, private file grants, and job scheduling. */
|
|
public final class UpdaterInstrumentation extends Instrumentation {
|
|
private final StringBuilder results = new StringBuilder();
|
|
private Updater updater;
|
|
|
|
@Override
|
|
public void onCreate(Bundle arguments) {
|
|
super.onCreate(arguments);
|
|
start();
|
|
}
|
|
|
|
private void ok(String message) {
|
|
results.append("PASS: ").append(message).append('\n');
|
|
}
|
|
|
|
private void source(String name) throws Exception {
|
|
updater.source("http://127.0.0.1:8766/" + name);
|
|
}
|
|
|
|
private void rejected(String feed, String message) throws Exception {
|
|
source(feed);
|
|
try {
|
|
Updater.Release release = updater.check(false);
|
|
if (release != null) updater.download(release, percent -> {});
|
|
} catch (Exception expected) {
|
|
if (expected.getMessage() == null || !expected.getMessage().contains(message)) throw expected;
|
|
if (new File(updater.apk().getParentFile(), "Luma.apk.part").exists())
|
|
throw new AssertionError("partial file retained");
|
|
ok(feed + " rejected: " + message);
|
|
return;
|
|
}
|
|
throw new AssertionError(feed + " was accepted");
|
|
}
|
|
|
|
@Override
|
|
public void onStart() {
|
|
Bundle result = new Bundle();
|
|
Map<String, ?> original = null;
|
|
int code = Activity.RESULT_CANCELED;
|
|
try {
|
|
updater = new Updater(getTargetContext());
|
|
original = new HashMap<>(updater.prefs.getAll());
|
|
if (updater.installedCode() != 3)
|
|
throw new AssertionError("Install the 1.1.0 bootstrap before this fixture test");
|
|
updater.prefs.edit().putInt("hours", 24).apply();
|
|
UpdateJob.schedule(getTargetContext());
|
|
JobInfo job =
|
|
getTargetContext().getSystemService(JobScheduler.class).getPendingJob(UpdateJob.ID);
|
|
if (job == null
|
|
|| !job.isPersisted()
|
|
|| job.getIntervalMillis() != 86400000L
|
|
|| job.getNetworkType() != JobInfo.NETWORK_TYPE_ANY)
|
|
throw new AssertionError("incorrect scheduled job");
|
|
ok("daily job is persisted and requires a network");
|
|
updater.source("http://127.0.0.1:8766/Elijah/Luma/raw/branch/main/output/update.json");
|
|
Updater.Release release = updater.check(false);
|
|
if (release == null
|
|
|| !release.url.contains(
|
|
"/raw/commit/" + String.join("", Collections.nCopies(40, "a")) + "/output/Luma.apk"))
|
|
throw new AssertionError("APK is not pinned to the feed commit");
|
|
updater.download(release, percent -> {});
|
|
if (!updater.ready(release)) throw new AssertionError("verified APK not ready");
|
|
try (ParcelFileDescriptor file =
|
|
getTargetContext().getContentResolver().openFileDescriptor(UpdateProvider.URI, "r")) {
|
|
if (file.getStatSize() != release.size)
|
|
throw new AssertionError("installer cannot read APK");
|
|
}
|
|
ok("commit-pinned download passes checksum, package, version and signing-key verification");
|
|
try {
|
|
getTargetContext().getContentResolver().openFileDescriptor(UpdateProvider.URI, "w");
|
|
throw new AssertionError("provider allowed writing");
|
|
} catch (FileNotFoundException expected) {
|
|
ok("installer provider denies write access");
|
|
}
|
|
try {
|
|
getTargetContext()
|
|
.getContentResolver()
|
|
.openFileDescriptor(Uri.parse("content://tv.luma.launcher.updates/other"), "r");
|
|
throw new AssertionError("provider exposed another path");
|
|
} catch (FileNotFoundException expected) {
|
|
ok("installer provider exposes only the APK URI");
|
|
}
|
|
rejected("badhash.json", "checksum");
|
|
rejected("wrongkey.json", "different key");
|
|
rejected("wrongpackage.json", "newer matching");
|
|
rejected("wrongcode.json", "newer matching");
|
|
rejected("oversized.json", "invalid");
|
|
source("current.json");
|
|
if (updater.check(false) != null || updater.available() != null)
|
|
throw new AssertionError("current version offered as update");
|
|
ok("current version is not offered as an update");
|
|
try {
|
|
Updater.validUrl("http://example.com/update.json");
|
|
throw new AssertionError("public HTTP accepted");
|
|
} catch (IOException expected) {
|
|
ok("public sources require HTTPS");
|
|
}
|
|
updater.prefs.edit().putInt("hours", 0).apply();
|
|
UpdateJob.schedule(getTargetContext());
|
|
if (getTargetContext().getSystemService(JobScheduler.class).getPendingJob(UpdateJob.ID)
|
|
!= null) throw new AssertionError("Off did not cancel the job");
|
|
ok("Off cancels the automatic update job");
|
|
code = Activity.RESULT_OK;
|
|
} catch (Throwable error) {
|
|
results.append("FAIL: ").append(error).append('\n');
|
|
} finally {
|
|
if (original != null) {
|
|
SharedPreferences.Editor edit = updater.prefs.edit().clear();
|
|
for (Map.Entry<String, ?> entry : original.entrySet()) {
|
|
Object value = entry.getValue();
|
|
if (value instanceof String) edit.putString(entry.getKey(), (String) value);
|
|
else if (value instanceof Long) edit.putLong(entry.getKey(), (Long) value);
|
|
else if (value instanceof Integer) edit.putInt(entry.getKey(), (Integer) value);
|
|
else if (value instanceof Boolean) edit.putBoolean(entry.getKey(), (Boolean) value);
|
|
}
|
|
edit.commit();
|
|
UpdateJob.schedule(getTargetContext());
|
|
}
|
|
result.putString("stream", results.toString());
|
|
finish(code, result);
|
|
}
|
|
}
|
|
}
|