RealBackupDecodeTest.kt

2.2 KB · raw

1package com.peachpasswords.ui.vault23import com.peachpasswords.vault.PeachScript4import com.peachpasswords.vault.VaultEntry5import org.junit.Assert.assertEquals6import org.junit.Assert.assertTrue7import org.junit.Test89/**10 * Decode-path checks using a deterministic synthetic PeachScript backup.11 *12 * The fixture is generated through the production export API, so it exercises13 * the same encrypted envelope and PUA text decoding as a real backup without14 * depending on a developer's home directory.15 */16class RealBackupDecodeTest {1718    private val password = "synthetic-backup-password"1920    private fun backup(): String = PeachScriptExport.buildEncrypted(21        entries = listOf(22            VaultEntry(23                id = "11111111-2222-3333-4444-555555555555",24                type = "login",25                title = "Synthetic GitHub",26                username = "fixture-user",27                password = "fixture-secret",28                url = "https://github.com",29                notes = "generated test fixture",30                createdAt = 0,31                modifiedAt = 0,32            ),33        ),34        backupPassword = password,35    )3637    @Test38    fun synthetic_backup_is_recognised_and_decodes_to_bytes() {39        val text = backup()4041        assertTrue(PeachScriptImport.looksLikePeachScript(text))4243        val normalised = PeachScript.normalise(text)44        assertTrue(PeachScript.isPeachScript(normalised))4546        val bytes = PeachScript.decode(normalised)47        assertTrue("decoded to nothing", bytes.isNotEmpty())4849        val result = PeachScriptImport.decryptAndParse(text, password)50        assertTrue(result is PeachScriptImport.DecryptResult.Success)51        result as PeachScriptImport.DecryptResult.Success52        assertEquals("fixture-secret", result.entries.single().password)53    }5455    @Test56    fun wrong_password_is_reported_as_wrong_not_corrupted() {57        val result = PeachScriptImport.decryptAndParse(backup(), "definitely-not-the-password")5859        assertTrue(60            "synthetic export with a bad password should report WrongPassword, got $result",61            result is PeachScriptImport.DecryptResult.WrongPassword,62        )63    }64}