tiny testcase creator and example
[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 successful result code (2001). CER/A are"
53   echo "         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. By default, 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: frame number (there must be .metadata and .xml files); $2: check Result-Code indicator
79 update_testcase () {
80   # metadata aspect:
81   #
82   # date=Wed Oct  7 01:51:16 CEST 2015
83   # timestamp=1444175476.212667000
84   # src=gt_traf
85   # dst=vcbavipt
86   # code=258
87   # isrequest=0
88   # applicationid=16777238
89   mtd=${1}.*metadata
90   xml=( `ls ${1}.*xml 2>/dev/null` )
91   [ ${#xml[@]} -ne 1 ] && _exit "There must be one xml message file corresponding to '$mtd' !!"
92   resultcode=$2
93
94   # Ignore disconnect peer messages on testcase format (they have no session-id):
95   grep "^code=282$" $mtd > /dev/null
96   [ $? -eq 0 ] && return
97
98   code=$(grep ^code $mtd | cut -d= -f2)
99   isrequest=$(grep ^isrequest $mtd | cut -d= -f2)
100   sessionid=$(grep Session-Id $xml | cut -d\" -f4)
101   lines=$(wc -l $TESTCASE_BN | awk '{ print $1 }')
102
103   s_waitfe="test|1|waitfe|$code|$((1-isrequest))"
104   [ -n "$sessionid" ] && s_waitfe="${s_waitfe}|||${sessionid}"
105
106   if [ $isrequest -eq 1 ]
107   then
108     echo "test|1|sendxml2e|$xml" >> $TESTCASE_BN
109     [ -n "$resultcode" ] && s_waitfe="${s_waitfe}|2001"
110     echo "$s_waitfe" >> $TESTCASE_BN
111   else
112     echo "$s_waitfe" >> $TESTCASE_BN
113     echo "test|1|sendxml2e|$xml|$((lines+1))" >> $TESTCASE_BN
114   fi
115 }
116
117 #############
118 # EXECUTION #
119 #############
120 echo
121 SOURCE_DIR=$1
122 SOURCE_DIR=`readlink -f $SOURCE_DIR`
123 [ ! -d "$SOURCE_DIR" ] && _exit "Can't found provided directory '$SOURCE_DIR'"
124 # Work on source directory:
125 cd $SOURCE_DIR
126
127 # Must have metadata:
128 ls *.*metadata >/dev/null 2>/dev/null
129 [ $? -ne 0 ] && _exit "Cannot found '*.*metadata' files on '$SOURCE_DIR' !!"
130
131 # Show possible end points if nothing provided:
132 END_POINTS="$2"
133 if [ -z "$END_POINTS" ]
134 then
135   src_eps=( $(grep "^src=" *.*metadata | cut -d= -f2 | sort -u) )
136   [ ${#src_eps[@]} -eq 0 ] && _exit "No source end-points detected: metadata must have an 'src=xxx' line."
137   echo "Input a space-separated list for desired end-points to be processed as test side: "
138   echo " (available source end-points: ${src_eps[*]})"
139   read END_POINTS
140   [ -z "$END_POINTS" ] && _exit "Invalid empty input !!"
141 fi
142
143 # Identify useful frames: those which are created at test-bed side (all except frames coming from tested systems):
144 rm -f *.needed
145 for tag in $END_POINTS
146 do
147   for metadata in $(grep "^src=$tag$" *.*metadata)
148   do
149     frame=$(echo $metadata | cut -d\. -f1)
150     touch ${frame}.needed
151   done
152 done
153 ls *.needed >/dev/null 2>/dev/null
154 [ $? -ne 0 ] && _exit "No frame has been selected within '$SOURCE_DIR' for provided end-points ($END_POINTS) !!"
155
156 # Messages classification:
157 0> cers_4_starting
158 0> requests_4_sending
159 0> answers_4_programming
160 0> $TESTCASE_BN
161
162 # Optional timeout:
163 TIMEOUT_SEC=$3
164 [ -n "$TIMEOUT_SEC" ] && echo "test|1|timeout|$((TIMEOUT_SEC * 1000))" >> $TESTCASE_BN
165
166 # Process frames:
167 for frame in `ls *.needed | cut -d\. -f1 | sort -n`
168 do
169   file=( `ls ${frame}.*metadata 2>/dev/null` )
170   [ ${#file[@]} -ne 1 ] && _exit "There must be one metadata file corresponding to frame '$frame' !!"
171   
172   # Ignore keep alives:
173   grep -q "^code=280$" $file
174   [ $? -eq 0 ] && continue
175   
176   grep -q "^isrequest=1$" $file
177   if [ $? -eq 0 ]
178   then
179     grep -q "^code=257$" $file
180     [ $? -eq 0 ] && { echo $frame >> cers_4_starting ; continue ; }
181     echo $frame >> requests_4_sending
182     update_testcase $frame check_result_code
183   else
184     # Ignore CEA's:
185     grep -q "^code=257$" $file
186     [ $? -eq 0 ] && continue
187     echo $frame >> answers_4_programming
188     update_testcase $frame
189   fi
190 done
191 rm -f *.needed
192
193 # We will replace all the requests hop-by-hop's with a unique value, to avoid bad sniffing cases (different sources using bad values).
194 # For example, the frame number could be valid enough.
195 hbh_ini=
196 hbh_fin=
197 # involved frames:
198 grep sendxml $TESTCASE_BN > .involved_frames
199 n_involved=`wc -l .involved_frames | awk '{ print $1 }'`
200 count=1
201 for frame in `cat .involved_frames | cut -d\| -f4 | cut -d\. -f1`
202 do
203   xml=( `ls ${frame}.*xml` )
204   mtd=( `ls ${frame}.*metadata` )
205   isrequest=$(grep "isrequest=1" $mtd)
206   if [ -n "$isrequest" ]
207   then
208     hbh_ini="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
209     sed -i 's/'$hbh_ini'/hop-by-hop-id="'$frame'"/' $xml
210   fi
211   count=$((count+1))
212 done
213