Fix CEA scenaries
[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> \"[test_end_points]\" [timeout]"
23   echo
24   echo "       source_directory:"
25   echo
26   echo "         The source directory may contain .xml and .metadata files grouped by pairs and alphabetically classified:"
27   echo
28   echo "            11.hex.as.xml  15.hex.as.xml  19.hex.as.xml ..."
29   echo "            11.metadata    15.metadata    19.metadata   ..."
30   echo "            13.hex.as.xml  17.hex.as.xml  21.hex.as.xml ..."
31   echo "            13.metadata    17.metadata    21.metadata   ..."
32   echo
33   echo "         The order comes from first dot-separated part, which is normally the decoded frame from a pcap file:"
34   echo
35   echo "            11.hex.as.xml: would be the ANNA-Diameter decoded message from hexadecimal sniffed one."
36   echo "            11.metadata:   would contain parsed metadata from tshark or another fulfilling this information:"
37   echo
38   echo "                           date=Sat Dec 19 11:50:55 CET 2015"
39   echo "                           timestamp=1450522255.205673000"
40   echo "                           src=gentraf"
41   echo "                           dst=testbed"
42   echo "                           code=257"
43   echo "                           isrequest=1"
44   echo "                           applicationid=0"
45   echo
46   echo "         Presented example has been achieved using the 'example/diameter/pcapDecoder/tsharkDecoder.sh' script"
47   echo "         and then decoding with 'example/diameter/batchConverter' tool with appropiate diameter dictionaries."
48   echo
49   echo
50   echo "       test_end_points: space-separated list of possible test end points (hostnames, ip addresses)"
51   echo
52   echo "         This script will create a basic testcase based on frames timeline (11, 13, 15, etc.), from the desired"
53   echo "         end-point list. For example, from the 'gentraf' point of view, outgoing requests will be translated as"
54   echo "         sendxml operations with corresponding waits for answers and corresponding result codes (normally 2001)"
55   echo "         if those answers are provided (2001 will be assigned as result-code condition if no answer is found)."
56   echo "         CER/A are detected creating a symbolic link to better reference them, and DPR/A are ignored."
57   echo
58   echo "         The provided test end point list shall match metadata 'src' field in order to filter the valid testcase"
59   echo "         messages. In case of nothing provided (empty string) a list with the possible values from the source"
60   echo "         directory will be shown."
61   echo
62   echo
63   echo "       timeout:"
64   echo
65   echo "         Timeout in seconds for the testcase. If not provided, no timeout will be programmed for the testcase."
66   echo
67   echo
68   echo
69   echo "       The resulting '$TESTCASE_BN' will have all the operations needed to program the test case and will be"
70   echo "       written on same source directory referencing xml files as local-relative ones. Probably, 'change-dir'"
71   echo "       operation will be used before programming to ease the procedure without having to copy the stuff into"
72   echo "       installed ADML execution directory."
73   echo
74   echo "       Connection issues (CER/A), disconnect procedures (DPR/A), database populations and sanity checks are"
75   echo "       not responsability for this script. Such operations shall be externally performed to guarantee that"
76   echo "       programming this testcase is going to be valid in context of test execution. Anyway, CER messages"
77   echo "       will be detected and symbolically linked with the name 'cer.<origin host>.xml'."
78   _exit
79 }
80
81 # $1: xml file
82 getOriginHost () {
83   grep "<avp name=\"Origin-Host\"" $1 | cut -d\" -f4
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: metadata file; $2: xml file; $3: check Result-Code indicator
93 update_testcase () {
94   # metadata aspect:
95   #
96   # date=Wed Oct  7 01:51:16 CEST 2015
97   # timestamp=1444175476.212667000
98   # src=gt_traf
99   # dst=vcbavipt
100   # code=258
101   # isrequest=0
102   # applicationid=16777238
103   local mtd=$1
104   local xml=$2
105   local resultcode=$3
106
107   # Ignore disconnect peer messages on testcase format (they have no session-id):
108   grep "^code=282$" $mtd > /dev/null
109   [ $? -eq 0 ] && return
110
111   local code=$(grep ^code $mtd | cut -d= -f2)
112   local isrequest=$(grep ^isrequest $mtd | cut -d= -f2)
113   local sessionid=$(grep Session-Id $xml | cut -d\" -f4)
114   local lines=$(wc -l $TESTCASE_BN | awk '{ print $1 }')
115
116   local s_waitfe="test|1|waitfe|$code|$((1-isrequest))"
117   [ -n "$sessionid" ] && s_waitfe="${s_waitfe}|||${sessionid}"
118
119   if [ $isrequest -eq 1 ]
120   then
121     echo "test|1|sendxml2e|$xml" >> $TESTCASE_BN
122     if [ -n "$resultcode" ]
123     then
124       local hbh="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
125       local hbh_matchs=( $(grep -l "$hbh" *.xml) )
126       local ans_xml=${hbh_matchs[1]}
127       local rc=2001
128       if [ -n "$ans_xml" ]
129       then 
130         _rc=$(getResultCode $ans_xml)
131         [ -n "$_rc" ] && rc=$_rc
132       fi
133       s_waitfe="${s_waitfe}|${rc}"
134     fi
135     echo "$s_waitfe" >> $TESTCASE_BN
136   else
137     echo "$s_waitfe" >> $TESTCASE_BN
138     echo "test|1|sendxml2e|$xml|$((lines+1))" >> $TESTCASE_BN
139   fi
140 }
141
142 #############
143 # EXECUTION #
144 #############
145 SOURCE_DIR=$1
146 [ -z "$SOURCE_DIR" ] && usage
147 SOURCE_DIR=`readlink -f $SOURCE_DIR`
148 [ ! -d "$SOURCE_DIR" ] && _exit "Can't found provided directory '$SOURCE_DIR'"
149 # Work on source directory:
150 cd $SOURCE_DIR
151
152 # Must have metadata:
153 ls *.*metadata >/dev/null 2>/dev/null
154 [ $? -ne 0 ] && _exit "Cannot found '*.*metadata' files on '$SOURCE_DIR' !!"
155
156 # Show possible end points if nothing provided:
157 END_POINTS="$2"
158 if [ -z "$END_POINTS" ]
159 then
160   src_eps=( $(grep "^src=" *.*metadata | cut -d= -f2 | sort -u) )
161   [ ${#src_eps[@]} -eq 0 ] && _exit "No source end-points detected: metadata must have an 'src=xxx' line."
162   if [ ${#src_eps[@]} -eq 1 ]
163   then
164     END_POINTS=$src_eps
165     echo "Detected a unique end-point: $END_POINTS"
166   else
167     echo "Input a space-separated list for desired end-points to be processed as test side: "
168     echo " (available source end-points: ${src_eps[*]})"
169     read END_POINTS
170     [ -z "$END_POINTS" ] && _exit "Invalid empty input !!"
171   fi
172 fi
173
174 # Identify useful frames: those which are created at test-bed side (all except frames coming from tested systems):
175 rm -f *.needed
176 for tag in $END_POINTS
177 do
178   for metadata in $(grep "^src=$tag$" *.*metadata)
179   do
180     frame=$(echo $metadata | cut -d\. -f1)
181     touch ${frame}.needed
182   done
183 done
184 ls *.needed >/dev/null 2>/dev/null
185 [ $? -ne 0 ] && _exit "No frame has been selected within '$SOURCE_DIR' for provided end-points ($END_POINTS) !!"
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=$3
198 [ -n "$TIMEOUT_SEC" ] && echo "test|1|timeout|$((TIMEOUT_SEC * 1000))" >> $TESTCASE_BN
199
200 # Process frames:
201 for frame in `ls *.needed | cut -d\. -f1 | sort -n`
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
208   # Ignore keep alives:
209   grep -q "^code=280$" $mtd
210   [ $? -eq 0 ] && continue
211   
212   grep -q "^isrequest=1$" $mtd
213   if [ $? -eq 0 ]
214   then
215     # CER's:
216     grep -q "^code=257$" $mtd
217     if [ $? -eq 0 ]
218     then
219       echo $frame >> cers_4_starting
220       originHost=$(getOriginHost $xml)
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     update_testcase $mtd $xml check_result_code
229   else
230     # CEA's:
231     grep -q "^code=257$" $mtd
232     if [ $? -eq 0 ]
233     then
234       echo $frame >> ceas_4_establishing
235       originHost=$(getOriginHost $xml)
236       [ -z "$originHost" ] &&  _exit "Missing Origin-Host (frame $frame, CEA message) !!"
237       echo "$originHost" >> ceas_4_establishing_origin_hosts
238       ln -sf $xml cer.${originHost}.xml
239       continue
240     fi
241     # Other answers:
242     echo $frame >> answers_4_programming
243     update_testcase $mtd $xml
244   fi
245 done
246 rm -f *.needed
247
248
249 # We will replace all the requests hop-by-hop's with a unique value, to avoid bad sniffing cases (different sources using bad values).
250 # For example, the frame number could be valid enough.
251 hbh_ini=
252 hbh_fin=
253 # involved frames:
254 involved_xmls=
255 grep sendxml $TESTCASE_BN > .involved_frames
256 n_involved=`wc -l .involved_frames | awk '{ print $1 }'`
257 count=1
258 for frame in `cat .involved_frames | cut -d\| -f4 | cut -d\. -f1`
259 do
260   xml=( `ls ${frame}.*xml` )
261   involved_xmls="$involved_xmls $xml"
262   mtd=( `ls ${frame}.*metadata` )
263   isrequest=$(grep "isrequest=1" $mtd)
264   if [ -n "$isrequest" ]
265   then
266     hbh_ini="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
267     sed -i 's/'$hbh_ini'/hop-by-hop-id="'$frame'"/' $xml
268   fi
269   count=$((count+1))
270 done
271
272 # PCAPs without CER messages ...
273 if [ ! -s cers_4_starting_origin_hosts ]
274 then
275   if [ $n_involved -ne 0 ]
276   then 
277     grep "<avp name=\"Origin-Host\"" $involved_xmls | cut -d\" -f4 | sort -u > cers_4_starting_origin_hosts
278     for oh in `cat cers_4_starting_origin_hosts`
279     do
280       cer_file=cer.${oh}.xml
281       touch $cer_file
282       echo "WARNING: perhaps you should configure '$cer_file' because it is missing in the pcap file provided."
283       echo "         Currently it contains a basic template but you must fill/fix the unknowns '$WHAT'."
284       cat << EOF > $cer_file
285 <message version="1" name="CER" application-id="0" hop-by-hop-id="1" end-by-end-id="1">
286    <avp name="Origin-Host" data="$oh"/>
287    <avp name="Origin-Realm" data="$(echo $oh | cut -d\. -f2-) $WHAT"/>
288    <avp name="Auth-Application-Id" data="16777236 $WHAT 16777238 $WHAT"/>
289    <avp name="Origin-State-Id" data="1"/>
290    <avp name="Host-IP-Address" data="1|192.168.14.42 $WHAT"/>
291    <avp name="Vendor-Id" data="193"/>
292    <avp name="Product-Name" data="afNode $WHAT ggsnNode $WHAT"/>
293    <avp name="Firmware-Revision" data="1"/>
294 </message>
295 EOF
296     done
297   #else
298   #  echo "WARNING: No 'sendxml' primitives on testcase ??"
299   fi
300 fi
301
302 # PCAPs without CEA messages ...
303 if [ ! -s ceas_4_establishing_origin_hosts ]
304 then
305   if [ $n_involved -ne 0 ]
306   then 
307     grep "<avp name=\"Origin-Host\"" $involved_xmls | cut -d\" -f4 | sort -u > ceas_4_establishing_origin_hosts
308     for oh in `cat ceas_4_establishing_origin_hosts`
309     do
310       cea_file=cea.${oh}.xml
311       touch $cea_file
312       echo "WARNING: perhaps you should configure '$cea_file' because it is missing in the pcap file provided."
313       echo "         Currently it contains a basic template but you must fill/fix the unknowns '$WHAT'."
314       cat << EOF > $cea_file
315 <message version="1" name="CEA" application-id="0" hop-by-hop-id="1" end-by-end-id="1">
316    <avp name="Result-Code" data="2001" alias="DIAMETER_SUCCESS"/>
317    <avp name="Origin-Host" data="$oh"/>
318    <avp name="Origin-Realm" data="$(echo $oh | cut -d\. -f2-) $WHAT"/>
319    <avp name="Host-IP-Address" data="1|192.168.12.42 $WHAT"/>
320    <avp name="Vendor-Id" data="193"/>
321    <avp name="Product-Name" data="SAPC"/>
322    <avp name="Supported-Vendor-Id" data="5535"/>
323    <avp name="Supported-Vendor-Id" data="10415"/>
324    <avp name="Auth-Application-Id" data="16777238"/>
325    <avp name="Auth-Application-Id" data="16777236"/>
326    <avp name="Vendor-Specific-Application-Id">
327       <avp name="Vendor-Id" data="10415"/>
328       <avp name="Auth-Application-Id" data="16777238"/>
329    </avp>
330    <avp name="Vendor-Specific-Application-Id">
331       <avp name="Vendor-Id" data="10415"/>
332       <avp name="Auth-Application-Id" data="16777236"/>
333    </avp>
334    <avp name="Firmware-Revision" data="1"/>
335 </message>
336 EOF
337     done
338   #else
339   #  echo "WARNING: No 'sendxml' primitives on testcase ??"
340   fi
341 fi
342
343 exit 0
344