C1-RecoveryEscrowTest.kt

5.7 KB · raw

1/*2 * C1 — RecoveryEscrowTest3 * Target destination: app/src/test/java/com/peachpasswords/vault/RecoveryEscrowTest.kt4 *5 * The 5 tests from brief Part 3d, written against the REAL repo/crypto API and6 * the C1 additions (unlockWithRecoveryWords/resetPasswordWithRecoveryWords/7 * hasRecoveryEscrow + StoredVault.recoveryEscrow).8 *9 * Self-contained FakeStorage (the one in VaultRepositoryTest.kt is file-private,10 * so we declare our own). After the C1 StoredVault delta lands, this FakeStorage11 * needs no change — it stores the whole StoredVault object, so the new12 * recoveryEscrow field round-trips automatically.13 *14 * Recovery words must be a CHECKSUM-VALID BIP39 phrase. We get one for free from15 * rootKeyToWords(rootKey) — exactly the words this vault would display, and the16 * same construction the existing recovery_words tests use.17 */1819package com.peachpasswords.vault2021import kotlinx.coroutines.runBlocking22import org.junit.Assert.assertEquals23import org.junit.Assert.assertFalse24import org.junit.Assert.assertNotNull25import org.junit.Assert.assertNull26import org.junit.Assert.assertTrue27import org.junit.Test2829private class FakeStorage : VaultStorage {30    var slotA: StoredVault? = null31    var slotB: String? = null32    private var ver: Long = 033    private var settings = VaultSettings()34    override fun loadSlotA() = slotA35    override fun hasVault() = slotA != null36    override fun writeSlotA(slot: StoredVault): Boolean { slotA = slot; return true }37    override fun writeSlotB(blobBase64: String): Boolean { slotB = blobBase64; return true }38    var decoy: StoredVault? = null39    override fun loadDecoySlot() = decoy40    override fun writeDecoySlot(slot: StoredVault): Boolean { decoy = slot; return true }41    override fun hasDecoySlot() = decoy != null42    override fun clearDecoySlot() { decoy = null }43    override fun version() = ver44    override fun stampLocalMutation(): Long { ver = maxOf(System.currentTimeMillis(), ver + 1); return ver }45    override fun loadSettings() = settings46    override fun writeSettings(s: VaultSettings): Boolean { settings = s; return true }47    override fun clearVault() { slotA = null; slotB = null; ver = 0; settings = VaultSettings() }48}4950class RecoveryEscrowTest {5152    /** Recovery words this vault would actually show — derived from its sync root key. */53    private fun recoveryWordsOf(repo: VaultRepository): List<String> {54        val key = repo.syncMasterKey()!!55        return try { rootKeyToWords(key) } finally { VaultCrypto.zero(key) }56    }5758    private fun login(title: String, password: String = "pw") =59        VaultEntry(id = "", title = title, type = "login", password = password, createdAt = 0, modifiedAt = 0)6061    // 1 — setRecoveryWords stores a non-null RecoveryEscrow in StoredVault.62    @Test fun set_recovery_words_writes_escrow() = runBlocking {63        val storage = FakeStorage()64        val repo = VaultRepository(storage)65        repo.createVault("pw")66        assertNull(storage.slotA?.recoveryEscrow)67        assertTrue(repo.setRecoveryWords(recoveryWordsOf(repo)))68        assertNotNull(storage.slotA?.recoveryEscrow)69        assertTrue(repo.hasRecoveryEscrow())70    }7172    // 2 — unlockWithRecoveryWords with correct words succeeds.73    @Test fun unlock_with_correct_words_succeeds() = runBlocking {74        val storage = FakeStorage()75        val repo = VaultRepository(storage)76        repo.createVault("pw")77        repo.addEntry(login("GitHub"))78        val words = recoveryWordsOf(repo)79        repo.setRecoveryWords(words)80        repo.lock()81        assertTrue(repo.unlockWithRecoveryWords(words))82        // Recovered session sees the entry that was sealed into the escrow.83        assertTrue(repo.getEntries().any { it.title == "GitHub" })84    }8586    // 3 — unlockWithRecoveryWords with wrong words fails.87    @Test fun unlock_with_wrong_words_fails() = runBlocking {88        val storage = FakeStorage()89        val repo = VaultRepository(storage)90        repo.createVault("pw")91        repo.setRecoveryWords(recoveryWordsOf(repo))92        repo.lock()93        // A different, still-checksum-valid phrase from an unrelated root key.94        val wrong = rootKeyToWords(ByteArray(32) { (it + 7).toByte() })95        assertFalse(repo.unlockWithRecoveryWords(wrong))96    }9798    // 4 — unlockWithRecoveryWords with no escrow set returns false.99    @Test fun unlock_with_no_escrow_fails() = runBlocking {100        val storage = FakeStorage()101        val repo = VaultRepository(storage)102        repo.createVault("pw")103        val words = recoveryWordsOf(repo) // never call setRecoveryWords104        repo.lock()105        assertFalse(repo.hasRecoveryEscrow())106        assertFalse(repo.unlockWithRecoveryWords(words))107    }108109    // 5 — Full round-trip: create → set words → reset password via recovery → unlock with new password.110    @Test fun full_recovery_resets_password() = runBlocking {111        val storage = FakeStorage()112        val repo = VaultRepository(storage)113        repo.createVault("old-pass")114        repo.addEntry(login("Bank"))115        val words = recoveryWordsOf(repo)116        repo.setRecoveryWords(words)117        repo.lock()118119        assertTrue(repo.unlockWithRecoveryWords(words))120        assertTrue(repo.resetPasswordWithRecoveryWords(words, "brand-new-pass"))121        assertTrue(repo.isUnlocked.value)122        repo.lock()123124        // Old password no longer works; the new one does and the entry survived.125        assertEquals(UnlockResult.WrongPassword, repo.unlock("old-pass"))126        assertEquals(UnlockResult.Success, repo.unlock("brand-new-pass"))127        assertTrue(repo.getEntries().any { it.title == "Bank" })128        // Escrow survived the password reset, so a second recovery still works.129        assertTrue(repo.hasRecoveryEscrow())130    }131}