-
Notifications
You must be signed in to change notification settings - Fork 4
/
setup_test.go
171 lines (153 loc) · 4.37 KB
/
setup_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package radiusauth
import (
"reflect"
"testing"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
func TestSetup(t *testing.T) {
// c := caddy.NewTestController("http", "radiusauth {\n server localhost:1812\nsecret TOOMANYSECRETS\n}")
c := caddy.NewTestController("http",
`radiusauth {
server localhost:1812
secret TOOMANYSECRETS
}`)
err := setup(c)
if err != nil {
t.Errorf("Expected no errors, but got: %v", err)
}
mids := httpserver.GetConfig(c).Middleware()
if len(mids) == 0 {
t.Fatal("Expected middleware, got 0 instead")
}
handler := mids[0](httpserver.EmptyNext)
myHandler, ok := handler.(RADIUS)
if !ok {
t.Fatalf("Expected handler to be type RADIUS, got: %#v", handler)
}
if !httpserver.SameNext(myHandler.Next, httpserver.EmptyNext) {
t.Error("'Next' field of handler was not set properly")
}
}
func TestRADIUSAuthParse(t *testing.T) {
tests := []struct {
inputRADIUSConfig string
shouldErr bool
expectedRADIUSConfig radiusConfig
}{
// Test 0
{`radiusauth {
server localhost:1812
secret TOOMANYSECRETS
realm "Private"
cache /tmp
cachetimeout 300
nasid "Caddy"
except /public /assests
}`,
false, radiusConfig{
Server: []string{"localhost:1812"},
Secret: "TOOMANYSECRETS",
// Timeout: 3,
// Retries: 1,
nasid: "Caddy",
realm: "Private",
requestFilter: &ignoredPathFilter{ignoredPaths: []string{"/public", "/assests"}},
cache: "/tmp",
cachetimeout: 300000000000, // nanoseconds
}},
// Test 1 - test 'except' + 'only' filters - SHOULD FAIL
{`radiusauth {
server 127.0.0.1:1812
secret TOOMANYS3cret5
except /public
only /private
}`,
true, radiusConfig{
Server: []string{"127.0.0.1:1812"},
Secret: "TOOMANYS3cret5",
Timeout: 3,
Retries: 1,
nasid: "Caddy",
// realm: "Private",
requestFilter: nil,
cache: "/tmp",
cachetimeout: 60,
}},
// Test 2 - only paths should start with a /
{`radiusauth {
server 127.0.0.1:1812
secret TOOMANYS3cret5
only /private secret
}`,
true, radiusConfig{
Server: []string{"127.0.0.1:1812"},
Secret: "TOOMANYS3cret5",
// Timeout: 3,
// Retries: 1,
// nasid: "Caddy",
// // realm: "Private",
// requestFilter: nil,
// cache: "/tmp",
// cachetimeout: 60,
}},
// Test 3 - except paths should start with a /
{`radiusauth {
server 127.0.0.1:1812
secret TOOMANYS3cret5
except /public assests
}`,
true, radiusConfig{
Server: []string{"127.0.0.1:1812"},
Secret: "TOOMANYS3cret5",
// Timeout: 3,
// Retries: 1,
// nasid: "Caddy",
// // realm: "Private",
// requestFilter: nil,
// cache: "/tmp",
// cachetimeout: 60,
}},
// Test 4 - server argument must be provided
{`radiusauth {
server 127.0.0.1
secret TOOMANYS3cret5
nasid "Caddy"
}`,
true, radiusConfig{
Server: []string{"127.0.0.1"},
Secret: "TOOMANYS3cret5",
nasid: "Caddy",
}},
}
for i, test := range tests {
actualRadiusAuthConfigs, err := parseRadiusConfig(caddy.NewTestController("http", test.inputRADIUSConfig))
if err == nil && test.shouldErr {
t.Errorf("Test %d didn't error, but it should have", i)
} else if err != nil && !test.shouldErr {
t.Errorf("Test %d errored, but it shouldn't have; got '%v'", i, err)
}
if err != nil && test.shouldErr {
// If shouldErr don't test individual values
continue
}
a := &actualRadiusAuthConfigs
b := &test.expectedRADIUSConfig
if reflect.DeepEqual(a.Server, b.Server) == false {
t.Errorf("Test %d expected server to be %s, but got %s",
i, a.Server, b.Server)
}
if reflect.DeepEqual(a.Secret, b.Secret) == false {
t.Errorf("Test %d expected secret to be %s, but got %s",
i, a.Secret, b.Secret)
}
if reflect.DeepEqual(a.realm, b.realm) == false {
t.Errorf("Test %d expected realm to be %s, but got %s",
i, a.realm, b.realm)
}
if reflect.DeepEqual(a.requestFilter, b.requestFilter) == false {
t.Errorf("Test %d expected filter to be %#v, but got %#v",
i, a.requestFilter, b.requestFilter)
}
}
}