aboutsummaryrefslogtreecommitdiff
path: root/internal/readpassword/passfile_test.go
blob: dbfe159c61107c593113067294905782a293e974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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"},
		{"file with spaces.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)
		}
		// Calling readPassFileConcatenate with only one element should give the
		// same result
		pw = readPassFileConcatenate([]string{"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 , mirror:
// http://web.archive.org/web/20200426174352/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 , mirror:
// http://web.archive.org/web/20200426174352/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=TestPassfileNewline$")
	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 , mirror:
// http://web.archive.org/web/20200426174352/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")
}

// TestPassFileConcatenate tests readPassFileConcatenate
func TestPassFileConcatenate(t *testing.T) {
	files := []string{
		"passfile_test_files/file with spaces.txt",
		"passfile_test_files/mypassword_garbage.txt",
	}
	res := string(readPassFileConcatenate(files))
	if res != "mypasswordmypassword" {
		t.Errorf("wrong result: %q", res)
	}
}