libgocryptfs/internal/readpassword/passfile_test.go
Jakob Unterwurzacher 4b251f3ce1 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
2022-01-03 15:18:59 +01:00

77 lines
1.9 KiB
Go

package readpassword
import (
"testing"
)
func TestPassfile(t *testing.T) {
testcases := []struct {
file string
want string
}{
{"mypassword.txt", "mypassword"},
{"mypassword_garbage.txt", "mypassword"},
{"mypassword_missing_newline.txt", "mypassword"},
{"file with spaces.txt", "mypassword"},
}
for _, tc := range testcases {
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, 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 fail instead of returning an empty string.
func TestPassfileEmpty(t *testing.T) {
_, err := readPassFile("passfile_test_files/empty.txt")
if err == nil {
t.Fatal("should have failed")
}
}
// File containing just a newline.
// readPassFile() should fal instead of returning an empty string.
func TestPassfileNewline(t *testing.T) {
_, err := readPassFile("passfile_test_files/newline.txt")
if err == nil {
t.Fatal("should have failed")
}
}
// File containing "\ngarbage".
// readPassFile() should return an error.
func TestPassfileEmptyFirstLine(t *testing.T) {
_, err := readPassFile("passfile_test_files/empty_first_line.txt")
if err == nil {
t.Fatal("should have failed")
}
}
// TestPassFileConcatenate tests readPassFileConcatenate
func TestPassFileConcatenate(t *testing.T) {
files := []string{
"passfile_test_files/file with spaces.txt",
"passfile_test_files/mypassword_garbage.txt",
}
res, err := readPassFileConcatenate(files)
if err != nil {
t.Fatal(err)
}
if string(res) != "mypasswordmypassword" {
t.Errorf("wrong result: %q", res)
}
}