RecoveryEscrowTest.kt

5.6 KB · raw

1package com.peachpasswords.vault23import kotlinx.coroutines.runBlocking4import org.junit.Assert.assertEquals5import org.junit.Assert.assertFalse6import org.junit.Assert.assertNotNull7import org.junit.Assert.assertNull8import org.junit.Assert.assertTrue9import org.junit.Test1011private class RecoveryFakeStorage : VaultStorage {12    var slotA: StoredVault? = null13    var slotB: String? = null14    private var ver: Long = 015    private var settings = VaultSettings()16    override fun loadSlotA() = slotA17    override fun hasVault() = slotA != null18    override fun writeSlotA(slot: StoredVault): Boolean { slotA = slot; return true }19    override fun writeSlotB(blobBase64: String): Boolean { slotB = blobBase64; return true }20    var decoy: StoredVault? = null21    override fun loadDecoySlot() = decoy22    override fun writeDecoySlot(slot: StoredVault): Boolean { decoy = slot; return true }23    override fun hasDecoySlot() = decoy != null24    override fun clearDecoySlot() { decoy = null }25    override fun version() = ver26    override fun stampLocalMutation(): Long { ver = maxOf(System.currentTimeMillis(), ver + 1); return ver }27    override fun loadSettings() = settings28    override fun writeSettings(s: VaultSettings): Boolean { settings = s; return true }29    override fun clearVault() { slotA = null; slotB = null; ver = 0; settings = VaultSettings() }30}3132class RecoveryEscrowTest {3334    /** Recovery words this vault would actually show — derived from its sync root key. */35    private fun recoveryWordsOf(repo: VaultRepository): List<String> {36        val key = repo.syncMasterKey()!!37        return try { rootKeyToWords(key) } finally { VaultCrypto.zero(key) }38    }3940    private fun login(title: String, password: String = "pw") =41        VaultEntry(id = "", title = title, type = "login", password = password, createdAt = 0, modifiedAt = 0)4243    // 1 — setRecoveryWords stores a non-null RecoveryEscrow in StoredVault.44    @Test fun set_recovery_words_writes_escrow() = runBlocking {45        val storage = RecoveryFakeStorage()46        val repo = VaultRepository(storage)47        repo.createVault("pw")48        assertNull(storage.slotA?.recoveryEscrow)49        assertTrue(repo.setRecoveryWords(recoveryWordsOf(repo)))50        assertNotNull(storage.slotA?.recoveryEscrow)51        assertTrue(repo.hasRecoveryEscrow())52    }5354    // 2 — unlockWithRecoveryWords with correct words succeeds.55    @Test fun unlock_with_correct_words_succeeds() = runBlocking {56        val storage = RecoveryFakeStorage()57        val repo = VaultRepository(storage)58        repo.createVault("pw")59        repo.addEntry(login("GitHub"))60        val words = recoveryWordsOf(repo)61        repo.setRecoveryWords(words)62        repo.lock()63        assertTrue(repo.unlockWithRecoveryWords(words))64        // Recovered session sees the entry that was sealed into the escrow.65        assertTrue(repo.getEntries().any { it.title == "GitHub" })66    }6768    // 3 — unlockWithRecoveryWords with wrong words fails.69    @Test fun unlock_with_wrong_words_fails() = runBlocking {70        val storage = RecoveryFakeStorage()71        val repo = VaultRepository(storage)72        repo.createVault("pw")73        repo.setRecoveryWords(recoveryWordsOf(repo))74        repo.lock()75        // A different, still-checksum-valid phrase from an unrelated root key.76        val wrong = rootKeyToWords(ByteArray(32) { (it + 7).toByte() })77        assertFalse(repo.unlockWithRecoveryWords(wrong))78    }7980    // 4 — unlockWithRecoveryWords with no escrow set returns false.81    @Test fun unlock_with_no_escrow_fails() = runBlocking {82        val storage = RecoveryFakeStorage()83        val repo = VaultRepository(storage)84        repo.createVault("pw")85        val words = recoveryWordsOf(repo) // never call setRecoveryWords86        repo.lock()87        assertFalse(repo.hasRecoveryEscrow())88        assertFalse(repo.unlockWithRecoveryWords(words))89    }9091    // 5 — Full round-trip: create → set words → reset password via recovery → unlock with new password.92    @Test fun full_recovery_resets_password() = runBlocking {93        val storage = RecoveryFakeStorage()94        val repo = VaultRepository(storage)95        repo.createVault("old-pass")96        repo.addEntry(login("Bank"))97        val words = recoveryWordsOf(repo)98        repo.setRecoveryWords(words)99        repo.lock()100101        assertTrue(repo.unlockWithRecoveryWords(words))102        assertTrue(repo.resetPasswordWithRecoveryWords(words, "brand-new-pass"))103        assertTrue(repo.isUnlocked.value)104        repo.lock()105106        // Old password no longer works; the new one does and the entry survived.107        assertEquals(UnlockResult.WrongPassword, repo.unlock("old-pass"))108        assertEquals(UnlockResult.Success, repo.unlock("brand-new-pass"))109        assertTrue(repo.getEntries().any { it.title == "Bank" })110        // Escrow survived the password reset, so a second recovery still works.111        assertTrue(repo.hasRecoveryEscrow())112    }113114    // 6 — Round-trips for a phrase containing the duplicate "abandon" (WORD_LIST115    // index 0/1). Index-based entropy derivation can't invert these; the escrow116    // keys off the word strings, so seal+unlock stay consistent.117    @Test fun escrow_round_trips_for_phrase_with_duplicate_abandon() = runBlocking {118        val storage = RecoveryFakeStorage()119        val repo = VaultRepository(storage)120        repo.createVault("pw")121        repo.addEntry(login("Mail"))122        val words = List(12) { "abandon" }123        repo.setRecoveryWords(words)124        assertNotNull(storage.slotA?.recoveryEscrow)125        repo.lock()126        assertTrue(repo.unlockWithRecoveryWords(words))127        assertTrue(repo.getEntries().any { it.title == "Mail" })128    }129}