Modern Apps git

← Modern-Apps.git

appstore: add hermetic F-Droid download seam for offline tests

Commit
08c31c089ff66454976bb5b03851fc0019abc10e
Author
Vayun Mathur <[email protected]>
When
2026-11-14 23:08 UTC (21 days ago)
Parents
4673954
Several unit tests assert offline behaviour (a failed repo-index
download fails the sync closed, a failed reproducibility feed
propagates, a failed catalogue sync keeps seeded rows). They passed
only on hosts with no network; on a host whose proxy reaches the
real F-Droid/Izzy endpoints the downloads succeed and the
assertions invert.

Add FDroidRepository.downloadOverride, an internal test seam that
replaces downloadToFile wholesale when set. Production never sets
it. The offline tests install a handler that throws IOException
and clear it afterwards, so loopback-download tests sharing the
JVM still see real behaviour. No assertion was weakened: the sync
still runs, it just fails at the same download call it would fail
at with no network.

Covers ReproducibleFeedsTest, CatalogSyncPruneTest,
UpdateCheckWorkerDoWorkTest and TvUpdateCheckTest.

Verified: targeted :appstore:testDevUnitTest run for the four
classes (see final full-suite run).

Diff

appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt
diff --git a/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt b/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.ktindex c82484260f..1cfe023d58 100644--- a/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt+++ b/appstore/src/main/java/com/vayunmathur/appstore/data/FDroidRepository.kt@@ -100,7 +100,20 @@ object FDroidRepository {         return IndexRef(name, sha)     } +    /**+     * Test seam: when set, replaces the network download entirely. Offline+     * unit tests install a handler that throws [java.io.IOException] so the+     * fetch-failure paths run deterministically even on a host with live+     * network access; production code never sets this. Tests must clear it+     * again so loopback-download tests in the same JVM see real behaviour.+     */+    internal var downloadOverride: ((String, File) -> Unit)? = null+     internal fun downloadToFile(url: String, outFile: File) {+        downloadOverride?.let { handler ->+            handler(url, outFile)+            return+        }         val rawConnection = URL(url).openConnection()         val sslSocketFactory = NetworkClient.defaultSslSocketFactory         if (sslSocketFactory != null && rawConnection is HttpsURLConnection) {
appstore/src/test/java/com/vayunmathur/appstore/data/CatalogSyncPruneTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/data/CatalogSyncPruneTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/data/CatalogSyncPruneTest.ktindex 6460b4c8b1..a4bab33c2d 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/data/CatalogSyncPruneTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/data/CatalogSyncPruneTest.kt@@ -4,6 +4,7 @@ import android.content.Context import androidx.room3.Room import androidx.sqlite.driver.bundled.BundledSQLiteDriver import androidx.test.core.app.ApplicationProvider+import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals@@ -30,6 +31,11 @@ class CatalogSyncPruneTest {      @BeforeTest     fun setUp() {+        // Hermetic offline: every repo-index download fails, whatever+        // network the host running the tests happens to have.+        FDroidRepository.downloadOverride = { url, _ ->+            throw java.io.IOException("offline test double for $url")+        }         val ctx = ApplicationProvider.getApplicationContext<Context>()         db = Room.inMemoryDatabaseBuilder(ctx, AppDatabase::class.java)             .setDriver(BundledSQLiteDriver())@@ -39,6 +45,11 @@ class CatalogSyncPruneTest {         catalog = CatalogRepository(ctx, db, scope)     } +    @AfterTest+    fun tearDown() {+        FDroidRepository.downloadOverride = null+    }+     @Test     fun `sync prunes legacy repos and their rows and seeds the defaults`() = runTest {         val legacyUrl = "https://example.org/legacy-repo"
appstore/src/test/java/com/vayunmathur/appstore/data/ReproducibleFeedsTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/data/ReproducibleFeedsTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/data/ReproducibleFeedsTest.ktindex 27936a3ad7..12c2a82fff 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/data/ReproducibleFeedsTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/data/ReproducibleFeedsTest.kt@@ -23,6 +23,20 @@ class ReproducibleFeedsTest {      private val context: Context get() = ApplicationProvider.getApplicationContext() +    @kotlin.test.BeforeTest+    fun forceOfflineDownloads() {+        // Hermetic offline: every feed download fails, whatever network the+        // host running the tests happens to have.+        FDroidRepository.downloadOverride = { url, _ ->+            throw java.io.IOException("offline test double for $url")+        }+    }++    @kotlin.test.AfterTest+    fun restoreDownloads() {+        FDroidRepository.downloadOverride = null+    }+     @Test     fun `izzy admits only versions with an explicit verdict`() {         val verified = IzzyReproducibleBuilds.Verified(setOf("com.example.a:7", "com.example.b:1"))
appstore/src/test/java/com/vayunmathur/appstore/data/UpdateCheckWorkerDoWorkTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/data/UpdateCheckWorkerDoWorkTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/data/UpdateCheckWorkerDoWorkTest.ktindex 69da401ae1..7d84c0f7f5 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/data/UpdateCheckWorkerDoWorkTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/data/UpdateCheckWorkerDoWorkTest.kt@@ -7,6 +7,7 @@ import android.content.Context import androidx.test.core.app.ApplicationProvider import androidx.work.ListenableWorker import androidx.work.testing.TestListenableWorkerBuilder+import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals@@ -35,6 +36,11 @@ class UpdateCheckWorkerDoWorkTest {      @BeforeTest     fun setUp() {+        // Hermetic offline: the worker's catalogue sync must fail and keep+        // the seeded rows, whatever network the test host happens to have.+        FDroidRepository.downloadOverride = { url, _ ->+            throw java.io.IOException("offline test double for $url")+        }         // The production database is SQLCipher via AndroidKeyStore, which         // Robolectric lacks: bind an in-memory database into the shared         // cache the database builder consults first.@@ -57,6 +63,11 @@ class UpdateCheckWorkerDoWorkTest {         }     } +    @AfterTest+    fun tearDown() {+        FDroidRepository.downloadOverride = null+    }+     private fun worker() = TestListenableWorkerBuilder<UpdateCheckWorker>(ctx).build()      private fun postedNotifications() = Shadows.shadowOf(
appstore/src/test/java/com/vayunmathur/appstore/tv/TvUpdateCheckTest.kt
diff --git a/appstore/src/test/java/com/vayunmathur/appstore/tv/TvUpdateCheckTest.kt b/appstore/src/test/java/com/vayunmathur/appstore/tv/TvUpdateCheckTest.ktindex 07002dcffb..577f7add29 100644--- a/appstore/src/test/java/com/vayunmathur/appstore/tv/TvUpdateCheckTest.kt+++ b/appstore/src/test/java/com/vayunmathur/appstore/tv/TvUpdateCheckTest.kt@@ -9,6 +9,7 @@ import com.vayunmathur.appstore.data.AppDatabase import com.vayunmathur.appstore.data.AppSource import com.vayunmathur.appstore.data.UnifiedApp import com.vayunmathur.appstore.data.toEntity+import kotlin.test.AfterTest import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals@@ -35,6 +36,11 @@ class TvUpdateCheckTest {      @BeforeTest     fun setUp() {+        // Hermetic offline: the check's catalogue sync must fail and keep+        // the seeded rows, whatever network the test host happens to have.+        com.vayunmathur.appstore.data.FDroidRepository.downloadOverride = { url, _ ->+            throw java.io.IOException("offline test double for $url")+        }         val app = ApplicationProvider.getApplicationContext<Application>()         ownPackage = app.packageName         db = Room.inMemoryDatabaseBuilder(app, AppDatabase::class.java)@@ -58,6 +64,11 @@ class TvUpdateCheckTest {         vm.enabledSourcesFlow.value = setOf(AppSource.MODERN_APPS)     } +    @AfterTest+    fun tearDown() {+        com.vayunmathur.appstore.data.FDroidRepository.downloadOverride = null+    }+     private fun await(description: String, condition: () -> Boolean) {         val deadline = System.currentTimeMillis() + 30_000         while (System.currentTimeMillis() < deadline) {