Improved wait for regexp operations
[anna.git] / example / diameter / launcher / Launcher.cpp
index b141865..0fcfd52 100644 (file)
@@ -12,6 +12,7 @@
 #include <math.h> // ceil
 #include <climits>
 #include <unistd.h> // chdir
+#include <regex>
 
 // Project
 #include <anna/timex/Engine.hpp>
@@ -792,8 +793,7 @@ throw(anna::RuntimeException) {
   a_communicator->accept();
 }
 
-
-bool Launcher::getDataBlockFromHexFile(const std::string &pathfile, anna::DataBlock &db) const throw() {
+bool Launcher::getDataBlockFromHexFile(const std::string &pathfile, anna::DataBlock &db) const throw(anna::RuntimeException) {
   // Get hex string
   static char buffer[8192];
   std::ifstream infile(pathfile.c_str(), std::ifstream::in);
@@ -808,7 +808,8 @@ bool Launcher::getDataBlockFromHexFile(const std::string &pathfile, anna::DataBl
     msg += hexString;
     anna::Logger::debug(msg, ANNA_FILE_LOCATION);
     );
-    anna::functions::fromHexString(hexString, db);
+
+    anna::functions::fromHexString(hexString, db); // could launch exception
     // Close file
     infile.close();
     return true;
@@ -817,6 +818,21 @@ bool Launcher::getDataBlockFromHexFile(const std::string &pathfile, anna::DataBl
   return false;
 }
 
+bool Launcher::getContentFromFile(const std::string &pathfile, std::string &content) const throw(anna::RuntimeException) {
+
+  std::ifstream inFile(pathfile.c_str(), std::ifstream::in);
+  if(!inFile.good()) {
+    throw RuntimeException(anna::functions::asString("Unable to open file '%s'", pathfile.c_str()), ANNA_FILE_LOCATION);
+  }
+
+  std::stringstream strStream;
+  strStream << inFile.rdbuf(); //read the file
+  content = strStream.str(); // holds the content of the file
+  inFile.close();
+
+  return true;
+}
+
 void Launcher::resetStatistics() throw() {
   if (a_workingNode) {
     a_workingNode->getCommEngine()->resetStatistics();
@@ -1222,16 +1238,6 @@ std::string Launcher::help() const throw() {
   result += "\n                              delay|<msecs>              Blocking step until the time lapse expires. Useful to give ";
   result += "\n                                                          some cadence control and time schedule for a specific case.";
   result += "\n                                                         A value of 0 could be used as a dummy step.";
-  result += "\n                              wait<fe/fc>|<condition>    Blocking step until condition is fulfilled. The message could";
-  result += "\n                                                          received from entity (waitfe) or from client (waitfc).";
-  result += "\n";
-  result += "\n                              wait<fe/fc>-regexp|<regexp>";
-  result += "\n                                                         Wait condition, from entity (waitfe-regexp) or client (waitfc-regexp)";
-  result += "\n                                                          to match the serialized xml content for received messages. CPU cost";
-  result += "\n                                                          is bigger than the former ones because the whole message must be";
-  result += "\n                                                          decoded and converted to xml instead of doing a direct hexadecimal";
-  result += "\n                                                          buffer search. The main advantage is the great flexibility to identify";
-  result += "\n                                                          any content with a regular expression.";
   result += "\n";
   result += "\n                              sh-command|<script>        External execution for script/executable via shell through a dedicated";
   result += "\n                                                          thread, providing the command and parameters. You could use dynamic";
@@ -1250,12 +1256,35 @@ std::string Launcher::help() const throw() {
   result += "\n                                                          with the possible outputs from the scripts. You could also put your";
   result += "\n                                                          job in background although sh-command will return 0-value immediately.";
   result += "\n";
-  result += "\n                           <condition>: Optional parameters which must be fulfilled to continue through the next step.";
-  result += "\n                                        Any received message over diameter interfaces will be evaluated against the";
-  result += "\n                                         corresponding test case starting from the current step until the first one";
-  result += "\n                                         whose condition is fulfilled. If no condition is fulfilled the event will be";
-  result += "\n                                         classified as 'uncovered' (normally a test case bad configuration, or perhaps";
-  result += "\n                                         a real unexpected message).";
+  result += "\n                              wait<fe/fc>-hex|<source_file>[|strict]";
+  result += "\n                                                         Wait condition, from entity (waitfe-hex) or client (waitfc-hex) to";
+  result += "\n                                                          match the hexadecimal representation for received messages against";
+  result += "\n                                                          source file (hex format). Fix mode must be enabled to avoid unexpected";
+  result += "\n                                                          matching behaviour. Specify 'strict' to use the hex content 'as is'.";
+  result += "\n                                                          If not, the hex content will be understood as whole message and then,";
+  result += "\n                                                          borders will be added (^<content>$) and sequence information bypassed";
+  result += "\n                                                          even for diameter answers.";
+  result += "\n";
+  result += "\n                              wait<fe/fc>-xml|<source_file>[|strict]";
+  result += "\n                                                         Wait condition from entity (waitfe-xml) or client (waitfc-xml) to";
+  result += "\n                                                          match the serialized xml content for received messages against";
+  result += "\n                                                          source file (xml representation). Fix mode must be enabled to avoid";
+  result += "\n                                                          unexpected matching behaviour. If you need a strict matching you";
+  result += "\n                                                          must add parameter 'strict', if not, regexp is built ignoring sequence";
+  result += "\n                                                          information (hop-by-hop-id=\"[0-9]+\" end-to-end-id=\"[0-9]+\").";
+  result += "\n                                                          All LF codes will be internally removed when comparison is executed";
+  result += "\n                                                          in order to ease xml content configuration.";
+  result += "\n";
+  result += "\n                              wait<fe/fc>|<condition>    Blocking step until condition is fulfilled. The message could";
+  result += "\n                                                          received from entity (waitfe) or from client (waitfc).";
+  result += "\n                                                         CPU cost is lower than former 'wait<fe/fc>-<xml|hex>' variants.";
+  result += "\n";
+  result += "\n                                          <condition>: Optional parameters which must be fulfilled to continue through the next step.";
+  result += "\n                                                       Any received message over diameter interfaces will be evaluated against the";
+  result += "\n                                                        corresponding test case starting from the current step until the first one";
+  result += "\n                                                        whose condition is fulfilled. If no condition is fulfilled the event will be";
+  result += "\n                                                        classified as 'uncovered' (normally a test case bad configuration, or perhaps";
+  result += "\n                                                        a real unexpected message).";
 
   // TODO(***)
   //  result += "\n                                        The way to identify the test case, is through registered Session-Id values for";
@@ -1268,50 +1297,50 @@ std::string Launcher::help() const throw() {
   //  result += "\n                                         comply with the incoming message, and reactivates it.";
   // The other solution: register Session-Id values for answers send to client from a local diameter server.
 
-  result += "\n                                        How to answer: a wait condition for a request will store the incoming message";
-  result += "\n                                         which fulfills that condition. This message is useful together with the peer";
-  result += "\n                                         connection source in a further send step configured with the corresponding";
-  result += "\n                                         response. You could also insert a delay between wait and send steps to be";
-  result += "\n                                         more realistic (processing time simulation in a specific ADML host node).";
-  result += "\n                                         Always, a response send step will get the needed information from the most";
-  result += "\n                                         recent wait step finding in reverse order (note that some race conditions";
-  result += "\n                                         could happen if your condition is not specific enough).";
+  result += "\n                                                       How to answer: a wait condition for a request will store the incoming message";
+  result += "\n                                                        which fulfills that condition. This message is useful together with the peer";
+  result += "\n                                                        connection source in a further send step configured with the corresponding";
+  result += "\n                                                        response. You could also insert a delay between wait and send steps to be";
+  result += "\n                                                        more realistic (processing time simulation in a specific ADML host node).";
+  result += "\n                                                        Always, a response send step will get the needed information from the most";
+  result += "\n                                                        recent wait step finding in reverse order (note that some race conditions";
+  result += "\n                                                        could happen if your condition is not specific enough).";
 
   result += "\n";
-  result += "\n                                        Condition format:";
+  result += "\n                                                       Condition format:";
   result += "\n";
-  result += "\n                                           [code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId]";
+  result += "\n                                                          [code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId]";
   result += "\n";
-  result += "\n                                             code: integer number";
-  result += "\n                                             bitR: 1 (request), 0 (answer)";
-  result += "\n                                             hopByHop: integer number or request send step reference: #<step number>";
+  result += "\n                                                            code: integer number";
+  result += "\n                                                            bitR: 1 (request), 0 (answer)";
+  result += "\n                                                            hopByHop: integer number or request send step reference: #<step number>";
   result += "\n";
-  result += "\n                                                       Using the hash reference, you would indicate a specific wait condition";
-  result += "\n                                                        for answers. The step number provided must correspond to any of the";
-  result += "\n                                                        previous send commands (sendxml2e/sendxml2c) configured for a request.";
-  result += "\n                                                       This 'hop-by-hop' variant eases the wait condition for answers in the";
-  result += "\n                                                        safest way.";
+  result += "\n                                                                      Using the hash reference, you would indicate a specific wait condition";
+  result += "\n                                                                       for answers. The step number provided must correspond to any of the";
+  result += "\n                                                                       previous send commands (sendxml2e/sendxml2c) configured for a request.";
+  result += "\n                                                                      This 'hop-by-hop' variant eases the wait condition for answers in the";
+  result += "\n                                                                       safest way.";
   result += "\n";
-  result += "\n                                             applicationId: integer number";
-  result += "\n                                             sessionId: string";
-  result += "\n                                             resultCode: integer number";
-  result += "\n                                             msisdn: string";
-  result += "\n                                             imsi: string";
-  result += "\n                                             serviceContextId: string";
+  result += "\n                                                            applicationId: integer number";
+  result += "\n                                                            sessionId: string";
+  result += "\n                                                            resultCode: integer number";
+  result += "\n                                                            msisdn: string";
+  result += "\n                                                            imsi: string";
+  result += "\n                                                            serviceContextId: string";
   result += "\n";
-  result += "\n                                        Take into account these rules, useful in general:";
+  result += "\n                                                       Take into account these rules, useful in general:";
   result += "\n";
-  result += "\n                                           - Be as much specific as possible defining conditions to avoid ambiguity sending";
-  result += "\n                                             messages out of context due to race conditions. Although you could program several";
-  result += "\n                                             times similar conditions, some risky practices will throw a warning trace (if you";
-  result += "\n                                             repeat the same condition within the same test case).";
-  result += "\n                                           - Adding a ResultCode and/or HopByHop to the condition are only valid waiting answers.";
-  result += "\n                                           - Requests hop-by-hop values must be different for all the test case requests.";
-  result += "\n                                             RFC says that a hop by hop must be unique for a specific connection, something that";
-  result += "\n                                             could be difficult to manage if we have multiple available connections from client";
-  result += "\n                                             side endpoint (entity or local server), even if we would have only one connection but";
-  result += "\n                                             several host interfaces. It is enough to configure different hop-by-hop values within";
-  result += "\n                                             each test case, because on reception, the Session-Id is used to identify that test case.";
+  result += "\n                                                          - Be as much specific as possible defining conditions to avoid ambiguity sending";
+  result += "\n                                                            messages out of context due to race conditions. Although you could program several";
+  result += "\n                                                            times similar conditions, some risky practices will throw a warning trace (if you";
+  result += "\n                                                            repeat the same condition within the same test case).";
+  result += "\n                                                          - Adding a ResultCode and/or HopByHop to the condition are only valid waiting answers.";
+  result += "\n                                                          - Requests hop-by-hop values must be different for all the test case requests.";
+  result += "\n                                                            RFC says that a hop by hop must be unique for a specific connection, something that";
+  result += "\n                                                            could be difficult to manage if we have multiple available connections from client";
+  result += "\n                                                            side endpoint (entity or local server), even if we would have only one connection but";
+  result += "\n                                                            several host interfaces. It is enough to configure different hop-by-hop values within";
+  result += "\n                                                            each test case, because on reception, the Session-Id is used to identify that test case.";
   result += "\n";
   result += "\n";
   result += "\n";
@@ -2169,7 +2198,8 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
       //             delay|      [msecs]
       //             wait<fe/fc>|[code]|[bitR]|[hopByHop]|[applicationId]|[sessionId]|[resultCode]|[msisdn]|[imsi]|[serviceContextId]
       //      wait<fe/fc>-answer|<step number>
-      //      wait<fe/fc>-regexp|<regexp>
+      //      wait<fe/fc>-xml   |<source_file>[|strict]
+      //      wait<fe/fc>-hex   |<source_file>[|strict]
       if(param2 == "") throw anna::RuntimeException("Missing command for test id operation", ANNA_FILE_LOCATION);
 
       // Commands:
@@ -2216,16 +2246,52 @@ void Launcher::eventOperation(const std::string &operation, std::string &respons
           throw anna::RuntimeException(anna::functions::asString("Missing condition for '%s' command in test id operation", param2.c_str()), ANNA_FILE_LOCATION);
         }
       }
-      else if ((param2 == "waitfe-regexp")||(param2 == "waitfc-regexp")) {
-        if (numParams > 3)
+      else if ((param2 == "waitfe-hex")||(param2 == "waitfc-hex")) {
+        if (numParams > 4)
           throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
-        if (param3 != "") {
-          bool fromEntity = (param2.substr(4,2) == "fe");
-          testManager.getTestCase(id)->addWaitRegexp(fromEntity, param3);
+        if(param3 == "") throw anna::RuntimeException(anna::functions::asString("Missing hex file for '%s' command in test id operation", param2.c_str()), ANNA_FILE_LOCATION);
+
+        // Get DataBlock from file with hex content:
+        if(!getDataBlockFromHexFile(param3, db_aux))
+          throw anna::RuntimeException("Error reading hex content from file provided", ANNA_FILE_LOCATION);
+
+        // Hexadecimal representation read from file:
+        std::string regexp = anna::functions::asHexString(db_aux);
+
+        // optional 'full':
+        if(param4 != "strict") {
+          //// If request, we will ignore sequence data:
+          //if (anna::diameter::codec::functions::requestBit(db_aux))
+            regexp.replace (24, 16, "[0-9A-Fa-f]{16}");
+
+          regexp.insert(0, "^");
+          regexp += "$";
         }
-        else {
-          throw anna::RuntimeException(anna::functions::asString("Missing condition for '%s' command in test id operation", param2.c_str()), ANNA_FILE_LOCATION);
+
+        bool fromEntity = (param2.substr(4,2) == "fe");
+        testManager.getTestCase(id)->addWaitRegexpHex(fromEntity, regexp);
+      }
+      else if ((param2 == "waitfe-xml")||(param2 == "waitfc-xml")) {
+        if (numParams > 4)
+          throw anna::RuntimeException("Wrong body content format on HTTP Request. Use 'help' management command to see more information.", ANNA_FILE_LOCATION);
+        if(param3 == "") throw anna::RuntimeException(anna::functions::asString("Missing xml file for '%s' command in test id operation", param2.c_str()), ANNA_FILE_LOCATION);
+
+        // Get xml content from file:
+        std::string regexp;
+        if(!getContentFromFile(param3, regexp))
+          throw anna::RuntimeException("Error reading xml content from file provided", ANNA_FILE_LOCATION);
+
+        // optional 'full':
+        if(param4 != "strict") {
+          std::string s_from = "hop-by-hop-id=\"[0-9]+\" end-to-end-id=\"[0-9]+\"";
+          std::string s_to = s_from;
+          regexp = std::regex_replace (regexp, std::regex(s_from), s_to);
+          //regexp.insert(0, "^");
+          //regexp += "$";
         }
+
+        bool fromEntity = (param2.substr(4,2) == "fe");
+        testManager.getTestCase(id)->addWaitRegexpXml(fromEntity, regexp);
       }
       else if (param2 == "sh-command") {
         // Allow pipes in command: