DecoySettingsIsolationTest.kt

3.6 KB · raw

1package com.peachpasswords.vault23import kotlinx.coroutines.runBlocking4import org.junit.Assert.assertEquals5import org.junit.Assert.assertFalse6import org.junit.Assert.assertTrue7import org.junit.Test89/**10 * PB-26-120: the duress design promises that a decoy session looks like an11 * ordinary installation and that everything it writes stays in the decoy. The12 * vault settings record — which carries the screen-capture control and the13 * favicon consent flag — is shared, plaintext state loaded at every session14 * start, and it broke both halves of that promise.15 */16class DecoySettingsIsolationTest {1718    private class MemStorage : VaultStorage {19        var slotA: StoredVault? = null20        var slotB: String? = null21        private var ver: Long = 022        private var settings = VaultSettings()23        override fun loadSlotA() = slotA24        override fun hasVault() = slotA != null25        override fun writeSlotA(slot: StoredVault): Boolean { slotA = slot; return true }26        override fun loadSlotB() = slotB27        override fun writeSlotB(blobBase64: String): Boolean { slotB = blobBase64; return true }28        var decoy: StoredVault? = null29        override fun loadDecoySlot() = decoy30        override fun writeDecoySlot(slot: StoredVault): Boolean { decoy = slot; return true }31        override fun hasDecoySlot() = decoy != null32        override fun clearDecoySlot() { decoy = null }33        override fun version() = ver34        override fun stampLocalMutation(): Long { ver = maxOf(System.currentTimeMillis(), ver + 1); return ver }35        override fun loadSettings() = settings36        override fun writeSettings(s: VaultSettings): Boolean { settings = s; return true }37        var history = VaultHistory()38        override fun loadVaultHistory() = history39        override fun writeVaultHistory(h: VaultHistory): Boolean { history = h; return true }40        override fun clearVaultHistory() { history = VaultHistory() }41        override fun clearVault() {42            slotA = null; slotB = null; decoy = null; ver = 043            settings = VaultSettings(); history = VaultHistory()44        }45    }4647    private val mainPassword = "correct-horse-battery-staple"48    private val duressPassword = "open-the-decoy-vault-456"4950    @Test fun a_decoy_session_neither_reads_nor_writes_the_real_settings() = runBlocking {51        val store = MemStorage()52        val repo = VaultRepository(store)53        repo.createVault(mainPassword)5455        // The real vault deliberately differs from the defaults.56        repo.updateSettings(repo.settings.value.copy(allowScreenshots = true, sortOrder = "az"))57        assertTrue("fixture: the real record carries the change", store.loadSettings().allowScreenshots)5859        repo.setDuressPassphrase(duressPassword)60        repo.lock()61        val duress = repo.tryUnlockDuress(duressPassword)!!62        assertEquals(UnlockResult.Success, repo.commitDuressUnlock(duress))6364        // Read side: the decoy shows its own defaults, not the real configuration.65        assertFalse("decoy must not surface the real capture setting", repo.settings.value.allowScreenshots)66        assertEquals("url-first", repo.settings.value.sortOrder)6768        // Write side: a decoy edit stays in memory and never reaches the record69        // the real vault loads at its next unlock.70        repo.updateSettings(repo.settings.value.copy(breachMonitoring = false))71        assertTrue("decoy write must not persist", store.loadSettings().breachMonitoring)72        assertTrue("the real record is untouched", store.loadSettings().allowScreenshots)73        assertFalse("the decoy's own view still reflects the edit", repo.settings.value.breachMonitoring)74    }75}