improv. output
[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
9 #############
10 # FUNCTIONS #
11 #############
12 _exit() {
13   echo
14   echo -e $1
15   echo
16   exit 1
17 }
18
19 usage() {
20   echo
21   echo "Usage: $0 <source_directory> \"[test_end_points]\" [timeout]"
22   echo
23   echo "       source_directory:"
24   echo
25   echo "         The source directory may contain .xml and .metadata files grouped by pairs and alphabetically classified:"
26   echo
27   echo "            11.hex.as.xml  15.hex.as.xml  19.hex.as.xml ..."
28   echo "            11.metadata    15.metadata    19.metadata   ..."
29   echo "            13.hex.as.xml  17.hex.as.xml  21.hex.as.xml ..."
30   echo "            13.metadata    17.metadata    21.metadata   ..."
31   echo
32   echo "         The order comes from first dot-separated part, which is normally the decoded frame from a pcap file:"
33   echo
34   echo "            11.hex.as.xml: would be the ANNA-Diameter decoded message from hexadecimal sniffed one."
35   echo "            11.metadata:   would contain parsed metadata from tshark or another fulfilling this information:"
36   echo
37   echo "                           date=Sat Dec 19 11:50:55 CET 2015"
38   echo "                           timestamp=1450522255.205673000"
39   echo "                           src=gentraf"
40   echo "                           dst=testbed"
41   echo "                           code=257"
42   echo "                           isrequest=1"
43   echo "                           applicationid=0"
44   echo
45   echo "         Presented example has been achieved using the 'example/diameter/pcapDecoder/tsharkDecoder.sh' script"
46   echo "         and then decoding with 'example/diameter/batchConverter' tool with appropiate diameter dictionaries."
47   echo
48   echo
49   echo "       test_end_points: space-separated list of possible test end points (hostnames, ip addresses)"
50   echo
51   echo "         This script will create a basic testcase based on frames timeline (11, 13, 15, etc.), from the desired"
52   echo "         end-point list. For example, from the 'gentraf' point of view, outgoing requests will be translated as"
53   echo "         sendxml operations with corresponding waits for answers and corresponding result codes (normally 2001)"
54   echo "         if those answers are provided (2001 will be assigned as result-code condition if no answer is found)."
55   echo "         CER/A are detected creating a symbolic link to better reference them, and DPR/A are ignored."
56   echo
57   echo "         The provided test end point list shall match metadata 'src' field in order to filter the valid testcase"
58   echo "         messages. In case of nothing provided (empty string) a list with the possible values from the source"
59   echo "         directory will be shown."
60   echo
61   echo
62   echo "       timeout:"
63   echo
64   echo "         Timeout in seconds for the testcase. If not provided, no timeout will be programmed for the testcase."
65   echo
66   echo
67   echo
68   echo "       The resulting '$TESTCASE_BN' will have all the operations needed to program the test case and will be"
69   echo "       written on same source directory referencing xml files as local-relative ones. Probably, 'change-dir'"
70   echo "       operation will be used before programming to ease the procedure without having to copy the stuff into"
71   echo "       installed ADML execution directory."
72   echo
73   echo "       Connection issues (CER/A), disconnect procedures (DPR/A), database populations and sanity checks are"
74   echo "       not responsability for this script. Such operations shall be externally performed to guarantee that"
75   echo "       programming this testcase is going to be valid in context of test execution. Anyway, CER messages"
76   echo "       will be detected and symbolically linked with the name 'cer.<origin host>.xml'."
77   _exit
78 }
79
80 # $1: xml file
81 getOriginHost () {
82   grep "<avp name=\"Origin-Host\"" $1 | cut -d\" -f4
83 }
84  
85 # $1: xml file
86 # Nested Result-Code's not supported here (i.e. charging application)
87 getResultCode () {
88   grep "<avp name=\"Result-Code\"" $1 | awk -F'data=' '{ print $2 }' | cut -d\" -f2
89 }
90  
91 # $1: metadata file; $2: xml file; $3: check Result-Code indicator
92 update_testcase () {
93   # metadata aspect:
94   #
95   # date=Wed Oct  7 01:51:16 CEST 2015
96   # timestamp=1444175476.212667000
97   # src=gt_traf
98   # dst=vcbavipt
99   # code=258
100   # isrequest=0
101   # applicationid=16777238
102   local mtd=$1
103   local xml=$2
104   local resultcode=$3
105
106   # Ignore disconnect peer messages on testcase format (they have no session-id):
107   grep "^code=282$" $mtd > /dev/null
108   [ $? -eq 0 ] && return
109
110   local code=$(grep ^code $mtd | cut -d= -f2)
111   local isrequest=$(grep ^isrequest $mtd | cut -d= -f2)
112   local sessionid=$(grep Session-Id $xml | cut -d\" -f4)
113   local lines=$(wc -l $TESTCASE_BN | awk '{ print $1 }')
114
115   local s_waitfe="test|1|waitfe|$code|$((1-isrequest))"
116   [ -n "$sessionid" ] && s_waitfe="${s_waitfe}|||${sessionid}"
117
118   if [ $isrequest -eq 1 ]
119   then
120     echo "test|1|sendxml2e|$xml" >> $TESTCASE_BN
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       s_waitfe="${s_waitfe}|${rc}"
133     fi
134     echo "$s_waitfe" >> $TESTCASE_BN
135   else
136     echo "$s_waitfe" >> $TESTCASE_BN
137     echo "test|1|sendxml2e|$xml|$((lines+1))" >> $TESTCASE_BN
138   fi
139 }
140
141 #############
142 # EXECUTION #
143 #############
144 SOURCE_DIR=$1
145 [ -z "$SOURCE_DIR" ] && usage
146 SOURCE_DIR=`readlink -f $SOURCE_DIR`
147 [ ! -d "$SOURCE_DIR" ] && _exit "Can't found provided directory '$SOURCE_DIR'"
148 # Work on source directory:
149 cd $SOURCE_DIR
150
151 # Must have metadata:
152 ls *.*metadata >/dev/null 2>/dev/null
153 [ $? -ne 0 ] && _exit "Cannot found '*.*metadata' files on '$SOURCE_DIR' !!"
154
155 # Show possible end points if nothing provided:
156 END_POINTS="$2"
157 if [ -z "$END_POINTS" ]
158 then
159   src_eps=( $(grep "^src=" *.*metadata | cut -d= -f2 | sort -u) )
160   [ ${#src_eps[@]} -eq 0 ] && _exit "No source end-points detected: metadata must have an 'src=xxx' line."
161   if [ ${#src_eps[@]} -eq 1 ]
162   then
163     END_POINTS=$src_eps
164     echo "Detected a unique end-point: $END_POINTS"
165   else
166     echo "Input a space-separated list for desired end-points to be processed as test side: "
167     echo " (available source end-points: ${src_eps[*]})"
168     read END_POINTS
169     [ -z "$END_POINTS" ] && _exit "Invalid empty input !!"
170   fi
171 fi
172
173 # Identify useful frames: those which are created at test-bed side (all except frames coming from tested systems):
174 rm -f *.needed
175 for tag in $END_POINTS
176 do
177   for metadata in $(grep "^src=$tag$" *.*metadata)
178   do
179     frame=$(echo $metadata | cut -d\. -f1)
180     touch ${frame}.needed
181   done
182 done
183 ls *.needed >/dev/null 2>/dev/null
184 [ $? -ne 0 ] && _exit "No frame has been selected within '$SOURCE_DIR' for provided end-points ($END_POINTS) !!"
185
186 # Messages classification:
187 0> cers_4_starting
188 0> cers_4_starting_origin_hosts
189 0> requests_4_sending
190 0> answers_4_programming
191 0> $TESTCASE_BN
192
193 # Optional timeout:
194 TIMEOUT_SEC=$3
195 [ -n "$TIMEOUT_SEC" ] && echo "test|1|timeout|$((TIMEOUT_SEC * 1000))" >> $TESTCASE_BN
196
197 # Process frames:
198 for frame in `ls *.needed | cut -d\. -f1 | sort -n`
199 do
200   mtd=( `ls ${frame}.*metadata 2>/dev/null` )
201   [ ${#mtd[@]} -ne 1 ] && _exit "There must be one metadata file corresponding to frame '$frame' !!"
202   xml=( `ls ${frame}.*xml 2>/dev/null` )
203   [ ${#xml[@]} -ne 1 ] && _exit "There must be one xml message file corresponding to '$mtd' (frame '$frame') !!"
204
205   # Ignore keep alives:
206   grep -q "^code=280$" $mtd
207   [ $? -eq 0 ] && continue
208   
209   grep -q "^isrequest=1$" $mtd
210   if [ $? -eq 0 ]
211   then
212     grep -q "^code=257$" $mtd
213     if [ $? -eq 0 ]
214     then
215       echo $frame >> cers_4_starting
216       originHost=$(getOriginHost $xml)
217       [ -z "$originHost" ] &&  _exit "Missing Origin-Host (frame $frame, CER message) !!"
218       echo "$originHost" >> cers_4_starting_origin_hosts
219       ln -sf $xml cer.${originHost}.xml
220       continue
221     fi
222     echo $frame >> requests_4_sending
223     update_testcase $mtd $xml check_result_code
224   else
225     # Ignore CEA's:
226     grep -q "^code=257$" $mtd
227     [ $? -eq 0 ] && continue
228     echo $frame >> answers_4_programming
229     update_testcase $mtd $xml
230   fi
231 done
232 rm -f *.needed
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 grep sendxml $TESTCASE_BN > .involved_frames
240 n_involved=`wc -l .involved_frames | awk '{ print $1 }'`
241 count=1
242 for frame in `cat .involved_frames | cut -d\| -f4 | cut -d\. -f1`
243 do
244   xml=( `ls ${frame}.*xml` )
245   mtd=( `ls ${frame}.*metadata` )
246   isrequest=$(grep "isrequest=1" $mtd)
247   if [ -n "$isrequest" ]
248   then
249     hbh_ini="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
250     sed -i 's/'$hbh_ini'/hop-by-hop-id="'$frame'"/' $xml
251   fi
252   count=$((count+1))
253 done
254
255 exit 0
256