aboutsummaryrefslogtreecommitdiff
path: root/internal/readpassword/stdin_test.go
blob: 5d5d8462050eef2ab06df47831938fd48d770770 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package readpassword

import (
	"fmt"
	"os"
	"os/exec"
	"testing"
)

// Provide password via stdin, terminated by "\n".
//
// 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 TestStdin(t *testing.T) {
	p1 := "g55434t55wef"
	if os.Getenv("TEST_SLAVE") == "1" {
		p2, err := readPasswordStdin("foo")
		if err != nil {
			fmt.Fprint(os.Stderr, err)
			os.Exit(1)
		}
		if p1 != string(p2) {
			fmt.Fprintf(os.Stderr, "%q != %q", p1, string(p2))
			os.Exit(1)
		}
		return
	}
	cmd := exec.Command(os.Args[0], "-test.run=TestStdin$")
	cmd.Env = append(os.Environ(), "TEST_SLAVE=1")
	cmd.Stderr = os.Stderr
	pipe, err := cmd.StdinPipe()
	if err != nil {
		t.Fatal(err)
	}
	err = cmd.Start()
	if err != nil {
		t.Fatal(err)
	}
	n, err := pipe.Write([]byte(p1 + "\n"))
	if n == 0 || err != nil {
		t.Fatal(err)
	}
	err = cmd.Wait()
	if err != nil {
		t.Fatalf("slave failed with %v", err)
	}
}

// Provide password via stdin, terminated by EOF (pipe close). This should not
// hang.
//
// 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 TestStdinEof(t *testing.T) {
	p1 := "asd45as5f4a36"
	if os.Getenv("TEST_SLAVE") == "1" {
		p2, err := readPasswordStdin("foo")
		if err != nil {
			fmt.Fprint(os.Stderr, err)
			os.Exit(1)
		}
		if p1 != string(p2) {
			fmt.Fprintf(os.Stderr, "%q != %q", p1, string(p2))
			os.Exit(1)
		}
		return
	}
	cmd := exec.Command(os.Args[0], "-test.run=TestStdinEof$")
	cmd.Env = append(os.Environ(), "TEST_SLAVE=1")
	cmd.Stderr = os.Stderr
	pipe, err := cmd.StdinPipe()
	if err != nil {
		t.Fatal(err)
	}
	err = cmd.Start()
	if err != nil {
		t.Fatal(err)
	}
	_, err = pipe.Write([]byte(p1))
	if err != nil {
		t.Fatal(err)
	}
	pipe.Close()
	err = cmd.Wait()
	if err != nil {
		t.Fatalf("slave failed with %v", err)
	}
}

// Provide empty password via stdin
func TestStdinEmpty(t *testing.T) {
	if os.Getenv("TEST_SLAVE") == "1" {
		_, err := readPasswordStdin("foo")
		if err != nil {
			os.Exit(1)
		}
	}
	cmd := exec.Command(os.Args[0], "-test.run=TestStdinEmpty$")
	cmd.Env = append(os.Environ(), "TEST_SLAVE=1")
	pipe, err := cmd.StdinPipe()
	if err != nil {
		t.Fatal(err)
	}
	err = cmd.Start()
	if err != nil {
		t.Fatal(err)
	}
	_, err = pipe.Write([]byte("\n"))
	if err != nil {
		t.Fatal(err)
	}
	pipe.Close()
	err = cmd.Wait()
	if err == nil {
		t.Fatalf("empty password should have failed")
	}
}