summaryrefslogtreecommitdiff
path: root/internal/readpassword/stdin_test.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2016-06-15 22:43:31 +0200
committerJakob Unterwurzacher2016-06-15 22:44:24 +0200
commitc89455063cfd9c531c0a671251ccfcd46f09403d (patch)
tree1bd5330aad0ac7b16ecb5b35150a304e56271be3 /internal/readpassword/stdin_test.go
parent218bf83ce399832a0eccfbd025e5dd0399db6bed (diff)
readpassword: create internal package for password reading
* Supports stdin * Add tests for extpass and stdin As per user request at https://github.com/rfjakob/gocryptfs/issues/30
Diffstat (limited to 'internal/readpassword/stdin_test.go')
-rw-r--r--internal/readpassword/stdin_test.go100
1 files changed, 100 insertions, 0 deletions
diff --git a/internal/readpassword/stdin_test.go b/internal/readpassword/stdin_test.go
new file mode 100644
index 0000000..2d9f93f
--- /dev/null
+++ b/internal/readpassword/stdin_test.go
@@ -0,0 +1,100 @@
+package readpassword
+
+import (
+ "fmt"
+ "os"
+ "os/exec"
+ "testing"
+)
+
+// Provide password via stdin, terminated by "\n".
+func TestStdin(t *testing.T) {
+ p1 := "g55434t55wef"
+ if os.Getenv("TEST_SLAVE") == "1" {
+ p2 := readPasswordStdin()
+ if p1 != p2 {
+ fmt.Fprintf(os.Stderr, "%q != %q", p1, p2)
+ os.Exit(1)
+ }
+ return
+ }
+ cmd := exec.Command(os.Args[0], "-test.run=TestStdin$")
+ cmd.Env = append(os.Environ(), "TEST_SLAVE=1")
+ cmd.Stderr = os.Stderr
+ pipe, err := cmd.StdinPipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = cmd.Start()
+ if err != nil {
+ t.Fatal(err)
+ }
+ n, err := pipe.Write([]byte(p1 + "\n"))
+ if n == 0 || err != nil {
+ t.Fatal(err)
+ }
+ err = cmd.Wait()
+ if err != nil {
+ t.Fatalf("slave failed with %v", err)
+ }
+}
+
+// Provide password via stdin, terminated by EOF (pipe close). This should not
+// hang.
+func TestStdinEof(t *testing.T) {
+ p1 := "asd45as5f4a36"
+ if os.Getenv("TEST_SLAVE") == "1" {
+ p2 := readPasswordStdin()
+ if p1 != p2 {
+ fmt.Fprintf(os.Stderr, "%q != %q", p1, p2)
+ os.Exit(1)
+ }
+ return
+ }
+ cmd := exec.Command(os.Args[0], "-test.run=TestStdinEof$")
+ cmd.Env = append(os.Environ(), "TEST_SLAVE=1")
+ cmd.Stderr = os.Stderr
+ pipe, err := cmd.StdinPipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = cmd.Start()
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = pipe.Write([]byte(p1))
+ if err != nil {
+ t.Fatal(err)
+ }
+ pipe.Close()
+ err = cmd.Wait()
+ if err != nil {
+ t.Fatalf("slave failed with %v", err)
+ }
+}
+
+// Provide empty password via stdin
+func TestStdinEmpty(t *testing.T) {
+ if os.Getenv("TEST_SLAVE") == "1" {
+ readPasswordStdin()
+ }
+ cmd := exec.Command(os.Args[0], "-test.run=TestStdinEmpty$")
+ cmd.Env = append(os.Environ(), "TEST_SLAVE=1")
+ pipe, err := cmd.StdinPipe()
+ if err != nil {
+ t.Fatal(err)
+ }
+ err = cmd.Start()
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, err = pipe.Write([]byte("\n"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ pipe.Close()
+ err = cmd.Wait()
+ if err == nil {
+ t.Fatalf("empty password should have failed")
+ }
+}