mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
fix: an interrupted Linux runtime download resumes instead of starting again (#847)
* fix: an interrupted Linux runtime download resumes instead of starting again installOnce() asked the downloader to resume but always deleted the archive in its finally block, so the next attempt began at zero. The archive is now named after the release version and checksum, a failed download keeps what it fetched, and archives of other releases (and the old fixed name) are removed. A checksum or extraction failure still deletes the file. * test: Downloader resumes a cut-off download from the bytes it kept Starts a socket server that promises the whole file, hangs up part way and then answers a Range request, and checks that the second downloadFile call resumes with the right offset and ends with identical bytes. * fix: a resumed download discards a 206 that does not start where the kept bytes end Downloader appended every 206 response to the partial file without looking at Content-Range. A server or proxy that answers a Range request with a different offset would leave a corrupt file. Check that the range starts at the kept length; otherwise delete the partial file and start again without a Range header. Adds a test with a server that answers the Range request from offset 0. --------- Co-authored-by: ovurrsl <ovurrsl@users.noreply.github.com>
This commit is contained in:
1 parent
98be0838c4
commit
03f1a14e81
4 files changed
+185
-2
No files matched your search
@@ -0,0 +1,111 @@
|
||||
package com.droiddeck.launcher.core
|
||||
|
||||
import java.io.File
|
||||
import java.net.ServerSocket
|
||||
import java.net.Socket
|
||||
import java.nio.file.Files
|
||||
import java.util.concurrent.CopyOnWriteArrayList
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* A download that was cut off keeps its bytes, and a second call with `resume` asks for the rest
|
||||
* with a Range header instead of starting again. The server is a plain socket so the first answer
|
||||
* can promise the whole file and then hang up halfway.
|
||||
*/
|
||||
class DownloaderResumeTest {
|
||||
private val data = ByteArray(300_000) { (it % 251).toByte() }
|
||||
private val cutAt = 100_000
|
||||
private val requests = CopyOnWriteArrayList<String?>()
|
||||
private lateinit var server: ServerSocket
|
||||
private lateinit var dir: File
|
||||
private lateinit var thread: Thread
|
||||
@Volatile private var misbehave = false
|
||||
|
||||
@Before fun setUp() {
|
||||
dir = Files.createTempDirectory("downloader-resume").toFile()
|
||||
server = ServerSocket(0)
|
||||
thread = Thread {
|
||||
try {
|
||||
while (!server.isClosed) {
|
||||
val socket = server.accept()
|
||||
// the client may hang up on a body it refuses; that must not stop the server
|
||||
runCatching { handle(socket) }
|
||||
}
|
||||
} catch (_: Exception) {
|
||||
// closed by tearDown
|
||||
}
|
||||
}.apply { isDaemon = true; start() }
|
||||
}
|
||||
|
||||
@After fun tearDown() {
|
||||
server.close()
|
||||
dir.deleteRecursively()
|
||||
}
|
||||
|
||||
private fun handle(socket: Socket) = socket.use {
|
||||
val input = it.getInputStream().bufferedReader(Charsets.ISO_8859_1)
|
||||
var range: String? = null
|
||||
while (true) {
|
||||
val line = input.readLine() ?: break
|
||||
if (line.isEmpty()) break
|
||||
if (line.startsWith("Range:", ignoreCase = true)) range = line.substringAfter(':').trim()
|
||||
}
|
||||
requests.add(range)
|
||||
val out = it.getOutputStream()
|
||||
val from = range?.removePrefix("bytes=")?.substringBefore('-')?.toIntOrNull()
|
||||
if (misbehave) {
|
||||
// answers a Range request with a 206 that starts at 0, and serves plain requests whole
|
||||
val start = if (from == null) "200 OK" else "206 Partial Content"
|
||||
val extra = if (from == null) "" else "Content-Range: bytes 0-${data.size - 1}/${data.size}\r\n"
|
||||
out.write("HTTP/1.1 $start\r\n${extra}Content-Length: ${data.size}\r\nConnection: close\r\n\r\n".toByteArray())
|
||||
out.write(data)
|
||||
out.flush()
|
||||
} else if (from == null) {
|
||||
out.write("HTTP/1.1 200 OK\r\nContent-Length: ${data.size}\r\nConnection: close\r\n\r\n".toByteArray())
|
||||
out.write(data, 0, cutAt)
|
||||
out.flush()
|
||||
// hang up with most of the promised body still unsent
|
||||
} else {
|
||||
val rest = data.size - from
|
||||
out.write(("HTTP/1.1 206 Partial Content\r\nContent-Range: bytes $from-${data.size - 1}/${data.size}\r\n" +
|
||||
"Content-Length: $rest\r\nConnection: close\r\n\r\n").toByteArray())
|
||||
out.write(data, from, rest)
|
||||
out.flush()
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun cutOffDownloadResumesFromWhatWasKept() {
|
||||
val url = "http://127.0.0.1:${server.localPort}/runtime.tar.zst"
|
||||
val target = File(dir, "runtime.tar.zst")
|
||||
|
||||
assertFalse("the cut-off transfer must not report success", Downloader.downloadFile(url, target, true, null))
|
||||
val kept = target.length()
|
||||
assertTrue("the partial bytes stay on disk (kept=$kept)", kept in 1 until data.size)
|
||||
|
||||
assertTrue(Downloader.downloadFile(url, target, true, null))
|
||||
assertEquals(data.size.toLong(), target.length())
|
||||
assertArrayEquals(data, target.readBytes())
|
||||
assertEquals(listOf<String?>(null, "bytes=$kept-"), requests.toList())
|
||||
}
|
||||
|
||||
@Test fun answerFromTheWrongOffsetIsNotAppended() {
|
||||
val url = "http://127.0.0.1:${server.localPort}/runtime.tar.zst"
|
||||
val target = File(dir, "runtime.tar.zst")
|
||||
|
||||
assertFalse(Downloader.downloadFile(url, target, true, null))
|
||||
val kept = target.length()
|
||||
assertTrue(kept in 1 until data.size)
|
||||
|
||||
misbehave = true
|
||||
assertTrue(Downloader.downloadFile(url, target, true, null))
|
||||
assertEquals(data.size.toLong(), target.length())
|
||||
assertArrayEquals("the file must not be the kept bytes plus a misplaced body", data, target.readBytes())
|
||||
assertEquals(listOf<String?>(null, "bytes=$kept-", null), requests.toList())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.droiddeck.launcher.runtime
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class LinuxRuntimeArchiveNameTest {
|
||||
private fun release(version: String, sha: String) =
|
||||
LinuxRuntimeInstaller.Release(version, "https://example.invalid/runtime.tar.zst", sha, 1L)
|
||||
|
||||
@Test fun sameReleaseKeepsTheSameName() {
|
||||
val a = LinuxRuntimeInstaller.archiveName(release("r9", "ab".repeat(32)))
|
||||
val b = LinuxRuntimeInstaller.archiveName(release("r9", "ab".repeat(32)))
|
||||
assertEquals(a, b)
|
||||
}
|
||||
|
||||
@Test fun anotherVersionOrChecksumGetsAnotherName() {
|
||||
val base = LinuxRuntimeInstaller.archiveName(release("r9", "ab".repeat(32)))
|
||||
assertNotEquals(base, LinuxRuntimeInstaller.archiveName(release("r10", "ab".repeat(32))))
|
||||
assertNotEquals(base, LinuxRuntimeInstaller.archiveName(release("r9", "cd".repeat(32))))
|
||||
}
|
||||
|
||||
@Test fun nameIsASafeFileNameThatTheCleanupRecognises() {
|
||||
val name = LinuxRuntimeInstaller.archiveName(release("r9/../x y", "AB/CD ".repeat(10)))
|
||||
assertFalse(name.contains('/'))
|
||||
assertFalse(name.contains(' '))
|
||||
assertTrue(name.startsWith("linuxfs"))
|
||||
assertTrue(name.endsWith(".tar.zst"))
|
||||
}
|
||||
|
||||
@Test fun shortOrEmptyChecksumStillMakesAName() {
|
||||
assertEquals("linuxfs-r9-.tar.zst", LinuxRuntimeInstaller.archiveName(release("r9", "")))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user