-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.sh
351 lines (282 loc) · 6.71 KB
/
test.sh
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
#!/bin/bash
# netmill tester
TESTS=(
help
http
http_local https_local
http_proxy https_proxy
dns_local dns_upstream
# dns_doh
cert
if
)
if test "$#" == "0" ; then
echo "Usage: test.sh TEST..."
echo "TEST: ${TESTS[@]}"
exit 1
fi
CMDS=("$@")
if test "$1" == "all" ; then
CMDS=("${TESTS[@]}")
fi
if test "$DEBUG" == 1 ; then
DEBUG=-D
else
DEBUG=
fi
set -e
if test "$V" == 1 ; then
set -x
fi
test_help() {
./netmill -help
./netmill cert help
./netmill dns help
./netmill http help
./netmill service help
./netmill req -help
}
test_kill_pid__filename() {
local filename=$1
if test -f $filename ; then
local pid=$(cat $filename)
rm $filename
kill -9 $pid || true
fi
}
test_interrupt_pid__filename() {
local filename=$1
pid=$(cat $filename)
kill $pid
rm $filename
}
test_http_local() {
# (re)start local file server
test_kill_pid__filename nml.pid
./netmill http listen 8080 www . &
echo $! >nml.pid
sleep .5
echo '### Normal download'
./netmill req "127.0.0.1:8080/README.md" -o nmltest/README.md
diff README.md nmltest/README.md
rm nmltest/README.md
echo '### HEAD method'
./netmill req "127.0.0.1:8080/README.md" -check -method HEAD -print_headers
echo '### Directory auto index'
./netmill req "127.0.0.1:8080/" -o nmltest/hs-dir.html
cat nmltest/hs-dir.html
echo '### 2 pipelined requests with 404 response'
cat <<EOF | nc 127.0.0.1 8080
GET /404-1 HTTP/1.1
Host: localhost:8080
GET /404-2 HTTP/1.1
Host: localhost:8080
Connection: close
EOF
echo '### Bad request'
cat <<EOF | nc 127.0.0.1 8080
bad request
EOF
echo '### Redirect'
cat <<EOF | nc -l 127.0.0.1 -p 8081 &
HTTP/1.1 302 Found
Connection: close
Location: http://localhost:8080/README.md
EOF
sleep .5
./netmill req 127.0.0.1:8081/README.md -o nmltest/README.md
diff README.md nmltest/README.md
rm nmltest/README.md
test_interrupt_pid__filename nml.pid
sleep .5
echo '### Connection refused'
./netmill req 127.0.0.1:8080/README.md -o nmltest/README.md || true
}
test_https_local() {
if ! test -f nmltest/htsv.pem ; then
./netmill cert generate \
subject "CN=netmill.test" \
output "nmltest/htsv.pem"
fi
# (re)start local file server
test_kill_pid__filename nml.pid
./netmill $DEBUG http \
listen 4443 \
threads 1 \
cert "nmltest/htsv.pem" \
www "." &
echo $! >nml.pid
sleep .5
echo '### Download via TLS'
rm -f nmltest/README.md
./netmill req "https://127.0.0.1:4443/README.md" -trust -o "nmltest/README.md"
diff README.md nmltest/README.md
rm nmltest/README.md
test_interrupt_pid__filename nml.pid
}
test_http_proxy() {
# (re)start local file server
test_kill_pid__filename nml.pid
./netmill http listen 8080 threads 1 www . &
echo $! >nml.pid
# (re)start proxy server
test_kill_pid__filename nml2.pid
./netmill $DEBUG http listen 8181 threads 1 proxy &
echo $! >nml2.pid
sleep .5
echo '### Normal download via proxy'
./netmill req "127.0.0.1:8080/README.md" -o "nmltest/README.md" \
-proxy "127.0.0.1:8181"
diff README.md nmltest/README.md
echo '### Repeat download via existing (cached) connection'
rm -f nmltest/README.md
./netmill req "127.0.0.1:8080/README.md" -o "nmltest/README.md" \
-proxy "127.0.0.1:8181"
diff README.md nmltest/README.md
rm nmltest/README.md
# echo POST request via proxy
# cat <<EOF | nc 127.0.0.1 8181 >/dev/null
# POST http://localhost:8080/ HTTP/1.1
# Host: localhost:8080
# Content-Length: 18
# Client-Header: Value
# hello from client
# EOF
test_interrupt_pid__filename nml.pid
echo '### Chunked response from upstream'
cat <<EOF | nc -l 127.0.0.1 8080 &
HTTP/1.1 200 OK UPSTREAM
Server: upstream
Transfer-Encoding: chunked
Upstream-Header: Value
Connection: close
2
he
3
llo
0
EOF
sleep .5
rm -f nmltest/README.md
local out=$(./netmill req "127.0.0.1:8080/README.md" -o "nmltest/README.md" \
-proxy "127.0.0.1:8181" \
-print_headers)
grep "HTTP/1.1 200 OK UPSTREAM" <<<$out
grep "Upstream-Header: Value" <<<$out
test "$(cat nmltest/README.md)" == "hello"
kill $! || true
test_interrupt_pid__filename nml2.pid
}
test_https_proxy() {
if ! test -f nmltest/htsv.pem ; then
./netmill cert generate \
subject "CN=netmill.test" \
output "nmltest/htsv.pem"
fi
# (re)start local file server
test_kill_pid__filename nml.pid
./netmill $DEBUG http \
listen 4443 \
threads 1 \
cert "nmltest/htsv.pem" \
www . &
echo $! >nml.pid
# (re)start proxy server
test_kill_pid__filename nml2.pid
./netmill $DEBUG http listen 8181 threads 1 proxy &
echo $! >nml2.pid
sleep .5
echo HTTPS request via HTTP proxy
rm -f nmltest/README.md
# ./netmill req "https://127.0.0.1:4443/README.md" -o "nmltest/README.md" \
# -proxy "127.0.0.1:8181"
curl -s -o "nmltest/README.md" -k \
--proxy http://127.0.0.1:8181 \
https://127.0.0.1:4443/README.md
diff README.md nmltest/README.md
rm nmltest/README.md
test_interrupt_pid__filename nml.pid
test_interrupt_pid__filename nml2.pid
}
test_http() {
test_http_local
test_http_proxy
test_https_local
test_https_proxy
}
test_dns_local() {
# (re)start DNS server with local hosts file
test_kill_pid__filename nml.pid
echo 'block.com' >nmltest/hosts
./netmill $DEBUG dns \
listen 127.0.0.1:5353 \
hosts nmltest/hosts &
echo $! >nml.pid
sleep .5
# resolve blocked host
dig @127.0.0.1 -p 5353 block.com
# resolve unknown host
dig @127.0.0.1 -p 5353 unknown.com
rm nmltest/hosts
test_interrupt_pid__filename nml.pid
}
test_dns_upstream() {
# (re)start DNS server with local hosts file
test_kill_pid__filename nml.pid
echo 'block.com' >nmltest/hosts
./netmill $DEBUG dns \
listen 127.0.0.1:5353 \
hosts nmltest/hosts &
echo $! >nml.pid
# (re)start DNS server with upstream server
test_kill_pid__filename nml2.pid
./netmill $DEBUG dns \
listen 127.0.0.1:5454 \
upstream 127.0.0.1:5353 &
echo $! >nml2.pid
sleep .5
# resolve blocked host
dig @127.0.0.1 -p 5454 block.com
# resolve unknown host
dig @127.0.0.1 -p 5454 unknown.com
rm nmltest/hosts
test_interrupt_pid__filename nml.pid
test_interrupt_pid__filename nml2.pid
}
test_dns_doh() {
echo '8.8.8.8 dns.google' >nmltest/hosts
# (re)start DNS server with DoH upstream server
test_kill_pid__filename nml.pid
./netmill $DEBUG dns \
listen 127.0.0.1:5454 \
hosts nmltest/hosts \
upstream https://dns.google &
echo $! >nml.pid
sleep .5
dig @127.0.0.1 -p 5454 google.com
rm nmltest/hosts
test_interrupt_pid__filename nml.pid
}
test_cert() {
./netmill cert generate \
subject CN=netmill.test \
output nmltest/cert.pem
test -f nmltest/cert.pem
rm nmltest/cert.pem
}
test_if() {
./netmill if
}
test_clean() {
test_kill_pid__filename nml.pid
test_kill_pid__filename nml2.pid
}
mkdir -p nmltest
rm -rf nmltest/*
for cmd in "${CMDS[@]}" ; do
rm -rf ./nmltest/*
test_$cmd
done
test_clean
rm -rf nmltest
echo DONE