fb60a3375562383e30a9343523799c0b9007c6bf
[anna.git] / example / diameter / launcher / resources / ft-client / tests / common.sh
1 #!/bin/bash
2
3 #############
4 # VARIABLES #
5 #############
6 tmpdir=$(mktemp -d)
7
8 #############
9 # FUNCTIONS #
10 #############
11
12 _exit () {
13   echo
14   echo $1
15   echo
16   rm -rf $tmpdir
17   exit 1
18 }
19
20 sigint_handler() {
21   _exit "Script interrupted. Cleanup & exit ..."
22 }
23
24 # $1: hex formatted file to send to the server
25 send_xml () {
26   ../operation.sh "sendxml|$1"
27 }
28
29 send_hex () {
30   sleep 1
31   ../operation.sh "sendxml|$1"
32 }
33
34 # $1: pattern; $2: file
35 check_pattern () {
36   echo "Matching pattern '$1' ..." 
37   cat $2 | tr '\n' ' ' | egrep "$1" >/dev/null
38   res=${PIPESTATUS[2]}
39   [ $? -ne 0 ] && _exit "Test failed: miss pattern !!"
40 }
41
42 test_ok () {
43   echo
44   echo $1
45   echo "Test OK !"
46   echo
47   exit 0
48 }
49
50 test_failed () {
51   echo
52   echo $1
53   echo "Test FAILED !"
54   echo
55   exit 1
56 }
57
58
59 # $1: traffic log to monitor
60 # $2: timeout (request expiration) in seconds
61 # Result: cats the message when completed
62 monitor_4_message () {
63   0>$1
64   rm -f .msg_received
65
66   [ -z "$2" ] && _exit "ERROR: must provide '$FUNCNAME' timeout (second argument)"
67   sleep $2 &
68   local timerPid=$!
69
70   # Monitor for incoming message:
71   tail -n0 -F --pid=$timerPid $1 | while read line 
72   do
73     if echo $line | grep "^</message>" >/dev/null; then
74       echo "Message received"
75       # stop the timer
76       kill -13 $timerPid
77       touch .msg_received
78     fi
79   done
80
81   if [ ! -f .msg_received ]; then
82     echo "Timeout expired"
83     return 1
84   fi
85
86   return 0
87 }
88
89 #############
90 # EXECUTION #
91 #############
92
93 # Trap sigint signal:
94 trap sigint_handler SIGINT
95