mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
Names say what each thing is: no package stutter, one word per concept, spelled out, none that differ only in case. The browser table puts one field per line. Wire names (flags, output fields, keys.json) are unchanged.
446 lines
15 KiB
Go
446 lines
15 KiB
Go
package export_test
|
|
|
|
import (
|
|
"archive/zip"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/moond4rk/hackbrowserdata/internal/category"
|
|
"github.com/moond4rk/hackbrowserdata/internal/export"
|
|
"github.com/moond4rk/hackbrowserdata/internal/golden"
|
|
"github.com/moond4rk/hackbrowserdata/internal/record"
|
|
)
|
|
|
|
const (
|
|
exportUTF8BOM = "\xEF\xBB\xBF"
|
|
exportURLWithHTMLChars = "https://shop.example.com/cart?item=42&qty=<3>"
|
|
exportURLWithHTMLCharsEscaped = `https://shop.example.com/cart?item=42\u0026qty=\u003c3\u003e`
|
|
)
|
|
|
|
type profileExport struct {
|
|
browser string
|
|
profile string
|
|
data *exportData
|
|
}
|
|
|
|
type exportData struct {
|
|
Passwords []record.Login
|
|
Cookies []record.Cookie
|
|
Histories []record.History
|
|
Downloads []record.Download
|
|
Bookmarks []record.Bookmark
|
|
CreditCards []record.CreditCard
|
|
Extensions []record.Extension
|
|
LocalStorage []record.LocalStorage
|
|
SessionStorage []record.SessionStorage
|
|
}
|
|
|
|
type exportURLForm struct {
|
|
file string
|
|
escaped bool
|
|
}
|
|
|
|
func TestExportGolden(t *testing.T) {
|
|
formats := []struct {
|
|
format string
|
|
fileExtension string
|
|
urlForms []exportURLForm
|
|
jsonFallback bool
|
|
}{
|
|
{
|
|
format: "csv",
|
|
fileExtension: "csv",
|
|
urlForms: []exportURLForm{{file: "cookie.csv"}, {file: "history.csv"}},
|
|
},
|
|
{
|
|
format: "json",
|
|
fileExtension: "json",
|
|
urlForms: []exportURLForm{{file: "cookie.json", escaped: true}, {file: "history.json", escaped: true}},
|
|
},
|
|
{
|
|
format: "cookie-editor",
|
|
fileExtension: "json",
|
|
urlForms: []exportURLForm{{file: "cookie.json"}, {file: "history.json", escaped: true}},
|
|
jsonFallback: true,
|
|
},
|
|
}
|
|
for _, testCase := range formats {
|
|
testCase := testCase
|
|
t.Run(testCase.format, func(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "results")
|
|
exportWrite(t, dir, testCase.format, false, profileExports())
|
|
got := golden.ConcatDir(t, dir)
|
|
if testCase.jsonFallback {
|
|
jsonDir := filepath.Join(t.TempDir(), "results")
|
|
exportWrite(t, jsonDir, "json", false, profileExports())
|
|
golden.AssertJSONFallback(t, dir, jsonDir)
|
|
got = golden.ConcatFiles(t, dir, "cookie.json")
|
|
}
|
|
golden.Assert(t, "export-"+testCase.format+".golden", got)
|
|
assert.Equal(t, exportCategoryFiles(testCase.fileExtension, category.All()...), exportDirFiles(t, dir))
|
|
exportAssertFileContract(t, dir)
|
|
for _, form := range testCase.urlForms {
|
|
exportAssertURLForm(t, dir, form)
|
|
}
|
|
|
|
sparse := filepath.Join(t.TempDir(), "sparse")
|
|
exportWrite(t, sparse, testCase.format, false, sparseProfileExports())
|
|
assert.Equal(t, exportCategoryFiles(testCase.fileExtension, category.Password, category.LocalStorage), exportDirFiles(t, sparse))
|
|
})
|
|
}
|
|
|
|
t.Run("zip", func(t *testing.T) {
|
|
loose := filepath.Join(t.TempDir(), "results")
|
|
exportWrite(t, loose, "csv", false, profileExports())
|
|
dir := filepath.Join(t.TempDir(), "results")
|
|
exportWrite(t, dir, "csv", true, profileExports())
|
|
|
|
assert.Equal(t, []string{"results.zip"}, exportDirFiles(t, dir))
|
|
archive := filepath.Join(dir, "results.zip")
|
|
golden.Assert(t, "export-zip.golden", golden.ZipListing(t, archive))
|
|
assert.Equal(t, golden.DirFiles(t, loose), exportReadZip(t, archive))
|
|
})
|
|
}
|
|
|
|
func exportWrite(t *testing.T, dir, format string, zipOutput bool, exports []profileExport) {
|
|
t.Helper()
|
|
parsedFormat, err := export.ParseFormat(format)
|
|
require.NoError(t, err)
|
|
writer, err := export.NewWriter(dir, parsedFormat, zipOutput)
|
|
require.NoError(t, err)
|
|
for _, batch := range exports {
|
|
require.NoError(t, writeProfileExport(writer, batch))
|
|
}
|
|
require.NoError(t, writer.Close())
|
|
}
|
|
|
|
func exportCategoryFiles(fileExtension string, categories ...category.Category) []string {
|
|
names := make([]string, 0, len(categories))
|
|
for _, dataCategory := range categories {
|
|
names = append(names, dataCategory.String()+"."+fileExtension)
|
|
}
|
|
sort.Strings(names)
|
|
return names
|
|
}
|
|
|
|
func exportDirFiles(t *testing.T, dir string) []string {
|
|
t.Helper()
|
|
entries, err := os.ReadDir(dir)
|
|
require.NoError(t, err)
|
|
names := make([]string, 0, len(entries))
|
|
for _, entry := range entries {
|
|
names = append(names, entry.Name())
|
|
}
|
|
return names
|
|
}
|
|
|
|
func exportReadFile(t *testing.T, dir, name string) string {
|
|
t.Helper()
|
|
data, err := os.ReadFile(filepath.Join(dir, name))
|
|
require.NoError(t, err)
|
|
return string(data)
|
|
}
|
|
|
|
func exportReadZip(t *testing.T, path string) map[string]string {
|
|
t.Helper()
|
|
zipReader, err := zip.OpenReader(path)
|
|
require.NoError(t, err)
|
|
defer zipReader.Close()
|
|
entries := make(map[string]string, len(zipReader.File))
|
|
for _, zipFile := range zipReader.File {
|
|
entries[zipFile.Name] = exportReadZipEntry(t, zipFile)
|
|
}
|
|
return entries
|
|
}
|
|
|
|
func exportReadZipEntry(t *testing.T, zipFile *zip.File) string {
|
|
t.Helper()
|
|
entryReader, err := zipFile.Open()
|
|
require.NoError(t, err)
|
|
defer entryReader.Close()
|
|
data, err := io.ReadAll(entryReader)
|
|
require.NoError(t, err)
|
|
return string(data)
|
|
}
|
|
|
|
func exportAssertFileContract(t *testing.T, dir string) {
|
|
t.Helper()
|
|
for _, name := range exportDirFiles(t, dir) {
|
|
content := exportReadFile(t, dir, name)
|
|
switch filepath.Ext(name) {
|
|
case ".csv":
|
|
assert.Truef(t, strings.HasPrefix(content, exportUTF8BOM), "%s must start with the UTF-8 BOM", name)
|
|
case ".json":
|
|
assert.Falsef(t, strings.HasPrefix(content, exportUTF8BOM), "%s must not start with a BOM", name)
|
|
default:
|
|
t.Errorf("unexpected output file %s", name)
|
|
}
|
|
if runtime.GOOS != "windows" {
|
|
info, err := os.Stat(filepath.Join(dir, name))
|
|
require.NoError(t, err)
|
|
assert.Equalf(t, os.FileMode(0o600), info.Mode().Perm(), "%s permissions", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func exportAssertURLForm(t *testing.T, dir string, form exportURLForm) {
|
|
t.Helper()
|
|
present, absent := exportURLWithHTMLChars, exportURLWithHTMLCharsEscaped
|
|
if form.escaped {
|
|
present, absent = absent, present
|
|
}
|
|
content := exportReadFile(t, dir, form.file)
|
|
assert.Containsf(t, content, present, "%s", form.file)
|
|
assert.NotContainsf(t, content, absent, "%s", form.file)
|
|
}
|
|
|
|
// profileExports lists entries in sort order with unique sort keys, so re-sorting a profile cannot change a golden.
|
|
func profileExports() []profileExport {
|
|
return []profileExport{
|
|
{browser: "Chrome", profile: "Default", data: &exportData{
|
|
Passwords: []record.Login{
|
|
{
|
|
URL: "https://accounts.example.com/signin?continue=/mail&hl=ja", Username: "alice@example.com",
|
|
Password: "pa$$ \"quoted\", comma\\backslash\nnewline",
|
|
CreatedAt: time.Date(2026, 3, 14, 15, 9, 26, 535897932, time.UTC), Store: record.StoreAccount,
|
|
},
|
|
{
|
|
URL: "https://例え.jp/ログイン", Username: "日本語ユーザー", Password: "🔑 emoji",
|
|
CreatedAt: time.Date(2025, 11, 5, 6, 7, 8, 0, time.UTC), Store: record.StoreLocal,
|
|
},
|
|
},
|
|
Cookies: []record.Cookie{
|
|
{
|
|
Host: ".example.com", Path: "/", Name: "return_to", Value: exportURLWithHTMLChars,
|
|
IsSecure: true, IsHTTPOnly: true, HasExpire: true, IsPersistent: true,
|
|
ExpireAt: time.Date(2027, 6, 30, 12, 34, 56, 789000000, time.UTC),
|
|
CreatedAt: time.Date(2026, 3, 14, 15, 9, 27, 100000000, time.UTC), SameSite: "lax",
|
|
},
|
|
{
|
|
Host: "www.example.com", Path: "/account", Name: "session_id", Value: `{"user":"日本語 🍪","dir":"C:\Temp"}`,
|
|
IsHTTPOnly: true, CreatedAt: time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC), SameSite: "strict",
|
|
},
|
|
},
|
|
Histories: []record.History{
|
|
{
|
|
URL: exportURLWithHTMLChars, Title: `Cart "deals" <b>&</b>, 日本語 🛒`, VisitCount: 42,
|
|
LastVisit: time.Date(2026, 3, 14, 15, 10, 0, 1000, time.UTC),
|
|
},
|
|
{URL: "file:///C:/Users/Alice/notes.html", Title: "multi\nline title", VisitCount: 3},
|
|
},
|
|
Downloads: []record.Download{
|
|
{
|
|
URL: "https://dl.example.com/files/big.iso?token=a&sig=<x>",
|
|
TargetPath: `C:\Users\Alice\Downloads\Ubuntu "final", v2.iso`,
|
|
MimeType: "application/x-iso9660-image", TotalBytes: 5368709121,
|
|
StartTime: time.Date(2026, 3, 14, 15, 0, 0, 250000000, time.UTC),
|
|
EndTime: time.Date(2026, 3, 14, 15, 30, 0, 750000000, time.UTC),
|
|
},
|
|
},
|
|
Bookmarks: []record.Bookmark{
|
|
{
|
|
ID: 7, Name: "Docs & <Specs>", Type: "url", URL: "https://docs.example.com/search?a=1&b=<2>", Folder: "Bookmarks bar",
|
|
CreatedAt: time.Date(2026, 2, 2, 2, 2, 2, 20202020, time.UTC),
|
|
},
|
|
{ID: 4294967298, Name: "日本語 📚", Type: "url", URL: "https://例え.jp/", Folder: "Other bookmarks"},
|
|
},
|
|
CreditCards: []record.CreditCard{
|
|
{
|
|
GUID: "00000000-0000-4000-8000-000000000001", Name: "山田 太郎", Number: "4111111111111111",
|
|
ExpMonth: "12", ExpYear: "2030", NickName: `Work "primary", 💳`, Address: "1-2-3 Chiyoda,\nTokyo 100-0001",
|
|
},
|
|
},
|
|
Extensions: []record.Extension{
|
|
{
|
|
Name: "Ad Blocker", ID: "abcdefghijklmnopabcdefghijklmnop", Description: `Blocks ads & trackers, "fast" <b>`,
|
|
Version: "1.58.0", HomepageURL: "https://adblock.example/?a=1&b=<2>", Enabled: true,
|
|
},
|
|
},
|
|
LocalStorage: []record.LocalStorage{
|
|
{URL: "https://app.example.com", Key: "prefs", Value: `{"theme":"dark","greeting":"こんにちは 👋","dir":"C:\Temp"}`},
|
|
{IsMeta: true, URL: "https://app.example.com", Value: "meta data, value bytes is [8 128 1]"},
|
|
},
|
|
SessionStorage: []record.SessionStorage{
|
|
{URL: "https://app.example.com", Key: "tab-id", Value: "42"},
|
|
},
|
|
}},
|
|
{browser: "Chrome", profile: "Profile 1", data: &exportData{
|
|
Passwords: []record.Login{
|
|
{
|
|
URL: "http://192.168.0.1:8080/admin", Username: "admin", Password: " leading space",
|
|
CreatedAt: time.Date(2025, 6, 1, 0, 0, 0, 1, time.UTC), Store: record.StoreLocal,
|
|
},
|
|
{URL: "android://hash@com.example.app/", Store: record.StoreLocal},
|
|
},
|
|
Cookies: []record.Cookie{
|
|
{
|
|
Host: ".tracker.example", Path: "/", Name: "_ga", Value: "GA1.2.1234567890.1700000000",
|
|
IsSecure: true, HasExpire: true, IsPersistent: true,
|
|
ExpireAt: time.Date(2028, 2, 29, 23, 59, 59, 0, time.UTC),
|
|
CreatedAt: time.Date(2025, 5, 5, 5, 5, 5, 123456789, time.UTC), SameSite: "none",
|
|
},
|
|
{Host: ".dev.example", Path: "/dev", Name: "multi_line", Value: "line one\nline two, with comma", SameSite: "unspecified"},
|
|
},
|
|
Histories: []record.History{
|
|
{
|
|
URL: "https://search.example.com/?q=a%26b&lang=日本語", VisitCount: 1000000,
|
|
LastVisit: time.Date(2025, 7, 7, 7, 7, 7, 700000000, time.UTC),
|
|
},
|
|
{URL: "about:blank", Title: " leading space", LastVisit: time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)},
|
|
},
|
|
Downloads: []record.Download{
|
|
{
|
|
URL: "https://例え.jp/資料.pdf", TargetPath: "/home/alice/Downloads/資料 📄.pdf", MimeType: "application/pdf",
|
|
StartTime: time.Date(2025, 8, 1, 8, 0, 0, 0, time.UTC),
|
|
},
|
|
},
|
|
Bookmarks: []record.Bookmark{
|
|
{
|
|
ID: 1, Name: `Quote "this", please`, Type: "url", URL: "https://example.org/a,b", Folder: "multi\nline folder",
|
|
CreatedAt: time.Date(2025, 3, 3, 3, 3, 3, 0, time.UTC),
|
|
},
|
|
},
|
|
CreditCards: []record.CreditCard{
|
|
{
|
|
GUID: "00000000-0000-4000-8000-000000000002", Name: "Alice O'Brien", Number: "5555555555554444",
|
|
ExpMonth: "1", ExpYear: "2027", CVC: "123", Comment: `note <b>&</b> \ end`,
|
|
},
|
|
},
|
|
Extensions: []record.Extension{
|
|
{Name: "日本語拡張 🧩", ID: "ponmlkjihgfedcbaponmlkjihgfedcba", Description: "line one\nline two", Version: "0.0.1"},
|
|
},
|
|
LocalStorage: []record.LocalStorage{
|
|
{URL: "https://example.org", Key: "a&b<c>", Value: "multi\nline"},
|
|
},
|
|
SessionStorage: []record.SessionStorage{
|
|
{URL: "https://example.org/?x=1&y=<2>", Key: "draft", Value: `He said "hi", then left \`},
|
|
},
|
|
}},
|
|
{browser: "Microsoft Edge", profile: "Default", data: &exportData{
|
|
Passwords: []record.Login{
|
|
{
|
|
URL: "https://login.example.org/<tenant>/authorize?client=a&scope=b", Username: `CORP\jdoe`, Password: "日本語パスワード",
|
|
CreatedAt: time.Date(2024, 2, 29, 12, 0, 0, 500000000, time.UTC), Store: record.StoreAccount,
|
|
},
|
|
},
|
|
Cookies: []record.Cookie{
|
|
{
|
|
Host: ".edge.example", Path: "/", Name: "epoch_expiry", Value: `back\slash "quoted" <b>&</b>`,
|
|
IsSecure: true, IsHTTPOnly: true, HasExpire: true, IsPersistent: true,
|
|
ExpireAt: time.Unix(0, 0).UTC(), CreatedAt: time.Date(2024, 12, 24, 18, 0, 0, 0, time.UTC),
|
|
},
|
|
{
|
|
Host: "edge.example", Path: "/", Name: "host_only", Value: "v", HasExpire: true, IsPersistent: true,
|
|
ExpireAt: time.Date(2026, 12, 31, 23, 59, 59, 999999999, time.UTC),
|
|
CreatedAt: time.Date(2024, 6, 15, 9, 30, 0, 0, time.UTC), SameSite: "strict",
|
|
},
|
|
},
|
|
Histories: []record.History{
|
|
{
|
|
URL: "edge://settings/?a=1&b=2", Title: `C:\Windows\System32`, VisitCount: 7,
|
|
LastVisit: time.Date(2024, 7, 4, 0, 0, 0, 500000000, time.UTC),
|
|
},
|
|
},
|
|
Downloads: []record.Download{
|
|
{URL: "blob:https://app.example/1234", TargetPath: `\\fileserver\share\Q3 summary.csv`, TotalBytes: 4294967296},
|
|
},
|
|
Bookmarks: []record.Bookmark{
|
|
{
|
|
ID: 12, Name: `C:\Shares\docs`, Type: "url", URL: "https://edge.example/", Folder: "Favorites bar",
|
|
CreatedAt: time.Date(2024, 4, 4, 4, 4, 4, 400000000, time.UTC),
|
|
},
|
|
},
|
|
CreditCards: []record.CreditCard{
|
|
{GUID: "00000000-0000-4000-8000-000000000003"},
|
|
},
|
|
Extensions: []record.Extension{
|
|
{
|
|
Name: "Edge Helper", ID: "aaaabbbbccccddddeeeeffffgggghhhh", Description: `Installed under C:\Program Files\Helper`,
|
|
Version: "2.0", HomepageURL: "https://helper.example/", Enabled: true,
|
|
},
|
|
},
|
|
LocalStorage: []record.LocalStorage{
|
|
{URL: "https://edge.example", Key: "日本語"},
|
|
},
|
|
SessionStorage: []record.SessionStorage{
|
|
{IsMeta: true, URL: "https://edge.example", Value: "meta data, value bytes is [16 2]"},
|
|
},
|
|
}},
|
|
}
|
|
}
|
|
|
|
func sparseProfileExports() []profileExport {
|
|
return []profileExport{
|
|
{browser: "Chrome", profile: "Default", data: &exportData{
|
|
Passwords: []record.Login{{URL: "https://example.com/", Username: "alice", Store: record.StoreLocal}},
|
|
Cookies: []record.Cookie{},
|
|
}},
|
|
{browser: "Chrome", profile: "Profile 1"},
|
|
{browser: "Microsoft Edge", profile: "Default", data: &exportData{
|
|
Histories: []record.History{},
|
|
LocalStorage: []record.LocalStorage{{URL: "https://example.com", Key: "k", Value: "v"}},
|
|
}},
|
|
}
|
|
}
|
|
|
|
func writeProfileExport(writer *export.Writer, batch profileExport) error {
|
|
if batch.data == nil {
|
|
return nil
|
|
}
|
|
for _, dataCategory := range category.All() {
|
|
records := profileRecords(batch.data, dataCategory)
|
|
for _, row := range records {
|
|
row.SetBrowserAndProfile(batch.browser, batch.profile)
|
|
}
|
|
if err := writer.Write(dataCategory, records); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func profileRecords(data *exportData, dataCategory category.Category) []record.Record {
|
|
switch dataCategory {
|
|
case category.Password:
|
|
return recordsOf(data.Passwords)
|
|
case category.Cookie:
|
|
return recordsOf(data.Cookies)
|
|
case category.Bookmark:
|
|
return recordsOf(data.Bookmarks)
|
|
case category.History:
|
|
return recordsOf(data.Histories)
|
|
case category.Download:
|
|
return recordsOf(data.Downloads)
|
|
case category.CreditCard:
|
|
return recordsOf(data.CreditCards)
|
|
case category.Extension:
|
|
return recordsOf(data.Extensions)
|
|
case category.LocalStorage:
|
|
return recordsOf(data.LocalStorage)
|
|
case category.SessionStorage:
|
|
return recordsOf(data.SessionStorage)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func recordsOf[T any, P interface {
|
|
*T
|
|
record.Record
|
|
}](items []T) []record.Record {
|
|
records := make([]record.Record, len(items))
|
|
for i := range items {
|
|
records[i] = P(&items[i])
|
|
}
|
|
return records
|
|
}
|