mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 20:36:04 +00:00
fix: an interrupted package install can no longer break the Linux runtime (#1078)
This commit is contained in:
1 parent
fe7b75318a
commit
6b8ce9e675
13 files changed
+500
-37
No files matched your search
@@ -2,12 +2,16 @@ package com.droiddeck.launcher.runtime
|
||||
|
||||
import com.droiddeck.launcher.BuildConfig
|
||||
import java.io.File
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class DesktopCatalogTest {
|
||||
@get:Rule val temporary = TemporaryFolder()
|
||||
|
||||
@@ -30,4 +34,26 @@ class DesktopCatalogTest {
|
||||
kwin.delete()
|
||||
assertFalse(DesktopCatalog.desktopInstalled(root))
|
||||
}
|
||||
|
||||
@Test fun anInterruptedInstallIsFoundAgainWithEverythingNeededToFinishIt() {
|
||||
val root = temporary.root
|
||||
val entry = DesktopCatalog.Entry(
|
||||
DesktopCatalog.DESKTOP_ID, "Desktop", 1, "desktop-kde-r2", "tar",
|
||||
"https://example.invalid/desktop-kde.tar.zst", "a".repeat(64), 846933224L, "", "", "",
|
||||
)
|
||||
assertTrue(DesktopCatalog.interrupted(root).isEmpty())
|
||||
File(root, ".droiddeck-pkg-${entry.id}.installing").writeText(DesktopCatalog.entryJson(entry).toString())
|
||||
File(root, ".droiddeck-pkg-emulators.installing").writeText("not json")
|
||||
File(root, ".droiddeck-pkg-emulators").writeText("1")
|
||||
|
||||
val found = DesktopCatalog.interrupted(root).single()
|
||||
|
||||
assertEquals(entry.id, found.id)
|
||||
assertEquals(entry.version, found.version)
|
||||
assertEquals(entry.url, found.url)
|
||||
assertEquals(entry.sha256, found.sha256)
|
||||
assertEquals(entry.size, found.size)
|
||||
assertEquals(entry.kind, found.kind)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
package com.droiddeck.launcher.runtime
|
||||
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry
|
||||
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream
|
||||
import org.apache.commons.compress.compressors.zstandard.ZstdCompressorOutputStream
|
||||
import org.junit.After
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.RuntimeEnvironment
|
||||
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class PackageExtractTest {
|
||||
private val dir = Files.createTempDirectory("package-extract").toFile()
|
||||
private val root = File(dir, "linuxfs").apply { mkdirs() }
|
||||
private val context = RuntimeEnvironment.getApplication()
|
||||
|
||||
@After fun tearDown() { dir.deleteRecursively() }
|
||||
|
||||
private sealed class Item(val name: String) {
|
||||
class Link(name: String, val target: String) : Item(name)
|
||||
class Data(name: String, val text: String) : Item(name)
|
||||
}
|
||||
|
||||
private fun archive(vararg items: Item): File {
|
||||
val tar = ByteArrayOutputStream()
|
||||
TarArchiveOutputStream(tar).use { out ->
|
||||
for (item in items) {
|
||||
when (item) {
|
||||
is Item.Link -> out.putArchiveEntry(TarArchiveEntry(item.name, TarArchiveEntry.LF_SYMLINK).apply { linkName = item.target })
|
||||
is Item.Data -> {
|
||||
val bytes = item.text.toByteArray()
|
||||
out.putArchiveEntry(TarArchiveEntry(item.name).apply { size = bytes.size.toLong(); mode = 0b111_101_101 })
|
||||
out.write(bytes)
|
||||
}
|
||||
}
|
||||
out.closeArchiveEntry()
|
||||
}
|
||||
}
|
||||
return File(dir, "pkg-${System.nanoTime()}.tar.zst").apply {
|
||||
ZstdCompressorOutputStream(outputStream()).use { it.write(tar.toByteArray()) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun lib(name: String) = File(root, "usr/lib/$name")
|
||||
|
||||
private fun installOld() {
|
||||
lib("").mkdirs()
|
||||
lib("libvulkan.so.1.4.357").writeText("old")
|
||||
Files.createSymbolicLink(lib("libvulkan.so.1").toPath(), Paths.get("libvulkan.so.1.4.357"))
|
||||
}
|
||||
|
||||
@Test fun linksListedBeforeTheirTargetsResolveAfterExtraction() {
|
||||
installOld()
|
||||
val pkg = archive(
|
||||
Item.Link("usr/lib/libvulkan.so.1", "libvulkan.so.1.4.363"),
|
||||
Item.Data("usr/lib/libvulkan.so.1.4.363", "new"),
|
||||
)
|
||||
|
||||
assertTrue(LinuxRuntimeInstaller.extract(context, pkg, root, null))
|
||||
|
||||
assertEquals("new", lib("libvulkan.so.1").readText())
|
||||
assertEquals(emptyList<String>(), RuntimeHealth.brokenLibraryLinks(root))
|
||||
assertFalse(lib("").list()!!.any { it.startsWith(".dd-part-") })
|
||||
}
|
||||
|
||||
@Test fun anExtractionStoppedBetweenALinkAndItsTargetLeavesTheOldLinkWorking() {
|
||||
installOld()
|
||||
val pkg = archive(
|
||||
Item.Link("usr/lib/libvulkan.so.1", "libvulkan.so.1.4.363"),
|
||||
Item.Data("usr/lib/libharfbuzz.so.0", "harfbuzz"),
|
||||
Item.Data("usr/lib/libharfbuzz.so.0/cannot-be-written", "stops here"),
|
||||
Item.Data("usr/lib/libvulkan.so.1.4.363", "new"),
|
||||
)
|
||||
|
||||
assertFalse(LinuxRuntimeInstaller.extract(context, pkg, root, null))
|
||||
|
||||
assertEquals("old", lib("libvulkan.so.1").readText())
|
||||
assertFalse(lib("libvulkan.so.1.4.363").exists())
|
||||
assertEquals(emptyList<String>(), RuntimeHealth.brokenLibraryLinks(root))
|
||||
}
|
||||
|
||||
@Test fun aFileReplacesAnExistingLinkInsteadOfWritingThroughIt() {
|
||||
installOld()
|
||||
val pkg = archive(Item.Data("usr/lib/libvulkan.so.1", "now a file"))
|
||||
|
||||
assertTrue(LinuxRuntimeInstaller.extract(context, pkg, root, null))
|
||||
|
||||
assertFalse(Files.isSymbolicLink(lib("libvulkan.so.1").toPath()))
|
||||
assertEquals("now a file", lib("libvulkan.so.1").readText())
|
||||
assertEquals("old", lib("libvulkan.so.1.4.357").readText())
|
||||
assertTrue(lib("libvulkan.so.1").canExecute())
|
||||
}
|
||||
|
||||
@Test fun theLastEntryForAPathWins() {
|
||||
lib("").mkdirs()
|
||||
lib("target").writeText("t")
|
||||
val pkg = archive(
|
||||
Item.Link("usr/lib/libdup.so", "target"),
|
||||
Item.Data("usr/lib/libdup.so", "file"),
|
||||
Item.Data("usr/lib/libother.so", "file"),
|
||||
Item.Link("usr/lib/libother.so", "target"),
|
||||
)
|
||||
|
||||
assertTrue(LinuxRuntimeInstaller.extract(context, pkg, root, null))
|
||||
|
||||
assertFalse(Files.isSymbolicLink(lib("libdup.so").toPath()))
|
||||
assertEquals("file", lib("libdup.so").readText())
|
||||
assertTrue(Files.isSymbolicLink(lib("libother.so").toPath()))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.droiddeck.launcher.runtime
|
||||
|
||||
import java.io.File
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Paths
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Rule
|
||||
import org.junit.Test
|
||||
import org.junit.rules.TemporaryFolder
|
||||
|
||||
class RuntimeHealthTest {
|
||||
@get:Rule val temporary = TemporaryFolder()
|
||||
|
||||
private fun link(root: File, name: String, target: String) =
|
||||
Files.createSymbolicLink(File(root, "usr/lib/$name").toPath(), Paths.get(target))
|
||||
|
||||
@Test fun reportsOnlyLibraryLinksWhoseTargetIsMissingInsideTheRuntime() {
|
||||
val root = temporary.root
|
||||
val lib = File(root, "usr/lib").apply { mkdirs() }
|
||||
File(lib, "libvulkan.so.1.4.357").writeText("lib")
|
||||
File(root, "etc").mkdirs()
|
||||
File(root, "etc/ld.conf").writeText("conf")
|
||||
link(root, "libvulkan.so.1", "libvulkan.so.1.4.357")
|
||||
link(root, "libvulkan.so", "libvulkan.so.1")
|
||||
link(root, "libabsolute.so", "/etc/ld.conf")
|
||||
link(root, "libharfbuzz.so.0", "libharfbuzz.so.0.61460.0")
|
||||
link(root, "libchain.so", "libharfbuzz.so.0")
|
||||
link(root, "libhost.so", File(lib, "libvulkan.so.1.4.357").absolutePath)
|
||||
|
||||
assertEquals(listOf("libchain.so", "libharfbuzz.so.0", "libhost.so"), RuntimeHealth.brokenLibraryLinks(root))
|
||||
}
|
||||
|
||||
@Test fun aMissingLibraryFolderIsNotReportedAsDamage() {
|
||||
assertEquals(emptyList<String>(), RuntimeHealth.brokenLibraryLinks(temporary.root))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user