188d65041a2696a6f5a058c747762120c6276928
[tripe] / server / tests.at
1 ### -*-autotest-*-
2 ###
3 ### Test script for the main server
4 ###
5 ### (c) 2008 Straylight/Edgeware
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of Trivial IP Encryption (TrIPE).
11 ###
12 ### TrIPE is free software; you can redistribute it and/or modify
13 ### it under the terms of the GNU General Public License as published by
14 ### the Free Software Foundation; either version 2 of the License, or
15 ### (at your option) any later version.
16 ###
17 ### TrIPE is distributed in the hope that it will be useful,
18 ### but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ### GNU General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with TrIPE; if not, write to the Free Software Foundation,
24 ### Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 m4_define([nl], [
27 ])
28
29 ## Configure a directory ready for use by tripe.
30 m4_define([SETUPDIR], [
31 cp $abs_top_srcdir/t/keyring-$1 ./keyring
32 key extract -f-secret keyring.pub
33 ])
34
35 ## Running standard programs with useful options.
36 m4_define([TRIPE],
37 [env TRIPE_PRIVHELPER=$abs_top_builddir/priv/tripe-privhelper \
38 $abs_top_builddir/server/tripe -F -d. -aadmin -p0 -b127.0.0.1 -talice])
39 m4_define([TRIPECTL], [$abs_top_builddir/client/tripectl -d. -aadmin])
40 m4_define([USLIP], [$abs_top_builddir/uslip/tripe-uslip])
41
42 ## Sequences. (These are used for testing the replay protection machinery.)
43 m4_define([R32], [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 dnl
44 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31])
45 m4_define([P32], [21 26 14 12 25 18 2 27 10 31 24 29 0 20 17 11 dnl
46 8 3 7 23 19 1 13 30 6 9 5 22 15 28 16 4])
47
48 ###--------------------------------------------------------------------------
49 ### Scaffolding for running a TrIPE server.
50
51 m4_define([WITH_TRIPEX], [
52
53 ## Remove the status file. This is how we notice that the server's died.
54 rm -f $1/server-status
55 > $1/expected-server-output
56 > $1/expected-server-errors
57
58 ## Autotest writes crap to standard output, which we don't want going to the
59 ## server. So keep a copy of the standard output, do the pipe, and recover
60 ## the old stdout inside the group.
61 exec 3>&1
62 { (
63 exec 1>&3 3>&-
64
65 ## Wait for the socket to appear. Watch for the server crashing during
66 ## initialization. Busy waiting is evil, but it's the best I can do and
67 ## not sleep for ages. (Yes, a second on each test group is a long time.)
68 while test ! -r $1/server-status && test ! -S $1/admin; do :; done
69
70 ## Test body...
71 $3
72
73 ## End of the test, now run the server. There's an awful hack here. If a
74 ## process running under strace exits with a signal, then strace will kill
75 ## itself with the same signal -- and therefore clobber the original
76 ## process's core file. So we arrange to run strace in one directory and
77 ## have the child process run in another.
78 ) && :; } | {
79 cd $1
80 mkdir -p strace-hack/
81 echo TRIPE $2 >&2
82 (cd strace-hack/
83 ulimit -c hard >/dev/null 2>&1
84 strace -f -o ../tripe.trace \
85 TRIPE -d.. $2 >../server-output 2>../server-errors)
86 stat=$?
87 echo $stat >server-status
88 if test $stat -ne 0; then
89 echo "exit status: $stat" >>server-errors
90 fi
91 }
92 exec 3>&-
93
94 ## Now check that the server's output matches our expectations.
95 mv $1/expected-server-output expout
96 mv $1/expected-server-errors experr
97 AT_CHECK([cat $1/server-output; cat >&2 $1/server-errors],,
98 [expout], [experr])
99 ])
100
101 m4_define([WITH_TRIPE], [WITH_TRIPEX([.], [$1], [$2])])
102
103 m4_define([WITH_2TRIPES],
104 [WITH_TRIPEX([$1], [$3 $4], [WITH_TRIPEX([$2], [$3 $5], [$6])])])
105
106 ###--------------------------------------------------------------------------
107 ### Very unpleasant coprocess handling.
108
109 ## COPROCESSES(TAG, PROC-A, PROC-B)
110 m4_define([COPROCESSES], [dnl
111 rm -f pipe-$1; mknod pipe-$1 p
112 { { $2 nl } <pipe-$1 | { $3 nl } >pipe-$1; } dnl
113 ])
114
115 ## TRIPECTL_INTERACT(ARGS, SHELLSTUFF)
116 m4_define([TRIPECTL_INTERACT], [
117 exec 3>&1
118 COPROCESSES([client], [exec 4>&1 1>&3 $1], [TRIPECTL $2])
119 ])
120
121 ## TRIPECTL_COMMAND(CMD, EXPECT)
122 m4_define([TRIPECTL_COMMAND], [
123 AT_CHECK([
124 m4_if([$1], [!], [:], [echo "$1" >&4])
125 read line
126 case "$line" in
127 "$2") ;;
128 *) echo 2>&1 "submitted $1: expected $2, found $line"; exit 1 ;;
129 esac
130 ])
131 exec 3>&-
132 ])
133
134 ###--------------------------------------------------------------------------
135 ### Make sure the thing basically works.
136
137 AT_SETUP([server basics])
138 SETUPDIR([ec])
139 AT_CHECK([echo port | TRIPE -p54321],, [INFO 54321[]nl[]OK[]nl])
140 AT_CLEANUP
141
142 ###--------------------------------------------------------------------------
143 ### Challenges.
144
145 AT_SETUP([server challenges])
146 AT_KEYWORDS([chal])
147 SETUPDIR([ec])
148
149 WITH_TRIPE(, [
150 ## A simple test.
151 AT_CHECK([chal=$(TRIPECTL GETCHAL); TRIPECTL checkchal $chal])
152
153 ## A wrong challenge. (This was valid once, but the probablity that the
154 ## server chose the same key is negligible.)
155 AT_CHECK([TRIPECTL checkchal AAAAAHyoOL+HMaE0Y9B3ivuszt0], [1],,
156 [tripectl: invalid-challenge[]nl])
157 echo WARN CHAL incorrect-tag >>expected-server-output
158
159 ## A duplicated challenge.
160 AT_CHECK([
161 chal=$(TRIPECTL GETCHAL)
162 TRIPECTL CHECKCHAL $chal
163 TRIPECTL CHECKCHAL $chal
164 ], [1],, [tripectl: invalid-challenge[]nl])
165 echo WARN CHAL replay duplicated-sequence >>expected-server-output
166
167 ## Out-of-order reception. There should be a window of 32 challenges; we
168 ## make 33 and check them in a strange order.
169 rm -f experr
170 echo "tripectl: invalid-challenge" >>experr
171 echo "WARN CHAL replay old-sequence" >>expected-server-output
172 for i in P32; do
173 echo "tripectl: invalid-challenge" >>experr
174 echo "WARN CHAL replay duplicated-sequence" >>expected-server-output
175 done
176 AT_CHECK([
177
178 ## Make the challenges.
179 for i in old R32; do TRIPECTL GETCHAL >chal-$i || exit 2; done
180
181 ## Now check them back.
182 for i in P32; do TRIPECTL CHECKCHAL $(cat chal-$i) || exit 3; done
183
184 ## Check the one which should have fallen off the front.
185 TRIPECTL CHECKCHAL $(cat chal-old) && exit 4
186
187 ## And make sure that the others are now considered duplicates.
188 for i in R32; do TRIPECTL CHECKCHAL $(cat chal-$i) && exit 5; done
189
190 ## All done: tidy cruft away.
191 rm -f chal-*
192 exit 0
193 ], [0],, [experr])
194 ])
195
196 AT_CLEANUP
197
198 ###--------------------------------------------------------------------------
199 ### Communication.
200
201 AT_SETUP([server communication])
202 AT_KEYWORDS([comm])
203 export TRIPE_SLIPIF=USLIP
204
205 for i in alice bob; do (mkdir $i; cd $i; SETUPDIR([ec])); done
206
207 WITH_2TRIPES([alice], [bob], [-nslip], [-talice], [-tbob], [
208
209 AT_CHECK([TRIPECTL -dalice PORT],, [stdout])
210 mv stdout alice/port
211
212 AT_CHECK([TRIPECTL -dbob PORT],, [stdout])
213 mv stdout bob/port
214
215 ## Watch for the key-exchange completion announcement, and then exit.
216 COPROCESSES([wait], [
217 echo WATCH +n
218 while read line; do
219 case "$line" in
220 OK) ;;
221 "NOTE KXDONE "*) break ;;
222 NOTE*) ;;
223 *) exit 63 ;;
224 esac
225 done
226 ], [
227 TRIPECTL -dalice
228 ]) &
229
230 ## Don't panic if you don't see the unexpected-source warning. It happens
231 ## for me, but it's not important either way.
232 AT_CHECK([TRIPECTL -dalice ADD bob INET 127.0.0.1 $(cat bob/port)])
233 echo >>bob/expected-server-output \
234 "WARN PEER - unexpected-source INET 127.0.0.1 $(cat alice/port)"
235 AT_CHECK([TRIPECTL -dbob ADD -key alice not-alice \
236 INET 127.0.0.1 $(cat alice/port)])
237
238 ## Check transport pinging.
239 AT_CHECK([TRIPECTL -dalice PING bob],, [ignore])
240 AT_CHECK([TRIPECTL -dbob PING not-alice],, [ignore])
241
242 ## Wait for the completion announcement.
243 wait
244
245 ## Check encrypted pinging.
246 AT_CHECK([TRIPECTL -dalice EPING bob],, [ignore])
247 AT_CHECK([TRIPECTL -dbob EPING not-alice],, [ignore])
248
249 ## Check that packets can flow from one to the other.
250 AT_CHECK([echo "from alice" | USLIP -p alice/bob])
251 AT_CHECK([USLIP -g bob/not-alice],, [from alice[]nl])
252
253 AT_CHECK([echo "from bob" | USLIP -p bob/not-alice])
254 AT_CHECK([USLIP -g alice/bob],, [from bob[]nl])
255 ])
256
257 AT_CLEANUP
258
259 ###--------------------------------------------------------------------------
260 ### Services.
261
262 AT_SETUP([server services])
263 AT_KEYWORDS([svc])
264 SETUPDIR([ec])
265
266 WITH_TRIPE(, [
267
268 ## Make sure it's not running yet.
269 AT_CHECK([TRIPECTL SVCENSURE test], [1],,
270 [tripectl: unknown-service test[]nl])
271
272 ## Run a simple service.
273 rm -f svc-test-running tripectl-status
274 COPROCESSES([svc], [
275 echo SVCCLAIM test 1.0.0
276 read line
277 case "$line" in
278 OK)
279 ;;
280 *)
281 echo >&2 "SVCCLAIM failed: $line"
282 exit 1
283 ;;
284 esac
285 echo ok >svc-test-running
286 while read line; do
287 set -- $line
288 case "$[]1,$[]3,$[]4" in
289 SVCJOB,test,HELP)
290 echo SVCINFO try not to use this service for anything useful
291 echo SVCOK $[]2
292 ;;
293 SVCJOB,test,GOOD)
294 echo SVCOK $[]2
295 ;;
296 SVCJOB,test,BAD)
297 echo SVCFAIL $[]2 this-command-always-fails
298 ;;
299 SVCJOB,test,UGLY)
300 tag=$2
301 while read line; do
302 set -- $line
303 case "$[]1,$[]2,$[]3,$[]4" in
304 SVCCANCEL,$tag,,) break ;;
305 SVCJOB,*,test,ESCAPE)
306 echo >&2 "attempt to escape from alkatraz"
307 exit 1
308 ;;
309 esac
310 done
311 ;;
312 SVCJOB,test,FIRST)
313 firsttag=$[]2
314 ;;
315 SVCJOB,test,SECOND)
316 echo SVCOK $firsttag
317 echo SVCOK $[]2
318 ;;
319 SVCJOB,*)
320 echo SVCFAIL $[]2 unknown-svc-command $[]4
321 ;;
322 SVCCLAIM,*)
323 break
324 ;;
325 OK,* | INFO,*)
326 ;;
327 FAIL,*)
328 echo "failure in service: $line" >&2
329 ;;
330 esac
331 done
332 ], [
333 TRIPECTL; echo $? >tripectl-status
334 ]) 2>tripectl-errors &
335
336 ## Wait until it starts up.
337 while test ! -r svc-test-running && test ! -r tripectl-status; do :; done
338
339 ## Make sure it's running.
340 AT_CHECK([TRIPECTL SVCQUERY test],, [name=test version=1.0.0[]nl])
341
342 ## Try some simple commands.
343 AT_CHECK([TRIPECTL SVCSUBMIT test GOOD])
344 AT_CHECK([TRIPECTL SVCSUBMIT test BAD], [1],,
345 [tripectl: this-command-always-fails[]nl])
346
347 ## And now with commands in the background.
348 TRIPECTL_INTERACT([
349 TRIPECTL_COMMAND([SVCSUBMIT test GOOD], [OK])
350 TRIPECTL_COMMAND([SVCSUBMIT -background foo test UGLY], [BGDETACH foo])
351 TRIPECTL_COMMAND([BGCANCEL foo], [OK])
352 TRIPECTL_COMMAND([SVCSUBMIT test ESCAPE],
353 [FAIL unknown-svc-command ESCAPE])
354 ])
355
356 ## Out-of-order completion.
357 TRIPECTL_INTERACT([
358 TRIPECTL_COMMAND([SVCSUBMIT -background one test FIRST], [BGDETACH one])
359 TRIPECTL_COMMAND([SVCSUBMIT -background two test SECOND], [BGDETACH two])
360 TRIPECTL_COMMAND([!], [BGOK one])
361 TRIPECTL_COMMAND([!], [BGOK two])
362 ])
363
364 ## All done.
365 exit 0
366 ])
367
368 AT_CLEANUP
369
370 ###----- That's all, folks --------------------------------------------------