Files
HackBrowserData/cmd/hack-browser-data/password_test.go
T
Roger 7e6f325508 refactor(app): per-stage inputs and per-dir planning (#689)
* refactor(app): per-command flags and per-stage inputs

One Options bag fed every stage and cmd patched restore's keys into it after validation; each stage now takes what it
reads, dir-level failures are their own type, and cmd prints what a run exported.

* refactor(app): group the plan by dir and profile

Execute now walks the plan's own shape instead of filtering a flat list and looking profiles up by name.

* refactor(app): file code by command and stage

Pure moves: each command's flags and validation sit beside its stage, and execute.go keeps only the runner.

* refactor: read the keychain password in cmd

Prompting and reading stdin are terminal work, so app now receives the password func; cobra already rejects both flags.

* refactor(app): plan each user data dir right before it runs

A run-wide Plan had to drop its extractors and rebuild them with the keychain; per-dir planning keeps one extractor.
2026-11-09 17:25:36 +08:00

59 lines
1.8 KiB
Go

package main
import (
"context"
"errors"
"io"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestKeychainPasswordFunc(t *testing.T) {
tests := []struct {
name string
flags keychainFlags
stdin string
want string
err error
}{
{name: "flag", flags: keychainFlags{password: "from-flag"}, stdin: "from-stdin\n", want: "from-flag"},
{name: "stdin", flags: keychainFlags{passwordStdin: true}, stdin: "from-stdin\r\nnext\n", want: "from-stdin"},
{name: "stdin without newline", flags: keychainFlags{passwordStdin: true}, stdin: "from-stdin", want: "from-stdin"},
{name: "none off a terminal", stdin: "from-stdin\n", err: errNoPassword},
}
for _, testCase := range tests {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
pipeReader, pipeWriter, err := os.Pipe()
require.NoError(t, err)
defer pipeReader.Close()
_, err = pipeWriter.WriteString(testCase.stdin)
require.NoError(t, errors.Join(err, pipeWriter.Close()))
got, err := keychainPasswordFunc(testCase.flags, pipeReader)(context.Background())
require.ErrorIs(t, err, testCase.err)
assert.Equal(t, testCase.want, got)
})
}
}
// TestKeychainPasswordFlagsAreExclusive covers the root command too, which runs dump with dump's flags.
func TestKeychainPasswordFlagsAreExclusive(t *testing.T) {
setHome(t, t.TempDir())
out := t.TempDir()
for _, args := range [][]string{
{"-d", out, "--keychain-pw", "pw", "--keychain-pw-stdin"},
{"dump", "-d", out, "--keychain-pw", "pw", "--keychain-pw-stdin"},
{"dumpkeys", "--keychain-pw", "pw", "--keychain-pw-stdin"},
} {
root := newRootCommand()
root.SetOut(io.Discard)
root.SetErr(io.Discard)
root.SetArgs(args)
assert.ErrorContains(t, root.Execute(), "keychain-pw", args)
}
}