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