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
+165
View File
@@ -0,0 +1,165 @@
package safari
import (
"path/filepath"
"github.com/moond4rk/hackbrowserdata/filemanager"
"github.com/moond4rk/hackbrowserdata/log"
"github.com/moond4rk/hackbrowserdata/types"
)
// profile is one Safari profile — the leaf extraction unit. Passwords come from
// the shared macOS Keychain; everything else reads from the profile's directories.
type profile struct {
ctx profileContext
browserName string
sourcePaths map[types.Category]resolvedPath
}
func (p *profile) name() string { return p.ctx.name }
func (p *profile) label() string { return p.browserName + "/" + p.name() }
func (p *profile) dir() string {
if p.ctx.isDefault() {
return p.ctx.legacyHome
}
return filepath.Join(p.ctx.container, "Safari", "Profiles", p.ctx.uuidUpper)
}
func (p *profile) extract(categories []types.Category, keychainPassword string) *types.BrowserData {
session, err := filemanager.NewSession()
if err != nil {
log.Debugf("new session for %s: %v", p.label(), err)
return &types.BrowserData{}
}
defer session.Cleanup()
tempPaths := p.acquireFiles(session, categories)
data := &types.BrowserData{}
for _, cat := range categories {
// Keychain is user-scope, not per-profile — attribute only to default to avoid duplicates.
if cat == types.Password {
if p.ctx.isDefault() {
p.extractCategory(data, cat, "", keychainPassword)
}
continue
}
// Extension plists (AppExtensions + WebExtensions) live directly in the container
// and are read in-place; attribute to default only until per-profile layouts are verified.
if cat == types.Extension {
if p.ctx.isDefault() {
p.extractCategory(data, cat, "", keychainPassword)
}
continue
}
path, ok := tempPaths[cat]
if !ok {
continue
}
p.extractCategory(data, cat, path, keychainPassword)
}
return data
}
func (p *profile) count(categories []types.Category, keychainPassword string) map[types.Category]int {
session, err := filemanager.NewSession()
if err != nil {
log.Debugf("new session for %s: %v", p.label(), err)
return nil
}
defer session.Cleanup()
tempPaths := p.acquireFiles(session, categories)
counts := make(map[types.Category]int)
for _, cat := range categories {
if cat == types.Password {
if p.ctx.isDefault() {
counts[cat] = p.countCategory(cat, "", keychainPassword)
}
continue
}
if cat == types.Extension {
if p.ctx.isDefault() {
counts[cat] = p.countCategory(cat, "", keychainPassword)
}
continue
}
path, ok := tempPaths[cat]
if !ok {
continue
}
counts[cat] = p.countCategory(cat, path, keychainPassword)
}
return counts
}
func (p *profile) acquireFiles(session *filemanager.Session, categories []types.Category) map[types.Category]string {
tempPaths := make(map[types.Category]string)
for _, cat := range categories {
rp, ok := p.sourcePaths[cat]
if !ok {
continue
}
dst := filepath.Join(session.TempDir(), cat.String())
if err := session.Acquire(rp.absPath, dst, rp.isDir); err != nil {
log.Debugf("acquire %s: %v", cat, err)
continue
}
tempPaths[cat] = dst
}
return tempPaths
}
func (p *profile) extractCategory(data *types.BrowserData, cat types.Category, path, keychainPassword string) {
var err error
switch cat {
case types.Password:
data.Passwords, err = extractPasswords(keychainPassword)
case types.History:
data.Histories, err = extractHistories(path)
case types.Cookie:
data.Cookies, err = extractCookies(path)
case types.Bookmark:
data.Bookmarks, err = extractBookmarks(path)
case types.Download:
data.Downloads, err = extractDownloads(path, p.ctx.downloadOwnerUUID())
case types.LocalStorage:
data.LocalStorage, err = extractLocalStorage(path)
case types.Extension:
data.Extensions, err = extractExtensions(p.ctx.container)
default:
return
}
if err != nil {
log.Debugf("extract %s for %s: %v", cat, p.label(), err)
}
}
func (p *profile) countCategory(cat types.Category, path, keychainPassword string) int {
var count int
var err error
switch cat {
case types.Password:
count, err = countPasswords(keychainPassword)
case types.History:
count, err = countHistories(path)
case types.Cookie:
count, err = countCookies(path)
case types.Bookmark:
count, err = countBookmarks(path)
case types.Download:
count, err = countDownloads(path, p.ctx.downloadOwnerUUID())
case types.LocalStorage:
count, err = countLocalStorage(path)
case types.Extension:
count, err = countExtensions(p.ctx.container)
default:
// Unsupported categories silently return 0.
}
if err != nil {
log.Debugf("count %s for %s: %v", cat, p.label(), err)
}
return count
}
+157
View File
@@ -0,0 +1,157 @@
package safari
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/types"
)
func TestCountCategory(t *testing.T) {
t.Run("History", func(t *testing.T) {
path := createTestDB(t, "History.db",
[]string{safariHistoryItemsSchema, safariHistoryVisitsSchema},
insertHistoryItem(1, "https://example.com", "example.com", 1),
)
p := &profile{}
assert.Equal(t, 1, p.countCategory(types.History, path, ""))
})
t.Run("Cookie", func(t *testing.T) {
path := buildTestBinaryCookies(t, []testCookie{
{domain: ".example.com", name: "a", path: "/", value: "1", expires: 2000000000.0, creation: 700000000.0},
{domain: ".go.dev", name: "b", path: "/", value: "2", expires: 2000000000.0, creation: 700000000.0},
})
p := &profile{}
assert.Equal(t, 2, p.countCategory(types.Cookie, path, ""))
})
t.Run("Bookmark", func(t *testing.T) {
path := buildTestBookmarksPlist(t, safariBookmark{
Type: bookmarkTypeList,
Children: []safariBookmark{
{Type: bookmarkTypeLeaf, URLString: "https://a.com", URIDictionary: uriDictionary{Title: "A"}},
{Type: bookmarkTypeLeaf, URLString: "https://b.com", URIDictionary: uriDictionary{Title: "B"}},
},
})
p := &profile{}
assert.Equal(t, 2, p.countCategory(types.Bookmark, path, ""))
})
t.Run("Download", func(t *testing.T) {
path := buildTestDownloadsPlist(t, safariDownloads{
DownloadHistory: []safariDownloadEntry{
{URL: "https://example.com/file.zip", Path: "/tmp/file.zip", TotalBytes: 100},
},
})
p := &profile{}
assert.Equal(t, 1, p.countCategory(types.Download, path, ""))
})
t.Run("LocalStorage", func(t *testing.T) {
dir := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://example.com": {{Key: "k1", Value: "v1"}, {Key: "k2", Value: "v2"}},
"https://go.dev": {{Key: "theme", Value: "dark"}},
})
p := &profile{}
assert.Equal(t, 3, p.countCategory(types.LocalStorage, dir, ""))
})
t.Run("UnsupportedCategory", func(t *testing.T) {
p := &profile{}
assert.Equal(t, 0, p.countCategory(types.CreditCard, "unused", ""))
assert.Equal(t, 0, p.countCategory(types.SessionStorage, "unused", ""))
})
}
func TestExtractCategory(t *testing.T) {
t.Run("History", func(t *testing.T) {
path := createTestDB(t, "History.db",
[]string{safariHistoryItemsSchema, safariHistoryVisitsSchema},
insertHistoryItem(1, "https://example.com", "example.com", 3),
insertHistoryItem(2, "https://go.dev", "go.dev", 1),
insertHistoryVisit(1, 1, 700000000.0, "Example"),
insertHistoryVisit(2, 2, 700000000.0, "Go"),
)
p := &profile{}
data := &types.BrowserData{}
p.extractCategory(data, types.History, path, "")
require.Len(t, data.Histories, 2)
// Sorted by visit count descending
assert.Equal(t, 3, data.Histories[0].VisitCount)
assert.Equal(t, 1, data.Histories[1].VisitCount)
})
t.Run("Cookie", func(t *testing.T) {
path := buildTestBinaryCookies(t, []testCookie{
{
domain: ".example.com", name: "session", path: "/", value: "abc",
secure: true, httpOnly: true, expires: 2000000000.0, creation: 700000000.0,
},
})
p := &profile{}
data := &types.BrowserData{}
p.extractCategory(data, types.Cookie, path, "")
require.Len(t, data.Cookies, 1)
assert.Equal(t, ".example.com", data.Cookies[0].Host)
assert.Equal(t, "session", data.Cookies[0].Name)
assert.True(t, data.Cookies[0].IsSecure)
assert.True(t, data.Cookies[0].IsHTTPOnly)
})
t.Run("Bookmark", func(t *testing.T) {
path := buildTestBookmarksPlist(t, safariBookmark{
Type: bookmarkTypeList,
Children: []safariBookmark{
{Type: bookmarkTypeLeaf, URLString: "https://github.com", URIDictionary: uriDictionary{Title: "GitHub"}},
},
})
p := &profile{}
data := &types.BrowserData{}
p.extractCategory(data, types.Bookmark, path, "")
require.Len(t, data.Bookmarks, 1)
assert.Equal(t, "GitHub", data.Bookmarks[0].Name)
assert.Equal(t, "https://github.com", data.Bookmarks[0].URL)
})
t.Run("Download", func(t *testing.T) {
path := buildTestDownloadsPlist(t, safariDownloads{
DownloadHistory: []safariDownloadEntry{
{URL: "https://example.com/file.zip", Path: "/tmp/file.zip", TotalBytes: 1024},
},
})
p := &profile{}
data := &types.BrowserData{}
p.extractCategory(data, types.Download, path, "")
require.Len(t, data.Downloads, 1)
assert.Equal(t, "https://example.com/file.zip", data.Downloads[0].URL)
assert.Equal(t, int64(1024), data.Downloads[0].TotalBytes)
})
t.Run("LocalStorage", func(t *testing.T) {
dir := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://github.com": {{Key: "theme", Value: "dark"}},
})
p := &profile{}
data := &types.BrowserData{}
p.extractCategory(data, types.LocalStorage, dir, "")
require.Len(t, data.LocalStorage, 1)
assert.Equal(t, "https://github.com", data.LocalStorage[0].URL)
assert.Equal(t, "theme", data.LocalStorage[0].Key)
assert.Equal(t, "dark", data.LocalStorage[0].Value)
})
t.Run("UnsupportedCategory", func(t *testing.T) {
p := &profile{}
data := &types.BrowserData{}
p.extractCategory(data, types.CreditCard, "unused", "")
assert.Empty(t, data.CreditCards)
})
}
+53 -162
View File
@@ -2,194 +2,85 @@ package safari
import (
"os"
"path/filepath"
"time"
"github.com/moond4rk/hackbrowserdata/filemanager"
"github.com/moond4rk/hackbrowserdata/log"
"github.com/moond4rk/hackbrowserdata/types"
)
// Browser is one Safari profile's data ready for extraction. Passwords come from the shared macOS
// Keychain; everything else reads from the profile's directories.
// Browser is one Safari installation, holding the default profile and any named
// profiles. Passwords come from the shared macOS Keychain; the login password is
// set on the installation and threaded to each profile at extract time.
type Browser struct {
cfg types.BrowserConfig
profile profileContext
keychainPassword string
sourcePaths map[types.Category]resolvedPath
profiles []*profile
}
// SetKeychainPassword sets the macOS login password used to unlock the Keychain.
func (b *Browser) SetKeychainPassword(password string) { b.keychainPassword = password }
// NewBrowsers returns one Browser per Safari profile with resolvable data. Named profiles are
// enumerated from SafariTabs.db.
func NewBrowsers(cfg types.BrowserConfig) ([]*Browser, error) {
var browsers []*Browser
// NewBrowser returns the Safari installation with one profile per Safari profile
// that has resolvable data, or nil if none. Named profiles are enumerated from
// SafariTabs.db.
func NewBrowser(cfg types.BrowserConfig) (*Browser, error) {
var profiles []*profile
for _, p := range discoverSafariProfiles(cfg.UserDataDir) {
paths := resolveProfilePaths(p)
if len(paths) == 0 {
continue
}
browsers = append(browsers, &Browser{
cfg: cfg,
profile: p,
profiles = append(profiles, &profile{
ctx: p,
browserName: cfg.Name,
sourcePaths: paths,
})
}
return browsers, nil
if len(profiles) == 0 {
return nil, nil
}
return &Browser{cfg: cfg, profiles: profiles}, nil
}
func (b *Browser) BrowserName() string { return b.cfg.Name }
func (b *Browser) UserDataDir() string { return b.cfg.UserDataDir }
// Profiles returns the identity of every Safari profile in this installation.
func (b *Browser) Profiles() []types.Profile {
out := make([]types.Profile, 0, len(b.profiles))
for _, p := range b.profiles {
out = append(out, types.Profile{Name: p.ctx.name, Dir: p.dir()})
}
return out
}
// Extract extracts every profile, threading the installation's keychain password.
func (b *Browser) Extract(categories []types.Category) ([]types.ExtractResult, error) {
results := make([]types.ExtractResult, 0, len(b.profiles))
for _, p := range b.profiles {
results = append(results, types.ExtractResult{
Profile: types.Profile{Name: p.ctx.name, Dir: p.dir()},
Data: p.extract(categories, b.keychainPassword),
})
}
return results, nil
}
// CountEntries counts entries per category for every profile.
func (b *Browser) CountEntries(categories []types.Category) ([]types.CountResult, error) {
results := make([]types.CountResult, 0, len(b.profiles))
for _, p := range b.profiles {
results = append(results, types.CountResult{
Profile: types.Profile{Name: p.ctx.name, Dir: p.dir()},
Counts: p.count(categories, b.keychainPassword),
})
}
return results, nil
}
func resolveProfilePaths(p profileContext) map[types.Category]resolvedPath {
return resolveSourcePaths(buildSources(p))
}
func (b *Browser) BrowserName() string { return b.cfg.Name }
func (b *Browser) ProfileName() string { return b.profile.name }
func (b *Browser) UserDataDir() string { return b.cfg.UserDataDir }
func (b *Browser) ProfileDir() string {
if b.profile.isDefault() {
return b.profile.legacyHome
}
return filepath.Join(b.profile.container, "Safari", "Profiles", b.profile.uuidUpper)
}
func (b *Browser) Extract(categories []types.Category) (*types.BrowserData, error) {
session, err := filemanager.NewSession()
if err != nil {
return nil, err
}
defer session.Cleanup()
tempPaths := b.acquireFiles(session, categories)
data := &types.BrowserData{}
for _, cat := range categories {
// Keychain is user-scope, not per-profile — attribute only to default to avoid duplicates.
if cat == types.Password {
if b.profile.isDefault() {
b.extractCategory(data, cat, "")
}
continue
}
// Extension plists (AppExtensions + WebExtensions) live directly in the container
// and are read in-place; attribute to default only until per-profile layouts are verified.
if cat == types.Extension {
if b.profile.isDefault() {
b.extractCategory(data, cat, "")
}
continue
}
path, ok := tempPaths[cat]
if !ok {
continue
}
b.extractCategory(data, cat, path)
}
return data, nil
}
func (b *Browser) CountEntries(categories []types.Category) (map[types.Category]int, error) {
session, err := filemanager.NewSession()
if err != nil {
return nil, err
}
defer session.Cleanup()
tempPaths := b.acquireFiles(session, categories)
counts := make(map[types.Category]int)
for _, cat := range categories {
if cat == types.Password {
if b.profile.isDefault() {
counts[cat] = b.countCategory(cat, "")
}
continue
}
if cat == types.Extension {
if b.profile.isDefault() {
counts[cat] = b.countCategory(cat, "")
}
continue
}
path, ok := tempPaths[cat]
if !ok {
continue
}
counts[cat] = b.countCategory(cat, path)
}
return counts, nil
}
func (b *Browser) acquireFiles(session *filemanager.Session, categories []types.Category) map[types.Category]string {
tempPaths := make(map[types.Category]string)
for _, cat := range categories {
rp, ok := b.sourcePaths[cat]
if !ok {
continue
}
dst := filepath.Join(session.TempDir(), cat.String())
if err := session.Acquire(rp.absPath, dst, rp.isDir); err != nil {
log.Debugf("acquire %s: %v", cat, err)
continue
}
tempPaths[cat] = dst
}
return tempPaths
}
func (b *Browser) extractCategory(data *types.BrowserData, cat types.Category, path string) {
var err error
switch cat {
case types.Password:
data.Passwords, err = extractPasswords(b.keychainPassword)
case types.History:
data.Histories, err = extractHistories(path)
case types.Cookie:
data.Cookies, err = extractCookies(path)
case types.Bookmark:
data.Bookmarks, err = extractBookmarks(path)
case types.Download:
data.Downloads, err = extractDownloads(path, b.profile.downloadOwnerUUID())
case types.LocalStorage:
data.LocalStorage, err = extractLocalStorage(path)
case types.Extension:
data.Extensions, err = extractExtensions(b.profile.container)
default:
return
}
if err != nil {
log.Debugf("extract %s for %s: %v", cat, b.BrowserName()+"/"+b.ProfileName(), err)
}
}
func (b *Browser) countCategory(cat types.Category, path string) int {
var count int
var err error
switch cat {
case types.Password:
count, err = countPasswords(b.keychainPassword)
case types.History:
count, err = countHistories(path)
case types.Cookie:
count, err = countCookies(path)
case types.Bookmark:
count, err = countBookmarks(path)
case types.Download:
count, err = countDownloads(path, b.profile.downloadOwnerUUID())
case types.LocalStorage:
count, err = countLocalStorage(path)
case types.Extension:
count, err = countExtensions(b.profile.container)
default:
// Unsupported categories silently return 0.
}
if err != nil {
log.Debugf("count %s for %s: %v", cat, b.BrowserName()+"/"+b.ProfileName(), err)
}
return count
}
type resolvedPath struct {
absPath string
isDir bool
+28 -176
View File
@@ -59,17 +59,18 @@ func TestNewBrowsers(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
dir := tt.setup(t)
cfg := types.BrowserConfig{Name: "Safari", Kind: types.Safari, UserDataDir: dir}
browsers, err := NewBrowsers(cfg)
b, err := NewBrowser(cfg)
require.NoError(t, err)
if tt.wantLen == 0 {
assert.Empty(t, browsers)
assert.Nil(t, b)
return
}
require.Len(t, browsers, tt.wantLen)
assert.Equal(t, "Safari", browsers[0].BrowserName())
assert.Equal(t, "default", browsers[0].ProfileName())
assert.Equal(t, dir, browsers[0].ProfileDir())
require.NotNil(t, b)
assert.Equal(t, "Safari", b.BrowserName())
require.Len(t, b.Profiles(), 1)
assert.Equal(t, "default", b.Profiles()[0].Name)
assert.Equal(t, dir, b.Profiles()[0].Dir)
})
}
}
@@ -106,32 +107,33 @@ func TestNewBrowsers_MultiProfile(t *testing.T) {
})
cfg := types.BrowserConfig{Name: "Safari", Kind: types.Safari, UserDataDir: legacyHome}
browsers, err := NewBrowsers(cfg)
b, err := NewBrowser(cfg)
require.NoError(t, err)
require.Len(t, browsers, 2)
require.NotNil(t, b)
require.Len(t, b.profiles, 2)
names := []string{browsers[0].ProfileName(), browsers[1].ProfileName()}
names := []string{b.profiles[0].ctx.name, b.profiles[1].ctx.name}
assert.Contains(t, names, "default")
assert.Contains(t, names, "work")
for _, b := range browsers {
switch b.ProfileName() {
for _, p := range b.profiles {
switch p.ctx.name {
case "default":
assert.Equal(t, legacyHome, b.ProfileDir())
assert.Contains(t, b.sourcePaths, types.History)
assert.Equal(t, filepath.Join(legacyHome, "History.db"), b.sourcePaths[types.History].absPath)
assert.Equal(t, legacyHome, p.dir())
assert.Contains(t, p.sourcePaths, types.History)
assert.Equal(t, filepath.Join(legacyHome, "History.db"), p.sourcePaths[types.History].absPath)
// Default profile's LocalStorage root (WebsiteData/Default) isn't created in this fixture,
// so it won't resolve — which is the point: resolveSourcePaths only registers paths that exist.
assert.NotContains(t, b.sourcePaths, types.LocalStorage)
assert.NotContains(t, p.sourcePaths, types.LocalStorage)
case "work":
assert.Equal(t, filepath.Join(container, "Safari", "Profiles", uuid), b.ProfileDir())
assert.Contains(t, b.sourcePaths, types.History)
assert.Equal(t, filepath.Join(container, "Safari", "Profiles", uuid), p.dir())
assert.Contains(t, p.sourcePaths, types.History)
assert.Equal(t,
filepath.Join(container, "Safari", "Profiles", uuid, "History.db"),
b.sourcePaths[types.History].absPath)
require.Contains(t, b.sourcePaths, types.LocalStorage)
assert.Equal(t, namedOriginsDir, b.sourcePaths[types.LocalStorage].absPath)
assert.True(t, b.sourcePaths[types.LocalStorage].isDir)
p.sourcePaths[types.History].absPath)
require.Contains(t, p.sourcePaths, types.LocalStorage)
assert.Equal(t, namedOriginsDir, p.sourcePaths[types.LocalStorage].absPath)
assert.True(t, p.sourcePaths[types.LocalStorage].isDir)
}
}
}
@@ -174,166 +176,16 @@ func TestCountEntries(t *testing.T) {
require.NoError(t, err)
require.NoError(t, os.WriteFile(filepath.Join(dir, "History.db"), data, 0o644))
browsers, err := NewBrowsers(types.BrowserConfig{
b, err := NewBrowser(types.BrowserConfig{
Name: "Safari", Kind: types.Safari, UserDataDir: dir,
})
require.NoError(t, err)
require.Len(t, browsers, 1)
require.NotNil(t, b)
counts, err := browsers[0].CountEntries([]types.Category{types.History})
results, err := b.CountEntries([]types.Category{types.History})
require.NoError(t, err)
assert.Equal(t, 2, counts[types.History])
}
// ---------------------------------------------------------------------------
// countCategory / extractCategory
// ---------------------------------------------------------------------------
func TestCountCategory(t *testing.T) {
t.Run("History", func(t *testing.T) {
path := createTestDB(t, "History.db",
[]string{safariHistoryItemsSchema, safariHistoryVisitsSchema},
insertHistoryItem(1, "https://example.com", "example.com", 1),
)
b := &Browser{}
assert.Equal(t, 1, b.countCategory(types.History, path))
})
t.Run("Cookie", func(t *testing.T) {
path := buildTestBinaryCookies(t, []testCookie{
{domain: ".example.com", name: "a", path: "/", value: "1", expires: 2000000000.0, creation: 700000000.0},
{domain: ".go.dev", name: "b", path: "/", value: "2", expires: 2000000000.0, creation: 700000000.0},
})
b := &Browser{}
assert.Equal(t, 2, b.countCategory(types.Cookie, path))
})
t.Run("Bookmark", func(t *testing.T) {
path := buildTestBookmarksPlist(t, safariBookmark{
Type: bookmarkTypeList,
Children: []safariBookmark{
{Type: bookmarkTypeLeaf, URLString: "https://a.com", URIDictionary: uriDictionary{Title: "A"}},
{Type: bookmarkTypeLeaf, URLString: "https://b.com", URIDictionary: uriDictionary{Title: "B"}},
},
})
b := &Browser{}
assert.Equal(t, 2, b.countCategory(types.Bookmark, path))
})
t.Run("Download", func(t *testing.T) {
path := buildTestDownloadsPlist(t, safariDownloads{
DownloadHistory: []safariDownloadEntry{
{URL: "https://example.com/file.zip", Path: "/tmp/file.zip", TotalBytes: 100},
},
})
b := &Browser{}
assert.Equal(t, 1, b.countCategory(types.Download, path))
})
t.Run("LocalStorage", func(t *testing.T) {
dir := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://example.com": {{Key: "k1", Value: "v1"}, {Key: "k2", Value: "v2"}},
"https://go.dev": {{Key: "theme", Value: "dark"}},
})
b := &Browser{}
assert.Equal(t, 3, b.countCategory(types.LocalStorage, dir))
})
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"))
})
}
func TestExtractCategory(t *testing.T) {
t.Run("History", func(t *testing.T) {
path := createTestDB(t, "History.db",
[]string{safariHistoryItemsSchema, safariHistoryVisitsSchema},
insertHistoryItem(1, "https://example.com", "example.com", 3),
insertHistoryItem(2, "https://go.dev", "go.dev", 1),
insertHistoryVisit(1, 1, 700000000.0, "Example"),
insertHistoryVisit(2, 2, 700000000.0, "Go"),
)
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.History, path)
require.Len(t, data.Histories, 2)
// Sorted by visit count descending
assert.Equal(t, 3, data.Histories[0].VisitCount)
assert.Equal(t, 1, data.Histories[1].VisitCount)
})
t.Run("Cookie", func(t *testing.T) {
path := buildTestBinaryCookies(t, []testCookie{
{
domain: ".example.com", name: "session", path: "/", value: "abc",
secure: true, httpOnly: true, expires: 2000000000.0, creation: 700000000.0,
},
})
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.Cookie, path)
require.Len(t, data.Cookies, 1)
assert.Equal(t, ".example.com", data.Cookies[0].Host)
assert.Equal(t, "session", data.Cookies[0].Name)
assert.True(t, data.Cookies[0].IsSecure)
assert.True(t, data.Cookies[0].IsHTTPOnly)
})
t.Run("Bookmark", func(t *testing.T) {
path := buildTestBookmarksPlist(t, safariBookmark{
Type: bookmarkTypeList,
Children: []safariBookmark{
{Type: bookmarkTypeLeaf, URLString: "https://github.com", URIDictionary: uriDictionary{Title: "GitHub"}},
},
})
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.Bookmark, path)
require.Len(t, data.Bookmarks, 1)
assert.Equal(t, "GitHub", data.Bookmarks[0].Name)
assert.Equal(t, "https://github.com", data.Bookmarks[0].URL)
})
t.Run("Download", func(t *testing.T) {
path := buildTestDownloadsPlist(t, safariDownloads{
DownloadHistory: []safariDownloadEntry{
{URL: "https://example.com/file.zip", Path: "/tmp/file.zip", TotalBytes: 1024},
},
})
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.Download, path)
require.Len(t, data.Downloads, 1)
assert.Equal(t, "https://example.com/file.zip", data.Downloads[0].URL)
assert.Equal(t, int64(1024), data.Downloads[0].TotalBytes)
})
t.Run("LocalStorage", func(t *testing.T) {
dir := buildTestLocalStorageDir(t, map[string][]testLocalStorageItem{
"https://github.com": {{Key: "theme", Value: "dark"}},
})
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.LocalStorage, dir)
require.Len(t, data.LocalStorage, 1)
assert.Equal(t, "https://github.com", data.LocalStorage[0].URL)
assert.Equal(t, "theme", data.LocalStorage[0].Key)
assert.Equal(t, "dark", data.LocalStorage[0].Value)
})
t.Run("UnsupportedCategory", func(t *testing.T) {
b := &Browser{}
data := &types.BrowserData{}
b.extractCategory(data, types.CreditCard, "unused")
assert.Empty(t, data.CreditCards)
})
require.Len(t, results, 1)
assert.Equal(t, 2, results[0].Counts[types.History])
}
// Anchor: 2024-01-15T10:30:00Z, in seconds past the Core Data epoch (2001-01-01Z).