mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
refactor: delete the superseded top-level packages (#667)
After the switch-over only two cmd tests used browser/, filemanager/, types/, and utils/fileutil. The list test now compares against goldens of the output it proved equal to the old code's, and the export test keeps its own fixture records; the binaries built before and after are byte-identical.
This commit is contained in:
1 parent
bbd353f314
commit
a794674bd7
53 files changed
+85
-6653
No files matched your search
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/internal/app"
|
||||
ibrowser "github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
)
|
||||
|
||||
const archiveLayoutKey = "chrome"
|
||||
@@ -76,7 +76,7 @@ func archiveLayoutEntryNames(t *testing.T, zipPath string) []string {
|
||||
|
||||
func TestArchiveGolden(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
def, ok := ibrowser.Lookup(archiveLayoutKey)
|
||||
def, ok := browser.Lookup(archiveLayoutKey)
|
||||
require.True(t, ok)
|
||||
archiveLayoutUserData(t, filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0])))
|
||||
|
||||
@@ -123,7 +123,7 @@ func TestArchiveRestoreRoundTrip(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
setHome(t, home)
|
||||
for _, inst := range chromiumFixtureInstalls {
|
||||
def, ok := ibrowser.Lookup(inst.key)
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ import (
|
||||
"github.com/moond4rk/hackbrowserdata/internal/category"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/export"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/record"
|
||||
"github.com/moond4rk/hackbrowserdata/types"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -29,7 +28,19 @@ const (
|
||||
type exportStamp struct {
|
||||
browser string
|
||||
profile string
|
||||
data *types.BrowserData
|
||||
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 {
|
||||
@@ -106,7 +117,7 @@ func exportWrite(t *testing.T, dir, format string, compress bool, stamps []expor
|
||||
w, err := export.NewWriter(dir, f, compress)
|
||||
require.NoError(t, err)
|
||||
for _, s := range stamps {
|
||||
require.NoError(t, writeResult(w, s.browser, types.ExtractResult{Profile: types.Profile{Name: s.profile}, Data: s.data}))
|
||||
require.NoError(t, writeStamp(w, s))
|
||||
}
|
||||
require.NoError(t, w.Close())
|
||||
}
|
||||
@@ -194,7 +205,7 @@ func exportAssertURLForm(t *testing.T, dir string, form exportURLForm) {
|
||||
// Entries are listed in extractor sort order with unique sort keys, so re-sorting a profile cannot change a golden.
|
||||
func exportStamps() []exportStamp {
|
||||
return []exportStamp{
|
||||
{browser: "Chrome", profile: "Default", data: &types.BrowserData{
|
||||
{browser: "Chrome", profile: "Default", data: &exportData{
|
||||
Passwords: []record.Login{
|
||||
{
|
||||
URL: "https://accounts.example.com/signin?continue=/mail&hl=ja", Username: "alice@example.com",
|
||||
@@ -261,7 +272,7 @@ func exportStamps() []exportStamp {
|
||||
{URL: "https://app.example.com", Key: "tab-id", Value: "42"},
|
||||
},
|
||||
}},
|
||||
{browser: "Chrome", profile: "Profile 1", data: &types.BrowserData{
|
||||
{browser: "Chrome", profile: "Profile 1", data: &exportData{
|
||||
Passwords: []record.Login{
|
||||
{
|
||||
URL: "http://192.168.0.1:8080/admin", Username: "admin", Password: " leading space",
|
||||
@@ -313,7 +324,7 @@ func exportStamps() []exportStamp {
|
||||
{URL: "https://example.org/?x=1&y=<2>", Key: "draft", Value: `He said "hi", then left \`},
|
||||
},
|
||||
}},
|
||||
{browser: "Microsoft Edge", profile: "Default", data: &types.BrowserData{
|
||||
{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: "日本語パスワード",
|
||||
@@ -368,27 +379,26 @@ func exportStamps() []exportStamp {
|
||||
|
||||
func exportSparseStamps() []exportStamp {
|
||||
return []exportStamp{
|
||||
{browser: "Chrome", profile: "Default", data: &types.BrowserData{
|
||||
{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: &types.BrowserData{
|
||||
{browser: "Microsoft Edge", profile: "Default", data: &exportData{
|
||||
Histories: []record.History{},
|
||||
LocalStorage: []record.LocalStorage{{URL: "https://example.com", Key: "k", Value: "v"}},
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
// writeResult writes the export fixtures, which are kept in today's BrowserData until the goldens move into export.
|
||||
func writeResult(w *export.Writer, browserName string, r types.ExtractResult) error {
|
||||
if r.Data == nil {
|
||||
func writeStamp(w *export.Writer, s exportStamp) error {
|
||||
if s.data == nil {
|
||||
return nil
|
||||
}
|
||||
for _, c := range category.All() {
|
||||
records := profileRecords(r.Data, c)
|
||||
records := profileRecords(s.data, c)
|
||||
for _, rec := range records {
|
||||
record.Stamp(rec, browserName, r.Name)
|
||||
record.Stamp(rec, s.browser, s.profile)
|
||||
}
|
||||
if err := w.Write(c, records); err != nil {
|
||||
return err
|
||||
@@ -397,7 +407,7 @@ func writeResult(w *export.Writer, browserName string, r types.ExtractResult) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func profileRecords(d *types.BrowserData, c category.Category) []record.Record {
|
||||
func profileRecords(d *exportData, c category.Category) []record.Record {
|
||||
switch c {
|
||||
case category.Password:
|
||||
return recordsOf(d.Passwords)
|
||||
|
||||
@@ -9,20 +9,20 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/internal/app"
|
||||
ibrowser "github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/masterkey"
|
||||
)
|
||||
|
||||
// The extract goldens pin dump's output on fixtures; the fixtures' keys are handed in, since no secret store holds them.
|
||||
func TestExtractGolden(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
keys := make(map[ibrowser.DirID]masterkey.Set)
|
||||
keys := make(map[browser.DirID]masterkey.Set)
|
||||
for _, inst := range chromiumFixtureInstalls {
|
||||
def, ok := ibrowser.Lookup(inst.key)
|
||||
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[ibrowser.DirID(inst.key)] = chromiumFixtureKeys
|
||||
keys[browser.DirID(inst.key)] = chromiumFixtureKeys
|
||||
}
|
||||
|
||||
assertExtractGoldens(t, "extract", func(t *testing.T, format string) string {
|
||||
@@ -32,7 +32,7 @@ func TestExtractGolden(t *testing.T) {
|
||||
|
||||
func TestExtractGoldenFirefox(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
def, ok := ibrowser.Lookup("firefox")
|
||||
def, ok := browser.Lookup("firefox")
|
||||
require.True(t, ok)
|
||||
firefoxFixtureUserData(t, filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0])))
|
||||
|
||||
@@ -53,7 +53,7 @@ func TestExtractGoldenSafari(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func extractGoldenRun(t *testing.T, home string, keys map[ibrowser.DirID]masterkey.Set, format string) string {
|
||||
func extractGoldenRun(t *testing.T, home string, keys map[browser.DirID]masterkey.Set, format string) string {
|
||||
t.Helper()
|
||||
out := t.TempDir()
|
||||
req, err := app.Validate(app.Flags{Browser: "all", Categories: "all", Format: format, Dir: out})
|
||||
|
||||
@@ -2,129 +2,61 @@ package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/moond4rk/hackbrowserdata/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/browser/chromium"
|
||||
"github.com/moond4rk/hackbrowserdata/browser/firefox"
|
||||
"github.com/moond4rk/hackbrowserdata/browser/safari"
|
||||
ibrowser "github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/category"
|
||||
"github.com/moond4rk/hackbrowserdata/types"
|
||||
"github.com/moond4rk/hackbrowserdata/internal/browser"
|
||||
)
|
||||
|
||||
// list prints what today's code printed on the same machine, its counts included; the comparison goes with the old
|
||||
// packages.
|
||||
func TestListMatchesToday(t *testing.T) {
|
||||
func TestListGolden(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
setHome(t, home)
|
||||
var paths []string
|
||||
for _, inst := range chromiumFixtureInstalls {
|
||||
chromiumFixtureUserData(t, userDataDirOf(t, home, inst.key), inst.key)
|
||||
dir := userDataDirOf(t, home, inst.key)
|
||||
chromiumFixtureUserData(t, dir, inst.key)
|
||||
paths = append(paths, dir, "<"+inst.key+">")
|
||||
}
|
||||
firefoxFixtureUserData(t, userDataDirOf(t, home, "firefox"))
|
||||
if runtime.GOOS == "darwin" {
|
||||
safariFixtureLibrary(t, filepath.Join(home, "Library"))
|
||||
}
|
||||
today := todayBrowsers(t, home)
|
||||
require.NotEmpty(t, today)
|
||||
dir := userDataDirOf(t, home, "firefox")
|
||||
firefoxFixtureUserData(t, dir)
|
||||
paths = append(paths, dir, "<firefox>", `\`, "/")
|
||||
assertListGoldens(t, "list", strings.NewReplacer(paths...))
|
||||
}
|
||||
|
||||
func TestListGoldenSafari(t *testing.T) {
|
||||
if runtime.GOOS != "darwin" {
|
||||
t.Skip("Safari is in the browser table on macOS only")
|
||||
}
|
||||
home := t.TempDir()
|
||||
setHome(t, home)
|
||||
library := filepath.Join(home, "Library")
|
||||
safariFixtureLibrary(t, library)
|
||||
assertListGoldens(t, "list-safari", strings.NewReplacer(library, "<library>"))
|
||||
}
|
||||
|
||||
// The goldens hold the fixture's paths as placeholders, so one golden serves every OS.
|
||||
func assertListGoldens(t *testing.T, prefix string, paths *strings.Replacer) {
|
||||
t.Helper()
|
||||
for _, detail := range []bool{false, true} {
|
||||
var out bytes.Buffer
|
||||
cmd := listCmd()
|
||||
cmd.SetOut(&out)
|
||||
args := []string{}
|
||||
name, args := prefix+".golden", []string{}
|
||||
if detail {
|
||||
args = append(args, "--detail")
|
||||
name, args = prefix+"-detail.golden", []string{"--detail"}
|
||||
}
|
||||
runCmd(t, cmd, args...)
|
||||
assert.Equalf(t, todayList(t, today, detail), out.String(), "--detail=%v", detail)
|
||||
assertGolden(t, name, paths.Replace(out.String()))
|
||||
}
|
||||
}
|
||||
|
||||
func userDataDirOf(t *testing.T, home, id string) string {
|
||||
t.Helper()
|
||||
def, ok := ibrowser.Lookup(id)
|
||||
def, ok := browser.Lookup(id)
|
||||
require.True(t, ok, id)
|
||||
return filepath.Join(home, filepath.FromSlash(def.Platform().UserDataDirs[0]))
|
||||
}
|
||||
|
||||
// todayBrowsers finds the fixture's browsers as today's discovery did: each table row's directory under home, kept
|
||||
// when its base finds a profile there.
|
||||
func todayBrowsers(t *testing.T, home string) []browser.Browser {
|
||||
t.Helper()
|
||||
var out []browser.Browser
|
||||
for _, def := range ibrowser.Supported() {
|
||||
cfg := types.BrowserConfig{Key: def.ID, Name: def.Name, UserDataDir: home + "/" + def.Platform().UserDataDirs[0]}
|
||||
var (
|
||||
b browser.Browser
|
||||
err error
|
||||
)
|
||||
switch def.Base {
|
||||
case ibrowser.Chromium:
|
||||
cfg.Kind = types.Chromium
|
||||
var c *chromium.Browser
|
||||
if c, err = chromium.NewBrowser(cfg); c != nil {
|
||||
b = c
|
||||
}
|
||||
case ibrowser.Firefox:
|
||||
cfg.Kind = types.Firefox
|
||||
var f *firefox.Browser
|
||||
if f, err = firefox.NewBrowser(cfg); f != nil {
|
||||
b = f
|
||||
}
|
||||
case ibrowser.Safari:
|
||||
cfg.Kind = types.Safari
|
||||
var s *safari.Browser
|
||||
if s, err = safari.NewBrowser(cfg); s != nil {
|
||||
b = s
|
||||
}
|
||||
}
|
||||
require.NoError(t, err, def.ID)
|
||||
if b != nil {
|
||||
out = append(out, b)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// todayList prints as today's list command did.
|
||||
func todayList(t *testing.T, browsers []browser.Browser, detail bool) string {
|
||||
t.Helper()
|
||||
var buf bytes.Buffer
|
||||
w := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', 0)
|
||||
if !detail {
|
||||
fmt.Fprintln(w, "Browser\tProfile\tPath")
|
||||
for _, b := range browsers {
|
||||
for _, p := range b.Profiles() {
|
||||
fmt.Fprintf(w, "%s\t%s\t%s\n", b.BrowserName(), p.Name, p.Dir)
|
||||
}
|
||||
}
|
||||
require.NoError(t, w.Flush())
|
||||
return buf.String()
|
||||
}
|
||||
fmt.Fprint(w, "Browser\tProfile")
|
||||
for _, c := range category.All() {
|
||||
fmt.Fprintf(w, "\t%s", c.String())
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
for _, b := range browsers {
|
||||
results, err := b.CountEntries(category.All())
|
||||
require.NoError(t, err)
|
||||
for _, r := range results {
|
||||
fmt.Fprintf(w, "%s\t%s", b.BrowserName(), r.Name)
|
||||
for _, c := range category.All() {
|
||||
fmt.Fprintf(w, "\t%d", r.Counts[c])
|
||||
}
|
||||
fmt.Fprintln(w)
|
||||
}
|
||||
}
|
||||
require.NoError(t, w.Flush())
|
||||
return buf.String()
|
||||
}
|
||||
Reference in new issue
Block a user