From 4b251f3ce1f0a0472ed10a00aeef70c69ba03a5d Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Mon, 3 Jan 2022 15:18:59 +0100 Subject: readpassword: bubble up errors instead of exiting the process This allows cleanups to happen in the caller, like removing the control socket. Fixes https://github.com/rfjakob/gocryptfs/issues/634 --- internal/readpassword/extpass_test.go | 73 +++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 33 deletions(-) (limited to 'internal/readpassword/extpass_test.go') diff --git a/internal/readpassword/extpass_test.go b/internal/readpassword/extpass_test.go index 5c23ab3..d38f42f 100644 --- a/internal/readpassword/extpass_test.go +++ b/internal/readpassword/extpass_test.go @@ -2,7 +2,6 @@ package readpassword import ( "os" - "os/exec" "testing" "github.com/rfjakob/gocryptfs/v2/internal/tlog" @@ -16,68 +15,76 @@ func TestMain(m *testing.M) { func TestExtpass(t *testing.T) { p1 := "ads2q4tw41reg52" - p2 := string(readPasswordExtpass([]string{"echo " + p1})) - if p1 != p2 { - t.Errorf("p1=%q != p2=%q", p1, p2) + p2, err := readPasswordExtpass([]string{"echo " + p1}) + if err != nil { + t.Fatal(err) + } + if p1 != string(p2) { + t.Errorf("p1=%q != p2=%q", p1, string(p2)) } } func TestOnceExtpass(t *testing.T) { p1 := "lkadsf0923rdfi48rqwhdsf" - p2 := string(Once([]string{"echo " + p1}, nil, "")) - if p1 != p2 { - t.Errorf("p1=%q != p2=%q", p1, p2) + p2, err := Once([]string{"echo " + p1}, nil, "") + if err != nil { + t.Fatal(err) + } + if p1 != string(p2) { + t.Errorf("p1=%q != p2=%q", p1, string(p2)) } } // extpass with two arguments func TestOnceExtpass2(t *testing.T) { p1 := "foo" - p2 := string(Once([]string{"echo", p1}, nil, "")) - if p1 != p2 { - t.Errorf("p1=%q != p2=%q", p1, p2) + p2, err := Once([]string{"echo", p1}, nil, "") + if err != nil { + t.Fatal(err) + } + if p1 != string(p2) { + t.Errorf("p1=%q != p2=%q", p1, string(p2)) } } // extpass with three arguments func TestOnceExtpass3(t *testing.T) { p1 := "foo bar baz" - p2 := string(Once([]string{"echo", "foo", "bar", "baz"}, nil, "")) - if p1 != p2 { - t.Errorf("p1=%q != p2=%q", p1, p2) + p2, err := Once([]string{"echo", "foo", "bar", "baz"}, nil, "") + if err != nil { + t.Fatal(err) + } + if p1 != string(p2) { + t.Errorf("p1=%q != p2=%q", p1, string(p2)) } } func TestOnceExtpassSpaces(t *testing.T) { p1 := "mypassword" - p2 := string(Once([]string{"cat", "passfile_test_files/file with spaces.txt"}, nil, "")) - if p1 != p2 { - t.Errorf("p1=%q != p2=%q", p1, p2) + p2, err := Once([]string{"cat", "passfile_test_files/file with spaces.txt"}, nil, "") + if err != nil { + t.Fatal(err) + } + if p1 != string(p2) { + t.Errorf("p1=%q != p2=%q", p1, string(p2)) } } func TestTwiceExtpass(t *testing.T) { p1 := "w5w44t3wfe45srz434" - p2 := string(Once([]string{"echo " + p1}, nil, "")) - if p1 != p2 { - t.Errorf("p1=%q != p2=%q", p1, p2) + p2, err := Once([]string{"echo " + p1}, nil, "") + if err != nil { + t.Fatal(err) + } + if p1 != string(p2) { + t.Errorf("p1=%q != p2=%q", p1, string(p2)) } } -// When extpass returns an empty string, we should crash. -// -// The TEST_SLAVE magic is explained at -// https://talks.golang.org/2014/testing.slide#23 . +// Empty extpass should fail func TestExtpassEmpty(t *testing.T) { - if os.Getenv("TEST_SLAVE") == "1" { - readPasswordExtpass([]string{"echo"}) - return - } - cmd := exec.Command(os.Args[0], "-test.run=TestExtpassEmpty$") - cmd.Env = append(os.Environ(), "TEST_SLAVE=1") - err := cmd.Run() - if err != nil { - return + _, err := readPasswordExtpass([]string{"echo"}) + if err == nil { + t.Fatal("empty password should have failed") } - t.Fatal("empty password should have failed") } -- cgit v1.2.3