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 RecoveryFakeStorage : 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 = RecoveryFakeStorage() 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 = RecoveryFakeStorage() 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 = RecoveryFakeStorage() 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 = RecoveryFakeStorage() 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 = RecoveryFakeStorage() 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()) } // 6 — Round-trips for a phrase containing the duplicate "abandon" (WORD_LIST // index 0/1). Index-based entropy derivation can't invert these; the escrow // keys off the word strings, so seal+unlock stay consistent. @Test fun escrow_round_trips_for_phrase_with_duplicate_abandon() = runBlocking { val storage = RecoveryFakeStorage() val repo = VaultRepository(storage) repo.createVault("pw") repo.addEntry(login("Mail")) val words = List(12) { "abandon" } repo.setRecoveryWords(words) assertNotNull(storage.slotA?.recoveryEscrow) repo.lock() assertTrue(repo.unlockWithRecoveryWords(words)) assertTrue(repo.getEntries().any { it.title == "Mail" }) } }