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