-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudo_test.go
115 lines (101 loc) · 2.54 KB
/
sudo_test.go
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
// SPDX-FileCopyrightText: 2023 M. Shulhan <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
//go:build integration
package awwan
import (
"bytes"
"context"
"testing"
"git.sr.ht/~shulhan/pakakeh.go/lib/test"
)
func TestExecLocal_sudo(t *testing.T) {
type testCase struct {
desc string
sudoPass string
expOutput string
expError []string
listStmt []Statement
}
var (
mockin = &mockStdin{}
mockout = &bytes.Buffer{}
req = &ExecRequest{
stdin: mockin,
}
err error
)
req.init(`.`)
req.registerLogWriter(`output`, mockout)
var cases = []testCase{{
desc: `SingleSudo`,
listStmt: []Statement{{
cmd: `sudo`,
args: []string{`echo "hello sudo"`},
raw: []byte(`sudo echo "hello sudo"`),
}},
sudoPass: "awwan\n",
expOutput: "[sudo] password for awwan: hello sudo\n",
}, {
desc: `MultipleSudo`,
listStmt: []Statement{{
cmd: `sudo`,
args: []string{`echo "hello sudo #1"`},
raw: []byte(`sudo echo "hello sudo #1"`),
}, {
cmd: `sudo`,
args: []string{`echo "hello sudo #2"`},
raw: []byte(`sudo echo "hello sudo #2"`),
}},
sudoPass: "awwan\nawwan\n",
expOutput: "[sudo] password for awwan: hello sudo #1\n[sudo] password for awwan: hello sudo #2",
}, {
desc: `WithInvalidPassword`,
listStmt: []Statement{{
cmd: `sudo`,
args: []string{`echo "hello sudo"`},
raw: []byte(`sudo echo "hello sudo"`),
}},
sudoPass: "invalid\n",
expError: []string{`ExecLocal: exit status 1`},
expOutput: "[sudo] password for awwan: sudo: 1 incorrect password attempt\n",
}, {
desc: `MultipleSudoOneInvalid`,
listStmt: []Statement{{
cmd: `sudo`,
args: []string{`echo "hello sudo #1"`},
raw: []byte(`sudo echo "hello sudo #1"`),
}, {
cmd: `sudo`,
args: []string{`echo "hello sudo #2"`},
raw: []byte(`sudo echo "hello sudo #2"`),
}},
sudoPass: "awwan\ninvalid\n",
expError: []string{
``,
`ExecLocal: exit status 1`,
},
expOutput: "[sudo] password for awwan: hello sudo #1\n[sudo] password for awwan: sudo: 1 incorrect password attempt\n",
}}
var (
ctx = context.Background()
c testCase
stmt Statement
x int
)
for _, c = range cases {
t.Log(c.desc)
mockout.Reset()
mockin.buf.Reset()
mockin.buf.WriteString(c.sudoPass)
for x, stmt = range c.listStmt {
err = ExecLocal(ctx, req, &stmt)
if err != nil {
t.Log(mockout.String())
var expError = c.expError[x]
test.Assert(t, `error`, expError, err.Error())
}
req.mlog.Flush()
}
test.Assert(t, c.desc+` output`, c.expOutput, mockout.String())
}
}