Files
Roger 9e9d1c531d refactor: rename identifiers and expand the browser table (#685)
Names say what each thing is: no package stutter, one word per concept, spelled out, none that differ only in case.
The browser table puts one field per line. Wire names (flags, output fields, keys.json) are unchanged.
2026-11-09 17:25:35 +08:00

62 lines
1.5 KiB
Go

//go:build windows
package snapshot
import (
"os"
"path/filepath"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCopyPath_FallsBackToTheLockedRead(t *testing.T) {
from := filepath.Join(t.TempDir(), "cookies.db")
testData := []byte("cookie data")
require.NoError(t, os.WriteFile(from, testData, 0o644))
handle := openExclusive(t, from)
defer syscall.CloseHandle(handle)
into := filepath.Join(t.TempDir(), "cookies.db")
require.NoError(t, copyPath(from, into, false), "copyPath should succeed via the locked-file fallback")
copied, err := os.ReadFile(into)
require.NoError(t, err)
assert.Equal(t, testData, copied)
}
func TestCopyPath_PlainWhenNotLocked(t *testing.T) {
from := filepath.Join(t.TempDir(), "unlocked.db")
testData := []byte("unlocked data")
require.NoError(t, os.WriteFile(from, testData, 0o644))
into := filepath.Join(t.TempDir(), "unlocked.db")
require.NoError(t, copyPath(from, into, false))
copied, err := os.ReadFile(into)
require.NoError(t, err)
assert.Equal(t, testData, copied)
}
// openExclusive uses share mode 0, which locks the file the way Chrome's locking_mode=EXCLUSIVE does.
func openExclusive(t *testing.T, path string) syscall.Handle {
t.Helper()
srcPtr, err := syscall.UTF16PtrFromString(path)
require.NoError(t, err)
handle, err := syscall.CreateFile(
srcPtr,
syscall.GENERIC_READ|syscall.GENERIC_WRITE,
0,
nil,
syscall.OPEN_EXISTING,
syscall.FILE_ATTRIBUTE_NORMAL,
0,
)
require.NoError(t, err)
return handle
}