aboutsummaryrefslogtreecommitdiff
path: root/internal/readpassword/passfile_test.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2018-12-15 17:09:38 +0100
committerJakob Unterwurzacher2018-12-15 17:09:38 +0100
commit295d432175292dbaef572093d784aab55f5c0b8f (patch)
treed5db468806d211d0ea5bbd589dad3a0a337de714 /internal/readpassword/passfile_test.go
parentb29ee62749244d1985a71f8df37a1582d9e790c9 (diff)
passfile: directly read file instead of invoking cat
Allows better error handling, gets rid of the call to an external program, and fixes https://github.com/rfjakob/gocryptfs/issues/278 .
Diffstat (limited to 'internal/readpassword/passfile_test.go')
-rw-r--r--internal/readpassword/passfile_test.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/internal/readpassword/passfile_test.go b/internal/readpassword/passfile_test.go
new file mode 100644
index 0000000..457170b
--- /dev/null
+++ b/internal/readpassword/passfile_test.go
@@ -0,0 +1,80 @@
+package readpassword
+
+import (
+ "os"
+ "os/exec"
+ "testing"
+)
+
+func TestPassfile(t *testing.T) {
+ testcases := []struct {
+ file string
+ want string
+ }{
+ {"mypassword.txt", "mypassword"},
+ {"mypassword_garbage.txt", "mypassword"},
+ {"mypassword_missing_newline.txt", "mypassword"},
+ }
+ for _, tc := range testcases {
+ pw := readPassFile("passfile_test_files/" + tc.file)
+ if string(pw) != tc.want {
+ t.Errorf("Wrong result: want=%q have=%q", tc.want, pw)
+ }
+ }
+}
+
+// readPassFile() should exit instead of returning an empty string.
+//
+// The TEST_SLAVE magic is explained at
+// https://talks.golang.org/2014/testing.slide#23 .
+func TestPassfileEmpty(t *testing.T) {
+ if os.Getenv("TEST_SLAVE") == "1" {
+ readPassFile("passfile_test_files/empty.txt")
+ return
+ }
+ cmd := exec.Command(os.Args[0], "-test.run=TestPassfileEmpty$")
+ cmd.Env = append(os.Environ(), "TEST_SLAVE=1")
+ err := cmd.Run()
+ if err != nil {
+ return
+ }
+ t.Fatal("should have exited")
+}
+
+// File containing just a newline.
+// readPassFile() should exit instead of returning an empty string.
+//
+// The TEST_SLAVE magic is explained at
+// https://talks.golang.org/2014/testing.slide#23 .
+func TestPassfileNewline(t *testing.T) {
+ if os.Getenv("TEST_SLAVE") == "1" {
+ readPassFile("passfile_test_files/newline.txt")
+ return
+ }
+ cmd := exec.Command(os.Args[0], "-test.run=TestPassfileEmpty$")
+ cmd.Env = append(os.Environ(), "TEST_SLAVE=1")
+ err := cmd.Run()
+ if err != nil {
+ return
+ }
+ t.Fatal("should have exited")
+}
+
+// File containing "\ngarbage".
+// readPassFile() should exit instead of returning an empty string.
+//
+// The TEST_SLAVE magic is explained at
+// https://talks.golang.org/2014/testing.slide#23 .
+func TestPassfileEmptyFirstLine(t *testing.T) {
+ if os.Getenv("TEST_SLAVE") == "1" {
+ readPassFile("passfile_test_files/empty_first_line.txt")
+ return
+ }
+ cmd := exec.Command(os.Args[0], "-test.run=TestPassfileEmptyFirstLine$")
+ cmd.Env = append(os.Environ(), "TEST_SLAVE=1")
+ err := cmd.Run()
+ if err != nil {
+ return
+ }
+ t.Fatal("should have exited")
+}