refactor(browser): split installation and profile abstractions (#603)

* refactor(browser): split installation and profile abstractions

A Chromium installation shares one master key across its profiles, but
modeling each profile as its own Browser re-derived the key per profile.
Browser now represents one installation holding its profiles and derives
the key once; new types.Profile/ExtractResult/CountResult carry per-profile
results.

* style: gofumpt safari_test.go

* test(chromium): rename shadowed loop var to path
This commit is contained in:
Roger
2026-05-31 16:37:23 +08:00
committed by GitHub
parent d5dc81f1c0
commit b901f7dff0
28 changed files with 1359 additions and 1206 deletions
+11 -127
View File
@@ -117,18 +117,19 @@ func TestNewBrowsers(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := types.BrowserConfig{Name: "Firefox", Kind: types.Firefox, UserDataDir: tt.dir}
browsers, err := NewBrowsers(cfg)
b, err := NewBrowser(cfg)
require.NoError(t, err)
if len(tt.wantProfiles) == 0 {
assert.Empty(t, browsers)
assert.Nil(t, b)
return
}
require.Len(t, browsers, len(tt.wantProfiles))
require.NotNil(t, b)
require.Len(t, b.profiles, len(tt.wantProfiles))
profileNames := make(map[string]bool)
for _, b := range browsers {
profileNames[filepath.Base(b.profileDir)] = true
for _, p := range b.profiles {
profileNames[filepath.Base(p.profileDir)] = true
}
for _, want := range tt.wantProfiles {
assert.True(t, profileNames[want], "should find profile %s", want)
@@ -187,134 +188,17 @@ func TestCountEntries(t *testing.T) {
mkDir(profileDir)
installFile(t, profileDir, setupMozHistoryDB(t), "places.sqlite")
browsers, err := NewBrowsers(types.BrowserConfig{
b, err := NewBrowser(types.BrowserConfig{
Name: "Firefox", Kind: types.Firefox, UserDataDir: dir,
})
require.NoError(t, err)
require.Len(t, browsers, 1)
require.NotNil(t, b)
// CountEntries works without master key.
counts, err := browsers[0].CountEntries([]types.Category{types.History})
results, err := b.CountEntries([]types.Category{types.History})
require.NoError(t, err)
assert.Equal(t, 3, counts[types.History])
}
func TestCountCategory(t *testing.T) {
t.Run("History", func(t *testing.T) {
path := setupMozHistoryDB(t)
b := &Browser{}
assert.Equal(t, 3, b.countCategory(types.History, path))
})
t.Run("Cookie", func(t *testing.T) {
path := setupMozCookieDB(t)
b := &Browser{}
assert.Equal(t, 2, b.countCategory(types.Cookie, path))
})
t.Run("Bookmark", func(t *testing.T) {
path := setupMozBookmarkDB(t)
b := &Browser{}
assert.Equal(t, 2, b.countCategory(types.Bookmark, path))
})
t.Run("Extension", func(t *testing.T) {
path := setupMozExtensionJSON(t)
b := &Browser{}
assert.Equal(t, 2, b.countCategory(types.Extension, path))
})
t.Run("UnsupportedCategory", func(t *testing.T) {
b := &Browser{}
assert.Equal(t, 0, b.countCategory(types.CreditCard, "unused"))
assert.Equal(t, 0, b.countCategory(types.SessionStorage, "unused"))
})
}
// ---------------------------------------------------------------------------
// extractCategory
// ---------------------------------------------------------------------------
// TestExtractCategory verifies that the switch dispatch works for each category.
func TestExtractCategory(t *testing.T) {
t.Run("History", func(t *testing.T) {
path := createTestDB(t, "places.sqlite",
[]string{mozPlacesSchema},
insertMozPlace(1, "https://example.com", "Example", 3, 1000000),
insertMozPlace(2, "https://go.dev", "Go", 1, 2000000),
)
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.History, nil, path)
require.Len(t, data.Histories, 2)
// Firefox sorts by visit count ascending
assert.Equal(t, 1, data.Histories[0].VisitCount)
assert.Equal(t, 3, data.Histories[1].VisitCount)
})
t.Run("Cookie", func(t *testing.T) {
path := createTestDB(t, "cookies.sqlite",
[]string{mozCookiesSchema},
insertMozCookie("session", "abc", ".example.com", "/", 1000000000000, 0, 0, 0),
)
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.Cookie, nil, path)
require.Len(t, data.Cookies, 1)
assert.Equal(t, "session", data.Cookies[0].Name)
assert.Equal(t, "abc", data.Cookies[0].Value) // Firefox cookies are not encrypted
})
t.Run("Bookmark", func(t *testing.T) {
path := createTestDB(t, "places.sqlite",
[]string{mozPlacesSchema, mozBookmarksSchema},
insertMozPlace(1, "https://github.com", "GitHub", 1, 1000000),
insertMozBookmark(1, 1, 1, "GitHub", 1000000),
)
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.Bookmark, nil, path)
require.Len(t, data.Bookmarks, 1)
assert.Equal(t, "GitHub", data.Bookmarks[0].Name)
})
t.Run("Extension", func(t *testing.T) {
path := createTestJSON(t, "extensions.json", `{
"addons": [
{
"id": "ublock@example.com",
"location": "app-profile",
"active": true,
"version": "1.0",
"defaultLocale": {"name": "uBlock Origin", "description": "Ad blocker"}
},
{
"id": "system@mozilla.com",
"location": "app-system-defaults",
"active": true
}
]
}`)
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.Extension, nil, path)
require.Len(t, data.Extensions, 1) // system extension skipped
assert.Equal(t, "uBlock Origin", data.Extensions[0].Name)
})
t.Run("UnsupportedCategory", func(t *testing.T) {
b := &Browser{}
data := &types.BrowserData{}
// CreditCard and SessionStorage are not supported by Firefox
b.extractCategory(data, types.CreditCard, nil, "unused")
b.extractCategory(data, types.SessionStorage, nil, "unused")
assert.Empty(t, data.CreditCards)
assert.Empty(t, data.SessionStorage)
})
require.Len(t, results, 1)
assert.Equal(t, 3, results[0].Counts[types.History])
}
// Anchor: 2024-01-15T10:30:00Z.