Files
HackBrowserData/internal/browser/extractor_test.go
T
Roger f36790d4dd refactor: pass secrets as values, fetched once per scope (#713)
* refactor: pass secrets as values, fetched once per scope

A master key travelled through five layers of function values and three
sync.Once guarded reuse; holding each secret where its scope lives shows
where it comes from. Only the keychain prompt still waits for first use.

* docs(rfcs): fetch secrets per extraction, not per record
2026-11-22 23:52:12 +08:00

80 lines
3.2 KiB
Go

package browser
import (
"context"
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/snapshot"
)
func writeSnapshotFile(t *testing.T, root, relativePath string) string {
t.Helper()
path := filepath.Join(root, filepath.FromSlash(relativePath))
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700))
require.NoError(t, os.WriteFile(path, []byte(relativePath), 0o600))
return path
}
func TestInputPath(t *testing.T) {
root := t.TempDir()
legacyCookies := writeSnapshotFile(t, root, "chrome/Default/Cookies")
localState := writeSnapshotFile(t, root, "chrome/Local State")
dirSnapshot := snapshot.Open(root).Snapshot("chrome")
input := NewInput(dirSnapshot, Profile{Name: "Default", RelativePath: "Default"}, nil, nil)
got, err := input.Path(Source{RelativePath: "Network/Cookies", Fallbacks: []string{"Cookies"}})
require.NoError(t, err)
assert.Equal(t, legacyCookies, got, "a candidate that was never captured is skipped")
got, err = input.Path(Source{RelativePath: "Local State", Shared: true})
require.NoError(t, err)
assert.Equal(t, localState, got, "a shared source resolves under the user data dir, not the profile")
_, err = input.Path(Source{RelativePath: "History"})
require.ErrorIs(t, err, fs.ErrNotExist)
}
func TestInputPath_FlatLayoutProfile(t *testing.T) {
root := t.TempDir()
history := writeSnapshotFile(t, root, "opera/History")
input := NewInput(snapshot.Open(root).Snapshot("opera"), Profile{Name: "Opera Stable"}, nil, nil)
got, err := input.Path(Source{RelativePath: "History"})
require.NoError(t, err)
assert.Equal(t, history, got)
}
// TestInputPath_FailedCopyIsReported makes a failed copy an error, not a reason to read a lower candidate.
func TestInputPath_FailedCopyIsReported(t *testing.T) {
from := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(from, "Default", "Network", "Cookies"), 0o700))
writeSnapshotFile(t, from, "Default/Cookies")
root, err := snapshot.NewRoot()
require.NoError(t, err)
defer func() { require.NoError(t, root.Close()) }()
require.NoError(t, root.Capture(context.Background(), []snapshot.File{
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/Network/Cookies"},
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/Cookies"},
}))
input := NewInput(root.Snapshot("chrome"), Profile{Name: "Default", RelativePath: "Default"}, nil, nil)
_, err = input.Path(Source{RelativePath: "Network/Cookies", Fallbacks: []string{"Cookies"}})
require.Error(t, err)
assert.NotErrorIs(t, err, fs.ErrNotExist)
}
func TestSourceCandidatePaths(t *testing.T) {
profile := Profile{Name: "Default", RelativePath: "Default"}
assert.Equal(t, []string{"Default/Network/Cookies", "Default/Cookies"}, Source{RelativePath: "Network/Cookies", Fallbacks: []string{"Cookies"}}.CandidatePaths(profile))
assert.Equal(t, []string{"Local State"}, Source{RelativePath: "Local State", Shared: true}.CandidatePaths(profile))
assert.Equal(t, []string{"History"}, Source{RelativePath: "History"}.CandidatePaths(Profile{Name: "default"}),
"a user data dir that is itself the profile")
}