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