Fix local server for multiple applications
[anna.git] / example / diameter / launcher / resources / scripts / tinyTestcase.sh
index e08120f..ba867b2 100755 (executable)
@@ -4,6 +4,10 @@
 # VARIABLES #
 #############
 SCR_DIR=`readlink -f $0 | xargs dirname`
+TESTCASE_BN=testcase.txt
+WHAT=?????????
+REQUEST_STEP=
+VARIANT__dflt=safe
 
 #############
 # FUNCTIONS #
@@ -16,57 +20,411 @@ _exit() {
 }
 
 usage() {
-  echo "Usage: $0 <source directory>"
   echo
-  echo "       The source directory may contain .xml and .metadata files grouped by pairs and alphabetically classified:"
+  echo "Usage: $0 <source_directory> <seconds_timeout> [variant: fast|[safe]]"
   echo
-  echo "          11.hex.as.xml  15.hex.as.xml  19.hex.as.xml ..."
-  echo "          11.metadata    15.metadata    19.metadata   ..."
-  echo "          13.hex.as.xml  17.hex.as.xml  21.hex.as.xml ..."
-  echo "          13.metadata    17.metadata    21.metadata   ..."
+  echo "       source_directory:"
+  echo "         The source directory may contain .xml and .metadata files grouped by pairs and alphabetically classified,"
+  echo "         and also (and this is VERY IMPORTANT), a file called 'origin-hosts' containing a list of Origin-Host"
+  echo "         values that represents the point of view of the ADML testing system, together with the type of the"
+  echo "         simulated ADML node (client/server):"
   echo
-  echo "       Normally the order comes from first dot-separated part, which is the decoded frame from pcap:"
+  echo "            <id>.hex.as.xml: ANNA-Diameter XML message format."
+  echo "            <id>.metadata:   Some decoded message information."
+  echo "            ..."
+  echo "            origin-hosts:    Example of content:"
+  echo "                                OCS3GPPNodeHostname.ocs3gpprealm.com server"
+  echo "                                ggsnNodeHostname.nodeHostRealm.com client"
   echo
-  echo "          11.hex.as.xml: would be the ANNA-Diameter decoded message from hexadecimal sniffed one."
-  echo "          11.metadata:   would contain parsed metadata from tshark or another fulfilling this information:"
-  echo
-  echo "                           date=Sat Dec 19 11:50:55 CET 2015"
-  echo "                           timestamp=1450522255.205673000"
-  echo "                           src=gentraf"
-  echo "                           dst=testbed"
+  echo "         The order for the xml and metadata files comes from first dot-separated part (<id>), which is normally the"
+  echo "         decoded frame from a pcap file. The xml files are ANNA-Diameter xml messages and a typical metadata file"
+  echo "         would be something like this:"
+  echo "                           date=vie ene 29 15:37:14 CET 2016"
+  echo "                           timestamp=1454078234.050988000"
+  echo "                           src=192.168.14.42"
+  echo "                           dst=192.168.12.40"
   echo "                           code=257"
-  echo "                           isrequest=1"
+  echo "                           isrequest=0"
   echo "                           applicationid=0"
+  echo "                           originhost=OCSNodeHostname.ocsrealm.com"
+  echo
+  echo "         You can use the 'example/diameter/pcapDecoder/tsharkDecoder.sh' script over a pcap file, in order to get"
+  echo "         the hexadecimal buffers and then process them with 'example/diameter/batchConverter' tool to get the xml"
+  echo "         messages and metadata for each pcap diameter frame."
+  echo
+  echo
+  echo "       variant:"
+  echo "         Two variants are implemented: 'fast', where wait conditions does not check the complete message, and"
+  echo "         'safe' which checks the full xml message content (good for function test). Default is '$VARIANT__dflt'."
+  echo
+  echo
+  echo "       seconds_timeout:"
+  echo "         Timeout in seconds for the testcase. If '-' provided, no timeout will be programmed for the testcase."
+  echo
   echo
-  echo "       Presented example has been achieved using the 'example/diameter/pcapDecoder/tsharkDecoder.sh' script"
-  echo "        and then decoding with 'example/diameter/batchConverter' tool with appropiate diameter dictionaries."
   echo
-  echo "      This script will create a basic testcase based on frames timeline (11, 13, 15, etc.), from the desired"
-  echo "      end-point. For example, from the 'gentraf' point of view, outgoing requests will be translated as send"
-  echo "      operations with corresponding waits for answers and successful result code (2001). CER/A are detected"
-  echo "      creating a symbolic link to better reference them, and DPR/A are ignored."
+  echo "       The resulting '$TESTCASE_BN' will have all the operations needed to program the test case and will be"
+  echo "       written on same source directory referencing xml files as local-relative ones. Normally, 'change-dir'"
+  echo "       operation will be used before programming to ease the procedure without having to copy the stuff into"
+  echo "       installed ADML execution directory."
   echo
-  echo "      The resulting 'testcase.txt' will have all the operations needed to program the test case and will be"
-  echo "      written on same source directory referencing xml files as local-relative ones. Probably, 'change-dir'"
-  echo "      operation will be used before programming to ease the procedure without having to copy the stuff into"
-  echo "      installed ADML execution directory."
+  echo "       Connection issues (CER/A), disconnect procedures (DPR/A), database populations and sanity checks are"
+  echo "       not responsability for this script. Anyway, CER/A messages detected are symbolically linked with the"
+  echo "       name 'ce<r/a>.<origin host>.xml', and missing ones are created with a basic template which the user"
+  echo "       could edit and fix with the appropiate values."
   echo
-  echo "      Connection issues (CER/A), disconnect procedures (DPR/A), database populations and sanity checks are"
-  echo "      not responsability for this script. Such operations shall be externally performed to guarantee that"
-  echo "      programming this testcase is going to be valid in context of test execution."
   _exit
 }
 
+# Check number:
+isnumber() {
+  local re='^[0-9]+$'
+  [[ "$1" =~ $re ]] && return 0
+  return 1
+}
+
+# $1: xml file
+# Nested Result-Code's not supported here (i.e. charging application)
+getResultCode () {
+  grep "<avp name=\"Result-Code\"" $1 | awk -F'data=' '{ print $2 }' | cut -d\" -f2
+}
+
+# $1: code; $2: isrequest; $3: frame to start searching: direction depends on isrequest (1: downwards, 0: upwards)
+search_xml () {
+  local code=$1
+  local isrequest=$2
+  local this_frame=$3
+  local frames=()
+
+  if [ $isrequest -eq 1 ]
+  then
+    # requests shall be before frame
+    frames=( $(ls *.*metadata | cut -d\. -f1 | sort -rn | awk -v frame=$this_frame  '{ if($1 < frame) print $1 }') )
+  else
+    # answers shall be after frame
+    frames=( $(ls *.*metadata | cut -d\. -f1 | sort -n | awk -v frame=$this_frame  '{ if($1 > frame) print $1 }') )
+  fi
+
+  local frame=
+  local mtd=
+  local found=
+  local _code=
+  local _isrequest=
+  for frame in ${frames[@]}
+  do
+    mtd=${frame}.metadata
+    [ ! -f $mtd ] && _exit "Unexpected error: can't found '$mtd' file !!"
+
+    _code=$(grep ^code $mtd | cut -d= -f2)
+    _isrequest=$(grep ^isrequest $mtd | cut -d= -f2)
+
+    [ $_code -ne $code -o $_isrequest -ne $isrequest ] && continue
+
+    found="$(ls ${frame}.*.xml 2>/dev/null)"
+    [ -z "$found" ] && _exit "Unexpected error: can't found xml file for frame '${frame}' file !!"
+    echo $found
+    return 0
+  done
+
+  return 1
+}
+
+# $1: metadata file; $2: xml file; $3: check Result-Code indicator; $4: client/server (ADML node role)
+update_testcase () {
+  # metadata aspect:
+  #
+  # date=Wed Oct  7 01:51:16 CEST 2015
+  # timestamp=1444175476.212667000
+  # src=gt_traf
+  # dst=vcbavipt
+  # code=258
+  # isrequest=0
+  # applicationid=16777238
+  local mtd=$1
+  local xml=$2
+  local resultcode=$3
+  local adml_type=$4
+
+  local wait_command=waitfe
+  # wait xml variant:
+  local waitxml_command=waitfe-xml
+  local send_command=sendxml2e
+  [ "$adml_type" = "server" ] && { wait_command=waitfc ; waitxml_command=waitfc-xml ; send_command=sendxml2c ; }
+
+
+  # Ignore disconnect peer messages on testcase format (they have no session-id):
+  grep "^code=282$" $mtd > /dev/null
+  [ $? -eq 0 ] && return
+
+  local code=$(grep ^code $mtd | cut -d= -f2)
+  local isrequest=$(grep ^isrequest $mtd | cut -d= -f2)
+  local sessionid=$(grep Session-Id $xml | cut -d\" -f4)
+  local lines=$(wc -l $TESTCASE_BN | awk '{ print $1 }')
+
+  local s_wait="test|1|$wait_command|$code|$((1-isrequest))"
+  # wait xml variant:
+  local this_frame=$(echo $xml | cut -d\. -f1)
+  local pairxml=$(search_xml $code $((1-isrequest)) $this_frame)
+  local s_waitxml="test|1|$waitxml_command|$pairxml"
+
+  [ -n "$sessionid" ] && s_wait="${s_wait}|||${sessionid}"
+
+  local s_send="test|1|$send_command|$xml"
+
+  if [ $isrequest -eq 1 ]
+  then
+    # Send the request
+    # Special case for SNR/SNA (code=8388636) and STR (code=275) going to SAPC: the Session-Id is created on client and received on SLR previously
+    if [ "$adml_type" = "server" ]
+    then
+      if [ "$code" = "8388636" -o "$code" = "275" ]
+      then
+        s_send="test|1|$send_command|$xml|$REQUEST_STEP"
+        s_wait="test|1|$wait_command|$code|0|||"
+      fi
+    fi
+
+    echo "$s_send" >> $TESTCASE_BN
+
+    if [ -n "$resultcode" ]
+    then
+      local hbh="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
+      local hbh_matchs=( $(grep -l "$hbh" *.xml) )
+      local ans_xml=${hbh_matchs[1]}
+      local rc=2001
+      if [ -n "$ans_xml" ]
+      then
+        _rc=$(getResultCode $ans_xml)
+        [ -n "$_rc" ] && rc=$_rc
+      fi
+
+      # Wait the answer:
+      s_wait="${s_wait}|${rc}"
+    fi
+
+    # Wait step:
+    if [ "$VARIANT" = "fast" ]
+    then
+      echo "$s_wait" >> $TESTCASE_BN
+    else
+      echo "$s_waitxml" >> $TESTCASE_BN
+    fi
+
+  else
+    local next_step_number=$((lines+1))
+
+    # Wait the request
+    # Special case for SLR/SLA (code=8388635) and STR (code=275) coming from SAPC: the Session-Id is created on client
+    if [ "$adml_type" = "server" ]
+    then
+      if [ "$code" = "8388635" -o "$code" = "275" ]
+      then
+        s_wait="test|1|$wait_command|$code|1"
+        REQUEST_STEP=$next_step_number
+      fi
+    fi
+
+    # Wait step:
+    if [ "$VARIANT" = "fast" ]
+    then
+      echo "$s_wait" >> $TESTCASE_BN
+    else
+      echo "$s_waitxml" >> $TESTCASE_BN
+    fi
+
+    # Send the answer
+    echo "test|1|$send_command|$xml|$next_step_number" >> $TESTCASE_BN
+  fi
+}
+
 #############
 # EXECUTION #
 #############
-cd $SCR_DIR
-source ../../configure.sh
-echo
 SOURCE_DIR=$1
 [ -z "$SOURCE_DIR" ] && usage
 SOURCE_DIR=`readlink -f $SOURCE_DIR`
 [ ! -d "$SOURCE_DIR" ] && _exit "Can't found provided directory '$SOURCE_DIR'"
+# Work on source directory:
+cd $SOURCE_DIR
+
+# Must have metadata:
+ls *.*metadata >/dev/null 2>/dev/null
+[ $? -ne 0 ] && _exit "Cannot found '*.*metadata' files on '$SOURCE_DIR' !!"
+
+# Check origin-hosts:
+ohs_file=$SOURCE_DIR/origin-hosts
+[ ! -f $ohs_file ] && _exit "Can't found the mandatory file '$ohs_file' !!"
+awk '{ print $NF }' $ohs_file | egrep -qw 'client|server'
+[ ${PIPESTATUS[1]} -ne 0 ] && _exit "Can't found any Origin-Host in '$ohs_file' with a valid ADML node role value (client or server) !!"
+
+# Identify useful frames: those which are created at test-bed side (all except frames coming from tested systems):
+0> .involved_frames
+0> .involved_origin_hosts
+n_involved_frames=0
+for oh in `awk '{ print $1 }' $ohs_file`
+do
+  for frame in $(grep -l "^originhost=$oh$" *.*metadata | cut -d\. -f1)
+  do
+    echo "$frame" >> .involved_frames
+    echo "$oh" >> .involved_origin_hosts
+    n_involved_frames=$((n_involved_frames + 1))
+  done
+done
+[ $n_involved_frames -eq 0 ] && _exit "No frame has been selected within '$SOURCE_DIR' for provided origin-hosts file !!"
+sort .involved_frames > .involved_frames_sort
+mv .involved_frames_sort .involved_frames
+
+# Messages classification:
+#0> cers_4_starting
+#0> ceas_4_establishing
+0> cers_4_starting_origin_hosts
+0> ceas_4_establishing_origin_hosts
+0> requests_4_sending
+0> answers_4_programming
+0> $TESTCASE_BN
+
+# Optional timeout:
+TIMEOUT_SEC=$2
+isnumber $TIMEOUT_SEC
+[ $? -eq 0 ] && echo "test|1|timeout|$((TIMEOUT_SEC * 1000))" >> $TESTCASE_BN
+
+# Variant:
+VARIANT=$VARIANT__dflt
+[ "$3" = "fast" ] && VARIANT=fast
+
+# Process frames:
+for frame in `cat .involved_frames`
+do
+  mtd=( `ls ${frame}.*metadata 2>/dev/null` )
+  [ ${#mtd[@]} -ne 1 ] && _exit "There must be one metadata file corresponding to frame '$frame' !!"
+  xml=( `ls ${frame}.*xml 2>/dev/null` )
+  [ ${#xml[@]} -ne 1 ] && _exit "There must be one xml message file corresponding to '$mtd' (frame '$frame') !!"
+  originHost=$(grep ^originhost= ${frame}.metadata | cut -d= -f2-)
+
+  # Ignore keep alives:
+  grep -q "^code=280$" $mtd
+  [ $? -eq 0 ] && continue
+
+  grep -q "^isrequest=1$" $mtd
+  if [ $? -eq 0 ]
+  then
+    # CER's:
+    grep -q "^code=257$" $mtd
+    if [ $? -eq 0 ]
+    then
+      #echo $frame >> cers_4_starting
+      [ -z "$originHost" ] &&  _exit "Missing Origin-Host (frame $frame, CER message) !!"
+      echo "$originHost" >> cers_4_starting_origin_hosts
+      ln -sf $xml cer.${originHost}.xml
+      continue
+    fi
+    # Other requests:
+    echo $frame >> requests_4_sending
+
+    # client or server:
+    adml_type=$(grep -w "$originHost" $ohs_file | awk '{ print $NF }')
+    update_testcase $mtd $xml check_result_code $adml_type
+  else
+    # CEA's:
+    grep -q "^code=257$" $mtd
+    if [ $? -eq 0 ]
+    then
+      #echo $frame >> ceas_4_establishing
+      originHost=$(grep ^originhost= ${frame}.metadata | cut -d= -f2-)
+      [ -z "$originHost" ] &&  _exit "Missing Origin-Host (frame $frame, CEA message) !!"
+      echo "$originHost" >> ceas_4_establishing_origin_hosts
+      ln -sf $xml cea.${originHost}.xml
+      continue
+    fi
+    # Other answers:
+    echo $frame >> answers_4_programming
+
+    # client or server:
+    adml_type=$(grep -w "$originHost" $ohs_file | awk '{ print $NF }')
+    update_testcase $mtd $xml "" $adml_type
+  fi
+done
+
+
+# We will replace all the requests hop-by-hop's with a unique value, to avoid bad sniffing cases (different sources using bad values).
+# For example, the frame number could be valid enough.
+hbh_ini=
+hbh_fin=
+for frame in `cat .involved_frames`
+do
+  xml=( `ls ${frame}.*xml` )
+  mtd=( `ls ${frame}.*metadata` )
+  isrequest=$(grep "isrequest=1" $mtd)
+  if [ -n "$isrequest" ]
+  then
+    hbh_ini="$(grep -o "hop-by-hop-id=\"[0-9]*\"" $xml)"
+    sed -i 's/'$hbh_ini'/hop-by-hop-id="'$frame'"/' $xml
+  fi
+done
+rm .involved_frames
+
+# Simplify origin hosts file (oh + adml role):
+sort -u .involved_origin_hosts > .involved_origin_hosts_unique
+grep -w -f .involved_origin_hosts_unique $ohs_file > .involved_origin_hosts_with_info
+mv .involved_origin_hosts_with_info $ohs_file
+rm .involved_origin_hosts .involved_origin_hosts_unique
+
+# Missing CERs:
+for oh in $(awk -v input=client '{if ($2 == input) print $1;}' $ohs_file)
+do
+  cer=$SOURCE_DIR/cer.${oh}.xml
+  if [ ! -f $cer ]
+  then
+    echo $oh >> cers_4_starting_origin_hosts
+    echo "Missing CER: `basename $cer` (a basic template has been created, please edit & fix the unknowns)"
+        cat << EOF > $cer
+<message version="1" name="CER" application-id="0" hop-by-hop-id="1" end-to-end-id="1">
+   <avp name="Origin-Host" data="$oh"/>
+   <avp name="Origin-Realm" data="$(echo $oh | cut -d\. -f2-) $WHAT"/>
+   <avp name="Auth-Application-Id" data="16777236 $WHAT 16777238 $WHAT"/>
+   <avp name="Origin-State-Id" data="1"/>
+   <avp name="Host-IP-Address" data="1|192.168.14.42 $WHAT"/>
+   <avp name="Vendor-Id" data="193"/>
+   <avp name="Product-Name" data="afNode $WHAT ggsnNode $WHAT"/>
+   <avp name="Firmware-Revision" data="1"/>
+</message>
+EOF
+  fi
+done
+
+# Missing CEAs:
+for oh in $(awk -v input=server '{if ($2 == input) print $1;}' $ohs_file)
+do
+  cea=$SOURCE_DIR/cea.${oh}.xml
+  if [ ! -f $cea ]
+  then
+    echo $oh >> ceas_4_establishing_origin_hosts
+    echo "Missing CEA: `basename $cea` (a basic template has been created, please edit & fix the unknowns)"
+        cat << EOF > $cea
+<message version="1" name="CEA" application-id="0" hop-by-hop-id="1" end-to-end-id="1">
+   <avp name="Result-Code" data="2001" alias="DIAMETER_SUCCESS"/>
+   <avp name="Origin-Host" data="$oh"/>
+   <avp name="Origin-Realm" data="$(echo $oh | cut -d\. -f2-) $WHAT"/>
+   <avp name="Host-IP-Address" data="1|192.168.12.42 $WHAT"/>
+   <avp name="Vendor-Id" data="193"/>
+   <avp name="Product-Name" data="SAPC"/>
+   <avp name="Supported-Vendor-Id" data="5535"/>
+   <avp name="Supported-Vendor-Id" data="10415"/>
+   <avp name="Auth-Application-Id" data="16777238"/>
+   <avp name="Auth-Application-Id" data="16777236"/>
+   <avp name="Vendor-Specific-Application-Id">
+      <avp name="Vendor-Id" data="10415"/>
+      <avp name="Auth-Application-Id" data="16777238"/>
+   </avp>
+   <avp name="Vendor-Specific-Application-Id">
+      <avp name="Vendor-Id" data="10415"/>
+      <avp name="Auth-Application-Id" data="16777236"/>
+   </avp>
+   <avp name="Firmware-Revision" data="1"/>
+</message>
+EOF
+  fi
+done
+
+exit 0
 
-# UNDER CONSTRUCTION
-# .........