2018-12-15 17:09:38 +01:00
|
|
|
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"},
|
2019-03-03 13:25:30 +01:00
|
|
|
{"file with spaces.txt", "mypassword"},
|
2018-12-15 17:09:38 +01:00
|
|
|
}
|
|
|
|
for _, tc := range testcases {
|
2022-01-03 15:18:59 +01:00
|
|
|
pw, err := readPassFile("passfile_test_files/" + tc.file)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-15 17:09:38 +01:00
|
|
|
if string(pw) != tc.want {
|
|
|
|
t.Errorf("Wrong result: want=%q have=%q", tc.want, pw)
|
|
|
|
}
|
2020-05-17 19:31:04 +02:00
|
|
|
// Calling readPassFileConcatenate with only one element should give the
|
|
|
|
// same result
|
2022-01-03 15:18:59 +01:00
|
|
|
pw, err = readPassFileConcatenate([]string{"passfile_test_files/" + tc.file})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-05-17 19:31:04 +02:00
|
|
|
if string(pw) != tc.want {
|
|
|
|
t.Errorf("Wrong result: want=%q have=%q", tc.want, pw)
|
|
|
|
}
|
2018-12-15 17:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 15:18:59 +01:00
|
|
|
// readPassFile() should fail instead of returning an empty string.
|
2018-12-15 17:09:38 +01:00
|
|
|
func TestPassfileEmpty(t *testing.T) {
|
2022-01-03 15:18:59 +01:00
|
|
|
_, err := readPassFile("passfile_test_files/empty.txt")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have failed")
|
2018-12-15 17:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// File containing just a newline.
|
2022-01-03 15:18:59 +01:00
|
|
|
// readPassFile() should fal instead of returning an empty string.
|
2018-12-15 17:09:38 +01:00
|
|
|
func TestPassfileNewline(t *testing.T) {
|
2022-01-03 15:18:59 +01:00
|
|
|
_, err := readPassFile("passfile_test_files/newline.txt")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have failed")
|
2018-12-15 17:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// File containing "\ngarbage".
|
2022-01-03 15:18:59 +01:00
|
|
|
// readPassFile() should return an error.
|
2018-12-15 17:09:38 +01:00
|
|
|
func TestPassfileEmptyFirstLine(t *testing.T) {
|
2022-01-03 15:18:59 +01:00
|
|
|
_, err := readPassFile("passfile_test_files/empty_first_line.txt")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have failed")
|
2018-12-15 17:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-17 19:31:04 +02:00
|
|
|
|
|
|
|
// TestPassFileConcatenate tests readPassFileConcatenate
|
|
|
|
func TestPassFileConcatenate(t *testing.T) {
|
|
|
|
files := []string{
|
|
|
|
"passfile_test_files/file with spaces.txt",
|
|
|
|
"passfile_test_files/mypassword_garbage.txt",
|
|
|
|
}
|
2022-01-03 15:18:59 +01:00
|
|
|
res, err := readPassFileConcatenate(files)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if string(res) != "mypasswordmypassword" {
|
2020-05-17 19:31:04 +02:00
|
|
|
t.Errorf("wrong result: %q", res)
|
|
|
|
}
|
|
|
|
}
|