aboutsummaryrefslogtreecommitdiffstats
path: root/dec
diff options
context:
space:
mode:
Diffstat (limited to 'dec')
-rw-r--r--dec/dec.go33
-rw-r--r--dec/dec_test.go96
2 files changed, 120 insertions, 9 deletions
diff --git a/dec/dec.go b/dec/dec.go
index ce0a719..049bc74 100644
--- a/dec/dec.go
+++ b/dec/dec.go
@@ -3,22 +3,34 @@ package main
import (
"flag"
"fmt"
+ "io"
"os"
"golang.org/x/term"
"roseh.moe/cmd/sym/internal/sym"
)
-var passwordFlag = flag.String("p", "", "use the specified password; if not provided, dec will prompt for a password")
+type options struct {
+ password string
+ force bool
-func dec() error {
- args := flag.Args()
- if len(args) == 0 && *passwordFlag == "" {
+ 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 *passwordFlag != "" {
- password = *passwordFlag
+ if o.password != "" {
+ password = o.password
} else {
fmt.Fprint(os.Stderr, "Enter password: ")
pw, err := term.ReadPassword(int(os.Stdin.Fd()))
@@ -29,10 +41,10 @@ func dec() error {
password = string(pw)
}
if len(args) == 0 {
- return sym.Decrypt(os.Stdout, os.Stdin, password)
+ return sym.Decrypt(o.stdout, o.stdin, password)
}
for _, fileName := range args {
- if err := sym.DecryptFile(fileName, password); err != nil {
+ if err := sym.DecryptFile(fileName, password, sym.Force(o.force)); err != nil {
return err
}
}
@@ -40,8 +52,11 @@ func dec() error {
}
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")
flag.Parse()
- if err := dec(); err != nil {
+ if err := o.dec(flag.Args()...); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
diff --git a/dec/dec_test.go b/dec/dec_test.go
new file mode 100644
index 0000000..823ab68
--- /dev/null
+++ b/dec/dec_test.go
@@ -0,0 +1,96 @@
+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)
+ }
+}