]> git.karo-electronics.de Git - karo-tx-redboot.git/blob - packages/net/ppp/v2_0/tests/test_server.sh
Initial revision
[karo-tx-redboot.git] / packages / net / ppp / v2_0 / tests / test_server.sh
1 #! /bin/bash
2 #==========================================================================
3 #
4 #      tests/test_server.sh
5 #
6 #      PPP test server script
7 #
8 #==========================================================================
9 #####ECOSGPLCOPYRIGHTBEGIN####
10 # -------------------------------------------
11 # This file is part of eCos, the Embedded Configurable Operating System.
12 # Copyright (C) 2003, 2004 eCosCentric Ltd.
13 #
14 # eCos is free software; you can redistribute it and/or modify it under
15 # the terms of the GNU General Public License as published by the Free
16 # Software Foundation; either version 2 or (at your option) any later version.
17 #
18 # eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19 # WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21 # for more details.
22 #
23 # You should have received a copy of the GNU General Public License along
24 # with eCos; if not, write to the Free Software Foundation, Inc.,
25 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 #
27 # As a special exception, if other files instantiate templates or use macros
28 # or inline functions from this file, or you compile this file and link it
29 # with other works to produce a work based on this file, this file does not
30 # by itself cause the resulting work to be covered by the GNU General Public
31 # License. However the source code for this file must still be made available
32 # in accordance with section (3) of the GNU General Public License.
33 #
34 # This exception does not invalidate any other reasons why a work based on
35 # this file might be covered by the GNU General Public License.
36 #
37 # -------------------------------------------
38 #####ECOSGPLCOPYRIGHTEND####
39 #==========================================================================
40 ######DESCRIPTIONBEGIN####
41 #
42 # Author(s):    nickg
43 # Contributors:
44 # Date:         2003-06-26
45 # Purpose:      
46 # Description:  
47 #              
48 #
49 #####DESCRIPTIONEND####
50 #
51 #==========================================================================
52
53 # --------------------------------------------------------------------
54 # Global variables
55
56 ppp_prev=
57 ppp_optarg=
58
59 ppp_dev=
60 ppp_myip=
61 ppp_hisip=
62 ppp_debug=
63 ppp_redboot=
64 ppp_redboot_baud=38400
65 ppp_baud=$ppp_redboot_baud
66 ppp_flow=crtscts
67 ppp_ping_interval=1
68
69 # --------------------------------------------------------------------
70 # Parse the options:
71
72 for ppp_option
73 do
74
75   # If the previous option needs an argument, assign it.
76   if test -n "$ppp_prev"; then
77     eval "$ppp_prev=\$ppp_option"
78     ppp_prev=
79     continue
80   fi
81
82   # If this option is of the form --thing=value then store
83   # the value into $ppp_optarg.
84   case "$ppp_option" in
85   -*=*) ppp_optarg=`echo "$ppp_option" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
86   *) ppp_optarg= ;;
87   esac
88
89   # Now parse the option
90
91   case "$ppp_option" in
92   
93   --dev) ppp_prev=ppp_dev ;;
94   --dev=*) ppp_dev=$ppp_optarg ;;
95
96   --myip) ppp_prev=ppp_myip ;;
97   --myip=*) ppp_myip=$ppp_optarg ;;
98
99   --hisip) ppp_prev=ppp_hisip ;;
100   --hisip=*) ppp_hisip=$ppp_optarg ;;
101
102   --baud) ppp_prev=ppp_baud ;;
103   --baud=*) ppp_baud=$ppp_optarg ;;
104
105   --flow) ppp_prev=ppp_flow ;;
106   --flow=*) ppp_flow=$ppp_optarg ;;
107
108   --redboot) ppp_redboot=y ;;
109
110   --redboot-baud) ppp_prev=ppp_redboot_baud ;;
111   --redboot-baud=*) ppp_redboot_baud=$ppp_optarg ;;
112
113   --debug) ppp_debug=y ;;
114
115   *)
116           echo "test_server: Unrecognized option: \"$ppp_option\"." >&2
117           exit 1
118           ;;
119   esac
120 done
121
122 # --------------------------------------------------------------------
123 # debug echo function. This only generates output if the --debug
124 # flag was given.
125
126 dbecho()
127 {
128     if [ $ppp_debug ] ; then
129         echo $*
130     fi
131 }
132
133 # --------------------------------------------------------------------
134 # Usage message
135
136 usage()
137 {
138     echo "test_server --dev=<devname>"
139     echo "            --myip=<myip_addr>"
140     echo "            --hisip=<hisip_addr>"
141     echo "            [--baud=<baud_rate>]"
142     echo "            [--flow=[crtscts|xonxoff|none]]"
143     echo "            [--redboot [--redboot-baud=<baud_rate>]]"
144     echo "            [--debug]"
145     exit 1
146 }
147
148 # --------------------------------------------------------------------
149 # Check that all the required options are present, and report their
150 # values.
151
152 if [ -z "$ppp_dev" ] ; then usage ; fi
153 if [ -z "$ppp_myip" ] ; then usage ; fi
154 if [ -z "$ppp_hisip" ] ; then usage ; fi
155
156 dbecho "Device : " $ppp_dev
157 dbecho "My IP  : " $ppp_myip
158 dbecho "His IP : " $ppp_hisip
159 dbecho "Baud   : " $ppp_baud
160 dbecho "Flow   : " $ppp_flow
161
162 if [ "$ppp_flow" == "none" ] ; then ppp_flow="" ; fi
163
164 # --------------------------------------------------------------------
165 # Bring the PPP link up by calling pppd. The pid of the PPPD is
166 # stored in pppd_pid for later use.
167
168 pppup()
169 {
170     dbecho pppd $ppp_dev $ppp_baud $ppp_flow local nodetach $ppp_myip:$ppp_hisip $* &
171     pppd $ppp_dev $ppp_baud $ppp_flow local nodetach $ppp_myip:$ppp_hisip $* &
172     pppd_pid=$!
173 #    dbecho "PPPD Pid: " $pppd_pid
174 }
175
176 # --------------------------------------------------------------------
177 # Simple test for bringing PPP up. Once the link is up the remote
178 # end is pinged for a while and then we bring the link down by
179 # signalling the PPPD.
180
181 ppp_up_test()
182 {
183     dbecho ppp_up_test
184     pppup
185     sleep 6
186     ping -i$ppp_ping_interval -w45 -s3000 -c20 $ppp_hisip
187     kill -SIGINT $pppd_pid
188     wait $pppd_pid
189 }
190
191 # --------------------------------------------------------------------
192 # Up/down test. In this case the link is brought down by the remote
193 # end.
194
195 ppp_updown_test()
196 {
197     dbecho ppp_updown_test
198     pppup
199     wait $pppd_pid
200 }
201
202
203 # --------------------------------------------------------------------
204 # Chat tests. These use chat itself to test the chat scripts on the
205 # remote end. The tests are:
206 #
207 # chat_test_1 - run throught the entire script
208 # chat_test_2 - simulate a carrier drop
209 # chat_test_3 - simulate a timeout
210
211 chat_test_1()
212 {
213     chat -V "Chat Test" "CONNECT\rlogin:\c"  ppp "Password:\c"  hithere ""  <$ppp_dev >$ppp_dev
214 }
215
216 chat_test_2()
217 {
218     chat -V "Chat Test" "CONNECT\rlogin:\c"  ppp "NO CARRIER"  <$ppp_dev >$ppp_dev
219 }
220
221 chat_test_3()
222 {
223     chat -V "Chat Test" "CONNECT\rlogin:\c"  ppp  <$ppp_dev >$ppp_dev
224 }
225
226 # --------------------------------------------------------------------
227 # Authentication tests. These bring up the PPPD with different
228 # authentication requirements against which the remote end tests
229 # itself. The tests are:
230 #
231 # auth_test_1 - authenticate by default method (usually CHAP)
232 # auth_test_2 - require PAP authentication
233 # auth_test_3 - require CHAP authentication
234
235 auth_test_1()
236 {
237     dbecho ppp_up_test
238     pppup auth
239     sleep 6
240     ps -p $pppd_pid >/dev/null
241     if [ "$?" == "0" ] ; then
242         ping -i$ppp_ping_interval -w45 -s3000 -c5 $ppp_hisip
243         kill -SIGINT $pppd_pid
244         wait $pppd_pid
245     fi
246 }
247
248 auth_test_2()
249 {
250     dbecho ppp_up_test
251     pppup auth require-pap
252     sleep 6
253     ps -p $pppd_pid >/dev/null
254     if [ "$?" == "0" ] ; then
255         ping -i$ppp_ping_interval -w45 -s3000 -c5 $ppp_hisip
256         kill -SIGINT $pppd_pid
257         wait $pppd_pid
258     fi
259 }
260
261 auth_test_3()
262 {
263     dbecho ppp_up_test
264     pppup auth require-chap
265     sleep 6
266     ps -p $pppd_pid >/dev/null
267     if [ "$?" == "0" ] ; then
268         ping -i$ppp_ping_interval -w45 -s3000 -c5 $ppp_hisip
269         kill -SIGINT $pppd_pid
270         wait $pppd_pid
271     fi
272 }
273
274
275 # --------------------------------------------------------------------
276 # TCP echo test. After bringing up the link this test runs the
277 # tcp_source and tcp_sink test programs to exercise the link.  This
278 # can take a long time so it is not really suitable for automated
279 # testing.
280
281 tcp_echo_test()
282 {
283     local sink_pid
284     dbecho tcp_echo_test
285     pppup
286     sleep 10
287     tcp_sink $ppp_hisip &
288     sink_pid=$!
289     sleep 5
290     tcp_source $ppp_hisip 60
291     wait $sink_pid
292     sleep 5
293     wait $pppd_pid
294 }
295
296 # --------------------------------------------------------------------
297 # Network characterisation test. After bringing up the link this test
298 # runs the nc_test_master test program to exercise the link.  This can
299 # take a long time so it is not really suitable for automated testing.
300
301 nc_test_slave_test()
302 {
303     dbecho nc_test_slave_test
304     pppup
305     sleep 10
306     nc_test_master $ppp_hisip
307     sleep 5
308     wait $pppd_pid
309 }
310
311 # --------------------------------------------------------------------
312 # Change the baud rate. Depending on the value sent as part of the
313 # BAUD message, change the link baudrate.
314
315 new_baud()
316 {
317     ppp_new_baud=`echo $ppp_test | sed 's/.*BAUD:\([0-9]*\).*/\1/'`
318     dbecho "New Baud " $ppp_new_baud
319     case $ppp_new_baud in
320         016) ppp_baud=14400; ppp_ping_interval=6 ;;
321         017) ppp_baud=19200; ppp_ping_interval=4 ;;
322         018) ppp_baud=38400; ppp_ping_interval=2 ;;
323         019) ppp_baud=57600; ppp_ping_interval=2 ;;
324         020) ppp_baud=115200; ppp_ping_interval=1 ;;
325         021) ppp_baud=230400 ;;
326
327         *) dbecho "Unknown baud rate: " $ppp_new_baud
328     esac
329     dbecho "New Baud Rate : " $ppp_baud
330 }        
331         
332
333
334 # --------------------------------------------------------------------
335 # Look for a RedBoot> prompt.
336
337 ppp_redboot_prompt()
338 {
339     local done=
340     dbecho ppp_redboot_prompt
341     
342     stty -F $ppp_dev $ppp_redboot_baud
343
344     while [ ! $done ] ; do
345         chat -V "RedBoot>" "\c" <$ppp_dev >$ppp_dev
346         if [ "$?" == "0" ] ; then done=1; fi
347     done
348 }
349
350 # --------------------------------------------------------------------
351 # Main loop.
352
353 while true ; do
354
355     if [ $ppp_redboot ] ; then ppp_redboot_prompt; fi
356
357     ppp_running=y
358     
359     while [ $ppp_running ] ; do
360
361         dbecho ""
362
363         dbecho "Setting baud rate : " $ppp_baud
364         stty -F $ppp_dev $ppp_baud
365
366         dbecho "Waiting for test..."
367
368         read ppp_test ppp_junk < $ppp_dev
369
370         ppp_test=`echo $ppp_test | sed 's/\([a-zA-Z_:0-9]*\).*/\1/'`
371         
372         dbecho "PPP test: >" $ppp_test "<"
373     
374         case $ppp_test in
375         
376             PPP_UPDOWN) ppp_updown_test ;;
377         
378             PPP_UP) ppp_up_test ;;
379
380             CHAT_TEST_1) chat_test_1 ;;
381             CHAT_TEST_2) chat_test_2 ;;
382             CHAT_TEST_3) chat_test_3 ;;
383
384             PPP_AUTH_1) auth_test_1 ;;
385             PPP_AUTH_2) auth_test_2 ;;
386             PPP_AUTH_3) auth_test_3 ;;
387
388             TCP_ECHO) tcp_echo_test ;;
389
390             NC_TEST_SLAVE) nc_test_slave_test ;;
391
392             BAUD:*) new_baud ;;
393
394             FINISH) unset ppp_running ;;
395             
396             *) echo "Unknown test: " $ppp_test ;;
397         
398         esac
399
400     done
401
402 done
403
404 # --------------------------------------------------------------------
405 # end of test_server.sh
406