test: move compat tests into the packages they cover (#669)

The pipeline goldens now live in app, export in export, and keys.json in masterkey, each with its own
testdata; cmd keeps the flag and list goldens. Shared fixtures and golden helpers became the
internal/browser/browsertest and internal/golden packages. The binary is byte-identical.
This commit is contained in:
Roger authored and GitHub committed 2026-10-29 16:12:50 +08:00
1 parent c0a03769f1
commit 07b66adc4d
40 files changed
+190 -125

No files matched your search

+9
View File
@@ -179,6 +179,15 @@ linters:
- errcheck
- lll
- goconst
# Test-support packages used only by _test.go; same leniency as the tests they serve.
- path: internal/(golden|browser/browsertest)/
linters:
- dupl
- funlen
- gosec
- errcheck
- lll
- goconst
- source: "defer"
linters:
- errcheck
+3 -1
View File
@@ -8,10 +8,12 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/moond4rk/hackbrowserdata/internal/golden"
)
func TestFlagSurface(t *testing.T) {
assertGolden(t, "flags.golden", flagSurface(rootCmd()))
golden.Assert(t, "flags.golden", flagSurface(rootCmd()))
}
func flagSurface(root *cobra.Command) string {
+21
View File
@@ -0,0 +1,21 @@
package main
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/require"
)
// setHome points the commands' home directory at a fixture: HOME, and USERPROFILE, which Windows reads instead.
func setHome(t *testing.T, home string) {
t.Helper()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
}
func runCmd(t *testing.T, cmd *cobra.Command, args ...string) {
t.Helper()
cmd.SetArgs(args)
require.NoError(t, cmd.Execute())
}
+9 -7
View File
@@ -10,19 +10,21 @@ import (
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/browser/browsertest"
"github.com/moond4rk/hackbrowserdata/internal/golden"
)
func TestListGolden(t *testing.T) {
home := t.TempDir()
setHome(t, home)
var paths []string
for _, inst := range chromiumFixtureInstalls {
dir := userDataDirOf(t, home, inst.key)
chromiumFixtureUserData(t, dir, inst.key)
paths = append(paths, dir, "<"+inst.key+">")
for _, inst := range browsertest.ChromiumInstalls {
dir := userDataDirOf(t, home, inst.Key)
browsertest.ChromiumUserData(t, dir, inst.Key)
paths = append(paths, dir, "<"+inst.Key+">")
}
dir := userDataDirOf(t, home, "firefox")
firefoxFixtureUserData(t, dir)
browsertest.FirefoxUserData(t, dir)
paths = append(paths, dir, "<firefox>", `\`, "/")
assertListGoldens(t, "list", strings.NewReplacer(paths...))
}
@@ -34,7 +36,7 @@ func TestListGoldenSafari(t *testing.T) {
home := t.TempDir()
setHome(t, home)
library := filepath.Join(home, "Library")
safariFixtureLibrary(t, library)
browsertest.SafariLibrary(t, library)
assertListGoldens(t, "list-safari", strings.NewReplacer(library, "<library>"))
}
@@ -50,7 +52,7 @@ func assertListGoldens(t *testing.T, prefix string, paths *strings.Replacer) {
name, args = prefix+"-detail.golden", []string{"--detail"}
}
runCmd(t, cmd, args...)
assertGolden(t, name, paths.Replace(out.String()))
golden.Assert(t, name, paths.Replace(out.String()))
}
}
+3 -1
View File
@@ -10,12 +10,14 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/browser/browsertest"
)
func TestStoppedRunPrintsNoErrorOrUsage(t *testing.T) {
home := t.TempDir()
setHome(t, home)
chromiumFixtureUserData(t, userDataDirOf(t, home, "chrome"), "chrome")
browsertest.ChromiumUserData(t, userDataDirOf(t, home, "chrome"), "chrome")
for _, tc := range []struct {
name, browser string
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,4 +1,4 @@
package main
package app_test
import (
"archive/zip"
@@ -14,6 +14,9 @@ import (
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/app"
"github.com/moond4rk/hackbrowserdata/internal/browser/browsertest"
"github.com/moond4rk/hackbrowserdata/internal/golden"
"github.com/moond4rk/hackbrowserdata/internal/browser"
)
@@ -97,7 +100,7 @@ func TestArchiveGolden(t *testing.T) {
out := filepath.Join(t.TempDir(), "browser-data.zip")
n, err := app.Archive(context.Background(), app.Discover(req), req, out)
require.NoError(t, err)
assertGolden(t, tt.golden, fmt.Sprintf("entries=%d\n", n)+zipListing(t, out))
golden.Assert(t, tt.golden, fmt.Sprintf("entries=%d\n", n)+golden.ZipListing(t, out))
prefix := archiveLayoutKey + "/"
names := archiveLayoutEntryNames(t, out)
@@ -118,20 +121,22 @@ func TestArchiveGolden(t *testing.T) {
}
}
// archive on one host and restore on another, both through the CLI, write what dump writes.
// archive on one host and restore on another, through the pipeline, write what dump writes.
func TestArchiveRestoreRoundTrip(t *testing.T) {
home := t.TempDir()
setHome(t, home)
for _, inst := range chromiumFixtureInstalls {
def, ok := browser.Lookup(inst.key)
require.True(t, ok, inst.key)
chromiumFixtureUserData(t, filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0])), inst.key)
for _, inst := range browsertest.ChromiumInstalls {
def, ok := browser.Lookup(inst.Key)
require.True(t, ok, inst.Key)
browsertest.ChromiumUserData(t, filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0])), inst.Key)
}
req, err := app.ValidateArchive(app.Flags{Browser: "all", Categories: "all"})
require.NoError(t, err)
req.Home = home
archive := filepath.Join(t.TempDir(), "data.zip")
runCmd(t, archiveCmd(), "-o", archive)
_, err = app.Archive(context.Background(), app.Discover(req), req, archive)
require.NoError(t, err)
require.Contains(t, archiveLayoutEntryNames(t, archive), "manifest.json")
out := t.TempDir()
chromiumFixtureRestore(t, "--keys", chromiumFixtureKeysFile(t), "--data-zip", archive, "-d", out)
assert.Equal(t, readGolden(t, "extract-json.golden"), bundleDir(t, out))
out := restoreGolden(t, app.RestoreFlags{Keys: chromiumFixtureKeysFile(t), DataZip: archive, Dir: t.TempDir()})
assert.Equal(t, golden.Read(t, "extract-json.golden"), golden.Bundle(t, out))
}
@@ -1,4 +1,4 @@
package main
package app_test
import (
"context"
@@ -9,6 +9,9 @@ import (
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/app"
"github.com/moond4rk/hackbrowserdata/internal/browser/browsertest"
"github.com/moond4rk/hackbrowserdata/internal/golden"
"github.com/moond4rk/hackbrowserdata/internal/browser"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
)
@@ -17,12 +20,12 @@ import (
func TestExtractGolden(t *testing.T) {
home := t.TempDir()
keys := make(map[browser.DirID]masterkey.Set)
for _, inst := range chromiumFixtureInstalls {
def, ok := browser.Lookup(inst.key)
require.True(t, ok, inst.key)
require.Equal(t, inst.name, def.Name, "records are stamped with the table's display name")
chromiumFixtureUserData(t, filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0])), inst.key)
keys[browser.DirID(inst.key)] = chromiumFixtureKeys
for _, inst := range browsertest.ChromiumInstalls {
def, ok := browser.Lookup(inst.Key)
require.True(t, ok, inst.Key)
require.Equal(t, inst.Name, def.Name, "records are stamped with the table's display name")
browsertest.ChromiumUserData(t, filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0])), inst.Key)
keys[browser.DirID(inst.Key)] = browsertest.ChromiumKeys
}
assertExtractGoldens(t, "extract", func(t *testing.T, format string) string {
@@ -34,7 +37,7 @@ func TestExtractGoldenFirefox(t *testing.T) {
home := t.TempDir()
def, ok := browser.Lookup("firefox")
require.True(t, ok)
firefoxFixtureUserData(t, filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0])))
browsertest.FirefoxUserData(t, filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0])))
assertExtractGoldens(t, "extract-firefox", func(t *testing.T, format string) string {
return extractGoldenRun(t, home, nil, format)
@@ -46,7 +49,7 @@ func TestExtractGoldenSafari(t *testing.T) {
t.Skip("Safari is in the browser table on macOS only")
}
home := t.TempDir()
safariFixtureLibrary(t, filepath.Join(home, "Library"))
browsertest.SafariLibrary(t, filepath.Join(home, "Library"))
assertExtractGoldens(t, "extract-safari", func(t *testing.T, format string) string {
return extractGoldenRun(t, home, nil, format)
@@ -81,12 +84,12 @@ func assertExtractGoldens(t *testing.T, prefix string, run func(t *testing.T, fo
tt := tt
t.Run(tt.format, func(t *testing.T) {
out := run(t, tt.format)
got := bundleDir(t, out)
got := golden.Bundle(t, out)
if tt.jsonFallback {
assertJSONFallback(t, out, run(t, "json"))
got = bundleFiles(t, out, "cookie.json")
golden.AssertJSONFallback(t, out, run(t, "json"))
got = golden.BundleFiles(t, out, "cookie.json")
}
assertGolden(t, prefix+"-"+tt.format+".golden", got)
golden.Assert(t, prefix+"-"+tt.format+".golden", got)
})
}
}
@@ -1,7 +1,8 @@
package main
package app_test
import (
"bytes"
"context"
"os"
"path/filepath"
"testing"
@@ -10,6 +11,10 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/app"
"github.com/moond4rk/hackbrowserdata/internal/browser/browsertest"
"github.com/moond4rk/hackbrowserdata/internal/golden"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
"github.com/moond4rk/hackbrowserdata/internal/snapshot"
)
@@ -17,19 +22,18 @@ import (
// Restore writes what dump writes: the extract golden, stamped with each browser's display name.
func TestRestoreGolden(t *testing.T) {
data := t.TempDir()
chromiumFixtureInstallations(t, data)
browsertest.ChromiumInstallations(t, data)
keys := chromiumFixtureKeysFile(t)
want := readGolden(t, "extract-json.golden")
want := golden.Read(t, "extract-json.golden")
t.Run("data-dir", func(t *testing.T) {
out := t.TempDir()
chromiumFixtureRestore(t, "--keys", keys, "--data-dir", data, "-d", out)
assert.Equal(t, want, bundleDir(t, out))
out := restoreGolden(t, app.RestoreFlags{Keys: keys, DataDir: data, Dir: t.TempDir()})
assert.Equal(t, want, golden.Bundle(t, out))
})
t.Run("zip", func(t *testing.T) {
out := filepath.Join(t.TempDir(), "results")
chromiumFixtureRestore(t, "--keys", keys, "--data-dir", data, "-d", out, "--zip")
restoreGolden(t, app.RestoreFlags{Keys: keys, DataDir: data, Dir: out, Zip: true})
entries, err := os.ReadDir(out)
require.NoError(t, err)
@@ -37,11 +41,11 @@ func TestRestoreGolden(t *testing.T) {
require.Equal(t, "results.zip", entries[0].Name())
archive := filepath.Join(out, "results.zip")
assertGolden(t, "restore-zip.golden", zipListing(t, archive))
golden.Assert(t, "restore-zip.golden", golden.ZipListing(t, archive))
unzipped, err := snapshot.Unzip(archive)
require.NoError(t, err)
defer unzipped.Close()
assert.Equal(t, want, bundleDir(t, unzipped.Path()))
assert.Equal(t, want, golden.Bundle(t, unzipped.Path()))
})
// An archive written before the manifest is the bare <browser-key>/ layout, which the vaults resolve.
@@ -49,17 +53,16 @@ func TestRestoreGolden(t *testing.T) {
dataZip := filepath.Join(t.TempDir(), "data.zip")
require.NoError(t, snapshot.Open(data).Zip(dataZip))
out := t.TempDir()
chromiumFixtureRestore(t, "--keys", keys, "--data-zip", dataZip, "-d", out)
assert.Equal(t, want, bundleDir(t, out))
out := restoreGolden(t, app.RestoreFlags{Keys: keys, DataZip: dataZip, Dir: t.TempDir()})
assert.Equal(t, want, golden.Bundle(t, out))
})
}
func chromiumFixtureKeysFile(t *testing.T) string {
t.Helper()
profiles := make([]string, 0, len(chromiumFixtureProfiles))
for _, p := range chromiumFixtureProfiles {
profiles = append(profiles, p.dir)
profiles := make([]string, 0, len(browsertest.ChromiumProfiles))
for _, p := range browsertest.ChromiumProfiles {
profiles = append(profiles, p.Dir)
}
dump := masterkey.Dump{
// A literal rather than DumpVersion: keys files written by today's dumpkeys must keep restoring.
@@ -68,12 +71,12 @@ func chromiumFixtureKeysFile(t *testing.T) string {
Host: masterkey.Host{OS: "windows", Arch: "amd64"},
Vaults: []masterkey.Vault{},
}
for _, inst := range chromiumFixtureInstalls {
for _, inst := range browsertest.ChromiumInstalls {
dump.Vaults = append(dump.Vaults, masterkey.Vault{
Browser: inst.key,
Browser: inst.Key,
Kind: "chromium",
Profiles: profiles,
Keys: chromiumFixtureKeys,
Keys: browsertest.ChromiumKeys,
})
}
@@ -84,7 +87,29 @@ func chromiumFixtureKeysFile(t *testing.T) string {
return path
}
func chromiumFixtureRestore(t *testing.T, args ...string) {
// restoreGolden drives the restore pipeline the way the cobra command does, and returns the output directory.
func restoreGolden(t *testing.T, f app.RestoreFlags) string {
t.Helper()
runCmd(t, restoreCmd(), args...)
if f.Categories == "" {
f.Categories = "all"
}
if f.Format == "" {
f.Format = "json"
}
req, dump, err := app.ValidateRestore(f)
require.NoError(t, err)
data := snapshot.Open(f.DataDir)
if f.DataZip != "" {
data, err = snapshot.Unzip(f.DataZip)
require.NoError(t, err)
}
defer data.Close()
d, keys, err := app.RestoreDiscovery(data, dump, req)
require.NoError(t, err)
req.Keys = keys
_, err = app.Execute(context.Background(), app.NewPlan(d, req), req)
require.NoError(t, err)
return f.Dir
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,4 +1,4 @@
package main
package browsertest
import (
"database/sql"
@@ -1,4 +1,4 @@
package main
package browsertest
import (
"crypto/sha256"
@@ -20,42 +20,42 @@ import (
)
// Only GCM-sealed values (v10 under a 32-byte key, v20) decrypt identically on every OS.
var chromiumFixtureKeys = masterkey.Set{
var ChromiumKeys = masterkey.Set{
masterkey.V10: []byte("0123456789abcdef0123456789abcdef"),
masterkey.V20: []byte("0123456789abcdef"),
}
var chromiumFixtureInstalls = []struct {
key string
name string
var ChromiumInstalls = []struct {
Key string
Name string
}{
{key: "chrome", name: "Chrome"},
{key: "edge", name: "Microsoft Edge"},
{Key: "chrome", Name: "Chrome"},
{Key: "edge", Name: "Microsoft Edge"},
}
var chromiumFixtureProfiles = []struct {
dir string
var ChromiumProfiles = []struct {
Dir string
label string
start time.Time
accountLogins bool
}{
{dir: "Default", label: "home", start: time.Date(2024, 3, 1, 9, 0, 0, 0, time.UTC), accountLogins: true},
{dir: "Profile 1", label: "work", start: time.Date(2024, 6, 1, 9, 0, 0, 0, time.UTC)},
{Dir: "Default", label: "home", start: time.Date(2024, 3, 1, 9, 0, 0, 0, time.UTC), accountLogins: true},
{Dir: "Profile 1", label: "work", start: time.Date(2024, 6, 1, 9, 0, 0, 0, time.UTC)},
}
func chromiumFixtureInstallations(t *testing.T, root string) {
func ChromiumInstallations(t *testing.T, root string) {
t.Helper()
for _, inst := range chromiumFixtureInstalls {
chromiumFixtureUserData(t, filepath.Join(root, inst.key), inst.key)
for _, inst := range ChromiumInstalls {
ChromiumUserData(t, filepath.Join(root, inst.Key), inst.Key)
}
}
func chromiumFixtureUserData(t *testing.T, userDataDir, browserKey string) {
func ChromiumUserData(t *testing.T, userDataDir, browserKey string) {
t.Helper()
fixtureWrite(t, filepath.Join(userDataDir, "Local State"), "{}")
for _, p := range chromiumFixtureProfiles {
for _, p := range ChromiumProfiles {
site := p.label + "." + browserKey + ".test"
chromiumFixtureProfile(t, filepath.Join(userDataDir, p.dir), site, p.start, p.accountLogins)
chromiumFixtureProfile(t, filepath.Join(userDataDir, p.Dir), site, p.start, p.accountLogins)
}
}
@@ -64,10 +64,10 @@ func chromiumFixtureProfile(t *testing.T, dir, site string, start time.Time, acc
at := func(offset time.Duration) int64 { return chromiumFixtureMicros(start.Add(offset)) }
link := func(host, path string) string { return "https://" + host + "." + site + path }
v10 := func(plaintext string) string {
return chromiumFixtureSeal(t, "v10", chromiumFixtureKeys[masterkey.V10], plaintext)
return chromiumFixtureSeal(t, "v10", ChromiumKeys[masterkey.V10], plaintext)
}
v20 := func(plaintext string) string {
return chromiumFixtureSeal(t, "v20", chromiumFixtureKeys[masterkey.V20], plaintext)
return chromiumFixtureSeal(t, "v20", ChromiumKeys[masterkey.V20], plaintext)
}
// Chrome 130+ seals SHA256(host_key) ahead of the cookie value, which extraction must strip again.
hostBound := func(host, value string) string {
@@ -1,4 +1,4 @@
package main
package browsertest
import (
"crypto/sha256"
@@ -45,7 +45,7 @@ var (
oidDESEDE3CBC = asn1.ObjectIdentifier{1, 2, 840, 113549, 3, 7}
)
func firefoxFixtureUserData(t *testing.T, profilesDir string) {
func FirefoxUserData(t *testing.T, profilesDir string) {
t.Helper()
fixtureWrite(t, filepath.Join(profilesDir, "profiles.ini"), "[General]\nStartWithLastProfile=1\n")
fixtureWrite(t, filepath.Join(profilesDir, "Crash Reports", "InstallTime20240101000000"), "1704067200")
@@ -1,4 +1,4 @@
package main
package browsertest
import (
"bytes"
@@ -23,7 +23,7 @@ const (
var safariFixtureStart = time.Date(2024, 3, 1, 9, 0, 0, 0, time.UTC)
func safariFixtureLibrary(t *testing.T, library string) {
func SafariLibrary(t *testing.T, library string) {
t.Helper()
at := safariFixtureStart.Add
visit := func(offset time.Duration) float64 { return safariFixtureCoreData(at(offset)) }
@@ -1,4 +1,4 @@
package main
package export_test
import (
"archive/zip"
@@ -16,6 +16,7 @@ import (
"github.com/moond4rk/hackbrowserdata/internal/category"
"github.com/moond4rk/hackbrowserdata/internal/export"
"github.com/moond4rk/hackbrowserdata/internal/golden"
"github.com/moond4rk/hackbrowserdata/internal/record"
)
@@ -77,14 +78,14 @@ func TestExportGolden(t *testing.T) {
t.Run(tc.format, func(t *testing.T) {
dir := filepath.Join(t.TempDir(), "results")
exportWrite(t, dir, tc.format, false, exportStamps())
got := bundleDir(t, dir)
got := golden.Bundle(t, dir)
if tc.jsonFallback {
jsonDir := filepath.Join(t.TempDir(), "results")
exportWrite(t, jsonDir, "json", false, exportStamps())
assertJSONFallback(t, dir, jsonDir)
got = bundleFiles(t, dir, "cookie.json")
golden.AssertJSONFallback(t, dir, jsonDir)
got = golden.BundleFiles(t, dir, "cookie.json")
}
assertGolden(t, "export-"+tc.format+".golden", got)
golden.Assert(t, "export-"+tc.format+".golden", got)
assert.Equal(t, exportCategoryFiles(tc.ext, category.All()...), exportDirFiles(t, dir))
exportAssertFileContract(t, dir)
for _, form := range tc.urlForms {
@@ -105,8 +106,8 @@ func TestExportGolden(t *testing.T) {
assert.Equal(t, []string{"results.zip"}, exportDirFiles(t, dir))
archive := filepath.Join(dir, "results.zip")
assertGolden(t, "export-zip.golden", zipListing(t, archive))
assert.Equal(t, readDirFiles(t, loose), exportReadZip(t, archive))
golden.Assert(t, "export-zip.golden", golden.ZipListing(t, archive))
assert.Equal(t, golden.DirFiles(t, loose), exportReadZip(t, archive))
})
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,4 +1,5 @@
package main
// Package golden compares test output against files in the calling package's testdata/, which -update rewrites.
package golden
import (
"archive/zip"
@@ -10,28 +11,37 @@ import (
"testing"
"time"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var update = flag.Bool("update", false, "rewrite the golden files from the current code")
const goldenDir = "../../testdata"
const dir = "testdata"
func assertGolden(t *testing.T, name, got string) {
// Assert compares got with testdata/<name>, rewriting it first when -update is set.
func Assert(t testing.TB, name, got string) {
t.Helper()
path := filepath.Join(goldenDir, name)
path := filepath.Join(dir, name)
if *update {
require.NoError(t, os.MkdirAll(goldenDir, 0o755))
require.NoError(t, os.MkdirAll(dir, 0o755))
require.NoError(t, os.WriteFile(path, []byte(got), 0o644))
}
want, err := os.ReadFile(path)
require.NoError(t, err)
assert.Equal(t, string(want), got, "regenerate with: go test ./cmd/hack-browser-data -run '%s' -update", t.Name())
assert.Equal(t, string(want), got, "regenerate with: go test ./... -run '%s' -update", t.Name())
}
func bundleDir(t *testing.T, dir string) string {
// Read returns testdata/<name>.
func Read(t testing.TB, name string) string {
t.Helper()
data, err := os.ReadFile(filepath.Join(dir, name))
require.NoError(t, err)
return string(data)
}
// Bundle renders every regular file in dir as a named, length-tagged block, in directory order.
func Bundle(t testing.TB, dir string) string {
t.Helper()
entries, err := os.ReadDir(dir)
require.NoError(t, err)
@@ -40,10 +50,11 @@ func bundleDir(t *testing.T, dir string) string {
require.Truef(t, e.Type().IsRegular(), "unexpected non-regular entry %s in %s", e.Name(), dir)
names = append(names, e.Name())
}
return bundleFiles(t, dir, names...)
return BundleFiles(t, dir, names...)
}
func bundleFiles(t *testing.T, dir string, names ...string) string {
// BundleFiles renders the named files under dir as named, length-tagged blocks, in the given order.
func BundleFiles(t testing.TB, dir string, names ...string) string {
t.Helper()
var b strings.Builder
for _, name := range names {
@@ -58,7 +69,8 @@ func bundleFiles(t *testing.T, dir string, names ...string) string {
return b.String()
}
func readDirFiles(t *testing.T, dir string) map[string]string {
// DirFiles maps each file name under dir to its contents.
func DirFiles(t testing.TB, dir string) map[string]string {
t.Helper()
entries, err := os.ReadDir(dir)
require.NoError(t, err)
@@ -71,17 +83,19 @@ func readDirFiles(t *testing.T, dir string) map[string]string {
return files
}
// Cookie-Editor's own format covers cookies only; every other category is written exactly as the json format writes it.
func assertJSONFallback(t *testing.T, cookieEditorDir, jsonDir string) {
// AssertJSONFallback checks that Cookie-Editor output matches the json output for every category but cookies, whose
// own format Cookie-Editor alone writes.
func AssertJSONFallback(t testing.TB, cookieEditorDir, jsonDir string) {
t.Helper()
got, want := readDirFiles(t, cookieEditorDir), readDirFiles(t, jsonDir)
got, want := DirFiles(t, cookieEditorDir), DirFiles(t, jsonDir)
require.Contains(t, got, "cookie.json")
delete(got, "cookie.json")
delete(want, "cookie.json")
assert.Equal(t, want, got)
}
func zipListing(t *testing.T, path string) string {
// ZipListing renders a zip's entries as one deterministic line each: name, method, modified time, size, and CRC.
func ZipListing(t testing.TB, path string) string {
t.Helper()
r, err := zip.OpenReader(path)
require.NoError(t, err)
@@ -93,23 +107,3 @@ func zipListing(t *testing.T, path string) string {
}
return b.String()
}
func readGolden(t *testing.T, name string) string {
t.Helper()
data, err := os.ReadFile(filepath.Join(goldenDir, name))
require.NoError(t, err)
return string(data)
}
// setHome points the commands' home directory at a fixture: HOME, and USERPROFILE, which Windows reads instead.
func setHome(t *testing.T, home string) {
t.Helper()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
}
func runCmd(t *testing.T, cmd *cobra.Command, args ...string) {
t.Helper()
cmd.SetArgs(args)
require.NoError(t, cmd.Execute())
}
@@ -1,4 +1,4 @@
package main
package masterkey_test
import (
"bytes"
@@ -13,6 +13,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/golden"
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
)
@@ -103,7 +104,7 @@ func TestKeysGolden(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
var buf bytes.Buffer
require.NoError(t, tt.dump(t).WriteJSON(&buf))
assertGolden(t, tt.golden, buf.String())
golden.Assert(t, tt.golden, buf.String())
})
}
}
@@ -125,7 +126,7 @@ func TestKeysRead(t *testing.T) {
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
data, err := os.ReadFile(filepath.Join(goldenDir, tt.golden))
data, err := os.ReadFile(filepath.Join("testdata", tt.golden))
require.NoError(t, err)
if tt.edit != nil {
data = keysEditJSON(t, data, tt.edit)
File renamed without changes.
File renamed without changes.
+1 -1
View File
@@ -31,7 +31,7 @@ These guard the contract; a failure means the rewrite changed something users ca
Export and extractor tests compare against checked-in golden outputs. Deterministic ordering is what makes goldens stable — without it, goldens would flap. A `-update` flag regenerates them on intentional changes. Goldens double as documentation of the exact output shape, useful for the README and for talks.
The compatibility goldens — the flag sets, every output format, `keys.json`, and the archive layout — live in the repository-root `testdata/` and are read by the tests in `cmd/hack-browser-data`, the one package that never moves, so every phase compares its output against the same files. After the switch-over, Phase 8 moves each test and its goldens into the package it covers ([RFC-012](012-migration-roadmap.md)). A golden holds what no other golden already implies: the Cookie-Editor golden holds only `cookie.json`, because every other category is asserted to match the json output byte for byte, and `restore` is asserted to match the extract output except for its `browser` stamp.
Each golden lives beside the package whose output it pins: the export goldens in `export`, `keys.json` in `masterkey`, and the `extract`, `archive`, and `restore` goldens — all of which run the pipeline — in `app`; the flag-surface and `list` goldens stay in `cmd/hack-browser-data`. Shared fixture builders are in `internal/browser/browsertest`, the comparison helpers in `internal/golden`. A golden holds what no other golden already implies: the Cookie-Editor golden holds only `cookie.json`, because every other category is asserted to match the json output byte for byte, and `restore` is asserted to match the extract output except for its `browser` stamp.
## Regression tests for the known bugs
+1 -1
View File
@@ -66,7 +66,7 @@ Leaves first, bottom-up along the dependency arrows ([RFC-002](002-package-layou
### Phase 8 — Delete & document
- Remove the superseded top-level packages — `browser/`, `crypto/` except the ABE unit's `crypto/windows/`, `filemanager/`, `log/`, `masterkey/`, `output/`, `types/`, and `utils/` except `utils/injector` — once nothing references them.
- Move the compatibility tests out of `cmd/hack-browser-data` into the packages they cover — the export goldens into `export`, `keys.json` into `masterkey`, the archive into `snapshot`, the extract goldens into `browser/chromium`, `browser/firefox`, and `browser/safari`, and `restore` into `app` — each with its goldens in its own `testdata/`; `cmd` keeps the flag-surface and `list` tests, with their goldens in its own `testdata/`, and the root `testdata/` goes away.
- Move the compatibility tests out of `cmd/hack-browser-data` into the packages whose code they drive — the export goldens into `export`, `keys.json` into `masterkey`, and the `extract`, `archive`, and `restore` goldens, which all run the pipeline, into `app` — each with its goldens in its own `testdata/`. Shared fixture builders move to `internal/browser/browsertest` and the golden helpers to `internal/golden`. `cmd` keeps the flag-surface and `list` tests, the only ones that go through the cobra layer, with their goldens in its own `testdata/`, and the root `testdata/` goes away.
- Update CLAUDE.md (the new paths), the README (Safari, the `cookie-editor` behavior), and the root help text.
- Bring the RFCs to their finished state: the format and technique RFCs (013–021) move from today's type names — `BrowserConfig`, `WindowsABE`, `winutil.Table`, `Hints`, the `Retrievers` struct — to the names in [RFC-004](004-naming-decisions.md); the design RFCs describe the code as it is instead of comparing it with today's; and this roadmap is removed.
- Final `go mod tidy` under go@1.20; confirm the `go` directive is still `1.20` and `otiai10/copy` and `mint` are gone.