Files
keepassgo/androidtestsrc/org/julianfamily/keepassgo/AutofillCacheStoreBehaviorTest.java
T
Joe Julian d18627cda4
ci / lint-test (pull_request) Successful in 5m45s
ci / build (pull_request) Successful in 6m19s
Scope Android autofill candidates
2026-04-27 19:50:55 -07:00

270 lines
11 KiB
Java

package org.julianfamily.keepassgo;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
public final class AutofillCacheStoreBehaviorTest {
public static void main(String[] args) {
testFindBestMatchUsesAndroidAppTargets();
testChooserCandidatesCollapseToExactAndroidAppMatch();
testChooserCandidatesStayScopedToExactHostMatches();
testChooserCandidatesStayScopedToParentHostMatches();
testCacheStoreChooserCandidatesStayScopedToExactHostMatches();
testCacheStoreChooserCandidatesFallBackToAllEntriesOnlyWhenUnrelated();
testCacheStoreRelevantCandidatesDoNotFallBackToAllEntries();
}
private static void testFindBestMatchUsesAndroidAppTargets() {
List<AutofillTargetMatcher.Entry> entries = new ArrayList<>();
entries.add(new AutofillTargetMatcher.Entry(
"blink-entry",
"Blink",
"linuscaldwell",
"bellagio-stack",
"account.blinknetwork.com",
"https://account.blinknetwork.com",
Arrays.asList("https://account.blinknetwork.com", "androidapp://com.blinknetwork.mobile2"),
Arrays.asList("Crew", "Apps")
));
AutofillTargetMatcher.Entry got = AutofillTargetMatcher.findBestMatch(entries, "androidapp://com.blinknetwork.mobile2");
if (got == null || !"blink-entry".equals(got.id)) {
throw new AssertionError("findBestMatch(entries, androidapp target) = " + describe(got) + ", want blink-entry");
}
}
private static void testChooserCandidatesCollapseToExactAndroidAppMatch() {
List<AutofillTargetMatcher.Entry> entries = new ArrayList<>();
entries.add(new AutofillTargetMatcher.Entry(
"blink-entry",
"Blink",
"linuscaldwell",
"bellagio-stack",
"account.blinknetwork.com",
"https://account.blinknetwork.com",
Arrays.asList("https://account.blinknetwork.com", "androidapp://com.blinknetwork.mobile2"),
Arrays.asList("Crew", "Apps")
));
entries.add(new AutofillTargetMatcher.Entry(
"night-fox-entry",
"Night Fox",
"nightfox",
"vault-code",
"gitlab.com",
"https://gitlab.com",
Arrays.asList("https://gitlab.com"),
Arrays.asList("Crew", "Internet")
));
List<AutofillTargetMatcher.Entry> got = AutofillTargetMatcher.chooserCandidates(entries, "androidapp://com.blinknetwork.mobile2");
if (got.size() != 1 || !"blink-entry".equals(got.get(0).id)) {
throw new AssertionError("chooserCandidates(entries, androidapp target) = " + describe(got) + ", want [blink-entry]");
}
}
private static void testChooserCandidatesStayScopedToExactHostMatches() {
List<AutofillTargetMatcher.Entry> entries = new ArrayList<>();
entries.add(new AutofillTargetMatcher.Entry(
"bellagio-primary",
"Bellagio Primary",
"dannyocean",
"vault-code",
"bellagio.example.invalid",
"https://bellagio.example.invalid/login",
Arrays.asList("https://bellagio.example.invalid/login"),
Arrays.asList("Crew", "Internet")
));
entries.add(new AutofillTargetMatcher.Entry(
"bellagio-backup",
"Bellagio Backup",
"rustyryan",
"backup-code",
"bellagio.example.invalid",
"https://bellagio.example.invalid/admin",
Arrays.asList("https://bellagio.example.invalid/admin"),
Arrays.asList("Crew", "Internet")
));
entries.add(new AutofillTargetMatcher.Entry(
"night-fox-entry",
"Night Fox",
"nightfox",
"vault-code",
"gitlab.com",
"https://gitlab.com",
Arrays.asList("https://gitlab.com"),
Arrays.asList("Crew", "Internet")
));
List<AutofillTargetMatcher.Entry> got = AutofillTargetMatcher.chooserCandidates(entries, "https://bellagio.example.invalid/security");
if (got.size() != 2 || !containsIDs(got, "bellagio-primary", "bellagio-backup")) {
throw new AssertionError("chooserCandidates(entries, exact host) = " + describe(got) + ", want only Bellagio entries");
}
}
private static void testChooserCandidatesStayScopedToParentHostMatches() {
List<AutofillTargetMatcher.Entry> entries = new ArrayList<>();
entries.add(new AutofillTargetMatcher.Entry(
"bellagio-parent",
"Bellagio Parent",
"linuscaldwell",
"chip-stack",
"example.invalid",
"https://example.invalid/login",
Arrays.asList("https://example.invalid/login"),
Arrays.asList("Crew", "Internet")
));
entries.add(new AutofillTargetMatcher.Entry(
"night-fox-entry",
"Night Fox",
"nightfox",
"vault-code",
"gitlab.com",
"https://gitlab.com",
Arrays.asList("https://gitlab.com"),
Arrays.asList("Crew", "Internet")
));
List<AutofillTargetMatcher.Entry> got = AutofillTargetMatcher.chooserCandidates(entries, "https://bellagio.example.invalid/security");
if (got.size() != 1 || !"bellagio-parent".equals(got.get(0).id)) {
throw new AssertionError("chooserCandidates(entries, parent host) = " + describe(got) + ", want [bellagio-parent]");
}
}
private static void testCacheStoreChooserCandidatesStayScopedToExactHostMatches() {
List<AutofillCacheStore.Entry> entries = new ArrayList<>();
entries.add(new AutofillCacheStore.Entry(
"bellagio-primary",
"Bellagio Primary",
"dannyocean",
"vault-code",
"bellagio.example.invalid",
"https://bellagio.example.invalid/login",
Arrays.asList("https://bellagio.example.invalid/login"),
Arrays.asList("Crew", "Internet")
));
entries.add(new AutofillCacheStore.Entry(
"bellagio-backup",
"Bellagio Backup",
"rustyryan",
"backup-code",
"bellagio.example.invalid",
"https://bellagio.example.invalid/admin",
Arrays.asList("https://bellagio.example.invalid/admin"),
Arrays.asList("Crew", "Internet")
));
entries.add(new AutofillCacheStore.Entry(
"night-fox-entry",
"Night Fox",
"nightfox",
"vault-code",
"gitlab.com",
"https://gitlab.com",
Arrays.asList("https://gitlab.com"),
Arrays.asList("Crew", "Internet")
));
List<AutofillCacheStore.Entry> got = AutofillCacheStore.chooserCandidatesFromEntries(entries, "https://bellagio.example.invalid/security");
if (got.size() != 2 || !containsStoreIDs(got, "bellagio-primary", "bellagio-backup")) {
throw new AssertionError("AutofillCacheStore chooser candidates = " + describeStore(got) + ", want only Bellagio entries");
}
}
private static void testCacheStoreChooserCandidatesFallBackToAllEntriesOnlyWhenUnrelated() {
List<AutofillCacheStore.Entry> entries = new ArrayList<>();
entries.add(new AutofillCacheStore.Entry(
"bellagio-primary",
"Bellagio Primary",
"dannyocean",
"vault-code",
"bellagio.example.invalid",
"https://bellagio.example.invalid/login",
Arrays.asList("https://bellagio.example.invalid/login"),
Arrays.asList("Crew", "Internet")
));
entries.add(new AutofillCacheStore.Entry(
"night-fox-entry",
"Night Fox",
"nightfox",
"vault-code",
"gitlab.com",
"https://gitlab.com",
Arrays.asList("https://gitlab.com"),
Arrays.asList("Crew", "Internet")
));
List<AutofillCacheStore.Entry> got = AutofillCacheStore.chooserCandidatesFromEntries(entries, "https://tessio.example.invalid/login");
if (got.size() != 2 || !containsStoreIDs(got, "bellagio-primary", "night-fox-entry")) {
throw new AssertionError("AutofillCacheStore unrelated chooser candidates = " + describeStore(got) + ", want full fallback list");
}
}
private static void testCacheStoreRelevantCandidatesDoNotFallBackToAllEntries() {
List<AutofillCacheStore.Entry> entries = new ArrayList<>();
entries.add(new AutofillCacheStore.Entry(
"bellagio-primary",
"Bellagio Primary",
"dannyocean",
"vault-code",
"bellagio.example.invalid",
"https://bellagio.example.invalid/login",
Arrays.asList("https://bellagio.example.invalid/login"),
Arrays.asList("Crew", "Internet")
));
entries.add(new AutofillCacheStore.Entry(
"night-fox-entry",
"Night Fox",
"nightfox",
"vault-code",
"gitlab.com",
"https://gitlab.com",
Arrays.asList("https://gitlab.com"),
Arrays.asList("Crew", "Internet")
));
List<AutofillCacheStore.Entry> got = AutofillCacheStore.relevantCandidatesFromEntries(entries, "https://tessio.example.invalid/login");
if (!got.isEmpty()) {
throw new AssertionError("AutofillCacheStore relevant unrelated candidates = " + describeStore(got) + ", want []");
}
}
private static String describe(AutofillTargetMatcher.Entry entry) {
if (entry == null) {
return "null";
}
return entry.id;
}
private static String describe(List<AutofillTargetMatcher.Entry> entries) {
List<String> ids = new ArrayList<>();
for (AutofillTargetMatcher.Entry entry : entries) {
ids.add(entry.id);
}
return ids.toString();
}
private static boolean containsIDs(List<AutofillTargetMatcher.Entry> entries, String... wantIDs) {
List<String> ids = new ArrayList<>();
for (AutofillTargetMatcher.Entry entry : entries) {
ids.add(entry.id);
}
return ids.containsAll(Arrays.asList(wantIDs)) && ids.size() == wantIDs.length;
}
private static String describeStore(List<AutofillCacheStore.Entry> entries) {
List<String> ids = new ArrayList<>();
for (AutofillCacheStore.Entry entry : entries) {
ids.add(entry.id);
}
return ids.toString();
}
private static boolean containsStoreIDs(List<AutofillCacheStore.Entry> entries, String... wantIDs) {
List<String> ids = new ArrayList<>();
for (AutofillCacheStore.Entry entry : entries) {
ids.add(entry.id);
}
return ids.containsAll(Arrays.asList(wantIDs)) && ids.size() == wantIDs.length;
}
}