ImportParsersTest.kt

2.5 KB · raw

1package com.peachpasswords.ui.vault23import com.peachpasswords.vault.VaultEntry4import org.junit.Assert.assertEquals5import org.junit.Assert.assertFalse6import org.junit.Assert.assertNull7import org.junit.Assert.assertTrue8import org.junit.Test910class ImportParsersTest {1112    @Test fun detect_blank_is_null() {13        assertNull(CsvImport.detectProvider(""))14        assertNull(CsvImport.detectProvider("   \n  "))15    }1617    @Test fun chrome_csv_detects_and_parses() {18        val csv = "name,url,username,password\nGitHub,https://github.com,june,hunter2\n"19        assertEquals(CsvImport.CHROME_CSV, CsvImport.detectProvider(csv))20        val rows = CsvImport.parse(csv, CsvImport.CHROME_CSV)21        assertEquals(1, rows.size)22        assertEquals("GitHub", rows[0].title)23        assertEquals("https://github.com", rows[0].url)24        assertEquals("june", rows[0].username)25        assertEquals("hunter2", rows[0].password)26    }2728    @Test fun csv_quoted_fields_with_commas_are_preserved() {29        val rows = CsvImport.parseCsv("name,url,username,password\n\"Bank, NA\",https://b.com,j,\"p,w\"\n")30        // Header + 1 data row.31        assertEquals(2, rows.size)32        assertEquals(listOf("Bank, NA", "https://b.com", "j", "p,w"), rows[1])33    }3435    @Test fun csv_doubled_quotes_unescape() {36        assertEquals(listOf("a\"b", "c"), CsvImport.splitCsvLine("\"a\"\"b\",c"))37    }3839    @Test fun detect_and_parse_returns_success_for_chrome() {40        val csv = "name,url,username,password\nA,https://a.com,u,p\nB,https://b.com,u2,p2\n"41        val r = CsvImport.detectAndParse(csv)42        assertTrue(r is ImportParseResult.Success)43        r as ImportParseResult.Success44        assertEquals(2, r.entries.size)45    }4647    @Test fun peachscript_export_is_recognised() {48        val out = PeachScriptExport.buildEncrypted(49            listOf(50                VaultEntry(51                    id = "11111111-2222-3333-4444-555555555555", type = "login",52                    title = "GH", username = "u", password = "p", createdAt = 0, modifiedAt = 0,53                ),54            ),55            "backup-pass-123",56        )57        assertTrue(PeachScriptImport.looksLikePeachScript(out))58        assertTrue(PeachScriptImport.isEncrypted(out))59    }6061    @Test fun non_peachscript_text_is_rejected() {62        assertFalse(PeachScriptImport.looksLikePeachScript("just text"))63        // The old JSON marker is not PeachScript anymore (PUA-encoded binary only).64        assertFalse(PeachScriptImport.looksLikePeachScript("""{"peachscript":1,"encrypted":true}"""))65    }66}