Add to git.
[rws.git] / test_rws.sh
1 #!/bin/sh -
2 #
3 # Simple shell script which tests whether rws is basically working.
4 # - by Richard W.M. Jones <rich@annexia.org>
5 #
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Library General Public
8 # License as published by the Free Software Foundation; either
9 # version 2 of the License, or (at your option) any later version.
10 #
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # Library General Public License for more details.
15 #
16 # You should have received a copy of the GNU Library General Public
17 # License along with this library; if not, write to the Free
18 # Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #
20 # $Id: test_rws.sh,v 1.4 2003/02/05 23:02:51 rich Exp $
21
22 # A random, hopefully free, port.
23 port=14136
24
25 # We need either 'wget' or 'nc'.
26 wget --help >/dev/null 2>&1
27 if [ $? -eq 0 ]; then
28         mode=wget
29 else
30         nc -h 2>/dev/null
31         if [ $? -eq 1 ]; then
32                 mode=nc
33         else
34                 echo "Please install either 'wget' or 'nc'."
35                 echo "This test did not run."
36                 exit 0
37         fi
38 fi
39
40 echo "Using $mode to fetch URLs."
41
42 tmp=/tmp/rws.$$
43 rm -rf $tmp
44 mkdir $tmp
45
46 # Create the configuration directory.
47 mkdir -p $tmp/etc/rws/hosts
48
49 cat > $tmp/etc/rws/rws.conf <<EOF
50 mime types file: $tmp/etc/mime.types
51 error log: $tmp/log/error_log
52 access log: $tmp/log/access_log
53 icon for application/*:         /icons/binary.gif 20x22 "Application"
54 icon for application/x-tar:     /icons/tar.gif 20x22 "Unix tape archive file"
55 icon for application/x-gzip:    /icons/compressed.gif 20x22 "Compressed file"
56 icon for application/zip:       /icons/compressed.gif 20x22 "Compressed file"
57 icon for audio/*:               /icons/sound1.gif 20x22 "Audio file"
58 icon for image/*:               /icons/image2.gif 20x22 "Image"
59 icon for message/*:             /icons/quill.gif 20x22 "Mail message"
60 icon for text/*:                /icons/text.gif 20x22 "Text file"
61 icon for video/*:               /icons/movie.gif 20x22 "Video file"
62 no type icon:                   /icons/generic.gif 20x22 "File"
63 unknown icon:                   /icons/unknown.gif 20x22 "Unknown file type"
64 directory icon:                 /icons/dir.gif 20x22 "Directory"
65 link icon:                      /icons/link.gif 20x22 "Symbolic link"
66 special icon:                   /icons/sphere2.gif 20x22 "Special file"
67 EOF
68
69 cat > $tmp/etc/rws/hosts/default <<EOF
70 alias /
71         path:   $tmp/html
72         show:   1
73         list:   1
74 end alias
75 alias /so-bin/
76         path:   $tmp/so-bin
77         exec so: 1
78 end alias
79 alias /cgi-bin/
80         path:   $tmp/cgi-bin
81         exec:   1
82 end alias
83 EOF
84 (cd $tmp/etc/rws/hosts; ln -s default localhost:$port)
85
86 cat > $tmp/etc/mime.types <<EOF
87 text/html html
88 EOF
89
90 mkdir $tmp/log
91
92 # Create the content directory.
93 mkdir $tmp/html
94 mkdir $tmp/html/files
95
96 cat > $tmp/html/index.html <<EOF
97 <html>
98 <body>
99 This is the test page.
100 MAGIC-1234
101 </body>
102 </html>
103 EOF
104
105 cp *.o $tmp/html/files
106
107 # Create the so-bin directory.
108 mkdir $tmp/so-bin
109 cp examples/show_params.so $tmp/so-bin
110 chmod 0755 $tmp/so-bin/show_params.so
111
112 # Create the CGI directory
113 mkdir $tmp/cgi-bin
114 cat > $tmp/cgi-bin/test.sh <<EOF
115 #!/bin/sh
116 echo "HTTP/1.0 200 OK"
117 echo "Content-Type: text/plain"
118 echo
119 echo "This is the test CGI script"
120 echo "MAGIC-4321"
121 EOF
122 chmod 0755 $tmp/cgi-bin/test.sh
123
124 # Try to start up the server.
125 ./rwsd -p $port -f -a 127.0.0.1 -C $tmp/etc/rws &
126 rws_pid=$!; sleep 1
127
128 # Did it start up?
129 if kill -0 $rws_pid; then :;
130 else
131         echo "Server did not start up. Check any preceeding messages."
132         exit 1
133 fi
134
135 echo "Started rwsd instance."
136
137 # Fetch function: fetch (server, port, serverpath, file)
138 fetch()
139 {
140         server=$1
141         port=$2
142         serverpath=$3
143         file=$4
144
145         echo "Fetching http://$server:$port$serverpath ..."
146
147         if [ $mode = "nc" ]; then
148                 nc $server $port > $file <<EOF
149 GET $serverpath HTTP/1.0
150 User-Agent: test_rws.sh
151
152 EOF
153                 if [ $? -ne 0 ]; then exit 1; fi
154         else            # wget mode
155                 wget -q -O $file http://$server:$port$serverpath
156                 if [ $? -ne 0 ]; then kill $rws_pid; exit 1; fi
157         fi
158 }
159
160 # Fetch the test file.
161 fetch localhost $port /index.html $tmp/downloaded
162 if grep -q MAGIC-1234 $tmp/downloaded; then :;
163 else
164         echo "Download of a simple file failed!"
165         echo "Look at $tmp/downloaded for clues."
166         kill $rws_pid
167         exit 1
168 fi
169 rm $tmp/downloaded
170
171 # Fetch the directory listing.
172 fetch localhost $port /files/ $tmp/downloaded
173 if grep -q main.o $tmp/downloaded; then :;
174 else
175         echo "Download of a directory listing failed!"
176         echo "Look at $tmp/downloaded for clues."
177         kill $rws_pid
178         exit 1
179 fi
180 rm $tmp/downloaded
181
182 # Test shared object scripts.
183 echo "Testing shared object scripts."
184 fetch localhost $port '/so-bin/show_params.so?key=value' $tmp/downloaded
185 if grep -q 'This is the show_params shared object script' $tmp/downloaded
186 then :;
187 else
188         echo "Execution of a shared object script failed!"
189         echo "Look at $tmp/downloaded for clues."
190         kill $rws_pid
191         exit 1
192 fi
193 rm $tmp/downloaded
194
195 # Test CGI scripts.
196 echo "Testing CGI scripts."
197 fetch localhost $port /cgi-bin/test.sh $tmp/downloaded
198 if grep -q MAGIC-4321 $tmp/downloaded; then :;
199 else
200         echo "Execution of a CGI script failed!"
201         echo "Look at $tmp/downloaded for clues."
202         kill $rws_pid
203         exit 1
204 fi
205 rm $tmp/downloaded
206
207 echo "Test completed OK."
208
209 # Kill the server.
210 kill $rws_pid
211
212 # Remove the temporary directory.
213 rm -rf $tmp
214
215 exit 0