mirror of
https://github.com/moonD4rk/HackBrowserData.git
synced 2026-12-04 05:43:27 +01:00
* 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.
182 lines
6.3 KiB
Go
182 lines
6.3 KiB
Go
package snapshot
|
|
|
|
import (
|
|
"context"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
// userDataDir builds a Chromium-shaped source tree with a WAL database and a LevelDB directory.
|
|
func userDataDir(t *testing.T) string {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
for relativePath, data := range map[string]string{
|
|
"Local State": "{}",
|
|
"Default/Network/Cookies": "db",
|
|
"Default/Network/Cookies-wal": "wal",
|
|
"Default/Network/Cookies-shm": "shm",
|
|
"Default/Local Storage/leveldb/CURRENT": "MANIFEST-000001",
|
|
"Default/Local Storage/leveldb/LOCK": "",
|
|
"Default/Local Storage/leveldb/000003.log": "log",
|
|
} {
|
|
writeFile(t, filepath.Join(root, filepath.FromSlash(relativePath)), []byte(data))
|
|
}
|
|
return root
|
|
}
|
|
|
|
func TestTemp_IsPrivateAndCloseRemovesIt(t *testing.T) {
|
|
root, err := NewRoot()
|
|
require.NoError(t, err)
|
|
info, err := os.Stat(root.path)
|
|
require.NoError(t, err)
|
|
if runtime.GOOS != "windows" {
|
|
assert.Equal(t, fs.FileMode(0o700), info.Mode().Perm())
|
|
}
|
|
|
|
require.NoError(t, root.Close())
|
|
assert.NoDirExists(t, root.path)
|
|
}
|
|
|
|
func TestOpen_ReadsInPlaceAndCloseLeavesIt(t *testing.T) {
|
|
dir := t.TempDir()
|
|
writeFile(t, filepath.Join(dir, "chrome", "Local State"), []byte("{}"))
|
|
|
|
root := Open(dir)
|
|
capturedPath, err := root.Snapshot("chrome").Path("Local State")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(dir, "chrome", "Local State"), capturedPath)
|
|
|
|
require.NoError(t, root.Close())
|
|
assert.FileExists(t, capturedPath)
|
|
}
|
|
|
|
func TestCapture_FilesWithSidecarsAndDirsWithoutLocks(t *testing.T) {
|
|
from := userDataDir(t)
|
|
root, err := NewRoot()
|
|
require.NoError(t, err)
|
|
defer root.Close()
|
|
|
|
require.NoError(t, root.Capture(context.Background(), []File{
|
|
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Local State"},
|
|
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/Network/Cookies"},
|
|
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/Local Storage/leveldb", IsDir: true},
|
|
}))
|
|
|
|
dirSnapshot := root.Snapshot("chrome")
|
|
cookies, err := dirSnapshot.Path("Default/Network/Cookies")
|
|
require.NoError(t, err)
|
|
assert.Equal(t, filepath.Join(root.path, "chrome", "Default", "Network", "Cookies"), cookies)
|
|
for suffix, want := range map[string]string{"-wal": "wal", "-shm": "shm"} {
|
|
got, err := os.ReadFile(cookies + suffix)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, want, string(got))
|
|
}
|
|
|
|
leveldb, err := dirSnapshot.Path("Default/Local Storage/leveldb")
|
|
require.NoError(t, err)
|
|
assert.FileExists(t, filepath.Join(leveldb, "CURRENT"))
|
|
assert.FileExists(t, filepath.Join(leveldb, "000003.log"))
|
|
assert.NoFileExists(t, filepath.Join(leveldb, "LOCK"))
|
|
}
|
|
|
|
func TestCapture_KeepsGoingPastFailedFiles(t *testing.T) {
|
|
from := userDataDir(t)
|
|
root, err := NewRoot()
|
|
require.NoError(t, err)
|
|
defer root.Close()
|
|
|
|
require.NoError(t, root.Capture(context.Background(), []File{
|
|
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/History"}, // vanished before Capture reached it
|
|
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/Network"}, // a directory read as a file, so the copy fails
|
|
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Local State"},
|
|
}))
|
|
|
|
dirSnapshot := root.Snapshot("chrome")
|
|
_, err = dirSnapshot.Path("Default/History")
|
|
require.ErrorIs(t, err, fs.ErrNotExist)
|
|
|
|
_, err = dirSnapshot.Path("Default/Network")
|
|
require.Error(t, err)
|
|
require.NotErrorIs(t, err, fs.ErrNotExist, "a failed copy reads back as its own error")
|
|
|
|
_, err = dirSnapshot.Path("Local State")
|
|
require.NoError(t, err, "one file's failure does not stop the rest")
|
|
}
|
|
|
|
func TestCapture_StopsWhenCancelled(t *testing.T) {
|
|
from := userDataDir(t)
|
|
root, err := NewRoot()
|
|
require.NoError(t, err)
|
|
defer root.Close()
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
err = root.Capture(ctx, []File{{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Local State"}})
|
|
require.ErrorIs(t, err, context.Canceled)
|
|
|
|
_, err = root.Snapshot("chrome").Path("Local State")
|
|
require.ErrorIs(t, err, fs.ErrNotExist)
|
|
}
|
|
|
|
func TestCapture_StaysInsideItsOwnSnapshot(t *testing.T) {
|
|
from := userDataDir(t)
|
|
root, err := NewRoot()
|
|
require.NoError(t, err)
|
|
defer root.Close()
|
|
|
|
require.NoError(t, root.Capture(context.Background(), []File{
|
|
{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "../edge/Local State"},
|
|
{UserDataDirID: "../chrome", UserDataDirPath: from, RelativePath: "Local State"},
|
|
{UserDataDirID: "chrome/sub", UserDataDirPath: from, RelativePath: "Local State"},
|
|
{UserDataDirID: ".", UserDataDirPath: from, RelativePath: "Local State"},
|
|
{UserDataDirID: "", UserDataDirPath: from, RelativePath: "Local State"},
|
|
}))
|
|
|
|
_, err = root.Snapshot("chrome").Path("../edge/Local State")
|
|
require.Error(t, err)
|
|
entries, err := os.ReadDir(root.path)
|
|
require.NoError(t, err)
|
|
assert.Empty(t, entries, "nothing may be written outside a file's own snapshot")
|
|
}
|
|
|
|
func TestSnapshot_UncapturedFileIsNotExist(t *testing.T) {
|
|
root, err := NewRoot()
|
|
require.NoError(t, err)
|
|
defer root.Close()
|
|
|
|
_, err = root.Snapshot("chrome").Path("Default/Login Data")
|
|
require.ErrorIs(t, err, fs.ErrNotExist)
|
|
}
|
|
|
|
func TestCapture_ALaterCaptureReplacesAnEarlierFailure(t *testing.T) {
|
|
from := t.TempDir()
|
|
root, err := NewRoot()
|
|
require.NoError(t, err)
|
|
defer root.Close()
|
|
|
|
history := filepath.Join(from, "Default", "History")
|
|
require.NoError(t, os.MkdirAll(history, 0o700)) // a directory read as a file, so the copy fails
|
|
require.NoError(t, root.Capture(context.Background(), []File{{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/History"}}))
|
|
_, err = root.Snapshot("chrome").Path("Default/./History")
|
|
require.Error(t, err)
|
|
require.NotErrorIs(t, err, fs.ErrNotExist, "a failure is found under any spelling of the path")
|
|
|
|
require.NoError(t, os.Remove(history))
|
|
writeFile(t, history, []byte("history"))
|
|
require.NoError(t, root.Capture(context.Background(), []File{{UserDataDirID: "chrome", UserDataDirPath: from, RelativePath: "Default/./History"}}))
|
|
_, err = root.Snapshot("chrome").Path("Default/History")
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func writeFile(t *testing.T, path string, data []byte) {
|
|
t.Helper()
|
|
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755))
|
|
require.NoError(t, os.WriteFile(path, data, 0o600))
|
|
}
|