improve output in case of decoding fail. Improve Sy cases with STR management
[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   local send_command=sendxml2e
97   [ "$adml_type" = "server" ] && { wait_command=waitfc ; send_command=sendxml2c ; }
98
99
100   # Ignore disconnect peer messages on testcase format (they have no session-id):
101   grep "^code=282$" $mtd > /dev/null
102   [ $? -eq 0 ] && return
103
104   local code=$(grep ^code $mtd | cut -d= -f2)
105   local isrequest=$(grep ^isrequest $mtd | cut -d= -f2)
106   local sessionid=$(grep Session-Id $xml | cut -d\" -f4)
107   local lines=$(wc -l $TESTCASE_BN | awk '{ print $1 }')
108
109   local s_wait="test|1|$wait_command|$code|$((1-isrequest))"
110   [ -n "$sessionid" ] && s_wait="${s_wait}|||${sessionid}"
111
112   local s_send="test|1|$send_command|$xml"
113
114   if [ $isrequest -eq 1 ]
115   then
116     # Send the request
117     # 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
118     if [ "$adml_type" = "server" ]
119     then
120       if [ "$code" = "8388636" -o "$code" = "275" ]
121       then
122         s_send="test|1|$send_command|$xml|$REQUEST_STEP"
123         s_wait="test|1|$wait_command|$code|0|||"
124       fi
125     fi
126
127     echo "$s_send" >> $TESTCASE_BN
128
129     if [ -n "$resultcode" ]
130     then
131       local hbh="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
132       local hbh_matchs=( $(grep -l "$hbh" *.xml) )
133       local ans_xml=${hbh_matchs[1]}
134       local rc=2001
135       if [ -n "$ans_xml" ]
136       then 
137         _rc=$(getResultCode $ans_xml)
138         [ -n "$_rc" ] && rc=$_rc
139       fi
140
141       # Wait the answer:
142       s_wait="${s_wait}|${rc}"
143     fi
144     echo "$s_wait" >> $TESTCASE_BN
145   else
146     local next_step_number=$((lines+1))
147
148     # Wait the request
149     # Special case for SLR/SLA (code=8388635) and STR (code=275) coming from SAPC: the Session-Id is created on client
150     if [ "$adml_type" = "server" ]
151     then
152       if [ "$code" = "8388635" -o "$code" = "275" ]
153       then
154         s_wait="test|1|$wait_command|$code|1"
155         REQUEST_STEP=$next_step_number
156       fi
157     fi
158     echo "$s_wait" >> $TESTCASE_BN
159
160     # Send the answer
161     echo "test|1|$send_command|$xml|$next_step_number" >> $TESTCASE_BN
162   fi
163 }
164
165 #############
166 # EXECUTION #
167 #############
168 SOURCE_DIR=$1
169 [ -z "$SOURCE_DIR" ] && usage
170 SOURCE_DIR=`readlink -f $SOURCE_DIR`
171 [ ! -d "$SOURCE_DIR" ] && _exit "Can't found provided directory '$SOURCE_DIR'"
172 # Work on source directory:
173 cd $SOURCE_DIR
174
175 # Must have metadata:
176 ls *.*metadata >/dev/null 2>/dev/null
177 [ $? -ne 0 ] && _exit "Cannot found '*.*metadata' files on '$SOURCE_DIR' !!"
178
179 # Check origin-hosts:
180 ohs_file=$SOURCE_DIR/origin-hosts
181 [ ! -f $ohs_file ] && _exit "Can't found the mandatory file '$ohs_file' !!"
182 awk '{ print $NF }' $ohs_file | egrep -qw 'client|server'
183 [ ${PIPESTATUS[1]} -ne 0 ] && _exit "Can't found any Origin-Host in '$ohs_file' with a valid ADML node role value (client or server) !!"
184
185 # Identify useful frames: those which are created at test-bed side (all except frames coming from tested systems):
186 0> .involved_frames
187 0> .involved_origin_hosts
188 n_involved_frames=0
189 for oh in `awk '{ print $1 }' $ohs_file`
190 do
191   for frame in $(grep -l "^originhost=$oh$" *.*metadata | cut -d\. -f1)
192   do
193     echo "$frame" >> .involved_frames
194     echo "$oh" >> .involved_origin_hosts
195     n_involved_frames=$((n_involved_frames + 1))
196   done
197 done
198 [ $n_involved_frames -eq 0 ] && _exit "No frame has been selected within '$SOURCE_DIR' for provided origin-hosts file !!"
199 sort .involved_frames > .involved_frames_sort
200 mv .involved_frames_sort .involved_frames
201
202 # Messages classification:
203 #0> cers_4_starting
204 #0> ceas_4_establishing
205 0> cers_4_starting_origin_hosts
206 0> ceas_4_establishing_origin_hosts
207 0> requests_4_sending
208 0> answers_4_programming
209 0> $TESTCASE_BN
210
211 # Optional timeout:
212 TIMEOUT_SEC=$2
213 [ -n "$TIMEOUT_SEC" ] && echo "test|1|timeout|$((TIMEOUT_SEC * 1000))" >> $TESTCASE_BN
214
215 # Process frames:
216 for frame in `cat .involved_frames`
217 do
218   mtd=( `ls ${frame}.*metadata 2>/dev/null` )
219   [ ${#mtd[@]} -ne 1 ] && _exit "There must be one metadata file corresponding to frame '$frame' !!"
220   xml=( `ls ${frame}.*xml 2>/dev/null` )
221   [ ${#xml[@]} -ne 1 ] && _exit "There must be one xml message file corresponding to '$mtd' (frame '$frame') !!"
222   originHost=$(grep ^originhost= ${frame}.metadata | cut -d= -f2-)
223
224   # Ignore keep alives:
225   grep -q "^code=280$" $mtd
226   [ $? -eq 0 ] && continue
227   
228   grep -q "^isrequest=1$" $mtd
229   if [ $? -eq 0 ]
230   then
231     # CER's:
232     grep -q "^code=257$" $mtd
233     if [ $? -eq 0 ]
234     then
235       #echo $frame >> cers_4_starting
236       [ -z "$originHost" ] &&  _exit "Missing Origin-Host (frame $frame, CER message) !!"
237       echo "$originHost" >> cers_4_starting_origin_hosts
238       ln -sf $xml cer.${originHost}.xml
239       continue
240     fi
241     # Other requests:
242     echo $frame >> requests_4_sending
243     
244     # client or server:
245     adml_type=$(grep -w "$originHost" $ohs_file | awk '{ print $NF }')
246     update_testcase $mtd $xml check_result_code $adml_type
247   else
248     # CEA's:
249     grep -q "^code=257$" $mtd
250     if [ $? -eq 0 ]
251     then
252       #echo $frame >> ceas_4_establishing
253       originHost=$(grep ^originhost= ${frame}.metadata | cut -d= -f2-)
254       [ -z "$originHost" ] &&  _exit "Missing Origin-Host (frame $frame, CEA message) !!"
255       echo "$originHost" >> ceas_4_establishing_origin_hosts
256       ln -sf $xml cea.${originHost}.xml
257       continue
258     fi
259     # Other answers:
260     echo $frame >> answers_4_programming
261
262     # client or server:
263     adml_type=$(grep -w "$originHost" $ohs_file | awk '{ print $NF }')
264     update_testcase $mtd $xml "" $adml_type
265   fi
266 done
267
268
269 # We will replace all the requests hop-by-hop's with a unique value, to avoid bad sniffing cases (different sources using bad values).
270 # For example, the frame number could be valid enough.
271 hbh_ini=
272 hbh_fin=
273 for frame in `cat .involved_frames`
274 do
275   xml=( `ls ${frame}.*xml` )
276   mtd=( `ls ${frame}.*metadata` )
277   isrequest=$(grep "isrequest=1" $mtd)
278   if [ -n "$isrequest" ]
279   then
280     hbh_ini="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
281     sed -i 's/'$hbh_ini'/hop-by-hop-id="'$frame'"/' $xml
282   fi
283 done
284 rm .involved_frames
285
286 # Simplify origin hosts file (oh + adml role):
287 sort -u .involved_origin_hosts > .involved_origin_hosts_unique
288 grep -w -f .involved_origin_hosts_unique $ohs_file > .involved_origin_hosts_with_info
289 mv .involved_origin_hosts_with_info $ohs_file
290 rm .involved_origin_hosts .involved_origin_hosts_unique
291
292 # Missing CERs:
293 for oh in $(awk -v input=client '{if ($2 == input) print $1;}' $ohs_file)
294 do
295   cer=$SOURCE_DIR/cer.${oh}.xml
296   if [ ! -f $cer ]
297   then
298     echo $oh >> cers_4_starting_origin_hosts
299     echo "Missing CER: `basename $cer` (a basic template has been created, please edit & fix the unknowns)"
300         cat << EOF > $cer
301 <message version="1" name="CER" application-id="0" hop-by-hop-id="1" end-by-end-id="1">
302    <avp name="Origin-Host" data="$oh"/>
303    <avp name="Origin-Realm" data="$(echo $oh | cut -d\. -f2-) $WHAT"/>
304    <avp name="Auth-Application-Id" data="16777236 $WHAT 16777238 $WHAT"/>
305    <avp name="Origin-State-Id" data="1"/>
306    <avp name="Host-IP-Address" data="1|192.168.14.42 $WHAT"/>
307    <avp name="Vendor-Id" data="193"/>
308    <avp name="Product-Name" data="afNode $WHAT ggsnNode $WHAT"/>
309    <avp name="Firmware-Revision" data="1"/>
310 </message>
311 EOF
312   fi
313 done
314
315 # Missing CEAs:
316 for oh in $(awk -v input=server '{if ($2 == input) print $1;}' $ohs_file)
317 do
318   cea=$SOURCE_DIR/cea.${oh}.xml
319   if [ ! -f $cea ]
320   then
321     echo $oh >> ceas_4_establishing_origin_hosts
322     echo "Missing CEA: `basename $cea` (a basic template has been created, please edit & fix the unknowns)"
323         cat << EOF > $cea
324 <message version="1" name="CEA" application-id="0" hop-by-hop-id="1" end-by-end-id="1">
325    <avp name="Result-Code" data="2001" alias="DIAMETER_SUCCESS"/>
326    <avp name="Origin-Host" data="$oh"/>
327    <avp name="Origin-Realm" data="$(echo $oh | cut -d\. -f2-) $WHAT"/>
328    <avp name="Host-IP-Address" data="1|192.168.12.42 $WHAT"/>
329    <avp name="Vendor-Id" data="193"/>
330    <avp name="Product-Name" data="SAPC"/>
331    <avp name="Supported-Vendor-Id" data="5535"/>
332    <avp name="Supported-Vendor-Id" data="10415"/>
333    <avp name="Auth-Application-Id" data="16777238"/>
334    <avp name="Auth-Application-Id" data="16777236"/>
335    <avp name="Vendor-Specific-Application-Id">
336       <avp name="Vendor-Id" data="10415"/>
337       <avp name="Auth-Application-Id" data="16777238"/>
338    </avp>
339    <avp name="Vendor-Specific-Application-Id">
340       <avp name="Vendor-Id" data="10415"/>
341       <avp name="Auth-Application-Id" data="16777236"/>
342    </avp>
343    <avp name="Firmware-Revision" data="1"/>
344 </message>
345 EOF
346   fi
347 done
348
349 exit 0
350