Cer and cea helper
[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   echo
79   echo "       PCAPS WITHOUT CER/CEA MESSAGES:"
80   echo
81   echo "       There is a helper tool to build CER/CEA messages for involved origin hosts. This is useful when the"
82   echo "       pcap file analyzed miss the capabilities exchange messages: touch the file 'create_cer_and_cea' inside"
83   echo "       the source directory, and follow the instructions." 
84   _exit
85 }
86
87 # $1: xml file
88 getOriginHost () {
89   grep "<avp name=\"Origin-Host\"" $1 | cut -d\" -f4
90 }
91  
92 # $1: xml file
93 # Nested Result-Code's not supported here (i.e. charging application)
94 getResultCode () {
95   grep "<avp name=\"Result-Code\"" $1 | awk -F'data=' '{ print $2 }' | cut -d\" -f2
96 }
97  
98 # $1: metadata file; $2: xml file; $3: check Result-Code indicator
99 update_testcase () {
100   # metadata aspect:
101   #
102   # date=Wed Oct  7 01:51:16 CEST 2015
103   # timestamp=1444175476.212667000
104   # src=gt_traf
105   # dst=vcbavipt
106   # code=258
107   # isrequest=0
108   # applicationid=16777238
109   local mtd=$1
110   local xml=$2
111   local resultcode=$3
112
113   # Ignore disconnect peer messages on testcase format (they have no session-id):
114   grep "^code=282$" $mtd > /dev/null
115   [ $? -eq 0 ] && return
116
117   local code=$(grep ^code $mtd | cut -d= -f2)
118   local isrequest=$(grep ^isrequest $mtd | cut -d= -f2)
119   local sessionid=$(grep Session-Id $xml | cut -d\" -f4)
120   local lines=$(wc -l $TESTCASE_BN | awk '{ print $1 }')
121
122   local s_waitfe="test|1|waitfe|$code|$((1-isrequest))"
123   [ -n "$sessionid" ] && s_waitfe="${s_waitfe}|||${sessionid}"
124
125   if [ $isrequest -eq 1 ]
126   then
127     echo "test|1|sendxml2e|$xml" >> $TESTCASE_BN
128     if [ -n "$resultcode" ]
129     then
130       local hbh="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
131       local hbh_matchs=( $(grep -l "$hbh" *.xml) )
132       local ans_xml=${hbh_matchs[1]}
133       local rc=2001
134       if [ -n "$ans_xml" ]
135       then 
136         _rc=$(getResultCode $ans_xml)
137         [ -n "$_rc" ] && rc=$_rc
138       fi
139       s_waitfe="${s_waitfe}|${rc}"
140     fi
141     echo "$s_waitfe" >> $TESTCASE_BN
142   else
143     echo "$s_waitfe" >> $TESTCASE_BN
144     echo "test|1|sendxml2e|$xml|$((lines+1))" >> $TESTCASE_BN
145   fi
146 }
147
148 #############
149 # EXECUTION #
150 #############
151 SOURCE_DIR=$1
152 [ -z "$SOURCE_DIR" ] && usage
153 SOURCE_DIR=`readlink -f $SOURCE_DIR`
154 [ ! -d "$SOURCE_DIR" ] && _exit "Can't found provided directory '$SOURCE_DIR'"
155 # Work on source directory:
156 cd $SOURCE_DIR
157
158 # Must have metadata:
159 ls *.*metadata >/dev/null 2>/dev/null
160 [ $? -ne 0 ] && _exit "Cannot found '*.*metadata' files on '$SOURCE_DIR' !!"
161
162 # Show possible end points if nothing provided:
163 END_POINTS="$2"
164 if [ -z "$END_POINTS" ]
165 then
166   src_eps=( $(grep "^src=" *.*metadata | cut -d= -f2 | sort -u) )
167   [ ${#src_eps[@]} -eq 0 ] && _exit "No source end-points detected: metadata must have an 'src=xxx' line."
168   if [ ${#src_eps[@]} -eq 1 ]
169   then
170     END_POINTS=$src_eps
171     echo "Detected a unique end-point: $END_POINTS"
172   else
173     echo "Input a space-separated list for desired end-points to be processed as test side: "
174     echo " (available source end-points: ${src_eps[*]})"
175     read END_POINTS
176     [ -z "$END_POINTS" ] && _exit "Invalid empty input !!"
177   fi
178 fi
179
180 # Identify useful frames: those which are created at test-bed side (all except frames coming from tested systems):
181 rm -f *.needed
182 for tag in $END_POINTS
183 do
184   for metadata in $(grep "^src=$tag$" *.*metadata)
185   do
186     frame=$(echo $metadata | cut -d\. -f1)
187     touch ${frame}.needed
188   done
189 done
190 ls *.needed >/dev/null 2>/dev/null
191 [ $? -ne 0 ] && _exit "No frame has been selected within '$SOURCE_DIR' for provided end-points ($END_POINTS) !!"
192
193 # Messages classification:
194 0> cers_4_starting
195 0> ceas_4_establishing
196 0> cers_4_starting_origin_hosts
197 0> ceas_4_establishing_origin_hosts
198 0> requests_4_sending
199 0> answers_4_programming
200 0> $TESTCASE_BN
201
202 # Optional timeout:
203 TIMEOUT_SEC=$3
204 [ -n "$TIMEOUT_SEC" ] && echo "test|1|timeout|$((TIMEOUT_SEC * 1000))" >> $TESTCASE_BN
205
206 # Process frames:
207 for frame in `ls *.needed | cut -d\. -f1 | sort -n`
208 do
209   mtd=( `ls ${frame}.*metadata 2>/dev/null` )
210   [ ${#mtd[@]} -ne 1 ] && _exit "There must be one metadata file corresponding to frame '$frame' !!"
211   xml=( `ls ${frame}.*xml 2>/dev/null` )
212   [ ${#xml[@]} -ne 1 ] && _exit "There must be one xml message file corresponding to '$mtd' (frame '$frame') !!"
213
214   # Ignore keep alives:
215   grep -q "^code=280$" $mtd
216   [ $? -eq 0 ] && continue
217   
218   grep -q "^isrequest=1$" $mtd
219   if [ $? -eq 0 ]
220   then
221     # CER's:
222     grep -q "^code=257$" $mtd
223     if [ $? -eq 0 ]
224     then
225       echo $frame >> cers_4_starting
226       originHost=$(getOriginHost $xml)
227       [ -z "$originHost" ] &&  _exit "Missing Origin-Host (frame $frame, CER message) !!"
228       echo "$originHost" >> cers_4_starting_origin_hosts
229       ln -sf $xml cer.${originHost}.xml
230       continue
231     fi
232     # Other requests:
233     echo $frame >> requests_4_sending
234     update_testcase $mtd $xml check_result_code
235   else
236     # CEA's:
237     grep -q "^code=257$" $mtd
238     if [ $? -eq 0 ]
239     then
240       echo $frame >> ceas_4_establishing
241       originHost=$(getOriginHost $xml)
242       [ -z "$originHost" ] &&  _exit "Missing Origin-Host (frame $frame, CEA message) !!"
243       echo "$originHost" >> ceas_4_establishing_origin_hosts
244       ln -sf $xml cea.${originHost}.xml
245       continue
246     fi
247     # Other answers:
248     echo $frame >> answers_4_programming
249     update_testcase $mtd $xml
250   fi
251 done
252 rm -f *.needed
253
254
255 # We will replace all the requests hop-by-hop's with a unique value, to avoid bad sniffing cases (different sources using bad values).
256 # For example, the frame number could be valid enough.
257 hbh_ini=
258 hbh_fin=
259 # involved frames:
260 involved_xmls=
261 grep sendxml $TESTCASE_BN > .involved_frames
262 n_involved=`wc -l .involved_frames | awk '{ print $1 }'`
263 count=1
264 for frame in `cat .involved_frames | cut -d\| -f4 | cut -d\. -f1`
265 do
266   xml=( `ls ${frame}.*xml` )
267   involved_xmls="$involved_xmls $xml"
268   mtd=( `ls ${frame}.*metadata` )
269   isrequest=$(grep "isrequest=1" $mtd)
270   if [ -n "$isrequest" ]
271   then
272     hbh_ini="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
273     sed -i 's/'$hbh_ini'/hop-by-hop-id="'$frame'"/' $xml
274   fi
275   count=$((count+1))
276 done
277
278 if [ -f $SOURCE_DIR/create_cer_and_cea ]
279 then
280   # PCAPs without CER messages ...
281   if [ ! -s cers_4_starting_origin_hosts ]
282   then
283     if [ $n_involved -ne 0 ]
284     then 
285       grep "<avp name=\"Origin-Host\"" $involved_xmls | cut -d\" -f4 | sort -u > cers_4_starting_origin_hosts
286       for oh in `cat cers_4_starting_origin_hosts`
287       do
288         cer_file=cer.${oh}.xml
289         touch $cer_file
290         echo "WARNING: perhaps you should configure '$cer_file' because it is missing in the pcap file provided."
291         echo "         Currently it contains a basic template but you must fill/fix the unknowns '$WHAT'."
292         cat << EOF > $cer_file
293 <message version="1" name="CER" application-id="0" hop-by-hop-id="1" end-by-end-id="1">
294    <avp name="Origin-Host" data="$oh"/>
295    <avp name="Origin-Realm" data="$(echo $oh | cut -d\. -f2-) $WHAT"/>
296    <avp name="Auth-Application-Id" data="16777236 $WHAT 16777238 $WHAT"/>
297    <avp name="Origin-State-Id" data="1"/>
298    <avp name="Host-IP-Address" data="1|192.168.14.42 $WHAT"/>
299    <avp name="Vendor-Id" data="193"/>
300    <avp name="Product-Name" data="afNode $WHAT ggsnNode $WHAT"/>
301    <avp name="Firmware-Revision" data="1"/>
302 </message>
303 EOF
304       done
305     #else
306     #  echo "WARNING: No 'sendxml' primitives on testcase ??"
307     fi
308   fi
309
310   # PCAPs without CEA messages ...
311   if [ ! -s ceas_4_establishing_origin_hosts ]
312   then
313     if [ $n_involved -ne 0 ]
314     then 
315       grep "<avp name=\"Origin-Host\"" $involved_xmls | cut -d\" -f4 | sort -u > ceas_4_establishing_origin_hosts
316       for oh in `cat ceas_4_establishing_origin_hosts`
317       do
318         cea_file=cea.${oh}.xml
319         touch $cea_file
320         echo "WARNING: perhaps you should configure '$cea_file' because it is missing in the pcap file provided."
321         echo "         Currently it contains a basic template but you must fill/fix the unknowns '$WHAT'."
322         cat << EOF > $cea_file
323 <message version="1" name="CEA" application-id="0" hop-by-hop-id="1" end-by-end-id="1">
324    <avp name="Result-Code" data="2001" alias="DIAMETER_SUCCESS"/>
325    <avp name="Origin-Host" data="$oh"/>
326    <avp name="Origin-Realm" data="$(echo $oh | cut -d\. -f2-) $WHAT"/>
327    <avp name="Host-IP-Address" data="1|192.168.12.42 $WHAT"/>
328    <avp name="Vendor-Id" data="193"/>
329    <avp name="Product-Name" data="SAPC"/>
330    <avp name="Supported-Vendor-Id" data="5535"/>
331    <avp name="Supported-Vendor-Id" data="10415"/>
332    <avp name="Auth-Application-Id" data="16777238"/>
333    <avp name="Auth-Application-Id" data="16777236"/>
334    <avp name="Vendor-Specific-Application-Id">
335       <avp name="Vendor-Id" data="10415"/>
336       <avp name="Auth-Application-Id" data="16777238"/>
337    </avp>
338    <avp name="Vendor-Specific-Application-Id">
339       <avp name="Vendor-Id" data="10415"/>
340       <avp name="Auth-Application-Id" data="16777236"/>
341    </avp>
342    <avp name="Firmware-Revision" data="1"/>
343 </message>
344 EOF
345       done
346     #else
347     #  echo "WARNING: No 'sendxml' primitives on testcase ??"
348     fi
349   fi
350 fi
351
352 exit 0
353