Files
HackBrowserData/internal/browser/extractor_test.go
T
Roger 3e9150249c refactor: rename core types and spell out every name (#677)
Def, Request, DirID, and Source said too little, and short variables and doc comments that skip their name hid intent.
varnamelen and staticcheck ST1020-22 now keep both from coming back.
2026-11-02 11:06:05 +08:00

95 lines
3.8 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/masterkey"
"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 TestProfileSnapshotPath(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")
profileSnapshot := NewProfileSnapshot(dirSnapshot, Profile{Name: "Default", Dir: "Default"}, nil)
got, err := profileSnapshot.Path(FileSpec{Paths: []string{"Network/Cookies", "Cookies"}})
require.NoError(t, err)
assert.Equal(t, legacyCookies, got, "a candidate that was never captured is skipped")
got, err = profileSnapshot.Path(FileSpec{Paths: []string{"Local State"}, Shared: true})
require.NoError(t, err)
assert.Equal(t, localState, got, "a shared file spec resolves under the user data dir, not the profile")
_, err = profileSnapshot.Path(FileSpec{Paths: []string{"History"}})
require.ErrorIs(t, err, fs.ErrNotExist)
}
func TestProfileSnapshotPath_FlatLayoutProfile(t *testing.T) {
root := t.TempDir()
history := writeSnapshotFile(t, root, "opera/History")
profileSnapshot := NewProfileSnapshot(snapshot.Open(root).Snapshot("opera"), Profile{Name: "Opera Stable"}, nil)
got, err := profileSnapshot.Path(FileSpec{Paths: []string{"History"}})
require.NoError(t, err)
assert.Equal(t, history, got)
}
// TestProfileSnapshotPath_FailedCopyIsReported makes a failed copy an error, not a reason to read a lower candidate.
func TestProfileSnapshotPath_FailedCopyIsReported(t *testing.T) {
from := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(from, "Default", "Network", "Cookies"), 0o700))
writeSnapshotFile(t, from, "Default/Cookies")
snapshotDir, err := snapshot.Temp()
require.NoError(t, err)
defer func() { require.NoError(t, snapshotDir.Close()) }()
require.NoError(t, snapshotDir.Capture(context.Background(), []snapshot.File{
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/Network/Cookies"},
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/Cookies"},
}))
profileSnapshot := NewProfileSnapshot(snapshotDir.Snapshot("chrome"), Profile{Name: "Default", Dir: "Default"}, nil)
_, err = profileSnapshot.Path(FileSpec{Paths: []string{"Network/Cookies", "Cookies"}})
require.Error(t, err)
assert.NotErrorIs(t, err, fs.ErrNotExist)
}
func TestProfileSnapshotMasterKeys(t *testing.T) {
dirSnapshot := snapshot.Open(t.TempDir()).Snapshot("chrome")
assert.Nil(t, NewProfileSnapshot(dirSnapshot, Profile{}, nil).MasterKeys(context.Background()))
calls := 0
want := masterkey.Set{masterkey.V10: []byte("k")}
profileSnapshot := NewProfileSnapshot(dirSnapshot, Profile{}, func(context.Context) masterkey.Set {
calls++
return want
})
assert.Equal(t, want, profileSnapshot.MasterKeys(context.Background()))
assert.Equal(t, 1, calls)
}
func TestFileSpec_CandidatePaths(t *testing.T) {
profile := Profile{Name: "Default", Dir: "Default"}
assert.Equal(t, []string{"Default/Network/Cookies", "Default/Cookies"}, FileSpec{Paths: []string{"Network/Cookies", "Cookies"}}.CandidatePaths(profile))
assert.Equal(t, []string{"Local State"}, FileSpec{Paths: []string{"Local State"}, Shared: true}.CandidatePaths(profile))
assert.Equal(t, []string{"History"}, FileSpec{Paths: []string{"History"}}.CandidatePaths(Profile{Name: "default"}),
"a user data dir that is itself the profile")
}