diff --git a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopLanguage.kt b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopLanguage.kt index 83bfa6748a..3cd0c01e0e 100644 --- a/app/src/main/java/com/droiddeck/launcher/runtime/DesktopLanguage.kt +++ b/app/src/main/java/com/droiddeck/launcher/runtime/DesktopLanguage.kt @@ -32,6 +32,9 @@ object DesktopLanguage { internal fun baseUrl(desktopUrl: String) = desktopUrl.substringBeforeLast('/') + "/" + /** The index carries every pack's sha256, so it never goes through a download mirror. */ + internal fun fetchIndex(base: String): Map? = Downloader.downloadDirect(base + INDEX)?.let(::parseIndex) + private fun marker(root: File, tag: String, name: String) = File(root, ".droiddeck-i18n-$tag-$name") /** Installs the desktop's translations for [locale]; false when a download failed and is worth retrying. */ @@ -44,7 +47,7 @@ object DesktopLanguage { val pending = candidates(locale).filterNot { marker(root, tag, it).isFile } if (pending.isEmpty()) return true val base = baseUrl(BuildConfig.DESKTOP_KDE_URL) - val index = parseIndex(Downloader.downloadString(base + INDEX) ?: return false) + val index = fetchIndex(base) ?: return false var complete = true for (name in pending) { val pack = index[name] diff --git a/app/src/test/java/com/droiddeck/launcher/runtime/DesktopLanguageIndexSourceTest.kt b/app/src/test/java/com/droiddeck/launcher/runtime/DesktopLanguageIndexSourceTest.kt new file mode 100644 index 0000000000..3b4b008c50 --- /dev/null +++ b/app/src/test/java/com/droiddeck/launcher/runtime/DesktopLanguageIndexSourceTest.kt @@ -0,0 +1,62 @@ +package com.droiddeck.launcher.runtime + +import android.content.Context +import androidx.test.core.app.ApplicationProvider +import com.droiddeck.launcher.core.DownloadSource +import com.droiddeck.launcher.core.Downloader +import java.net.ServerSocket +import java.util.concurrent.CopyOnWriteArrayList +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [33]) +class DesktopLanguageIndexSourceTest { + private val base = "https://github.com/Droid-Deck/desktop/releases/download/t/" + private val tunnels = CopyOnWriteArrayList() + private lateinit var context: Context + private lateinit var proxy: ServerSocket + + @Before fun setUp() { + proxy = ServerSocket(0) + Thread { + try { + while (!proxy.isClosed) { + proxy.accept().use { socket -> + val line = socket.getInputStream().bufferedReader().readLine() ?: return@use + tunnels += line.removePrefix("CONNECT ").substringBefore(':') + socket.getOutputStream().write("HTTP/1.1 403 Forbidden\r\nContent-Length: 0\r\n\r\n".toByteArray()) + } + } + } catch (_: Exception) { + } + }.apply { isDaemon = true }.start() + context = ApplicationProvider.getApplicationContext() + DownloadSource.init(context) + DownloadSource.setMode(context, DownloadSource.MODE_MIRROR) + DownloadSource.setProxy(context, "127.0.0.1:${proxy.localPort}") + } + + @After fun tearDown() { + DownloadSource.setMode(context, DownloadSource.MODE_AUTO) + DownloadSource.setProxy(context, "") + proxy.close() + } + + @Test fun mirrorModeWouldOtherwiseSendTheIndexThroughAMirror() { + Downloader.downloadString(base + "desktop-kde.i18n.txt") + assertTrue(tunnels.toString(), tunnels.any { host -> DownloadSource.MIRRORS.any { it.host == host } }) + } + + @Test fun indexIsFetchedOnlyFromGithubInMirrorMode() { + assertNull(DesktopLanguage.fetchIndex(base)) + assertEquals(listOf("github.com"), tunnels.toList()) + } +}