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/passfile_test.go | 76 +++++++++++----------------------- 1 file changed, 25 insertions(+), 51 deletions(-) (limited to 'internal/readpassword/passfile_test.go') diff --git a/internal/readpassword/passfile_test.go b/internal/readpassword/passfile_test.go index dbfe159..425e4e5 100644 --- a/internal/readpassword/passfile_test.go +++ b/internal/readpassword/passfile_test.go @@ -1,8 +1,6 @@ package readpassword import ( - "os" - "os/exec" "testing" ) @@ -17,76 +15,49 @@ func TestPassfile(t *testing.T) { {"file with spaces.txt", "mypassword"}, } for _, tc := range testcases { - pw := readPassFile("passfile_test_files/" + tc.file) + pw, err := readPassFile("passfile_test_files/" + tc.file) + if err != nil { + t.Fatal(err) + } if string(pw) != tc.want { t.Errorf("Wrong result: want=%q have=%q", tc.want, pw) } // Calling readPassFileConcatenate with only one element should give the // same result - pw = readPassFileConcatenate([]string{"passfile_test_files/" + tc.file}) + pw, err = readPassFileConcatenate([]string{"passfile_test_files/" + tc.file}) + if err != nil { + t.Fatal(err) + } 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 , mirror: -// http://web.archive.org/web/20200426174352/https://talks.golang.org/2014/testing.slide#23 +// readPassFile() should fail instead of returning an empty string. func TestPassfileEmpty(t *testing.T) { - if os.Getenv("TEST_SLAVE") == "1" { - readPassFile("passfile_test_files/empty.txt") - return + _, err := readPassFile("passfile_test_files/empty.txt") + if err == nil { + t.Fatal("should have failed") } - 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 , mirror: -// http://web.archive.org/web/20200426174352/https://talks.golang.org/2014/testing.slide#23 +// readPassFile() should fal instead of returning an empty string. 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=TestPassfileNewline$") - cmd.Env = append(os.Environ(), "TEST_SLAVE=1") - err := cmd.Run() - if err != nil { - return + _, err := readPassFile("passfile_test_files/newline.txt") + if err == nil { + t.Fatal("should have failed") } - 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 , mirror: -// http://web.archive.org/web/20200426174352/https://talks.golang.org/2014/testing.slide#23 +// readPassFile() should return an error. 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 + _, err := readPassFile("passfile_test_files/empty_first_line.txt") + if err == nil { + t.Fatal("should have failed") } - t.Fatal("should have exited") } // TestPassFileConcatenate tests readPassFileConcatenate @@ -95,8 +66,11 @@ func TestPassFileConcatenate(t *testing.T) { "passfile_test_files/file with spaces.txt", "passfile_test_files/mypassword_garbage.txt", } - res := string(readPassFileConcatenate(files)) - if res != "mypasswordmypassword" { + res, err := readPassFileConcatenate(files) + if err != nil { + t.Fatal(err) + } + if string(res) != "mypasswordmypassword" { t.Errorf("wrong result: %q", res) } } -- cgit v1.2.3