-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
104 lines (89 loc) · 3.03 KB
/
main_test.go
File metadata and controls
104 lines (89 loc) · 3.03 KB
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
package main
import (
"flag"
"fmt"
"os"
"testing"
)
func TestMain(m *testing.M) {
// Setup stuff can go here
// Run actual tests
flag.Parse()
exitCode := m.Run()
// Cleanup
os.Exit(exitCode)
}
// Test for sshAuthMethod.Set()
func TestSshAuthMethodSet(t *testing.T) {
type testCase struct {
argument string
isAllowed, isIgnored, shouldError bool
}
testCases := []testCase{
testCase{argument: "a", isAllowed: true, isIgnored: false},
testCase{argument: "f", isAllowed: false, isIgnored: false},
testCase{argument: "i", isAllowed: false, isIgnored: true},
testCase{argument: "x", shouldError: true},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Testing argument %s", tc.argument), func(t *testing.T) {
s := sshAuthMethod{}
err := s.Set(tc.argument)
if err != nil && !tc.shouldError {
t.Errorf("Got error but shouldn't, %s", err)
}
if s.Allowed != tc.isAllowed {
t.Errorf("Allowed is %t, should be %t", s.Allowed, tc.isAllowed)
}
if s.Ignored != tc.isIgnored {
t.Errorf("Ignored is %t, should be %t", s.Ignored, tc.isIgnored)
}
})
}
}
// Test for sshAuthMethod.SetFromAuthLine()
func TestSshAuthMethodSetFromAuthLine(t *testing.T) {
type testCase struct {
name, argument string
isOfferedFromSSHD bool
}
testCases := []testCase{
testCase{name: "none", argument: "debug1: randomText: none", isOfferedFromSSHD: true},
testCase{name: "none", argument: "debug1: randomText: password", isOfferedFromSSHD: false},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("Testing argument %s", tc.argument), func(t *testing.T) {
s := sshAuthMethod{Name: tc.name}
s.SetFromAuthLine(tc.argument)
if s.OfferedFromSSHD != tc.isOfferedFromSSHD {
t.Errorf("OfferedFromSSHD is %t, should be %t", s.OfferedFromSSHD, tc.isOfferedFromSSHD)
}
})
}
}
// Test for sshAuthMethod.IsOK()
// includes sshAuthMethod.generateStatus()
func TestSshAuthMethodIsOK(t *testing.T) {
type testCase struct {
isOfferedFromSSHD, isAllowed, isIgnored, isOK bool
}
testCases := []testCase{
testCase{isOfferedFromSSHD: true, isAllowed: true, isIgnored: true, isOK: true},
testCase{isOfferedFromSSHD: true, isAllowed: true, isIgnored: false, isOK: true},
testCase{isOfferedFromSSHD: true, isAllowed: false, isIgnored: true, isOK: true},
testCase{isOfferedFromSSHD: true, isAllowed: false, isIgnored: false, isOK: false},
testCase{isOfferedFromSSHD: false, isAllowed: true, isIgnored: true, isOK: true},
testCase{isOfferedFromSSHD: false, isAllowed: true, isIgnored: false, isOK: false},
testCase{isOfferedFromSSHD: false, isAllowed: false, isIgnored: true, isOK: true},
testCase{isOfferedFromSSHD: false, isAllowed: false, isIgnored: false, isOK: true},
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("Testing #%d", i), func(t *testing.T) {
s := sshAuthMethod{Allowed: tc.isAllowed, Ignored: tc.isIgnored, OfferedFromSSHD: tc.isOfferedFromSSHD}
status := s.IsOK()
if status != tc.isOK {
t.Errorf("Status is %t, should be %t", status, tc.isOK)
}
})
}
}