aboutsummaryrefslogtreecommitdiffstats
path: root/dec
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2025-10-11 19:30:46 -0700
committerRose Hogenson <rosehogenson@posteo.net>2025-10-11 19:30:46 -0700
commit69060292c43fec008d2f7f084625703b42a82fb9 (patch)
tree064acc3824f2f2d2dac62a88a2b5f7ba24debaad /dec
parentAdd a -f flag to overwrite, and update tests (diff)
downloadsym-69060292c43fec008d2f7f084625703b42a82fb9.tar.zst
Move some tests around
Diffstat (limited to 'dec')
-rw-r--r--dec/dec.go50
-rw-r--r--dec/dec_test.go96
2 files changed, 3 insertions, 143 deletions
diff --git a/dec/dec.go b/dec/dec.go
index 049bc74..2414bfb 100644
--- a/dec/dec.go
+++ b/dec/dec.go
@@ -3,60 +3,16 @@ package main
import (
"flag"
"fmt"
- "io"
"os"
- "golang.org/x/term"
"roseh.moe/cmd/sym/internal/sym"
)
-type options struct {
- password string
- force bool
-
- stdin io.Reader
- stdout io.Writer
-}
-
-func (o *options) dec(args ...string) error {
- if o.stdin == nil {
- o.stdin = os.Stdin
- }
- if o.stdout == nil {
- o.stdout = os.Stdout
- }
- if len(args) == 0 && o.password == "" {
- return fmt.Errorf("-p is required when reading from stdin")
- }
- var password string
- if o.password != "" {
- password = o.password
- } else {
- fmt.Fprint(os.Stderr, "Enter password: ")
- pw, err := term.ReadPassword(int(os.Stdin.Fd()))
- fmt.Fprintln(os.Stderr)
- if err != nil {
- return err
- }
- password = string(pw)
- }
- if len(args) == 0 {
- return sym.Decrypt(o.stdout, o.stdin, password)
- }
- for _, fileName := range args {
- if err := sym.DecryptFile(fileName, password, sym.Force(o.force)); err != nil {
- return err
- }
- }
- return nil
-}
-
func main() {
- o := new(options)
- flag.StringVar(&o.password, "p", "", "use the specified password; if not provided, dec will prompt for a password")
- flag.BoolVar(&o.force, "f", false, "overwrite output files even if they already exist")
+ o := sym.DefaultDecryptOptions
+ o.RegisterFlags(flag.CommandLine)
flag.Parse()
- if err := o.dec(flag.Args()...); err != nil {
+ if err := o.Run(flag.Args()...); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
diff --git a/dec/dec_test.go b/dec/dec_test.go
deleted file mode 100644
index 823ab68..0000000
--- a/dec/dec_test.go
+++ /dev/null
@@ -1,96 +0,0 @@
-package main
-
-import (
- "bytes"
- "os"
- "path/filepath"
- "testing"
-
- "roseh.moe/cmd/sym/internal/sym"
-)
-
-func mustWriteFile(t *testing.T, path string, content []byte) {
- t.Helper()
- if err := os.WriteFile(path, content, 0600); err != nil {
- t.Fatalf("Failed to write test file: %s", err)
- }
-}
-
-func mustReadFile(t *testing.T, path string) []byte {
- t.Helper()
- content, err := os.ReadFile(path)
- if err != nil {
- t.Fatalf("Failed to read file: %s", err)
- }
- return content
-}
-
-func mustRemove(t *testing.T, path string) {
- t.Helper()
- if err := os.Remove(path); err != nil {
- t.Fatalf("Failed to remove file: %s", err)
- }
-}
-
-func TestDec(t *testing.T) {
- t.Parallel()
-
- const password = "asdf"
- fileContent := []byte("test file content")
- fileName := filepath.Join(t.TempDir(), "file")
- mustWriteFile(t, fileName, fileContent)
- if err := sym.EncryptFile(fileName, password); err != nil {
- t.Errorf("EncryptFile failed: %s", err)
- }
- mustRemove(t, fileName)
- err := (&options{password: password}).dec(fileName + ".enc")
- if err != nil {
- t.Errorf("dec failed: %s", err)
- }
- gotFileContents := mustReadFile(t, fileName)
- if !bytes.Equal(gotFileContents, fileContent) {
- t.Errorf("dec returned incorrect contents %q, want %q", gotFileContents, fileContent)
- }
-}
-
-func TestDec_UsageError(t *testing.T) {
- t.Parallel()
-
- err := (&options{}).dec()
- if err == nil {
- t.Errorf("dec without -p when reading from stdin, want error")
- }
-}
-
-func TestDec_NotFound(t *testing.T) {
- t.Parallel()
-
- err := (&options{password: "asdf"}).dec("my-nonexistent-file-name.txt")
- if err == nil {
- t.Errorf("dec succeeded with nonexistent file, want error")
- }
-}
-
-func TestDec_Stdin(t *testing.T) {
- t.Parallel()
-
- const password = "asdf"
- content := []byte("test contents")
- encrypted := new(bytes.Buffer)
- if err := sym.EncryptBinary(encrypted, bytes.NewReader(content), password); err != nil {
- t.Fatalf("Failed to encrypt: %s", err)
- }
- gotContentBuf := new(bytes.Buffer)
- opts := &options{
- password: password,
- stdin: bytes.NewReader(encrypted.Bytes()),
- stdout: gotContentBuf,
- }
- if err := opts.dec(); err != nil {
- t.Fatalf("dec failed: %s", err)
- }
- gotContent := gotContentBuf.Bytes()
- if !bytes.Equal(gotContent, content) {
- t.Errorf("dec returned incorrect contents %q, want %q", gotContent, content)
- }
-}