Files
Roger a734e777a8 refactor(app): export one function per command (#712)
* refactor(app): export one function per command

Step 2 of #710. A command's logic was split between cmd and stage-shaped app calls; cmd now parses flags, calls one function and prints what it returns. An error means the command never ran, and a run that ended early says so in Summary.Err.

* fix(app): check dumpkeys and archive -o up front

A missing output directory was reported only after the keys were retrieved or the files copied.

* docs(rfcs): move restore's input out of the command layer

Restore now discovers its directories in an archive itself, so the command layer no longer holds the data it reads.
2026-11-22 14:45:39 +08:00

118 lines
3.5 KiB
Go

package app_test
import (
"archive/zip"
"context"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/moond4rk/hackbrowserdata/internal/app"
"github.com/moond4rk/hackbrowserdata/internal/golden"
"github.com/moond4rk/hackbrowserdata/internal/browser"
)
const archiveLayoutBrowser = "chrome"
var archiveLayoutProfileFiles = []string{
"Preferences",
"Login Data",
"Network/Cookies",
"History",
"Bookmarks",
"Web Data",
"Secure Preferences",
"Local Storage/leveldb/000003.log",
"Local Storage/leveldb/CURRENT",
"Local Storage/leveldb/LOCK",
"Local Storage/leveldb/MANIFEST-000001",
"Session Storage/000004.log",
"Session Storage/CURRENT",
"Session Storage/LOCK",
"Session Storage/MANIFEST-000001",
}
var archiveLayoutExtraFiles = []string{
"Local State",
"Default/Login Data For Account",
"Default/Network/Cookies-wal",
"Default/Network/Cookies-shm",
"Default/Favicons",
"System Profile/Preferences",
"System Profile/History",
}
func archiveLayoutUserData(t *testing.T, root string) {
t.Helper()
files := append([]string{}, archiveLayoutExtraFiles...)
for _, profile := range []string{"Default", "Profile 1"} {
for _, relativePath := range archiveLayoutProfileFiles {
files = append(files, profile+"/"+relativePath)
}
}
for _, relativePath := range files {
absolutePath := filepath.Join(root, filepath.FromSlash(relativePath))
require.NoError(t, os.MkdirAll(filepath.Dir(absolutePath), 0o755))
require.NoError(t, os.WriteFile(absolutePath, []byte(relativePath), 0o600))
}
}
func archiveLayoutEntryNames(t *testing.T, zipPath string) []string {
t.Helper()
zipReader, err := zip.OpenReader(zipPath)
require.NoError(t, err)
defer zipReader.Close()
names := make([]string, 0, len(zipReader.File))
for _, zipFile := range zipReader.File {
names = append(names, zipFile.Name)
}
return names
}
func TestArchiveGolden(t *testing.T) {
home := t.TempDir()
namedBrowser, ok := browser.Lookup(archiveLayoutBrowser)
require.True(t, ok)
archiveLayoutUserData(t, filepath.Join(home, filepath.FromSlash(namedBrowser.Platform().UserDataDirPaths[0])))
tests := []struct {
name string
golden string
categories string
}{
{name: "all", golden: "archive-all.golden", categories: "all"},
{name: "cookie", golden: "archive-cookie.golden", categories: "cookie"},
}
for _, testCase := range tests {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
out := filepath.Join(t.TempDir(), "browser-data.zip")
options, err := app.ValidateArchive(app.ArchiveFlags{Browser: archiveLayoutBrowser, Categories: testCase.categories, OutputPath: out})
require.NoError(t, err)
options.HomeDir = home
_, fileCount, err := app.Archive(context.Background(), options)
require.NoError(t, err)
golden.Assert(t, testCase.golden, fmt.Sprintf("entries=%d\n", fileCount)+golden.ZipListing(t, out))
prefix := archiveLayoutBrowser + "/"
names := archiveLayoutEntryNames(t, out)
for _, name := range names {
assert.Truef(t, strings.HasPrefix(name, prefix), "entry %q lacks the %q prefix", name, prefix)
assert.NotContains(t, name, `\`)
assert.NotEqual(t, "LOCK", path.Base(name))
assert.Falsef(t, strings.HasPrefix(name, prefix+"System Profile/"), "entry %q comes from a skipped profile", name)
}
assert.Contains(t, names, prefix+"Default/Network/Cookies")
assert.Contains(t, names, prefix+"Default/Network/Cookies-wal")
assert.NotContains(t, names, prefix+"Default/Favicons")
})
}
}