Blob


1 #!/bin/sh
3 set -e
5 # usage: config <global config> <stuff for localhost>
6 # generates a configuration file reg.conf
7 config() {
8 cat <<EOF > reg.conf
9 daemon off
10 ipv6 off
11 port 10965
12 $1
13 server "localhost" {
14 cert "cert.pem"
15 key "key.pem"
16 root "testdata"
17 $2
18 }
19 EOF
20 }
22 checkconf() {
23 ./../gmid -n -c reg.conf
24 }
26 # usage: get <path>
27 # return the body of the request on stdout
28 get() {
29 (./gg.py "$1" 10965 | sed 1d) || true
30 }
32 # usage: head <path>
33 # return the meta response line on stdout
34 head() {
35 (./gg.py "$1" 10965 | sed 1q) || true
36 }
38 run() {
39 # filter out logs for GET requests
40 (./../gmid -c reg.conf 2>&1 | grep -v GET) >&2 &
41 pid=$!
42 }
44 # usage: check [exit-message]
45 # check if gmid is still running
46 check() {
47 if ! ps $pid >/dev/null; then
48 echo ${1:-"gmid crashed?"}
49 exit 1
50 fi
51 }
53 # quit gmid
54 quit() {
55 pkill gmid || true
56 wait || true
57 }
59 # usage: eq a b errmsg
60 # if a and b aren't equal strings, exit with errmsg
61 eq() {
62 if ! [ "$1" = "$2" ]; then
63 echo "$3: \"$1\" not equal \"$2\""
64 exit 1
65 fi
66 }
68 onexit() {
69 rm -f bigfile bigfile.sha
70 quit
71 }
73 # tests
75 trap 'onexit' INT TERM EXIT
77 endl=`printf "\r\n"`
78 lf=`echo`
80 config "" ""
81 checkconf
82 run
84 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
85 eq "$(get /)" "# hello world$ln" "Unexpected body for /"
86 echo OK GET /
88 eq "$(head /foo)" "51 not found" "Unexpected head /foo"
89 eq "$(get /foo)" "" "Unexpected body /foo"
90 echo OK GET /foo
92 # should redirect if asked for a directory but without the trailing /
93 eq "$(head /dir)" "30 /dir/" "Unexpected redirect for /dir"
94 eq "$(get /dir)" "" "Unexpected body for redirect"
95 echo OK GET /dir
97 # 51 for a directory without index.gmi
98 eq "$(head /dir/)" "51 not found" "Unexpected head for /"
99 eq "$(get /dir/)" "" "Unexpected body for error"
100 echo OK GET /dir/
102 eq "$(head /dir/foo.gmi)" "20 text/gemini" "Unexpected head for /dir/foo.gmi"
103 eq "$(get /dir/foo.gmi)" "# hello world$ln" "Unexpected body for /dir/foo.gmi"
104 echo OK GET /dir/foo.gmi
106 # try a big file
107 eq "$(head /bigfile)" "20 application/octet-stream" "Unexpected head for /bigfile"
108 get /bigfile > bigfile
109 ./sha bigfile bigfile.sha
110 eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /bigfile"
111 echo OK GET /bigfile
113 # shouldn't be executing cgi scripts
114 eq "$(head /hello)" "20 application/octet-stream" "Unexpected head for /hello"
115 echo OK GET /hello
117 check "should be running"
118 quit
120 # try with custom mime
121 config 'mime "text/x-funny-text" "gmi"' 'default type "application/x-trash"'
122 checkconf
123 run
125 eq "$(head /)" "20 text/x-funny-text" "Unexpected head for /"
126 echo OK GET / with custom mime
128 eq "$(head /hello)" "20 application/x-trash" "Unexpected head for /hello"
129 echo OK GET /hello with custom mime
131 check "should be running"
132 quit
134 # try with custom lang
135 config '' 'lang "it"'
136 checkconf
137 run
139 eq "$(head /)" "20 text/gemini; lang=it" "Unexpected head for /"
140 echo OK GET / with custom lang
142 check "should be running"
143 quit
145 # finally try with CGI scripts
146 config '' 'cgi ""'
147 checkconf
148 run
150 eq "$(head /hello)" "20 text/gemini" "Unexpected head for /hello"
151 eq "$(get /hello)" "# hello world$ln" "Unexpected body for /hello"
152 echo OK GET /hello with cgi
154 eq "$(head /slow)" "20 text/gemini" "Unexpected head for /slow"
155 eq "$(get /slow)" "# hello world$ln" "Unexpected body for /slow"
156 echo OK GET /slow with cgi
158 eq "$(head /err)" "" "Unexpected head for /err"
159 eq "$(get /err)" "" "Unexpected body for /err"
160 echo OK GET /err with cgi
162 eq "$(head /invalid | wc -c | xargs)" 2049 "Unexpected body for /invalid"
163 eq "$(get /invalid)" "" "Unexpected body for /invalid"
164 echo OK GET /invalid with cgi
166 check "should be running"
167 quit