Fixed multiple AVP error. Missing fix RFC 6733 section 7.5 regarding avps within...
[anna.git] / example / diameter / launcher / resources / ft-client / tests / experiment2 / go.sh
1 #!/bin/bash
2
3 #############
4 # VARIABLES #
5 #############
6 CWD=`dirname $0`
7 tmpdir=$(mktemp -d)
8 SCR_BN=`basename $0`
9 OPER_SCR=`readlink -f $CWD/../../operation.sh`
10 RESULT_LOG=$CWD/result.log
11 RECV_LOG=`readlink -f $CWD/../../launcher.log.recvfe`
12
13 #############
14 # FUNCTIONS #
15 #############
16
17 sigint_handler () {
18   _exit "Script interrupted. Cleanup and exit ..."
19 }
20
21 # $1: message; $2: optional rc (1 by default)
22 _exit () {
23   rc=1
24   [ -n "$2" ] && rc=$2
25   echo
26   echo -e $1
27   echo
28   rm -rf $tmpdir
29   exit $rc
30 }
31
32 # $1: hex formatted file to send to the server
33 operation_sendxml2e () {
34   sleep 0.5
35   $OPER_SCR "sendxml2e|$1"
36   return $?
37 }
38
39 # $1: hex formatted file to send to the server
40 operation_sendhex2e () {
41   sleep 0.5
42   $OPER_SCR "sendhex2e|$1"
43   return $?
44 }
45
46 # $1: file to monitor
47 # $2: timeout (request expiration) in seconds
48 # Result: cats the message when completed
49 wait4message () {
50   0>$1
51   rm -f .msg_received
52
53   [ -z "$2" ] && _exit "ERROR: must provide '$FUNCNAME' timeout (second argument)"
54   sleep $2 &
55   local timerPid=$!
56
57   # Monitor for incoming message:
58   tail -n0 -F --pid=$timerPid $1 | while read line 
59   do
60     if echo $line | grep "^</message>" >/dev/null; then
61       echo "Message received:"
62       echo
63       cat $1
64       # stop the timer
65       kill -13 $timerPid
66       touch .msg_received
67     fi
68   done
69
70   if [ ! -f .msg_received ]; then
71     echo "Timeout expired"
72     return 1
73   fi
74
75   return 0
76 }
77
78 # xml content is serialized (removing CR's) to ease the pattern matching:
79 # $1: pattern; $2: file
80 check_pattern () {
81   echo -n "Matching pattern '$1' in file '$2' ... " 
82   cat $2 | tr '\n' ' ' | egrep "$1" >/dev/null
83   [ $? -ne 0 ] && { echo "ERROR" ; test_failed ; return 1 ; }
84   echo "OK"
85   return 0
86 }
87
88 # $1: pattern file; $2: file
89 check_pattern_file () {
90   echo -n "Matching whole file '$1' in file '$2' ... " 
91   grep -f $1 $2 >/dev/null
92   [ $? -ne 0 ] && { echo "ERROR" ; test_failed ; return 1 ; }
93   echo "OK"
94   return 0
95 }
96
97 # Tests result:
98 test_ok () {
99   echo "Verdict: $TC Ok" | tee -a $RESULT_LOG
100 }
101
102 test_failed () {
103   echo "Verdict: $TC Failed" | tee -a $RESULT_LOG
104 }
105
106 process_tc () {
107   local tc_dn=`dirname $TC`
108
109   echo
110   echo "***** Executing '$TC' ..."
111
112   grep -v ^# $TC > $tmpdir/tc
113   while read -r line
114   do
115     oper=$(echo $line | awk '{ print $1 }')
116     param1="$(echo $line | cut -d' ' -f2-)"
117
118     case $oper in
119       SENDXML2E)
120         operation_sendxml2e $tc_dn/$param1 &
121         [ $? -ne 0 ] && return 1
122       ;;
123
124       SENDHEX2E)
125         operation_sendhex2e $tc_dn/$param1 &
126         [ $? -ne 0 ] && return 1
127       ;;
128
129       WAIT4MESSAGE)
130         wait4message $RECV_LOG 5
131         [ $? -ne 0 ] && return 1
132       ;;
133
134       CHECKPATTERN)
135         check_pattern "$param1" $RECV_LOG
136         [ $? -ne 0 ] && return 1
137       ;;
138
139       CHECKPATTERNFILE)
140         check_pattern_file "$param1" $RECV_LOG
141         [ $? -ne 0 ] && return 1
142       ;;
143
144     esac
145
146   done < $tmpdir/tc
147
148   return 0
149 }
150
151
152 #############
153 # EXECUTION #
154 #############
155
156 # Trap sigint:
157 trap sigint_handler SIGINT
158
159 # Working directory is script dirname:
160 cd $CWD
161
162 # Intro:
163 echo
164 echo "------------------"
165 echo "Test cases manager"
166 echo "------------------"
167 # Mandatory parameter:
168 [ -z "$1" ] && _exit "Usage: $SCR_BN <test cases parent directory (or .tc file)>"
169 [ ! -f "$1" -a ! -d "$1" ] && _exit "Invalid file or directory '$1' !!"
170
171 # Gather .tc files to be processed:
172 [ ! -f $1 ] && echo -e "\nGathering list of test cases from '`readlink -f $1`' ..."
173 echo
174 find $1 -name *.tc | xargs readlink -f | tee -a $tmpdir/tc_list
175 echo
176 echo
177
178 echo "Start processing test cases:"
179 echo "----------------------------"
180 # Process test cases:
181 0> $RESULT_LOG
182 for TC in `cat $tmpdir/tc_list`
183 do
184   process_tc
185   if [ $? -eq 0 ]; then
186     test_ok
187   else
188     test_failed
189     cat $RECV_LOG
190   fi
191 done
192
193 _exit "Done !" 0
194