This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
setup_test.go
273 lines (247 loc) · 4.98 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package cgi
import (
"fmt"
"testing"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
func configure(expectErr bool, str string) (rules []ruleType, err error) {
var c *caddy.Controller
var mids []httpserver.Middleware
var cfg *httpserver.SiteConfig
var srvHnd httpserver.Handler
var hnd handlerType
var ok bool
// printf("--- Directive begin --\n%s\n--- Directive end ---\n", str)
c = caddy.NewTestController("http", str)
err = setup(c)
if err == nil {
if !expectErr {
cfg = httpserver.GetConfig(c)
mids = cfg.Middleware()
midLen := len(mids)
if midLen > 0 {
for j := 0; j < midLen && err == nil; j++ {
srvHnd = mids[j](httpserver.EmptyNext)
hnd, ok = srvHnd.(handlerType)
if ok {
rules = append(rules, hnd.rules...)
} else {
err = fmt.Errorf("expected middleware handler to be CGI handler")
}
}
} else {
err = fmt.Errorf("no middlewares present")
}
} else {
err = fmt.Errorf("expected error but got succcess")
}
} else if expectErr {
err = nil
}
return
}
// This examples demonstrates printing a CGI rule
func Example_rule() {
var err error
var rules []ruleType
var strList = []string{
`cgi {
match *.lua *.luac
except init.lua init.luac
except utility.lua
exec /usr/bin/lua /usr/local/cgi-bin/{match}
pass_env LUA_PATH LUA_CPATH
empty_env REMOTE_USER CONTENT_TYPE
}`,
`cgi {
match *.py *.pyc
exec /usr/bin/python -s
env PYTHONSTARTUP=/usr/share/init.py
}`,
`cgi /fossil /var/www/fossil`,
`cgi {
match /report/week
exec /var/www/report --mode=week
env NO_BANANAS=YES "NAME = Don Quixote"
env MODE=DEV
pass_env JWT_SECRET
}`,
}
for _, str := range strList {
rules, err = configure(false, str)
if err == nil {
printRules(rules)
} else {
printf("%s\n", err)
}
}
// Output:
// Rule 0
// Match 0: *.lua
// Match 1: *.luac
// Except 0: init.lua
// Except 1: init.luac
// Except 2: utility.lua
// Exe: /usr/bin/lua
// Pass all: false
// Inspect: false
// Arg 0: /usr/local/cgi-bin/{match}
// Pass env 0: LUA_PATH
// Pass env 1: LUA_CPATH
// Empty env 0: REMOTE_USER
// Empty env 1: CONTENT_TYPE
// Rule 0
// Match 0: *.py
// Match 1: *.pyc
// Exe: /usr/bin/python
// Pass all: false
// Inspect: false
// Arg 0: -s
// Env 0: PYTHONSTARTUP=[/usr/share/init.py]
// Rule 0
// Match 0: /fossil
// Exe: /var/www/fossil
// Pass all: false
// Inspect: false
// Rule 0
// Match 0: /report/week
// Exe: /var/www/report
// Pass all: false
// Inspect: false
// Arg 0: --mode=week
// Env 0: NO_BANANAS=[YES]
// Env 1: NAME=[Don Quixote]
// Env 2: MODE=[DEV]
// Pass env 0: JWT_SECRET
}
func TestSetup(t *testing.T) {
var err error
// Each of the following directives is submitted for parsing. If the string is
// prefixed with "0:", it is expected to parse successfully. If it is prefixed
// with "1:", an error is expected. The prefix is removed before parsing.
var directiveList = []string{
`0:cgi /report/daily /usr/bin/perl /usr/share/perl/report --mode=daily`,
`0:cgi {
match *.lua *.luac
except init.lua
exec /usr/bin/lua /usr/local/cgi-bin/{match}
pass_env LUA_PATH LUA_CPATH
}
cgi {
match *.py *.pyc
exec /usr/bin/python -s /usr/local/cgi-bin/{match}
env PYTHONSTARTUP=/usr/share/init.py
}
cgi {
match /fossil
exec /var/www/fossil /usr/local/cgi-bin/fossil
}
cgi {
match /report/week
exec /usr/local/cgi-bin/report --mode=week
env NO_BANANAS=YES "NAME = Don Quixote"
env MODE=DEV
pass_env JWT_SECRET
}`,
`0:cgi {
match /test
exec /usr/local/cgi-bin/foo
dir /tmp
}`,
`1:cgi {
match /test
exec /usr/local/cgi-bin/foo
dir /tmp /too/many/args
}`,
`1:cgi {
match /test
exec /usr/local/cgi-bin/foo
dir
}`,
`1:cgi {
match /test
exec /usr/local/cgi-bin/foo
dir /tmp
dir /oops
}`,
`1:cgi {
match /foo /foo/script -a
}`,
`1:cgi {
match /foo
}`,
`1:cgi {
match *.lua *.luac
}`,
`1:cgi {
match /*.pl
exec
}`,
`1:cgi {
match /*.pl
except
exec /usr/bin/perl
}`,
`0:cgi {
match /*.pl
except init.pl
exec /usr/bin/perl
inspect
}`,
`1:cgi {
match /*.pl
except init.pl
exec /usr/bin/perl
inspect foo
}`,
`0:cgi {
match /*.pl
except init.pl
exec /usr/bin/perl
}`,
`1:cgi {
match
exec /usr/bin/perl
}`,
`1:cgi {
match
`,
`1:cgi {
exec
`,
`1:cgi /report/daily /usr/bin/perl /usr/share/perl/report --mode=daily
xcgi /foo
cgi /bar /usr/bin/bar -a -b -c`,
`1:cgi`,
`1:cgi
env`,
`1:cgi {
env NO_BANANAS:MAYBE
}`,
`1:cgi {
match /
exec /bin/foo
pass_all_env baz
}`,
`1:cgi {
match
match /foo /bin/foo`,
`1:cgi {
match /report
exe /usr/local/bin/foo
exe /usr/local/bin/bar
}`,
`1:cgi /report/daily`,
}
for j := 0; j < len(directiveList) && err == nil; j++ {
str := directiveList[j]
_, err = configure(str[0:1] != "0", str[2:])
if err != nil {
printf("error [%s], str [%s]\n", err.Error(), str)
}
}
if err != nil {
t.Fatal(err)
}
}