Restore a pulled snapshot onto a TV (0.108.108)

Audit U12: Android 14 refuses to install an older Orbit, so until now a bad install could only be fixed forward.
The adb-only receiver (DUMP permission) gains 'restore': only with confirm RESTORE, only while Orbit's screen isn't
open, only the files a snapshot takes (schedules, settings and profiles prefs; never the catalogue, sign-in or PIN),
every file checked against restore.txt before anything changes, the current data kept aside as before-restore-<time>
(last two), and schedules swapped in by rename. tools/restore-tv.sh drives it from the PC.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-08 11:30:47 +00:00
co-authored by Claude Opus 5.5
parent 0df92c6d02
commit 1b77e18adf
7 changed files with 164 additions and 7 deletions
@@ -0,0 +1,65 @@
package tv.orbit.classic
import org.junit.Assert.*
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File
import java.security.MessageDigest
/** 8 November 2026 (audit U12): a pulled snapshot can be put back, only deliberately, only checked, keeping what was there. */
class InstallRestoreTest {
@get:Rule val tmp = TemporaryFolder()
private fun sha(f: File) = MessageDigest.getInstance("SHA-256").digest(f.readBytes()).joinToString("") { "%02x".format(it) }
private fun put(root: File, path: String, text: String) = File(root, path).apply { parentFile.mkdirs(); writeText(text) }
private fun manifest(src: File, paths: List<String>) = File(src, "restore.txt").writeText(paths.joinToString("") { "${sha(File(src, it))} $it\n" })
private fun box(): Pair<File, File> {
val data = tmp.newFolder("data"); val src = tmp.newFolder("src")
put(data, "no_backup/schedule/booked-0.json", "now"); put(data, "no_backup/schedule/only-now.json", "x")
put(data, "shared_prefs/orbit_profiles.xml", "<now/>"); put(data, "no_backup/catalogue.db", "library")
put(src, "no_backup/schedule/booked-0.json", "then"); put(src, "no_backup/schedule/taste/grown.events.jsonl", "{}\n")
put(src, "shared_prefs/orbit_profiles.xml", "<then/>")
return data to src
}
@Test fun putsTheSnapshotBackAndKeepsWhatWasThere() {
val (data, src) = box()
manifest(src, listOf("no_backup/schedule/booked-0.json", "no_backup/schedule/taste/grown.events.jsonl", "shared_prefs/orbit_profiles.xml"))
val said = InstallRestore.apply(data, src, "RESTORE", 1000L)
assertEquals("restored 3; aside before-restore-1000", said)
assertEquals("then", File(data, "no_backup/schedule/booked-0.json").readText())
assertEquals("{}\n", File(data, "no_backup/schedule/taste/grown.events.jsonl").readText())
assertFalse(File(data, "no_backup/schedule/only-now.json").exists())
assertEquals("<then/>", File(data, "shared_prefs/orbit_profiles.xml").readText())
// What was there, kept aside; the library untouched; nothing half-done left behind.
assertEquals("now", File(data, "no_backup/before-restore-1000/schedule/booked-0.json").readText())
assertEquals("<now/>", File(data, "no_backup/before-restore-1000/shared_prefs/orbit_profiles.xml").readText())
assertEquals("library", File(data, "no_backup/catalogue.db").readText())
assertFalse(File(data, "no_backup/schedule.restoring").exists()); assertFalse(File(data, "no_backup/schedule.replaced").exists())
}
@Test fun nothingChangesWithoutConfirmOrWithABadChecksum() {
val (data, src) = box()
manifest(src, listOf("no_backup/schedule/booked-0.json"))
assertTrue(InstallRestore.apply(data, src, null, 1L).startsWith("refused"))
File(src, "no_backup/schedule/booked-0.json").writeText("tampered")
assertTrue(InstallRestore.apply(data, src, "RESTORE", 1L).startsWith("refused: checksum"))
assertEquals("now", File(data, "no_backup/schedule/booked-0.json").readText())
assertFalse(File(data, "no_backup/before-restore-1").exists())
}
@Test fun onlyWhatASnapshotTakesCanComeBack() {
val (data, src) = box()
put(src, "no_backup/catalogue.db", "other library"); put(src, "shared_prefs/orbit_lock.xml", "<pin/>")
for (bad in listOf("no_backup/catalogue.db", "shared_prefs/orbit_lock.xml", "no_backup/schedule/../catalogue.db", "no_backup/orbit-connection.v1")) {
File(src, "restore.txt").writeText("${"0".repeat(64)} $bad\n")
assertTrue(bad, InstallRestore.apply(data, src, "RESTORE", 1L).startsWith("refused"))
}
assertEquals("library", File(data, "no_backup/catalogue.db").readText())
}
@Test fun keepsOnlyTheLastTwoSetAsides() {
val (data, src) = box()
manifest(src, listOf("no_backup/schedule/booked-0.json"))
for (t in 1L..4L) InstallRestore.apply(data, src, "RESTORE", t)
assertEquals(listOf("before-restore-3", "before-restore-4"), File(data, "no_backup").list()!!.filter { it.startsWith("before-restore-") }.sorted())
}
}