From aa3a599ec6893d366f4185b7ec5061874b13b7fd Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Sat, 14 Nov 2026 14:51:29 -0500 Subject: [PATCH] fix(runtime): record why a runtime install failed in the session events (#2591) * fix(runtime): record why a runtime install failed in the session events * fix(runtime): report empty or invalid runtime catalogs in the failure event * fix(runtime): keep each install failure reason on its own job * fix(runtime): name the busy reason for rejected installs and drop the owner's listener * fix(runtime): keep each install call's failure reason on its calling thread * fix(runtime): keep the interrupted reason when a joined install finishes during the wait --- .../com/droiddeck/launcher/SessionActivity.kt | 9 +- .../com/droiddeck/launcher/core/AddonPack.kt | 25 +- .../droiddeck/launcher/core/Downloader.java | 33 ++- .../runtime/LinuxRuntimeInstaller.java | 110 ++++++-- .../droiddeck/launcher/core/AddonPackTest.kt | 7 + .../launcher/core/DownloaderFailureTest.kt | 85 ++++++ .../runtime/RuntimeCatalogFailureTest.kt | 72 +++++ .../runtime/RuntimeInstallRollbackTest.kt | 250 ++++++++++++++++-- 8 files changed, 538 insertions(+), 53 deletions(-) create mode 100644 app/src/test/java/com/droiddeck/launcher/core/DownloaderFailureTest.kt create mode 100644 app/src/test/java/com/droiddeck/launcher/runtime/RuntimeCatalogFailureTest.kt diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 5648e6e847..949bcd74e0 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -2235,11 +2235,16 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde private fun installRuntime(): String? { val release = com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.fetchRelease() - ?: return getString(R.string.session_runtime_catalog_unreachable) + if (release == null) { + SessionEvents.record("runtime.failed", mapOf("step" to "catalog", "reason" to com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.catalogError())) + return getString(R.string.session_runtime_catalog_unreachable) + } runCatching { com.droiddeck.launcher.runtime.RuntimeInstallService.start(this, release) } val ok = com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.install(this, release, progressFor(RUNTIME_LINES, release.size / 1_000_000)) - return if (ok) null else getString(R.string.session_runtime_install_failed) + if (ok) return null + SessionEvents.record("runtime.failed", mapOf("step" to "install", "reason" to com.droiddeck.launcher.runtime.LinuxRuntimeInstaller.installError())) + return getString(R.string.session_runtime_install_failed) } private fun installDesktop(): String? { diff --git a/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt b/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt index 9e0452771d..1d270abbc8 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt +++ b/app/src/main/java/com/droiddeck/launcher/core/AddonPack.kt @@ -31,6 +31,9 @@ internal class AddonPack( val state: StateFlow = mutableState.asStateFlow() private val lock = Any() + @Volatile var failure: String? = null + private set + val available: Boolean get() = sha256.isNotEmpty() private val tag get() = "pack-$name-${sha256.take(16)}" private val urls get() = listOfNotNull( @@ -67,9 +70,13 @@ internal class AddonPack( if (installed(app)) { mutableState.value = State.Ready; return } if (mutableState.value is State.Downloading) return mutableState.value = State.Downloading(0f) + failure = null } scope.launch { - val ok = runCatching { install(app) }.onFailure { Log.w(TAG, "$name: install failed", it) }.getOrDefault(false) + val ok = runCatching { install(app) }.onFailure { + Log.w(TAG, "$name: install failed", it) + failure = it.toString() + }.getOrDefault(false) mutableState.value = if (ok) State.Ready else State.Failed if (ok) onReady(app) } @@ -93,20 +100,28 @@ internal class AddonPack( private fun install(context: Context): Boolean { if (!available) { - Log.w(TAG, if (reuseDeviceCopy) "$name: this build has no pack and none is installed" else "$name: this build has no pack") + failure = if (reuseDeviceCopy) "this build has no pack and none is installed" else "this build has no pack" + Log.w(TAG, "$name: $failure") return false } val root = root(context).apply { mkdirs() } val archive = File(root, "$sha256.tar.zst") + val problems = mutableListOf() val downloaded = bundled(context, archive) || urls.any { url -> Downloader.downloadFile(url, archive, true, sha256) { p -> mutableState.value = State.Downloading(p.coerceIn(0f, 1f)) } - .also { if (!it) archive.delete() } + .also { if (!it) { archive.delete(); Downloader.lastFailure()?.let(problems::add) } } + } + if (!downloaded) { + failure = problems.joinToString("; ").ifEmpty { "download failed" } + return false } - if (!downloaded) return false val staging = File(root, "$sha256.partial") staging.deleteRecursively() try { - if (!FileInputStream(archive).use { TarZst.extract(it, staging) }) return false + if (!FileInputStream(archive).use { TarZst.extract(it, staging) }) { + failure = "could not unpack the pack" + return false + } check(required.all { File(staging, it).isFile }) { "$name pack is incomplete" } File(staging, COMPLETE).writeText(sha256) val target = dir(context) diff --git a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java index 9bd86c5719..7d5427a8cb 100644 --- a/app/src/main/java/com/droiddeck/launcher/core/Downloader.java +++ b/app/src/main/java/com/droiddeck/launcher/core/Downloader.java @@ -31,8 +31,26 @@ public final class Downloader { return thread; }); + private static final ThreadLocal LAST_FAILURE = new ThreadLocal<>(); + private Downloader() {} + public static String lastFailure() { + return LAST_FAILURE.get(); + } + + private static void failed(String url, String reason) { + String host; + try { host = new java.net.URI(url).getHost(); } catch (java.net.URISyntaxException e) { host = null; } + String entry = (host == null ? url : host) + ": " + reason; + String earlier = LAST_FAILURE.get(); + LAST_FAILURE.set(earlier == null ? entry : earlier + "; " + entry); + } + + private static String describe(Exception e) { + return e.getMessage() == null ? e.getClass().getSimpleName() : e.getClass().getSimpleName() + ": " + e.getMessage(); + } + public static final class Cancellation implements java.util.function.BooleanSupplier { private final AtomicBoolean cancelled = new AtomicBoolean(); private final AtomicReference connection = new AtomicReference<>(); @@ -87,12 +105,14 @@ public final class Downloader { public static String downloadString(String url, java.util.function.BooleanSupplier cancelled) { List sources = DownloadSource.INSTANCE.candidates(url); + LAST_FAILURE.remove(); for (int i = 0; i < sources.size(); i++) { if (isCancelled(cancelled)) return null; if (i > 0) Log.i(TAG, "retrying " + url + " via " + sources.get(i)); Fetch fetch = fetch(sources.get(i), cancelled); if (isCancelled(cancelled)) return null; if (fetch.body != null) { + LAST_FAILURE.remove(); DownloadSource.INSTANCE.succeeded(sources.get(i)); return fetch.body; } @@ -103,6 +123,7 @@ public final class Downloader { } public static String downloadDirect(String url) { + LAST_FAILURE.remove(); return fetch(url).body; } @@ -138,6 +159,7 @@ public final class Downloader { String expectedSha256, java.util.function.BooleanSupplier cancelled, java.util.function.Consumer progress) { Identity identity = loadIdentity(destination, resume, expectedSha256); + LAST_FAILURE.remove(); for (int i = 0; i < sources.size(); i++) { String source = sources.get(i); if (isCancelled(cancelled)) return false; @@ -150,12 +172,14 @@ public final class Downloader { if (isCancelled(cancelled)) return false; if (!expectedSha256.equalsIgnoreCase(actualSha256)) { Log.w(TAG, "checksum mismatch from " + source + "; trying another source"); + failed(source, "checksum mismatch"); if (!discardPartial(destination, identity)) return false; continue; } } if (isCancelled(cancelled)) return false; resumeFile(destination).delete(); + LAST_FAILURE.remove(); DownloadSource.INSTANCE.succeeded(source); if (progress != null) progress.accept(1f); return true; @@ -260,6 +284,7 @@ public final class Downloader { } if (code / 100 != 2) { Log.w(TAG, url + " -> HTTP " + code); + failed(url, "HTTP " + code); return code == HttpURLConnection.HTTP_NOT_FOUND || code == HttpURLConnection.HTTP_GONE ? Outcome.FATAL : Outcome.RETRY; } @@ -308,11 +333,15 @@ public final class Downloader { if (cancelled instanceof Cancellation) ((Cancellation) cancelled).detach(input); } if (isCancelled(cancelled)) return Outcome.CANCELLED; - if (total > 0 && written < total) return Outcome.RETRY; + if (total > 0 && written < total) { + failed(url, "connection closed after " + written + " of " + total + " bytes"); + return Outcome.RETRY; + } return Outcome.DONE; } catch (Exception e) { if (isCancelled(cancelled)) return Outcome.CANCELLED; Log.w(TAG, "download " + url, e); + failed(url, describe(e)); return Outcome.RETRY; } finally { if (connection != null) { @@ -357,6 +386,7 @@ public final class Downloader { int code = connection.getResponseCode(); if (code / 100 != 2) { Log.w(TAG, url + " -> HTTP " + code); + failed(url, "HTTP " + code); return new Fetch(null, code == HttpURLConnection.HTTP_NOT_FOUND || code == HttpURLConnection.HTTP_GONE); } try (InputStream in = connection.getInputStream(); @@ -367,6 +397,7 @@ public final class Downloader { } catch (Exception e) { if (isCancelled(cancelled)) return new Fetch(null, false); Log.w(TAG, "GET " + url, e); + failed(url, describe(e)); return new Fetch(null, false); } finally { if (connection != null) { diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java index cf613008bb..fe853326ae 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java +++ b/app/src/main/java/com/droiddeck/launcher/runtime/LinuxRuntimeInstaller.java @@ -70,6 +70,7 @@ public final class LinuxRuntimeInstaller { volatile Step step = Step.OTHER; volatile int percent = -1; volatile boolean ok; + volatile String failure; final boolean removal; boolean maintenance; Job(boolean removal, String stage) { this.removal = removal; this.stage = stage; } @@ -78,6 +79,9 @@ public final class LinuxRuntimeInstaller { private static final Object JOB_LOCK = new Object(); private static Job running; private static volatile String removalError; + private static final ThreadLocal LAST_INSTALL = new ThreadLocal<>(); + private static final ThreadLocal CURRENT_INSTALL = new ThreadLocal<>(); + private static final ThreadLocal CATALOG_ERROR = new ThreadLocal<>(); private LinuxRuntimeInstaller() {} @@ -105,34 +109,66 @@ public final class LinuxRuntimeInstaller { } public static Release fetchRelease() { - String body = Downloader.downloadString(catalogUrl()); - if (body == null || body.isEmpty()) return null; + return fetchRelease(catalogUrl()); + } + + public static String catalogError() { return CATALOG_ERROR.get(); } + + static Release fetchRelease(String catalogUrl) { + CATALOG_ERROR.remove(); + String body = Downloader.downloadString(catalogUrl); + if (body == null) return catalogFailed(Downloader.lastFailure()); + if (body.isEmpty()) return catalogFailed("empty catalog"); try { JSONObject json = new JSONObject(body); String version = json.optString("version", ""); String url = json.optString("url", ""); String sha256 = json.optString("sha256", ""); - if (version.isEmpty() || url.isEmpty() || sha256.isEmpty()) return null; + if (version.isEmpty() || url.isEmpty() || sha256.isEmpty()) { + return catalogFailed("catalog lacks version, url or sha256"); + } return new Release(version, url, sha256, json.optLong("size", 0L)); } catch (Exception e) { - Log.w(TAG, "catalog: " + e); - return null; + return catalogFailed("invalid catalog: " + e); } } + private static Release catalogFailed(String reason) { + CATALOG_ERROR.set(reason); + Log.w(TAG, "catalog: " + reason); + return null; + } + private static final String USER_DATA = "root"; public static boolean install(Context context, Release release, ProgressListener listener) { Job job; boolean owner; synchronized (JOB_LOCK) { - if (running != null && (running.removal || running.maintenance)) return false; + if (running != null && (running.removal || running.maintenance)) { + Job rejected = new Job(false, null); + rejected.failure = running.removal ? "Runtime busy: removal in progress" : "Runtime busy: maintenance in progress"; + Log.w(TAG, rejected.failure); + LAST_INSTALL.set(rejected); + return false; + } owner = running == null; if (owner) { running = new Job(false, context.getString(R.string.user_apps_starting)); removalError = null; } job = running; if (listener != null) job.listeners.add(listener); } - if (!owner) return join(job, listener); + if (!owner) { + Boolean ok = await(job, listener); + if (ok != null) { + LAST_INSTALL.set(job); + return ok; + } + Job interrupted = new Job(false, null); + interrupted.failure = "Runtime install interrupted while waiting for the running install"; + LAST_INSTALL.set(interrupted); + return false; + } + CURRENT_INSTALL.set(job); try { Context app = context.getApplicationContext(); ProgressListener progress = new ProgressListener() { @@ -156,9 +192,10 @@ public final class LinuxRuntimeInstaller { if (job.ok && desktop) keepDesktop(app, progress); return job.ok; } finally { - synchronized (JOB_LOCK) { - running = null; - } + CURRENT_INSTALL.remove(); + LAST_INSTALL.set(job); + synchronized (JOB_LOCK) { running = null; } + if (listener != null) job.listeners.remove(listener); job.done.countDown(); } } @@ -247,6 +284,21 @@ public final class LinuxRuntimeInstaller { public static String removalError() { return removalError; } + public static String installError() { + Job job = LAST_INSTALL.get(); + return job == null ? null : job.failure; + } + + private static boolean fail(String step, String detail) { + String reason = detail == null || detail.isEmpty() ? step : step + ": " + detail; + Job job = CURRENT_INSTALL.get(); + if (job != null) job.failure = reason; + Log.w(TAG, reason); + return false; + } + + private static final String SWAP_FAILED = "Cannot replace the previous runtime"; + public static boolean hasRemovalPending(Context context) { return removalDirectory(LinuxRuntime.rootDir(context)).exists(); } @@ -266,13 +318,17 @@ public final class LinuxRuntimeInstaller { } private static boolean join(Job job, ProgressListener listener) { + return Boolean.TRUE.equals(await(job, listener)); + } + + private static Boolean await(Job job, ProgressListener listener) { try { if (listener != null) listener.onProgress(job.step, job.stage, job.percent); job.done.await(); return job.ok; } catch (InterruptedException e) { Thread.currentThread().interrupt(); - return false; + return null; } finally { if (listener != null) job.listeners.remove(listener); } @@ -329,8 +385,9 @@ public final class LinuxRuntimeInstaller { if (listener != null) listener.onProgress(Step.DOWNLOADING, downloading, 0); if (!LinuxRuntime.patchedProot(context) && !AddonPack.PROOT.await(context, () -> true, packProgress(listener, downloading)) && !SessionPrefs.allowStockProot(context)) - throw new IOException("Cannot download proot"); - if (!AddonPack.DIRECTAUDIO.await(context, () -> true, packProgress(listener, downloading))) throw new IOException("Cannot download DirectAudio"); + return fail("Cannot download proot", AddonPack.PROOT.getFailure()); + if (!AddonPack.DIRECTAUDIO.await(context, () -> true, packProgress(listener, downloading))) + return fail("Cannot download DirectAudio", AddonPack.DIRECTAUDIO.getFailure()); boolean ok = Downloader.downloadFile(release.url, archive, true, release.sha256, (fraction) -> { if (listener != null) { listener.onProgress(Step.DOWNLOADING, downloading, @@ -338,39 +395,36 @@ public final class LinuxRuntimeInstaller { } }); if (!ok) { - Log.w(TAG, "download failed"); keepPartial = true; - return false; + return fail("Cannot download the runtime", Downloader.lastFailure()); } if (listener != null) listener.onProgress(Step.VERIFYING, context.getString(R.string.rtinst_verifying), -1); String actual = Hashes.sha256(archive); - if (!release.sha256.equalsIgnoreCase(actual)) { - Log.w(TAG, "checksum mismatch: wanted " + release.sha256 + ", got " + actual); - return false; - } + if (!release.sha256.equalsIgnoreCase(actual)) + return fail("Runtime checksum mismatch", "wanted " + release.sha256 + ", got " + actual); File root = LinuxRuntime.rootDir(context); File staging = new File(root.getParentFile(), LinuxRuntime.DIR + ".new"); File old = new File(root.getParentFile(), LinuxRuntime.DIR + ".old"); recoverInterruptedSwap(root, staging, old); - if (!discard(staging, null) || !staging.mkdirs()) return false; + if (!discard(staging, null) || !staging.mkdirs()) return fail("Cannot prepare " + staging.getName(), null); if (listener != null) listener.onProgress(context.getString(R.string.rtinst_extracting), -1); if (!extract(context, archive, staging, root, listener)) { RuntimeFileTree.delete(staging, null); - return false; + return fail("Cannot unpack the runtime", null); } FileUtils.writeString(new File(staging, VERSION_FILE), release.version); if (!discard(old, null)) { discard(staging, null); - return false; + return fail(SWAP_FAILED, null); } java.util.List carryPaths = root.isDirectory() ? RuntimeFileTree.existingUserPaths(root, staging) : java.util.Collections.emptyList(); if (root.isDirectory() && !root.renameTo(old)) { RuntimeFileTree.delete(staging, null); - return false; + return fail(SWAP_FAILED, null); } File carryMarker = new File(staging, CARRY_MARKER); java.util.List encodedPaths = new java.util.ArrayList<>(); @@ -379,7 +433,7 @@ public final class LinuxRuntimeInstaller { if (old.isDirectory() && !old.renameTo(root)) Log.w(TAG, "could not restore the previous runtime; retained at " + old); discard(staging, null); - return false; + return fail(SWAP_FAILED, null); } java.util.List carried; @@ -396,7 +450,7 @@ public final class LinuxRuntimeInstaller { } else { Log.w(TAG, "incomplete rollback; retaining user data in " + staging); } - return false; + return fail("Cannot carry user data into the new runtime", e.getMessage()); } if (!staging.renameTo(root)) { @@ -407,7 +461,7 @@ public final class LinuxRuntimeInstaller { } catch (IOException e) { Log.w(TAG, "could not move all user data back after a failed swap", e); } old.renameTo(root); } - return false; + return fail(SWAP_FAILED, null); } new File(root, CARRY_MARKER).delete(); try { RuntimeFileTree.dropStaleState(root); } @@ -415,10 +469,10 @@ public final class LinuxRuntimeInstaller { discard(old, null); try { com.droiddeck.launcher.session.SessionFiles.INSTANCE.stage(context, root, false); } catch (Exception e) { Log.w(TAG, "could not stage the session files into the new runtime", e); } - return LinuxRuntime.isInstalled(context); + return LinuxRuntime.isInstalled(context) || fail("The runtime is incomplete after unpacking", null); } catch (Exception e) { Log.e(TAG, "install", e); - return false; + return fail("Install stopped", e.toString()); } finally { if (!keepPartial) archive.delete(); } diff --git a/app/src/test/java/com/droiddeck/launcher/core/AddonPackTest.kt b/app/src/test/java/com/droiddeck/launcher/core/AddonPackTest.kt index c367c63ecc..6e81d46a7d 100644 --- a/app/src/test/java/com/droiddeck/launcher/core/AddonPackTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/core/AddonPackTest.kt @@ -74,6 +74,13 @@ class AddonPackTest { assertEquals("a".repeat(64), pack.dir(context).name) } + @Test fun aBuildWithoutThePackSaysSoWhenAwaited() { + val pack = AddonPack("sample", "", 0L, listOf("tool"), reuseDeviceCopy = false) + + assertFalse(pack.await(context)) + assertEquals("this build has no pack", pack.failure) + } + @Test fun localBuildInstallsTheBundledPackWithoutDownloading() { val archive = tarZst("tool" to "x") val pack = AddonPack("sample", Hashes.sha256(archive), archive.length(), listOf("tool"), bundle = { archive.inputStream() }) diff --git a/app/src/test/java/com/droiddeck/launcher/core/DownloaderFailureTest.kt b/app/src/test/java/com/droiddeck/launcher/core/DownloaderFailureTest.kt new file mode 100644 index 0000000000..c177d63f6b --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/core/DownloaderFailureTest.kt @@ -0,0 +1,85 @@ +package com.droiddeck.launcher.core + +import java.io.File +import java.net.ServerSocket +import java.nio.file.Files +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertFalse +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test + +class DownloaderFailureTest { + private lateinit var dir: File + + @Before fun setUp() { + dir = Files.createTempDirectory("downloader-failure").toFile() + } + + @After fun tearDown() { + dir.deleteRecursively() + } + + private fun serve(status: String, body: ByteArray = ByteArray(0)): ServerSocket { + val server = ServerSocket(0) + Thread { + runCatching { + server.accept().use { socket -> + val input = socket.getInputStream().bufferedReader(Charsets.ISO_8859_1) + while (true) { + val line = input.readLine() ?: break + if (line.isEmpty()) break + } + socket.getOutputStream().apply { + write("HTTP/1.1 $status\r\nContent-Length: ${body.size}\r\nConnection: close\r\n\r\n".toByteArray()) + write(body) + flush() + } + } + } + }.apply { isDaemon = true; start() } + return server + } + + private fun download(url: String): Boolean = + Downloader.downloadCandidates(url, listOf(url), File(dir, "file"), false, null, null) + + @Test fun anHttpRefusalIsReportedWithTheHostAndCode() { + serve("403 Forbidden").use { server -> + assertFalse(download("http://127.0.0.1:${server.localPort}/pack.tar.zst")) + } + assertEquals("127.0.0.1: HTTP 403", Downloader.lastFailure()) + } + + @Test fun aRefusedConnectionIsReportedWithItsException() { + val port = ServerSocket(0).use { it.localPort } + assertFalse(download("http://127.0.0.1:$port/pack.tar.zst")) + val failure = Downloader.lastFailure().orEmpty() + assertTrue(failure, failure.startsWith("127.0.0.1: ConnectException")) + } + + @Test fun aSuccessfulDownloadClearsTheEarlierFailure() { + serve("500 Internal Server Error").use { server -> + assertFalse(download("http://127.0.0.1:${server.localPort}/pack.tar.zst")) + } + serve("200 OK", "data".toByteArray()).use { server -> + assertTrue(download("http://127.0.0.1:${server.localPort}/pack.tar.zst")) + } + assertNull(Downloader.lastFailure()) + } + + @Test fun everyFailedSourceIsListedInOrder() { + val first = serve("403 Forbidden") + val second = serve("503 Service Unavailable") + val urls = listOf(first, second).map { "http://127.0.0.1:${it.localPort}/pack.tar.zst" } + try { + assertFalse(Downloader.downloadCandidates(urls[0], urls, File(dir, "file"), false, null, null)) + } finally { + first.close() + second.close() + } + assertEquals("127.0.0.1: HTTP 403; 127.0.0.1: HTTP 503", Downloader.lastFailure()) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeCatalogFailureTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeCatalogFailureTest.kt new file mode 100644 index 0000000000..bf2bc01340 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeCatalogFailureTest.kt @@ -0,0 +1,72 @@ +package com.droiddeck.launcher.runtime + +import android.app.Application +import java.net.ServerSocket +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [28], application = Application::class) +class RuntimeCatalogFailureTest { + private fun serve(body: String): ServerSocket { + val server = ServerSocket(0) + val bytes = body.toByteArray() + Thread { + runCatching { + server.accept().use { socket -> + val input = socket.getInputStream().bufferedReader(Charsets.ISO_8859_1) + while (true) { + val line = input.readLine() ?: break + if (line.isEmpty()) break + } + socket.getOutputStream().apply { + write("HTTP/1.1 200 OK\r\nContent-Length: ${bytes.size}\r\nConnection: close\r\n\r\n".toByteArray()) + write(bytes) + flush() + } + } + } + }.apply { isDaemon = true; start() } + return server + } + + private fun fetch(body: String): LinuxRuntimeInstaller.Release? = + serve(body).use { LinuxRuntimeInstaller.fetchRelease("http://127.0.0.1:${it.localPort}/linuxfs.json") } + + @Test fun anEmptyCatalogIsReported() { + assertNull(fetch("")) + assertEquals("empty catalog", LinuxRuntimeInstaller.catalogError()) + } + + @Test fun aMalformedCatalogIsReported() { + assertNull(fetch("captive portal")) + val error = LinuxRuntimeInstaller.catalogError().orEmpty() + assertTrue(error, error.startsWith("invalid catalog: ")) + } + + @Test fun aCatalogMissingFieldsIsReported() { + assertNull(fetch("""{"version":"1","url":"http://127.0.0.1/rootfs.tar.zst"}""")) + assertEquals("catalog lacks version, url or sha256", LinuxRuntimeInstaller.catalogError()) + } + + @Test fun anUnreachableCatalogKeepsTheDownloadReason() { + val port = ServerSocket(0).use { it.localPort } + assertNull(LinuxRuntimeInstaller.fetchRelease("http://127.0.0.1:$port/linuxfs.json")) + val error = LinuxRuntimeInstaller.catalogError().orEmpty() + assertTrue(error, error.startsWith("127.0.0.1: ConnectException")) + } + + @Test fun aValidCatalogClearsTheEarlierError() { + assertNull(fetch("")) + val release = fetch("""{"version":"2","url":"http://127.0.0.1/rootfs.tar.zst","sha256":"${"a".repeat(64)}","size":5}""") + assertNotNull(release) + assertEquals("2", release!!.version) + assertNull(LinuxRuntimeInstaller.catalogError()) + } +} diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeInstallRollbackTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeInstallRollbackTest.kt index 45b8019bb8..ac17c29c82 100644 --- a/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeInstallRollbackTest.kt +++ b/app/src/test/java/com/droiddeck/launcher/runtime/RuntimeInstallRollbackTest.kt @@ -40,7 +40,7 @@ class RuntimeInstallRollbackTest { private data class Attempt(val root: File, val staging: File, val old: File, val result: Boolean) - private fun attempt(failRollback: Boolean): Attempt { + private fun packedContext(): Context { val files = tmp.newFolder("files") val cache = tmp.newFolder("cache") val context = object : ContextWrapper(RuntimeEnvironment.getApplication()) { @@ -58,6 +58,29 @@ class RuntimeInstallRollbackTest { required.forEach { put(directory, it, "test pack") } put(directory, AddonPack.COMPLETE, version) } + return context + } + + private fun serveOnce(server: ServerSocket, head: String, body: ByteArray = ByteArray(0)) = Thread { + runCatching { + server.accept().use { socket -> + socket.soTimeout = 5000 + val input = socket.getInputStream().bufferedReader(Charsets.ISO_8859_1) + while (true) { + val line = input.readLine() ?: break + if (line.isEmpty()) break + } + val out = socket.getOutputStream() + out.write(("$head\r\nContent-Length: ${body.size}\r\nConnection: close\r\n\r\n").toByteArray()) + out.write(body) + out.flush() + } + } + }.apply { isDaemon = true; start() } + + private fun attempt(failRollback: Boolean): Attempt { + val context = packedContext() + val files = context.filesDir val root = LinuxRuntime.rootDir(context) val staging = File(files, LinuxRuntime.DIR + ".new") val old = File(files, LinuxRuntime.DIR + ".old") @@ -87,22 +110,7 @@ class RuntimeInstallRollbackTest { } else failRollback && path == File(old, "root/rollback-block").toPath() } val server = ServerSocket(0) - val thread = Thread { - runCatching { - server.accept().use { socket -> - socket.soTimeout = 5000 - val input = socket.getInputStream().bufferedReader(Charsets.ISO_8859_1) - while (true) { - val line = input.readLine() ?: break - if (line.isEmpty()) break - } - val out = socket.getOutputStream() - out.write(("HTTP/1.1 200 OK\r\nContent-Length: ${payload.size}\r\nConnection: close\r\n\r\n").toByteArray()) - out.write(payload) - out.flush() - } - } - }.apply { isDaemon = true; start() } + val thread = serveOnce(server, "HTTP/1.1 200 OK", payload) try { val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", digest, payload.size.toLong()) val result = LinuxRuntimeInstaller.install(context, release, null) @@ -137,4 +145,212 @@ class RuntimeInstallRollbackTest { assertEquals("original app", File(attempt.root, "opt/appimages/user/game/app.AppImage").readText()) assertFalse(attempt.staging.exists()) } + + private fun failedInstall(context: Context, head: String): String? { + val server = ServerSocket(0) + val thread = serveOnce(server, head) + try { + val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", "a".repeat(64), 1L) + assertFalse(LinuxRuntimeInstaller.install(context, release, null)) + return LinuxRuntimeInstaller.installError() + } finally { + server.close() + thread.join(1000) + } + } + + @Test fun aRefusedRuntimeDownloadNamesTheStepAndTheServerAnswer() { + val context = packedContext() + assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 403", failedInstall(context, "HTTP/1.1 403 Forbidden")) + } + + @Test fun eachInstallReportsItsOwnFailureOnly() { + val context = packedContext() + failedInstall(context, "HTTP/1.1 403 Forbidden") + assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", failedInstall(context, "HTTP/1.1 404 Not Found")) + } + + @Test fun anInstallInFlightKeepsTheLastFailureReadable() { + val context = packedContext() + val first = failedInstall(context, "HTTP/1.1 403 Forbidden") + val server = ServerSocket(0) + val accepted = java.util.concurrent.CountDownLatch(1) + val release = java.util.concurrent.CountDownLatch(1) + val stall = Thread { + runCatching { + server.accept().use { socket -> + accepted.countDown() + release.await() + socket.getOutputStream().write("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".toByteArray()) + } + } + }.apply { isDaemon = true; start() } + var secondReason: String? = null + val second = Thread { + LinuxRuntimeInstaller.install(context, + LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", "a".repeat(64), 1L), null) + secondReason = LinuxRuntimeInstaller.installError() + }.apply { isDaemon = true; start() } + try { + assertTrue(accepted.await(10, java.util.concurrent.TimeUnit.SECONDS)) + assertTrue(LinuxRuntimeInstaller.isInstalling()) + assertEquals(first, LinuxRuntimeInstaller.installError()) + } finally { + release.countDown() + second.join(10000) + server.close() + stall.join(1000) + } + assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", secondReason) + assertEquals(first, LinuxRuntimeInstaller.installError()) + } + + @Test fun anInstallRejectedDuringMaintenanceReportsItsOwnReason() { + val context = packedContext() + failedInstall(context, "HTTP/1.1 403 Forbidden") + val maintenance = LinuxRuntimeInstaller.beginMaintenance(context, "maintenance")!! + try { + assertFalse(LinuxRuntimeInstaller.install(context, + LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:1/runtime.tar.zst", "a".repeat(64), 1L), null)) + assertEquals("Runtime busy: maintenance in progress", LinuxRuntimeInstaller.installError()) + } finally { + assertTrue(maintenance.run(null) { null }) + } + assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", failedInstall(context, "HTTP/1.1 404 Not Found")) + } + + @Test fun aRejectedInstallKeepsItsBusyReasonAfterALaterInstallCompletes() { + val context = packedContext() + val maintenance = LinuxRuntimeInstaller.beginMaintenance(context, "maintenance")!! + try { + assertFalse(LinuxRuntimeInstaller.install(context, + LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:1/runtime.tar.zst", "a".repeat(64), 1L), null)) + } finally { + assertTrue(maintenance.run(null) { null }) + } + var later: String? = null + Thread { later = failedInstall(context, "HTTP/1.1 404 Not Found") }.apply { start(); join(10000) } + assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", later) + assertEquals("Runtime busy: maintenance in progress", LinuxRuntimeInstaller.installError()) + } + + @Test fun aJoinedInstallReportsTheReasonOfTheInstallItJoined() { + val context = packedContext() + val server = ServerSocket(0) + val accepted = java.util.concurrent.CountDownLatch(1) + val answer = java.util.concurrent.CountDownLatch(1) + val attached = java.util.concurrent.CountDownLatch(1) + val joinedDone = java.util.concurrent.CountDownLatch(1) + val readNow = java.util.concurrent.CountDownLatch(1) + val stall = Thread { + runCatching { + server.accept().use { socket -> + accepted.countDown() + answer.await() + socket.getOutputStream().write("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".toByteArray()) + } + } + }.apply { isDaemon = true; start() } + val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", "a".repeat(64), 1L) + val owner = Thread { LinuxRuntimeInstaller.install(context, release, null) }.apply { isDaemon = true; start() } + var joinedResult = true + var joinedReason: String? = null + val joiner = Thread { + joinedResult = LinuxRuntimeInstaller.install(context, release) { _, _ -> attached.countDown() } + joinedDone.countDown() + readNow.await() + joinedReason = LinuxRuntimeInstaller.installError() + } + try { + assertTrue(accepted.await(10, java.util.concurrent.TimeUnit.SECONDS)) + joiner.apply { isDaemon = true; start() } + assertTrue(attached.await(10, java.util.concurrent.TimeUnit.SECONDS)) + answer.countDown() + assertTrue(joinedDone.await(10, java.util.concurrent.TimeUnit.SECONDS)) + owner.join(10000) + assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 403", failedInstall(context, "HTTP/1.1 403 Forbidden")) + readNow.countDown() + joiner.join(10000) + } finally { + answer.countDown() + readNow.countDown() + server.close() + stall.join(1000) + } + assertFalse(joinedResult) + assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", joinedReason) + } + + @Test fun anInterruptedJoinKeepsItsInterruptedReasonWhenTheOwnerFinishesFirst() { + val context = packedContext() + val server = ServerSocket(0) + val accepted = java.util.concurrent.CountDownLatch(1) + val answer = java.util.concurrent.CountDownLatch(1) + val stall = Thread { + runCatching { + server.accept().use { socket -> + accepted.countDown() + answer.await() + socket.getOutputStream().write("HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n".toByteArray()) + } + } + }.apply { isDaemon = true; start() } + val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", "a".repeat(64), 1L) + val ownerReason = arrayOfNulls(1) + val owner = Thread { + LinuxRuntimeInstaller.install(context, release, null) + ownerReason[0] = LinuxRuntimeInstaller.installError() + }.apply { isDaemon = true; start() } + var joinedResult = true + var joinedReason: String? = null + val ownerFinished = AtomicBoolean() + lateinit var joiner: Thread + joiner = Thread { + joinedResult = LinuxRuntimeInstaller.install(context, release) { _, _ -> + if (Thread.currentThread() === joiner && ownerFinished.compareAndSet(false, true)) { + answer.countDown() + owner.join(10000) + Thread.currentThread().interrupt() + } + } + joinedReason = LinuxRuntimeInstaller.installError() + Thread.interrupted() + } + try { + assertTrue(accepted.await(10, java.util.concurrent.TimeUnit.SECONDS)) + joiner.apply { isDaemon = true; start() } + joiner.join(10000) + } finally { + answer.countDown() + owner.join(10000) + server.close() + stall.join(1000) + } + assertTrue(ownerFinished.get()) + assertFalse(owner.isAlive) + assertEquals("Cannot download the runtime: 127.0.0.1: HTTP 404", ownerReason[0]) + assertFalse(joinedResult) + assertEquals("Runtime install interrupted while waiting for the running install", joinedReason) + } + + @Test fun aFinishedInstallKeepsNoReferenceToItsListener() { + val context = packedContext() + val server = ServerSocket(0) + val thread = serveOnce(server, "HTTP/1.1 403 Forbidden") + try { + val release = LinuxRuntimeInstaller.Release("synthetic", "http://127.0.0.1:${server.localPort}/runtime.tar.zst", "a".repeat(64), 1L) + assertFalse(LinuxRuntimeInstaller.install(context, release) { _, _ -> }) + } finally { + server.close() + thread.join(1000) + } + val job = (LinuxRuntimeInstaller::class.java.getDeclaredField("LAST_INSTALL").apply { isAccessible = true }.get(null) as ThreadLocal<*>).get()!! + val listeners = job.javaClass.getDeclaredField("listeners").apply { isAccessible = true }.get(job) as Collection<*> + assertTrue(listeners.isEmpty()) + } + + @Test fun aFailedSwapNamesTheStep() { + assertFalse(attempt(failRollback = false).result) + assertTrue(LinuxRuntimeInstaller.installError().orEmpty().startsWith("Cannot carry user data into the new runtime")) + } }