-
Notifications
You must be signed in to change notification settings - Fork 0
/
jacksquat_test.go
89 lines (72 loc) · 1.62 KB
/
jacksquat_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
package main
import (
"io"
"io/ioutil"
"os"
"testing"
)
type testReader struct {
data string
done bool
throwError bool
}
func (t *testReader) Read(p []byte) (n int, err error) {
if t.throwError {
return 0, io.ErrNoProgress
}
if t.done {
return 0, io.EOF
}
for i, b := range []byte(t.data) {
p[i] = b
}
t.done = true
return len(t.data), nil
}
func TestJackSquat(t *testing.T) {
// Retrieval of user name
userName, userID := thisUser()
if len(userName) == 0 {
t.Error("Getting user name failed")
}
if len(userID) == 0 {
t.Error("Getting user ID failed")
}
// Retrieval of TTY name
ttyName := thisTTYName()
if len(ttyName) == 0 {
t.Error("Getting TTY name failed")
}
const configFilename = "jacksquat.conf"
// Config file does not exist
os.Remove(configFilename)
getConfig(configFilename)
ioutil.WriteFile(configFilename,
[]byte(`{ "logtemplate": "login by {{.UserName}} (UID: {{.UserID}}) on {{.TTYName}}",
"noticetemplate": "Welcome {{.UserName}}. This is a captive account." }`),
0644)
// Config file exists with valid data
loopCount := 0
captureLogin(configFilename, 0, func() bool {
ret := true
if loopCount > 0 {
ret = false
}
loopCount++
return ret
})
os.Remove(configFilename)
if loopCount == 0 {
t.Error("capture loop was not called")
}
// Data parse error
tr := &testReader{"data", false, false}
if len(getConfigFromReader(tr).LogTemplate) > 0 {
t.Error("config returned data on data parse error")
}
// Reader returns error
tr.throwError = true
if len(getConfigFromReader(tr).LogTemplate) > 0 {
t.Error("config returned data on read error")
}
}