Fix bug char
[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. 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: 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 [ -z "$SOURCE_DIR" ] && usage
123 SOURCE_DIR=`readlink -f $SOURCE_DIR`
124 [ ! -d "$SOURCE_DIR" ] && _exit "Can't found provided directory '$SOURCE_DIR'"
125 # Work on source directory:
126 cd $SOURCE_DIR
127
128 # Must have metadata:
129 ls *.*metadata >/dev/null 2>/dev/null
130 [ $? -ne 0 ] && _exit "Cannot found '*.*metadata' files on '$SOURCE_DIR' !!"
131
132 # Show possible end points if nothing provided:
133 END_POINTS="$2"
134 if [ -z "$END_POINTS" ]
135 then
136   src_eps=( $(grep "^src=" *.*metadata | cut -d= -f2 | sort -u) )
137   [ ${#src_eps[@]} -eq 0 ] && _exit "No source end-points detected: metadata must have an 'src=xxx' line."
138   echo "Input a space-separated list for desired end-points to be processed as test side: "
139   echo " (available source end-points: ${src_eps[*]})"
140   read END_POINTS
141   [ -z "$END_POINTS" ] && _exit "Invalid empty input !!"
142 fi
143
144 # Identify useful frames: those which are created at test-bed side (all except frames coming from tested systems):
145 rm -f *.needed
146 for tag in $END_POINTS
147 do
148   for metadata in $(grep "^src=$tag$" *.*metadata)
149   do
150     frame=$(echo $metadata | cut -d\. -f1)
151     touch ${frame}.needed
152   done
153 done
154 ls *.needed >/dev/null 2>/dev/null
155 [ $? -ne 0 ] && _exit "No frame has been selected within '$SOURCE_DIR' for provided end-points ($END_POINTS) !!"
156
157 # Messages classification:
158 0> cers_4_starting
159 0> requests_4_sending
160 0> answers_4_programming
161 0> $TESTCASE_BN
162
163 # Optional timeout:
164 TIMEOUT_SEC=$3
165 [ -n "$TIMEOUT_SEC" ] && echo "test|1|timeout|$((TIMEOUT_SEC * 1000))" >> $TESTCASE_BN
166
167 # Process frames:
168 for frame in `ls *.needed | cut -d\. -f1 | sort -n`
169 do
170   file=( `ls ${frame}.*metadata 2>/dev/null` )
171   [ ${#file[@]} -ne 1 ] && _exit "There must be one metadata file corresponding to frame '$frame' !!"
172   
173   # Ignore keep alives:
174   grep -q "^code=280$" $file
175   [ $? -eq 0 ] && continue
176   
177   grep -q "^isrequest=1$" $file
178   if [ $? -eq 0 ]
179   then
180     grep -q "^code=257$" $file
181     [ $? -eq 0 ] && { echo $frame >> cers_4_starting ; continue ; }
182     echo $frame >> requests_4_sending
183     update_testcase $frame check_result_code
184   else
185     # Ignore CEA's:
186     grep -q "^code=257$" $file
187     [ $? -eq 0 ] && continue
188     echo $frame >> answers_4_programming
189     update_testcase $frame
190   fi
191 done
192 rm -f *.needed
193
194 # We will replace all the requests hop-by-hop's with a unique value, to avoid bad sniffing cases (different sources using bad values).
195 # For example, the frame number could be valid enough.
196 hbh_ini=
197 hbh_fin=
198 # involved frames:
199 grep sendxml $TESTCASE_BN > .involved_frames
200 n_involved=`wc -l .involved_frames | awk '{ print $1 }'`
201 count=1
202 for frame in `cat .involved_frames | cut -d\| -f4 | cut -d\. -f1`
203 do
204   xml=( `ls ${frame}.*xml` )
205   mtd=( `ls ${frame}.*metadata` )
206   isrequest=$(grep "isrequest=1" $mtd)
207   if [ -n "$isrequest" ]
208   then
209     hbh_ini="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
210     sed -i 's/'$hbh_ini'/hop-by-hop-id="'$frame'"/' $xml
211   fi
212   count=$((count+1))
213 done
214