Improved wait for regexp operations
[anna.git] / example / diameter / launcher / resources / scripts / tinyTestcase.sh
1 #!/bin/bash
2
3 #############
4 # VARIABLES #
5 #############
6 SCR_DIR=`readlink -f $0 | xargs dirname`
7 TESTCASE_BN=testcase.txt
8 WHAT=?????????
9 REQUEST_STEP=
10
11 #############
12 # FUNCTIONS #
13 #############
14 _exit() {
15   echo
16   echo -e $1
17   echo
18   exit 1
19 }
20
21 usage() {
22   echo
23   echo "Usage: $0 <source_directory> [seconds_timeout]"
24   echo
25   echo "       source_directory:"
26   echo "         The source directory may contain .xml and .metadata files grouped by pairs and alphabetically classified,"
27   echo "         and also (and this is VERY IMPORTANT), a file called 'origin-hosts' containing a list of Origin-Host"
28   echo "         values that represents the point of view of the ADML testing system, together with the type of the"
29   echo "         simulated ADML node (client/server):"
30   echo
31   echo "            <id>.hex.as.xml: ANNA-Diameter XML message format."
32   echo "            <id>.metadata:   Some decoded message information."
33   echo "            ..."
34   echo "            origin-hosts:    Example of content:"
35   echo "                                OCS3GPPNodeHostname.ocs3gpprealm.com server"
36   echo "                                ggsnNodeHostname.nodeHostRealm.com client"
37   echo
38   echo "         The order for the xml and metadata files comes from first dot-separated part (<id>), which is normally the"
39   echo "         decoded frame from a pcap file. The xml files are ANNA-Diameter xml messages and a typical metadata file"
40   echo "         would be something like this:"
41   echo "                           date=vie ene 29 15:37:14 CET 2016"
42   echo "                           timestamp=1454078234.050988000"
43   echo "                           src=192.168.14.42"
44   echo "                           dst=192.168.12.40"
45   echo "                           code=257"
46   echo "                           isrequest=0"
47   echo "                           applicationid=0"
48   echo "                           originhost=OCSNodeHostname.ocsrealm.com"
49   echo
50   echo "         You can use the 'example/diameter/pcapDecoder/tsharkDecoder.sh' script over a pcap file, in order to get"
51   echo "         the hexadecimal buffers and then process them with 'example/diameter/batchConverter' tool to get the xml"
52   echo "         messages and metadata for each pcap diameter frame."
53   echo
54   echo
55   echo "       seconds_timeout:"
56   echo "         Timeout in seconds for the testcase. If not provided, no timeout will be programmed for the testcase."
57   echo
58   echo
59   echo
60   echo "       The resulting '$TESTCASE_BN' will have all the operations needed to program the test case and will be"
61   echo "       written on same source directory referencing xml files as local-relative ones. Normally, 'change-dir'"
62   echo "       operation will be used before programming to ease the procedure without having to copy the stuff into"
63   echo "       installed ADML execution directory."
64   echo
65   echo "       Connection issues (CER/A), disconnect procedures (DPR/A), database populations and sanity checks are"
66   echo "       not responsability for this script. Anyway, CER/A messages detected are symbolically linked with the"
67   echo "       name 'ce<r/a>.<origin host>.xml', and missing ones are created with a basic template which the user"
68   echo "       could edit and fix with the appropiate values."
69   echo
70   _exit
71 }
72
73 # $1: xml file
74 # Nested Result-Code's not supported here (i.e. charging application)
75 getResultCode () {
76   grep "<avp name=\"Result-Code\"" $1 | awk -F'data=' '{ print $2 }' | cut -d\" -f2
77 }
78  
79 # $1: metadata file; $2: xml file; $3: check Result-Code indicator; $4: client/server (ADML node role)
80 update_testcase () {
81   # metadata aspect:
82   #
83   # date=Wed Oct  7 01:51:16 CEST 2015
84   # timestamp=1444175476.212667000
85   # src=gt_traf
86   # dst=vcbavipt
87   # code=258
88   # isrequest=0
89   # applicationid=16777238
90   local mtd=$1
91   local xml=$2
92   local resultcode=$3
93   local adml_type=$4
94
95   local wait_command=waitfe
96   # wait xml variant:
97   local waitxml_command=waitfe-xml
98   local send_command=sendxml2e
99   [ "$adml_type" = "server" ] && { wait_command=waitfc ; waitxml_command=waitfc-xml ; send_command=sendxml2c ; }
100
101
102   # Ignore disconnect peer messages on testcase format (they have no session-id):
103   grep "^code=282$" $mtd > /dev/null
104   [ $? -eq 0 ] && return
105
106   local code=$(grep ^code $mtd | cut -d= -f2)
107   local isrequest=$(grep ^isrequest $mtd | cut -d= -f2)
108   local sessionid=$(grep Session-Id $xml | cut -d\" -f4)
109   local lines=$(wc -l $TESTCASE_BN | awk '{ print $1 }')
110
111   local s_wait="test|1|$wait_command|$code|$((1-isrequest))"
112   # wait xml variant:
113   local s_waitxml="test|1|$waitxml_command|$xml"
114   [ -n "$sessionid" ] && s_wait="${s_wait}|||${sessionid}"
115
116   local s_send="test|1|$send_command|$xml"
117
118   if [ $isrequest -eq 1 ]
119   then
120     # Send the request
121     # Special case for SNR/SNA (code=8388636) and STR (code=275) going to SAPC: the Session-Id is created on client and received on SLR previously
122     if [ "$adml_type" = "server" ]
123     then
124       if [ "$code" = "8388636" -o "$code" = "275" ]
125       then
126         s_send="test|1|$send_command|$xml|$REQUEST_STEP"
127         s_wait="test|1|$wait_command|$code|0|||"
128       fi
129     fi
130
131     echo "$s_send" >> $TESTCASE_BN
132
133     if [ -n "$resultcode" ]
134     then
135       local hbh="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
136       local hbh_matchs=( $(grep -l "$hbh" *.xml) )
137       local ans_xml=${hbh_matchs[1]}
138       local rc=2001
139       if [ -n "$ans_xml" ]
140       then 
141         _rc=$(getResultCode $ans_xml)
142         [ -n "$_rc" ] && rc=$_rc
143       fi
144
145       # Wait the answer:
146       s_wait="${s_wait}|${rc}"
147     fi
148     echo "$s_waitxml" >> $TESTCASE_BN
149     echo "#$s_wait" >> $TESTCASE_BN
150   else
151     local next_step_number=$((lines+1))
152
153     # Wait the request
154     # Special case for SLR/SLA (code=8388635) and STR (code=275) coming from SAPC: the Session-Id is created on client
155     if [ "$adml_type" = "server" ]
156     then
157       if [ "$code" = "8388635" -o "$code" = "275" ]
158       then
159         s_wait="test|1|$wait_command|$code|1"
160         REQUEST_STEP=$next_step_number
161       fi
162     fi
163     echo "$s_wait" >> $TESTCASE_BN
164
165     # Send the answer
166     echo "test|1|$send_command|$xml|$next_step_number" >> $TESTCASE_BN
167   fi
168 }
169
170 #############
171 # EXECUTION #
172 #############
173 SOURCE_DIR=$1
174 [ -z "$SOURCE_DIR" ] && usage
175 SOURCE_DIR=`readlink -f $SOURCE_DIR`
176 [ ! -d "$SOURCE_DIR" ] && _exit "Can't found provided directory '$SOURCE_DIR'"
177 # Work on source directory:
178 cd $SOURCE_DIR
179
180 # Must have metadata:
181 ls *.*metadata >/dev/null 2>/dev/null
182 [ $? -ne 0 ] && _exit "Cannot found '*.*metadata' files on '$SOURCE_DIR' !!"
183
184 # Check origin-hosts:
185 ohs_file=$SOURCE_DIR/origin-hosts
186 [ ! -f $ohs_file ] && _exit "Can't found the mandatory file '$ohs_file' !!"
187 awk '{ print $NF }' $ohs_file | egrep -qw 'client|server'
188 [ ${PIPESTATUS[1]} -ne 0 ] && _exit "Can't found any Origin-Host in '$ohs_file' with a valid ADML node role value (client or server) !!"
189
190 # Identify useful frames: those which are created at test-bed side (all except frames coming from tested systems):
191 0> .involved_frames
192 0> .involved_origin_hosts
193 n_involved_frames=0
194 for oh in `awk '{ print $1 }' $ohs_file`
195 do
196   for frame in $(grep -l "^originhost=$oh$" *.*metadata | cut -d\. -f1)
197   do
198     echo "$frame" >> .involved_frames
199     echo "$oh" >> .involved_origin_hosts
200     n_involved_frames=$((n_involved_frames + 1))
201   done
202 done
203 [ $n_involved_frames -eq 0 ] && _exit "No frame has been selected within '$SOURCE_DIR' for provided origin-hosts file !!"
204 sort .involved_frames > .involved_frames_sort
205 mv .involved_frames_sort .involved_frames
206
207 # Messages classification:
208 #0> cers_4_starting
209 #0> ceas_4_establishing
210 0> cers_4_starting_origin_hosts
211 0> ceas_4_establishing_origin_hosts
212 0> requests_4_sending
213 0> answers_4_programming
214 0> $TESTCASE_BN
215
216 # Optional timeout:
217 TIMEOUT_SEC=$2
218 [ -n "$TIMEOUT_SEC" ] && echo "test|1|timeout|$((TIMEOUT_SEC * 1000))" >> $TESTCASE_BN
219
220 # Process frames:
221 for frame in `cat .involved_frames`
222 do
223   mtd=( `ls ${frame}.*metadata 2>/dev/null` )
224   [ ${#mtd[@]} -ne 1 ] && _exit "There must be one metadata file corresponding to frame '$frame' !!"
225   xml=( `ls ${frame}.*xml 2>/dev/null` )
226   [ ${#xml[@]} -ne 1 ] && _exit "There must be one xml message file corresponding to '$mtd' (frame '$frame') !!"
227   originHost=$(grep ^originhost= ${frame}.metadata | cut -d= -f2-)
228
229   # Ignore keep alives:
230   grep -q "^code=280$" $mtd
231   [ $? -eq 0 ] && continue
232   
233   grep -q "^isrequest=1$" $mtd
234   if [ $? -eq 0 ]
235   then
236     # CER's:
237     grep -q "^code=257$" $mtd
238     if [ $? -eq 0 ]
239     then
240       #echo $frame >> cers_4_starting
241       [ -z "$originHost" ] &&  _exit "Missing Origin-Host (frame $frame, CER message) !!"
242       echo "$originHost" >> cers_4_starting_origin_hosts
243       ln -sf $xml cer.${originHost}.xml
244       continue
245     fi
246     # Other requests:
247     echo $frame >> requests_4_sending
248     
249     # client or server:
250     adml_type=$(grep -w "$originHost" $ohs_file | awk '{ print $NF }')
251     update_testcase $mtd $xml check_result_code $adml_type
252   else
253     # CEA's:
254     grep -q "^code=257$" $mtd
255     if [ $? -eq 0 ]
256     then
257       #echo $frame >> ceas_4_establishing
258       originHost=$(grep ^originhost= ${frame}.metadata | cut -d= -f2-)
259       [ -z "$originHost" ] &&  _exit "Missing Origin-Host (frame $frame, CEA message) !!"
260       echo "$originHost" >> ceas_4_establishing_origin_hosts
261       ln -sf $xml cea.${originHost}.xml
262       continue
263     fi
264     # Other answers:
265     echo $frame >> answers_4_programming
266
267     # client or server:
268     adml_type=$(grep -w "$originHost" $ohs_file | awk '{ print $NF }')
269     update_testcase $mtd $xml "" $adml_type
270   fi
271 done
272
273
274 # We will replace all the requests hop-by-hop's with a unique value, to avoid bad sniffing cases (different sources using bad values).
275 # For example, the frame number could be valid enough.
276 hbh_ini=
277 hbh_fin=
278 for frame in `cat .involved_frames`
279 do
280   xml=( `ls ${frame}.*xml` )
281   mtd=( `ls ${frame}.*metadata` )
282   isrequest=$(grep "isrequest=1" $mtd)
283   if [ -n "$isrequest" ]
284   then
285     hbh_ini="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
286     sed -i 's/'$hbh_ini'/hop-by-hop-id="'$frame'"/' $xml
287   fi
288 done
289 rm .involved_frames
290
291 # Simplify origin hosts file (oh + adml role):
292 sort -u .involved_origin_hosts > .involved_origin_hosts_unique
293 grep -w -f .involved_origin_hosts_unique $ohs_file > .involved_origin_hosts_with_info
294 mv .involved_origin_hosts_with_info $ohs_file
295 rm .involved_origin_hosts .involved_origin_hosts_unique
296
297 # Missing CERs:
298 for oh in $(awk -v input=client '{if ($2 == input) print $1;}' $ohs_file)
299 do
300   cer=$SOURCE_DIR/cer.${oh}.xml
301   if [ ! -f $cer ]
302   then
303     echo $oh >> cers_4_starting_origin_hosts
304     echo "Missing CER: `basename $cer` (a basic template has been created, please edit & fix the unknowns)"
305         cat << EOF > $cer
306 <message version="1" name="CER" application-id="0" hop-by-hop-id="1" end-to-end-id="1">
307    <avp name="Origin-Host" data="$oh"/>
308    <avp name="Origin-Realm" data="$(echo $oh | cut -d\. -f2-) $WHAT"/>
309    <avp name="Auth-Application-Id" data="16777236 $WHAT 16777238 $WHAT"/>
310    <avp name="Origin-State-Id" data="1"/>
311    <avp name="Host-IP-Address" data="1|192.168.14.42 $WHAT"/>
312    <avp name="Vendor-Id" data="193"/>
313    <avp name="Product-Name" data="afNode $WHAT ggsnNode $WHAT"/>
314    <avp name="Firmware-Revision" data="1"/>
315 </message>
316 EOF
317   fi
318 done
319
320 # Missing CEAs:
321 for oh in $(awk -v input=server '{if ($2 == input) print $1;}' $ohs_file)
322 do
323   cea=$SOURCE_DIR/cea.${oh}.xml
324   if [ ! -f $cea ]
325   then
326     echo $oh >> ceas_4_establishing_origin_hosts
327     echo "Missing CEA: `basename $cea` (a basic template has been created, please edit & fix the unknowns)"
328         cat << EOF > $cea
329 <message version="1" name="CEA" application-id="0" hop-by-hop-id="1" end-to-end-id="1">
330    <avp name="Result-Code" data="2001" alias="DIAMETER_SUCCESS"/>
331    <avp name="Origin-Host" data="$oh"/>
332    <avp name="Origin-Realm" data="$(echo $oh | cut -d\. -f2-) $WHAT"/>
333    <avp name="Host-IP-Address" data="1|192.168.12.42 $WHAT"/>
334    <avp name="Vendor-Id" data="193"/>
335    <avp name="Product-Name" data="SAPC"/>
336    <avp name="Supported-Vendor-Id" data="5535"/>
337    <avp name="Supported-Vendor-Id" data="10415"/>
338    <avp name="Auth-Application-Id" data="16777238"/>
339    <avp name="Auth-Application-Id" data="16777236"/>
340    <avp name="Vendor-Specific-Application-Id">
341       <avp name="Vendor-Id" data="10415"/>
342       <avp name="Auth-Application-Id" data="16777238"/>
343    </avp>
344    <avp name="Vendor-Specific-Application-Id">
345       <avp name="Vendor-Id" data="10415"/>
346       <avp name="Auth-Application-Id" data="16777236"/>
347    </avp>
348    <avp name="Firmware-Revision" data="1"/>
349 </message>
350 EOF
351   fi
352 done
353
354 exit 0
355