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
/
cgi_test.go
367 lines (348 loc) · 9.39 KB
/
cgi_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package cgi
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"runtime"
"strings"
"testing"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
// handleGet returns a cgi handler (which implements ServeHTTP) based on the
// specified directive string. The directive can contain more than one block,
// but only the first is associated with the returned handler.
func handlerGet(directiveStr, rootStr string) (hnd handlerType, err error) {
var c *caddy.Controller
var mids []httpserver.Middleware
var cfg *httpserver.SiteConfig
var ok bool
var midLen int
c = caddy.NewTestController("http", directiveStr)
cfg = httpserver.GetConfig(c)
cfg.Root = rootStr
err = configureServer(c, cfg)
if err == nil {
mids = cfg.Middleware()
midLen = len(mids)
if midLen > 0 {
hnd, ok = mids[0](httpserver.EmptyNext).(handlerType)
if ok {
// .. success
} else {
err = fmt.Errorf("expected middleware handler to be CGI handler")
}
} else {
err = fmt.Errorf("no middlewares present")
}
}
return
}
func TestServe(t *testing.T) {
var err error
var code int
var hnd handlerType
var srv *httptest.Server
directiveList := []string{
`cgi /servertime {.}/test/example`,
`cgi {
match /servertime
except /servertime/1934/*/*
exec {.}/test/example --example
env CGI_GLOBAL=12
empty_env CGI_LOCAL
}`,
}
requestList := []string{
"/servertime",
"/servertime/1930/05/11?name=Edsger%20W.%20Dijkstra",
"/servertime/1934/02/15?name=Niklaus%20Wirth",
"/example.txt",
}
expectStr := `=== Directive 0 ===
cgi /servertime {.}/test/example
--- Request /servertime ---
code [0], error [example error message]
PATH_INFO []
CGI_GLOBAL []
Arg 1 []
QUERY_STRING []
HTTP_TOKEN_CLAIM_USER [quixote]
CGI_LOCAL is unset
--- Request /servertime/1930/05/11?name=Edsger%20W.%20Dijkstra ---
code [0], error [example error message]
PATH_INFO [/1930/05/11]
CGI_GLOBAL []
Arg 1 []
QUERY_STRING [name=Edsger%20W.%20Dijkstra]
HTTP_TOKEN_CLAIM_USER [quixote]
CGI_LOCAL is unset
--- Request /servertime/1934/02/15?name=Niklaus%20Wirth ---
code [0], error [example error message]
PATH_INFO [/1934/02/15]
CGI_GLOBAL []
Arg 1 []
QUERY_STRING [name=Niklaus%20Wirth]
HTTP_TOKEN_CLAIM_USER [quixote]
CGI_LOCAL is unset
--- Request /example.txt ---
=== Directive 1 ===
cgi {
match /servertime
except /servertime/1934/*/*
exec {.}/test/example --example
env CGI_GLOBAL=12
empty_env CGI_LOCAL
}
--- Request /servertime ---
code [0], error [example error message]
PATH_INFO []
CGI_GLOBAL [12]
Arg 1 [--example]
QUERY_STRING []
HTTP_TOKEN_CLAIM_USER [quixote]
CGI_LOCAL is set to []
--- Request /servertime/1930/05/11?name=Edsger%20W.%20Dijkstra ---
code [0], error [example error message]
PATH_INFO [/1930/05/11]
CGI_GLOBAL [12]
Arg 1 [--example]
QUERY_STRING [name=Edsger%20W.%20Dijkstra]
HTTP_TOKEN_CLAIM_USER [quixote]
CGI_LOCAL is set to []
--- Request /servertime/1934/02/15?name=Niklaus%20Wirth ---
--- Request /example.txt ---
`
// Testing the ServeHTTP method requires OS-specific CGI scripts, because a
// system call is made to respond to the request.
if runtime.GOOS == "linux" || runtime.GOOS == "darwin" {
var buf bytes.Buffer
for dirJ := 0; dirJ < len(directiveList) && err == nil; dirJ++ {
hnd, err = handlerGet(directiveList[dirJ], "./test")
if err == nil {
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Header.Set("Token-Claim-User", "quixote")
r.Header.Set("Token-Claim-Language", "en-GB")
r.Header.Add("Token-Claim-Language", "en-AU")
code, err = hnd.ServeHTTP(w, r)
if err != nil {
fmt.Fprintf(&buf, "code [%d], error [%s]\n", code, err)
}
}))
fmt.Fprintf(&buf, "=== Directive %d ===\n%s\n", dirJ, directiveList[dirJ])
for reqJ := 0; reqJ < len(requestList) && err == nil; reqJ++ {
var res *http.Response
fmt.Fprintf(&buf, "--- Request %s ---\n", requestList[reqJ])
res, err = http.Get(srv.URL + requestList[reqJ])
if err == nil {
_, err = buf.ReadFrom(res.Body)
res.Body.Close()
}
}
srv.Close()
}
}
if err == nil {
gotStr := buf.String()
if expectStr != gotStr {
err = errorf("expected %s, got %s\n", expectStr, gotStr)
}
}
if err != nil {
t.Fatalf("%s", err)
}
}
}
func checkEnv(envStr string) (err error) {
// envStr is reported by CGI program invoked with pass_all_env. It looks like
// "key1=val1\nkey2=val2..." Place keys into map and make sure all actual
// environment variables are included in this map. Some environment values get
// legitimately changed for spawned executable, so verify keys only.
list := strings.Split(envStr, "\n")
mp := make(map[string]bool)
for _, pr := range list {
pos := strings.Index(pr, "=")
if pos > 0 {
mp[pr[:pos]] = true
}
}
actualList := os.Environ()
actualLen := len(actualList)
for j := 0; j < actualLen && err == nil; j++ {
actualStr := actualList[j]
pos := strings.Index(actualStr, "=")
if pos > 0 {
k := actualStr[:pos]
_, ok := mp[k]
if !ok {
// err = fmt.Errorf("environment key \"%s\" not found in CGI environment", k)
fmt.Printf("environment key \"%s\" not found in CGI environment\n", k)
}
}
}
return
}
func TestDir(t *testing.T) {
var err error
var code int
var rsp string
var hnd handlerType
var srv *httptest.Server
request := `/tmpdir`
directive := `cgi {
match /tmpdir
exec {.}/test/showdir
dir /tmp
}`
// Testing the ServeHTTP method requires OS-specific CGI scripts, because a
// system call is made to respond to the request.
if runtime.GOOS == "linux" {
var buf bytes.Buffer
hnd, err = handlerGet(directive, "./test")
if err == nil {
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
code, err = hnd.ServeHTTP(w, r)
if err != nil {
fmt.Fprintf(&buf, "code [%d], error [%s]\n", code, err)
}
}))
var res *http.Response
res, err = http.Get(srv.URL + request)
if err == nil {
_, err = buf.ReadFrom(res.Body)
res.Body.Close()
}
srv.Close()
}
if err == nil {
rsp = strings.TrimSpace(buf.String())
if "/tmp" != rsp {
err = fmt.Errorf("expecting \"/tmp\", got \"%s\"", rsp)
}
}
if err != nil {
t.Fatalf("%s", err)
}
}
}
func TestPassAll(t *testing.T) {
var err error
var code int
var hnd handlerType
var srv *httptest.Server
request := `/full`
directive := `cgi {
match /full
exec {.}/test/fullenv
pass_all_env
}`
// Testing the ServeHTTP method requires OS-specific CGI scripts, because a
// system call is made to respond to the request.
if runtime.GOOS == "linux" {
var buf bytes.Buffer
hnd, err = handlerGet(directive, "./test")
if err == nil {
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
code, err = hnd.ServeHTTP(w, r)
if err != nil {
fmt.Fprintf(&buf, "code [%d], error [%s]\n", code, err)
}
}))
fmt.Fprintf(&buf, "=== Directive ===\n%s\n", directive)
var res *http.Response
fmt.Fprintf(&buf, "--- Request %s ---\n", request)
res, err = http.Get(srv.URL + request)
if err == nil {
_, err = buf.ReadFrom(res.Body)
res.Body.Close()
}
srv.Close()
}
if err == nil {
err = checkEnv(buf.String())
}
if err != nil {
t.Fatalf("%s", err)
}
}
}
func TestMatches(t *testing.T) {
var ok bool
var prefixStr, suffixStr string
// [request, pattern, expected success:1/expected error:0, prefix, suffix]
list := [][]string{
{"/foo/bar/baz", "/foo", "1", "/foo", "/bar/baz"},
{"/foo/bar/baz", "/foo/*/baz", "1", "/foo/bar/baz", ""},
{"/foo/bar/baz", "/foo/bar", "1", "/foo/bar", "/baz"},
{"/foo/bar/baz", "foo/bar", "0", "", ""},
}
for _, rec := range list {
ok, prefixStr, suffixStr = match(rec[0], []string{rec[1]})
if ok {
if rec[2] == "0" || rec[3] != prefixStr || rec[4] != suffixStr {
t.Fatalf("expected mismatch for \"%s\" and \"%s\"", rec[0], rec[1])
}
} else {
if rec[2] == "1" {
t.Fatalf("expected match for \"%s\" and \"%s\"", rec[0], rec[1])
}
}
}
}
func TestExceptions(t *testing.T) {
// [request, except pattern, expected success:1/expected error:0]
list := [][]string{
{"/foo/bar/baz.png", "/*/*/*.png", "1"},
{"/foo/bar/baz.png", "/foo/*/baz*", "1"},
{"/foo/bar/baz.png", "/foo/bar/baz.jpg", "0"},
{"/foo/bar/baz.png", "foo/bar", "0"},
}
for _, rec := range list {
ok := excluded(rec[0], []string{rec[1]})
if ok && rec[2] == "0" {
t.Fatalf("unexpected exception for \"%s\" and \"%s\"", rec[0], rec[1])
} else if !ok && rec[2] == "1" {
t.Fatalf("expected exception for \"%s\" and \"%s\"", rec[0], rec[1])
}
}
}
func TestInspect(t *testing.T) {
var err error
var hnd handlerType
var srv *httptest.Server
var buf bytes.Buffer
block := `cgi {
match /foo/*
exec bar --baz --quux
pass_env HOME
env VERY_LONG_KEY_INTENDED_TO_TEST_INSPECTION_REPORT=foo
inspect
}`
hnd, err = handlerGet(block, "./test")
if err == nil {
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, hndErr := hnd.ServeHTTP(w, r)
if err == nil && hndErr != nil {
err = hndErr
}
}))
var res *http.Response
res, err = http.Get(srv.URL + "/foo/bar.tcl")
if err == nil {
_, err = buf.ReadFrom(res.Body)
if err == nil {
str := buf.String()
if !strings.Contains(str, "CGI for Caddy") {
err = fmt.Errorf("unexpected response for \"inspect\" request")
}
}
res.Body.Close()
}
srv.Close()
}
if err != nil {
t.Fatalf("%s", err)
}
}