/* * C1 — RecoveryEscrowTest * Target destination: app/src/test/java/com/peachpasswords/vault/RecoveryEscrowTest.kt * * The 5 tests from brief Part 3d, written against the REAL repo/crypto API and * the C1 additions (unlockWithRecoveryWords/resetPasswordWithRecoveryWords/ * hasRecoveryEscrow + StoredVault.recoveryEscrow). * * Self-contained FakeStorage (the one in VaultRepositoryTest.kt is file-private, * so we declare our own). After the C1 StoredVault delta lands, this FakeStorage * needs no change — it stores the whole StoredVault object, so the new * recoveryEscrow field round-trips automatically. * * Recovery words must be a CHECKSUM-VALID BIP39 phrase. We get one for free from * rootKeyToWords(rootKey) — exactly the words this vault would display, and the * same construction the existing recovery_words tests use. */ package com.peachpasswords.vault import kotlinx.coroutines.runBlocking import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNotNull import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test private class FakeStorage : VaultStorage { var slotA: StoredVault? = null var slotB: String? = null private var ver: Long = 0 private var settings = VaultSettings() override fun loadSlotA() = slotA override fun hasVault() = slotA != null override fun writeSlotA(slot: StoredVault): Boolean { slotA = slot; return true } override fun writeSlotB(blobBase64: String): Boolean { slotB = blobBase64; return true } var decoy: StoredVault? = null override fun loadDecoySlot() = decoy override fun writeDecoySlot(slot: StoredVault): Boolean { decoy = slot; return true } override fun hasDecoySlot() = decoy != null override fun clearDecoySlot() { decoy = null } override fun version() = ver override fun stampLocalMutation(): Long { ver = maxOf(System.currentTimeMillis(), ver + 1); return ver } override fun loadSettings() = settings override fun writeSettings(s: VaultSettings): Boolean { settings = s; return true } override fun clearVault() { slotA = null; slotB = null; ver = 0; settings = VaultSettings() } } class RecoveryEscrowTest { /** Recovery words this vault would actually show — derived from its sync root key. */ private fun recoveryWordsOf(repo: VaultRepository): List { val key = repo.syncMasterKey()!! return try { rootKeyToWords(key) } finally { VaultCrypto.zero(key) } } private fun login(title: String, password: String = "pw") = VaultEntry(id = "", title = title, type = "login", password = password, createdAt = 0, modifiedAt = 0) // 1 — setRecoveryWords stores a non-null RecoveryEscrow in StoredVault. @Test fun set_recovery_words_writes_escrow() = runBlocking { val storage = FakeStorage() val repo = VaultRepository(storage) repo.createVault("pw") assertNull(storage.slotA?.recoveryEscrow) assertTrue(repo.setRecoveryWords(recoveryWordsOf(repo))) assertNotNull(storage.slotA?.recoveryEscrow) assertTrue(repo.hasRecoveryEscrow()) } // 2 — unlockWithRecoveryWords with correct words succeeds. @Test fun unlock_with_correct_words_succeeds() = runBlocking { val storage = FakeStorage() val repo = VaultRepository(storage) repo.createVault("pw") repo.addEntry(login("GitHub")) val words = recoveryWordsOf(repo) repo.setRecoveryWords(words) repo.lock() assertTrue(repo.unlockWithRecoveryWords(words)) // Recovered session sees the entry that was sealed into the escrow. assertTrue(repo.getEntries().any { it.title == "GitHub" }) } // 3 — unlockWithRecoveryWords with wrong words fails. @Test fun unlock_with_wrong_words_fails() = runBlocking { val storage = FakeStorage() val repo = VaultRepository(storage) repo.createVault("pw") repo.setRecoveryWords(recoveryWordsOf(repo)) repo.lock() // A different, still-checksum-valid phrase from an unrelated root key. val wrong = rootKeyToWords(ByteArray(32) { (it + 7).toByte() }) assertFalse(repo.unlockWithRecoveryWords(wrong)) } // 4 — unlockWithRecoveryWords with no escrow set returns false. @Test fun unlock_with_no_escrow_fails() = runBlocking { val storage = FakeStorage() val repo = VaultRepository(storage) repo.createVault("pw") val words = recoveryWordsOf(repo) // never call setRecoveryWords repo.lock() assertFalse(repo.hasRecoveryEscrow()) assertFalse(repo.unlockWithRecoveryWords(words)) } // 5 — Full round-trip: create → set words → reset password via recovery → unlock with new password. @Test fun full_recovery_resets_password() = runBlocking { val storage = FakeStorage() val repo = VaultRepository(storage) repo.createVault("old-pass") repo.addEntry(login("Bank")) val words = recoveryWordsOf(repo) repo.setRecoveryWords(words) repo.lock() assertTrue(repo.unlockWithRecoveryWords(words)) assertTrue(repo.resetPasswordWithRecoveryWords(words, "brand-new-pass")) assertTrue(repo.isUnlocked.value) repo.lock() // Old password no longer works; the new one does and the entry survived. assertEquals(UnlockResult.WrongPassword, repo.unlock("old-pass")) assertEquals(UnlockResult.Success, repo.unlock("brand-new-pass")) assertTrue(repo.getEntries().any { it.title == "Bank" }) // Escrow survived the password reset, so a second recovery still works. assertTrue(repo.hasRecoveryEscrow()) } }