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