package com.peachpasswords.autofill import com.peachpasswords.vault.VaultEntry import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Test class AutofillMatchTest { private val SIGNER = "AA:BB:CC:DD" private fun entry(title: String, url: String?) = VaultEntry(id = title, title = title, url = url, createdAt = 0, modifiedAt = 0) @Test fun strict_domain_match_hits_only_the_matching_site() { val peach = entry("Peach", "https://peachpasswords.com/login") val mail = entry("Fastmail", "https://fastmail.com") val result = AutofillResponseBuilder.matchEntries(listOf(mail, peach), "peachpasswords.com") assertEquals(listOf(peach), result.hits) assertTrue("no unrelated entries leak for a web origin", result.rest.isEmpty()) } @Test fun no_false_positive_on_substring_lookalike() { val gh = entry("GitHub", "https://github.com/login") val result = AutofillResponseBuilder.matchEntries(listOf(gh), "notgithub.com") assertTrue("github.com must not match notgithub.com", result.hits.isEmpty()) assertTrue(result.rest.isEmpty()) } @Test fun subdomain_origin_matches_registrable_domain() { val peach = entry("Peach", "https://peachpasswords.com") val result = AutofillResponseBuilder.matchEntries(listOf(peach), "app.peachpasswords.com") assertEquals(listOf(peach), result.hits) } @Test fun additional_urls_match_registrable_domain() { val peach = entry("Peach", "https://peachpasswords.com") .copy(additionalUrls = listOf("https://accounts.peachpasswords.co.uk/login")) val result = AutofillResponseBuilder.matchEntries(listOf(peach), "signin.peachpasswords.co.uk") assertEquals(listOf(peach), result.hits) } @Test fun additional_urls_do_not_match_substring_lookalikes() { val gh = entry("GitHub", null) .copy(additionalUrls = listOf("https://github.com/login")) val result = AutofillResponseBuilder.matchEntries(listOf(gh), "evilgithub.com") assertTrue(result.hits.isEmpty()) assertTrue(result.rest.isEmpty()) } @Test fun picker_web_origin_uses_domain_matching() { val peach = entry("Peach", "https://peachpasswords.com") val parsed = ParsedStructure( webDomain = "app.peachpasswords.com", originDomains = listOf("peachpasswords.com"), ) assertEquals(listOf(peach), autofillPickerEntries(listOf(peach), parsed, limit = 12)) } @Test fun picker_native_origin_matches_app_link_domains_and_associated_package() { val byDomain = entry("Native", "https://example.com/login") val associated = entry("Saved here", "com.example.app").copy(nativeSignerDigests = setOf(SIGNER)) val other = entry("Other", "https://other.example.net") val parsed = ParsedStructure( packageName = "com.example.app", originDomains = listOf("example.com"), nativeSignerDigests = setOf(SIGNER), ) assertEquals( listOf(byDomain, associated), autofillPickerEntries(listOf(byDomain, associated, other), parsed, limit = 12), ) } /** PAI-2026-001: a display label is presentation, not identity. */ @Test fun native_app_label_never_selects_a_credential() { val bank = entry("Bank", "https://real-bank.test/login") val parsed = ParsedStructure(packageName = "com.attacker.app") assertTrue( "a sideloaded app named after a vault entry must not be offered it", autofillPickerEntries(listOf(bank), parsed, limit = 12).isEmpty(), ) } @Test fun associated_package_match_is_exact_not_registrable_domain() { val mine = entry("Mine", "com.a.example.app").copy(nativeSignerDigests = setOf(SIGNER)) val theirs = entry("Theirs", "com.b.example.app").copy(nativeSignerDigests = setOf(SIGNER)) val parsed = ParsedStructure(packageName = "com.a.example.app", nativeSignerDigests = setOf(SIGNER)) assertEquals( listOf(mine), autofillPickerEntries(listOf(mine, theirs), parsed, limit = 12), ) } /** PB-26-134: a package name alone can be reused after uninstall. */ @Test fun native_origin_fails_closed_when_no_signer_was_recorded() { val legacy = entry("Legacy", "com.example.app") val parsed = ParsedStructure(packageName = "com.example.app", nativeSignerDigests = setOf(SIGNER)) assertTrue( "an entry with no recorded signer must not be offered to a native caller", autofillPickerEntries(listOf(legacy), parsed, limit = 12).isEmpty(), ) } @Test fun origin_without_domains_or_title_has_no_matches() { val app = entry("Native", "https://example.com/login") val parsed = ParsedStructure(packageName = "com.example.app") assertTrue(autofillPickerEntries(listOf(app), parsed, limit = 12).isEmpty()) } @Test fun picker_label_routes_locked_users_to_unlock() { assertEquals("Unlock Peach", autofillPickerLabel(unlocked = false, datasetCount = 3)) } @Test fun picker_label_names_no_match_state() { assertEquals("No saved logins for this site", autofillPickerLabel(unlocked = true, datasetCount = 0)) } @Test fun picker_label_keeps_search_affordance_when_matches_exist() { assertEquals("Search Peach…", autofillPickerLabel(unlocked = true, datasetCount = 1)) } @Test fun null_origin_yields_no_entries() { val a = entry("A", "https://a.com") val b = entry("B", "https://b.com") val result = AutofillResponseBuilder.matchEntries(listOf(a, b), null) assertTrue(result.all.isEmpty()) } @Test fun web_origin_matches_its_page_domain() { val bank = entry("Bank", "https://bank.example.com") val parsed = ParsedStructure( webDomain = "bank.example.com", originDomains = listOf("bank.example.com"), ) assertEquals(listOf(bank), AutofillResponseBuilder.matchEntries(listOf(bank), parsed).all) } @Test fun parser_identity_uses_only_framework_activity_component() { assertEquals( "com.example.host", authenticatedPackageName("com.example.host"), ) assertEquals(null, authenticatedPackageName(null)) } }