chiark / gitweb /
server/tun-slip.c: Pass correct stdout to child process.
[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 ## Keep Autotest writes crap to standard output, which we don't want going to
59 ## the server.  So keep a copy of the standard output, do the pipe, and
60 ## recover 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.
74 ) && :; } | {
75   cd $1
76   echo TRIPE $2 >&2
77   strace -f -o tripe.trace TRIPE $2 >server-output 2>server-errors
78   stat=$?
79   echo $stat >server-status
80   if test $stat -ne 0; then
81     echo "exit status: $stat" >>server-errors
82   fi
83 }
84 exec 3>&-
85
86 ## Now check that the server's output matches our expectations.
87 mv $1/expected-server-output expout
88 mv $1/expected-server-errors experr
89 AT_CHECK([cat $1/server-output; cat >&2 $1/server-errors],,
90          [expout], [experr])
91 ])
92
93 m4_define([WITH_TRIPE], [WITH_TRIPEX([.], [$1], [$2])])
94
95 m4_define([WITH_2TRIPES],
96           [WITH_TRIPEX([$1], [$3 $4], [WITH_TRIPEX([$2], [$3 $5], [$6])])])
97
98 ###--------------------------------------------------------------------------
99 ### Very unpleasant coprocess handling.
100
101 ## COPROCESSES(TAG, PROC-A, PROC-B)
102 m4_define([COPROCESSES], [dnl
103 rm -f pipe-$1; mknod pipe-$1 p
104 { { $2 nl } <pipe-$1 | { $3 nl } >pipe-$1; } dnl
105 ])
106
107 ## TRIPECTL_INTERACT(ARGS, SHELLSTUFF)
108 m4_define([TRIPECTL_INTERACT], [
109   exec 3<&1
110   COPROCESSES([client], [exec 4>&1 1>&3 $2], [TRIPECTL $1])
111 ])
112
113 ## TRIPECTL_COMMAND(CMD, EXPECT)
114 m4_define([TRIPECTL_COMMAND], [
115   AT_CHECK([
116     m4_if([$1], [!], [:], [echo "$1" >&4])
117     read line
118     case "$line" in
119       "$2") ;;
120       *) echo 2>&1 "submitted $1: expected $2, found $line"; exit 1 ;;
121     esac
122   ])
123   exec 3>&-
124 ])
125
126 ###--------------------------------------------------------------------------
127 ### Make sure the thing basically works.
128
129 AT_SETUP([server basics])
130 SETUPDIR([ec])
131 AT_CHECK([echo port | TRIPE -p54321],, [INFO 54321[]nl[]OK[]nl])
132 AT_CLEANUP
133
134 ###--------------------------------------------------------------------------
135 ### Challenges.
136
137 AT_SETUP([server challenges])
138 AT_KEYWORDS([chal])
139 SETUPDIR([ec])
140
141 WITH_TRIPE(, [
142   ## A simple test.
143   AT_CHECK([chal=$(TRIPECTL GETCHAL); TRIPECTL checkchal $chal])
144
145   ## A wrong challenge.  (This was valid once, but the probablity that the
146   ## server chose the same key is negligible.)
147   AT_CHECK([TRIPECTL checkchal AAAAAHyoOL+HMaE0Y9B3ivuszt0], [1],,
148            [tripectl: invalid-challenge[]nl])
149   echo WARN CHAL incorrect-tag >>expected-server-output
150
151   ## A duplicated challenge.
152   AT_CHECK([
153     chal=$(TRIPECTL GETCHAL)
154     TRIPECTL CHECKCHAL $chal
155     TRIPECTL CHECKCHAL $chal
156   ], [1],, [tripectl: invalid-challenge[]nl])
157   echo WARN CHAL replay duplicated-sequence >>expected-server-output
158
159   ## Out-of-order reception.  There should be a window of 32 challenges; we
160   ## make 33 and check them in a strange order.
161   rm -f experr
162   echo "tripectl: invalid-challenge" >>experr
163   echo "WARN CHAL replay old-sequence" >>expected-server-output
164   for i in P32; do
165     echo "tripectl: invalid-challenge" >>experr
166     echo "WARN CHAL replay duplicated-sequence" >>expected-server-output
167   done
168   AT_CHECK([
169
170     ## Make the challenges.
171     for i in old R32; do TRIPECTL GETCHAL >chal-$i || exit 2; done
172
173     ## Now check them back.
174     for i in P32; do TRIPECTL CHECKCHAL $(cat chal-$i) || exit 3; done
175
176     ## Check the one which should have fallen off the front.
177     TRIPECTL CHECKCHAL $(cat chal-old) && exit 4
178
179     ## And make sure that the others are now considered duplicates.
180     for i in R32; do TRIPECTL CHECKCHAL $(cat chal-$i) && exit 5; done
181
182     ## All done: tidy cruft away.
183     rm -f chal-*
184     exit 0
185   ], [0],, [experr])
186 ])
187
188 AT_CLEANUP
189
190 ###--------------------------------------------------------------------------
191 ### Communication.
192
193 AT_SETUP([server communication])
194 AT_KEYWORDS([comm])
195 export TRIPE_SLIPIF=USLIP
196
197 for i in alice bob; do (mkdir $i; cd $i; SETUPDIR([ec])); done
198
199 WITH_2TRIPES([alice], [bob], [-nslip], [-talice], [-tbob], [
200
201   AT_CHECK([TRIPECTL -dalice PORT],, [stdout])
202   mv stdout alice/port
203
204   AT_CHECK([TRIPECTL -dbob PORT],, [stdout])
205   mv stdout bob/port
206
207   ## Watch for the key-exchange completion announcement, and then exit.
208   COPROCESSES([wait], [
209     echo WATCH +n
210     while read line; do
211       case "$line" in
212         OK) ;;
213         "NOTE KXDONE "*) break ;;
214         NOTE*) ;;
215         *) exit 63 ;;
216       esac
217     done
218   ], [
219     TRIPECTL -dalice
220   ]) &
221
222   ## Don't panic if you don't see the unexpected-source warning.  It happens
223   ## for me, but it's not important either way.
224   AT_CHECK([TRIPECTL -dalice ADD bob INET 127.0.0.1 $(cat bob/port)])
225   echo >>bob/expected-server-output \
226     "WARN PEER - unexpected-source INET 127.0.0.1 $(cat alice/port)"
227   AT_CHECK([TRIPECTL -dbob ADD alice INET 127.0.0.1 $(cat alice/port)])
228
229   ## Check transport pinging.
230   AT_CHECK([TRIPECTL -dalice PING bob],, [ignore])
231   AT_CHECK([TRIPECTL -dbob PING alice],, [ignore])
232
233   ## Wait for the completion announcement.
234   wait
235
236   ## Check encrypted pinging.
237   AT_CHECK([TRIPECTL -dalice EPING bob],, [ignore])
238   AT_CHECK([TRIPECTL -dbob EPING alice],, [ignore])
239
240   ## Check that packets can flow from one to the other.
241   AT_CHECK([echo "from alice" | USLIP -p alice/bob])
242   AT_CHECK([USLIP -g bob/alice],, [from alice[]nl])
243
244   AT_CHECK([echo "from bob" | USLIP -p bob/alice])
245   AT_CHECK([USLIP -g alice/bob],, [from bob[]nl])
246 ])
247
248 AT_CLEANUP
249
250 ###--------------------------------------------------------------------------
251 ### Services.
252
253 AT_SETUP([server services])
254 AT_KEYWORDS([svc])
255 SETUPDIR([ec])
256
257 WITH_TRIPE(, [
258
259   ## Make sure it's not running yet.
260   AT_CHECK([TRIPECTL SVCENSURE test], [1],,
261            [tripectl: unknown-service test[]nl])
262
263   ## Run a simple service.
264   rm -f svc-test-running tripectl-status
265   COPROCESSES([svc], [
266     echo SVCCLAIM test 1.0.0
267     read line
268     case "$line" in
269       OK)
270         ;;
271       *)
272         echo >&2 "SVCCLAIM failed: $line"
273         exit 1
274         ;;
275     esac
276     echo ok >svc-test-running
277     while read line; do
278       set -- $line
279       case "$[]1,$[]3,$[]4" in
280         SVCJOB,test,HELP)
281           echo SVCINFO try not to use this service for anything useful
282           echo SVCOK $[]2
283           ;;
284         SVCJOB,test,GOOD)
285           echo SVCOK $[]2
286           ;;
287         SVCJOB,test,BAD)
288           echo SVCFAIL $[]2 this-command-always-fails
289           ;;
290         SVCJOB,test,UGLY)
291           tag=$2
292           while read line; do
293             set -- $line
294             case "$[]1,$[]2,$[]3,$[]4" in
295               SVCCANCEL,$tag,,) break ;;
296               SVCJOB,*,test,ESCAPE)
297                 echo >&2 "attempt to escape from alkatraz"
298                 exit 1
299                 ;;
300             esac
301           done
302           ;;
303         SVCJOB,test,FIRST)
304           firsttag=$[]2
305           ;;
306         SVCJOB,test,SECOND)
307           echo SVCOK $firsttag
308           echo SVCOK $[]2
309           ;;
310         SVCJOB,*)
311           echo SVCFAIL $[]2 unknown-svc-command $[]4
312           ;;
313         SVCCLAIM,*)
314           break
315           ;;
316         OK,* | INFO,*)
317           ;;
318         FAIL,*)
319           echo "failure in service: $line" >&2
320           ;;
321       esac
322     done
323   ], [
324     TRIPECTL; echo $? >tripectl-status
325   ]) 2>tripectl-errors &
326
327   ## Wait until it starts up.
328   while test ! -r svc-test-running && test ! -r tripectl-status; do :; done
329
330   ## Make sure it's running.
331   AT_CHECK([TRIPECTL SVCQUERY test],, [name=test version=1.0.0[]nl])
332
333   ## Try some simple commands.
334   AT_CHECK([TRIPECTL SVCSUBMIT test GOOD])
335   AT_CHECK([TRIPECTL SVCSUBMIT test BAD], [1],,
336            [tripectl: this-command-always-fails[]nl])
337
338   ## And now with commands in the background.
339   TRIPECTL_INTERACT([
340     TRIPECTL_COMMAND([SVCSUBMIT test GOOD], [OK])
341     TRIPECTL_COMMAND([SVCSUBMIT -background foo test UGLY], [BGDETACH foo])
342     TRIPECTL_COMMAND([BGCANCEL foo], [OK])
343     TRIPECTL_COMMAND([SVCSUBMIT test ESCAPE],
344                      [FAIL unknown-svc-command ESCAPE])
345   ])
346
347   ## Out-of-order completion.
348   TRIPECTL_INTERACT([
349     TRIPECTL_COMMAND([SVCSUBMIT -background one test FIRST], [BGDETACH one])
350     TRIPECTL_COMMAND([SVCSUBMIT -background two test SECOND], [BGDETACH two])
351     TRIPECTL_COMMAND([!], [BGOK one])
352     TRIPECTL_COMMAND([!], [BGOK two])
353   ])
354
355   ## All done.
356   exit 0
357 ])
358
359 AT_CLEANUP
360
361 ###----- That's all, folks --------------------------------------------------