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 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_title() { val byDomain = entry("Native", "https://example.com/login") val byTitle = entry("example app", "https://unrelated.test") val other = entry("Other", "https://other.example.net") val parsed = ParsedStructure( packageName = "com.example.app", appTitle = "Example App", originDomains = listOf("example.com"), ) assertEquals( listOf(byDomain, byTitle), autofillPickerEntries(listOf(byDomain, byTitle, other), parsed, limit = 12), ) } @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)) } }