e1804371dd184db18094071ec7d56e7c93fb74d4
[anna.git] / source / testing / TestStep.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 // Standard
10 #include <string>
11 #include <iostream>
12 #include <errno.h>
13
14 #include <signal.h>     // sigaction, sigemptyset, struct sigaction, SIGCHLD, SA_RESTART, SA_NOCLDSTOP
15 #include <stdio.h>      // perror
16 #include <stdlib.h>     // exit
17 #include <sys/wait.h>   // waitpid, pid_t, WNOHANG
18
19 // cmd with fork:
20 #include <sys/types.h>
21 #include <unistd.h>
22
23
24 // Project
25 #include <anna/testing/TestStep.hpp>
26
27 #include <anna/testing/defines.hpp>
28 #include <anna/diameter/codec/Message.hpp>
29 #include <anna/diameter.comm/OriginHost.hpp>
30 #include <anna/testing/TestCase.hpp>
31 #include <anna/testing/TestManager.hpp>
32 #include <anna/testing/TestTimer.hpp>
33 #include <anna/xml/Compiler.hpp>
34 #include <anna/core/util/Millisecond.hpp>
35 #include <anna/diameter.comm/Message.hpp>
36 #include <anna/diameter.comm/Entity.hpp>
37 #include <anna/diameter.comm/LocalServer.hpp>
38 #include <anna/diameter.comm/ClientSession.hpp>
39 #include <anna/diameter.comm/ServerSession.hpp>
40 #include <anna/diameter.comm/Server.hpp>
41 #include <anna/core/tracing/Logger.hpp>
42 #include <anna/diameter/codec/functions.hpp>
43 #include <anna/diameter/helpers/base/functions.hpp>
44
45
46
47 using namespace anna::testing;
48
49
50 namespace {
51
52   void handle_sigchld(int sig) {
53     while (waitpid((pid_t)(-1 /* any child (the only) */), 0, WNOHANG|WNOWAIT) > 0) {}
54   }
55
56   void cmdRunOnThread (TestStepCmd *step, const std::string &cmd) {
57
58     // Thread running:
59     step->setThreadRunning(true);
60
61     int status = -2;
62
63     struct sigaction sa;
64     sa.sa_handler = &handle_sigchld;
65     sigemptyset(&sa.sa_mask);
66     sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
67     if (sigaction(SIGCHLD, &sa, 0) != -1) {
68       status = system(cmd.c_str());
69       /* POPEN version:
70           char readbuf[256];
71           FILE *fp = popen(cmd.c_str(), "r");
72           if (fp) {
73             while(fgets(readbuf, sizeof(readbuf), fp))
74               step->appendOutput("\n");
75               step->appendOutput(readbuf);
76               status = pclose(fp);
77           }
78           else {
79             status = -1;
80           }
81        */
82     }
83     else {
84       perror(0);
85     }
86     // This can be implemented portably and somewhat more concisely with the signal function if you prefer:
87     // if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
88     //   perror(0);
89     //   exit(1);
90     // }
91
92     if (status < 0) {
93       char buf[256];
94       char const * str = strerror_r(errno, buf, 256);
95       step->setErrorMsg(anna::functions::asString("errno = %d (%s)", errno, str));
96     }
97
98     step->setResultCode(WEXITSTATUS(status)); // rc = status >>= 8; // divide by 256
99     step->complete();
100     // TODO: terminate thread when deprecated (RT signal ?)
101     // TODO: mutex the step while setting data here !!
102   }
103
104   void cmdRunOnThreadWithFork (TestStepCmd *step, const std::string &cmd) {
105
106     // Thread running:
107     step->setThreadRunning(true);
108
109     pid_t cpid, w;
110     int status = -2;
111
112     if ((cpid = fork()) < 0) {
113       step->setErrorMsg("Error in fork()");
114     }
115     else if (cpid == 0) {
116       // child
117       status = system(cmd.c_str());
118       _exit(WEXITSTATUS(status));
119     }
120     else {
121       // parent
122       step->setChildPid(cpid);
123       do {
124         w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
125         if (w != -1) {
126
127           if (WIFEXITED(status)) {
128             step->setResultCode(WEXITSTATUS(status)); // rc = status >>= 8; // divide by 256
129             break;
130           }
131           else if (WIFSIGNALED(status)) {
132             step->setErrorMsg(anna::functions::asString("killed by signal %d", WTERMSIG(status)));
133             step->setResultCode(128 + WTERMSIG(status));
134             break;
135           } else if (WIFSTOPPED(status)) {
136             step->setErrorMsg(anna::functions::asString("stopped by signal %d", WSTOPSIG(status)));
137           } else if (WIFCONTINUED(status)) {
138             step->setErrorMsg("continued");
139           }
140         }
141         else {
142           step->setErrorMsg("waitpid error");
143           step->setResultCode(-1);
144           break;
145         }
146       } while (!WIFEXITED(status) && !WIFSIGNALED(status));
147
148       step->complete();
149     }
150   }
151
152   bool decodeMessage(const anna::DataBlock &message, anna::diameter::codec::Message &messageCodec) throw() {
153
154     if (message.isEmpty())
155       return false;
156
157     bool result = true;
158     try {
159       messageCodec.clear();
160       messageCodec.decode(message);
161     }
162     catch (anna::RuntimeException &ex) {
163       ex.trace();
164       result = false;
165     }
166
167     return result;
168   }
169 }
170
171
172 ////////////////////////////////////////////////////////////////////////////////////////////////////////
173 // TestStep
174 ////////////////////////////////////////////////////////////////////////////////////////////////////////
175 void TestStep::initialize(TestCase *testCase) {
176   a_testCase = testCase;
177   a_completed = false;
178   a_type = Type::Unconfigured;
179   a_beginTimestamp = 0;
180   a_endTimestamp = 0;
181   a_number = testCase->steps() + 1; // testCase is not NULL
182 }
183
184 bool TestStep::decodeMessage(bool trust) throw() {
185   if (a_messageCodec) return true;
186   a_messageCodec = new anna::diameter::codec::Message;
187   if (::decodeMessage(a_message, *a_messageCodec)) return true;
188   if (trust) {
189     LOGDEBUG(anna::Logger::debug("Error DECODING, but trusting it ...", ANNA_FILE_LOCATION));
190     return true;
191   }
192
193   delete a_messageCodec;
194   a_messageCodec = NULL;
195   return false;
196 }
197
198 const char* TestStep::asText(const Type::_v type)
199 throw() {
200   static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait", "Command" };
201   return text [type];
202 }
203
204 anna::xml::Node* TestStep::asXML(anna::xml::Node* parent)
205 throw() {
206   anna::xml::Node* result = parent->createChild("TestStep");
207
208   result->createAttribute("Number", a_number);
209   result->createAttribute("Type", asText(a_type));
210   result->createAttribute("Completed", (a_completed ? "yes":"no"));
211
212   // Begin
213   std::string s_aux = a_beginTimestamp.asString();
214   //  int deltaMs = (int)(a_beginTimestamp - a_testCase->getStartTimestamp());
215   //  if (a_beginTimestamp != 0 && deltaMs > 0) s_aux += anna::functions::asString(" [%.3f]", deltaMs/1000.0);
216   result->createAttribute("BeginTimestamp", s_aux);
217
218   // End
219   s_aux = a_endTimestamp.asString();
220   //  deltaMs = (int)(a_endTimestamp - a_testCase->getStartTimestamp());
221   //  if (a_endTimestamp != 0 && deltaMs > 0) s_aux += anna::functions::asString(" [%.3f]", deltaMs/1000.0);
222   result->createAttribute("EndTimestamp", s_aux);
223
224   return result;
225 }
226
227 std::string TestStep::asXMLString() throw() {
228   anna::xml::Node root("root");
229   return anna::xml::Compiler().apply(asXML(&root));
230 }
231
232 bool TestStep::execute() throw() {
233
234   int ia = a_testCase->interactiveAmount();
235   if (ia > -1) {
236     if (ia == 0) return false;
237     a_testCase->interactiveExecution();
238     LOGDEBUG(anna::Logger::debug("Interactive execution ...", ANNA_FILE_LOCATION));
239     if (a_executed) return false; // avoid repeating (this implies amount consumption)
240   }
241
242   LOGDEBUG(anna::Logger::debug(anna::functions::asString("EXECUTING %s (step number %d) for Test Case %llu (%p)", asText(a_type), a_number, a_testCase->getId(), this), ANNA_FILE_LOCATION));
243   setBeginTimestamp(anna::functions::millisecond());
244   a_executed = true;
245   return do_execute();
246 }
247
248 void TestStep::complete() throw() {
249   LOGDEBUG(anna::Logger::debug(anna::functions::asString("COMPLETE %s (step number %d) for Test Case %llu (%p)", asText(a_type), a_number, a_testCase->getId(), this), ANNA_FILE_LOCATION));
250   a_completed = true;
251   setEndTimestamp(anna::functions::millisecond());
252   do_complete();
253 }
254
255 void TestStep::reset() throw() {
256   LOGDEBUG(anna::Logger::debug(anna::functions::asString("RESET %s (step number %d) for Test Case %llu (%p)", asText(a_type), a_number, a_testCase->getId(), this), ANNA_FILE_LOCATION));
257   // type and testCase kept
258   a_completed = false;
259   a_executed = false;
260   a_beginTimestamp = 0;
261   a_endTimestamp = 0;
262   do_reset();
263 }
264
265 void TestStep::next() throw() {
266   a_testCase->nextStep();
267   a_testCase->process();
268 }
269
270
271 ////////////////////////////////////////////////////////////////////////////////////////////////////////
272 // TestStepTimeout
273 ////////////////////////////////////////////////////////////////////////////////////////////////////////
274 anna::xml::Node* TestStepTimeout::asXML(anna::xml::Node* parent)
275 throw() {
276   anna::xml::Node* result = TestStep::asXML(parent); // end timestamp will be 0 if test finished OK
277   //parent->createChild("TestStepTimeout");
278   result->createAttribute("Timeout", a_timeout.asString());
279
280   return result;
281 }
282
283 bool TestStepTimeout::do_execute() throw() {
284   try {
285     a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_timeout, TestTimer::Type::TimeLeft);
286   }
287   catch (anna::RuntimeException &ex) {
288     ex.trace();
289     a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
290     a_testCase->setState(TestCase::State::Failed);
291   }
292
293   return true; // go next
294 }
295
296 void TestStepTimeout::do_complete() throw() {
297   int stepNumber = getNumber();
298   if (stepNumber == a_testCase->steps()) {
299     a_testCase->addDebugSummaryHint(anna::functions::asString("Timeout expired (step number %d) but it was the last test case step", stepNumber)); // before report (when set Failed state)
300     a_testCase->setState(TestCase::State::Success);
301   }
302   else if (a_testCase->getState() == TestCase::State::InProgress) { // sure
303     a_testCase->addDebugSummaryHint(anna::functions::asString("Timeout expired (step number %d) before test case finished", stepNumber)); // before report (when set Failed state)
304     a_testCase->setState(TestCase::State::Failed);
305   }
306
307   a_timer = NULL;
308 }
309
310 void TestStepTimeout::do_reset() throw() {
311   try {
312     TestManager::instantiate().cancelTimer(a_timer);
313   }
314   catch (anna::RuntimeException &ex) {
315     ex.trace();
316   }
317   a_timer = NULL;
318   //a_timeout = 0; THIS IS CONFIGURATION INFO
319 }
320
321 ////////////////////////////////////////////////////////////////////////////////////////////////////////
322 // TestStepSendxml
323 ////////////////////////////////////////////////////////////////////////////////////////////////////////
324 anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent)
325 throw() {
326   anna::xml::Node* result = TestStep::asXML(parent);
327   //parent->createChild("TestStepSendxml");
328   std::string msg = "", xmlmsg = "";
329
330   // Message
331   if (TestManager::instantiate().getDumpHex()) {
332     if (a_message.isEmpty()) {
333       msg = "<empty>";
334     }
335     else {
336       msg = "\n"; msg += a_message.asString(); msg += "\n";
337     }
338   }
339
340   if (decodeMessage()) {
341     xmlmsg = "\n";
342     xmlmsg += a_messageCodec->asXMLString();
343     xmlmsg += "\n";
344   }
345   else {
346     xmlmsg = "<unable to decode, check traces>"; 
347   }
348
349   if (msg != "") result->createAttribute("Message", msg);
350   if (xmlmsg != "") result->createAttribute("XMLMessage", xmlmsg);
351   result->createAttribute("Expired", (a_expired ? "yes":"no"));
352   if (a_waitForRequestStepNumber != -1)
353     result->createAttribute("WaitForRequestStepNumber", a_waitForRequestStepNumber);
354
355   return result;
356 }
357
358 bool TestStepSendxml::do_execute() throw() {
359   bool success = false;
360   std::string failReason;
361   anna::diameter::comm::Entity *entity = a_originHost->getEntity(); // by default
362   anna::diameter::comm::LocalServer *localServer = a_originHost->getDiameterServer(); // by default
363   const TestStepWait *tsw = NULL;
364   anna::diameter::comm::Message *msg = a_originHost->createCommMessage();
365
366   try {
367
368     if (a_waitForRequestStepNumber != -1) {
369
370       // Referenced request in the 'wait for request step':
371       tsw = static_cast<const TestStepWait*>(a_testCase->getStep(a_waitForRequestStepNumber));
372       const anna::DataBlock &referenceRequest = tsw->getMsgDataBlock();
373       std::string sessionIdReferenceRequest = anna::diameter::helpers::base::functions::getSessionId(referenceRequest);
374       bool thisIsAnswer = anna::diameter::codec::functions::isRequest(getMsgDataBlock());
375
376       if (thisIsAnswer) { // is an answer: try to copy sequence information; alert about Session-Id discrepance
377         anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(referenceRequest);
378         anna::diameter::EndToEnd ete = anna::diameter::codec::functions::getEndToEnd(referenceRequest);
379
380         // Update sequence:
381         anna::diameter::codec::functions::setHopByHop(a_message, hbh);
382         anna::diameter::codec::functions::setEndToEnd(a_message, ete);
383       }
384
385       // Session-Id substitution:
386       std::string thisSessionId = anna::diameter::helpers::base::functions::getSessionId(getMsgDataBlock());
387       if (thisSessionId != sessionIdReferenceRequest) {
388         static anna::diameter::codec::Message codecMsg;
389         codecMsg.decode(getMsgDataBlock());
390         codecMsg.getAvp("Session-Id")->getUTF8String()->setValue(sessionIdReferenceRequest);
391         a_message = codecMsg.code();
392         std::string trace = anna::functions::asString("Replacing %s Session-Id (%s) to set the corresponding request one (%s)", (thisIsAnswer ? "answer":"request"), thisSessionId.c_str(), sessionIdReferenceRequest.c_str());
393         LOGDEBUG(anna::Logger::debug(trace, ANNA_FILE_LOCATION));
394         a_testCase->addDebugSummaryHint(trace);
395       }
396     }
397
398     // Create comm message:
399     //msg->clearBody();
400     msg->setBody(a_message);
401
402
403     if (getType() == Type::Sendxml2e) {
404       anna::diameter::comm::ClientSession *usedClientSession = NULL;
405
406       if (tsw) { // is an answer for a received request on wait condition
407         anna::diameter::comm::ClientSession *clientSession = tsw->getClientSession();
408         if (clientSession) {
409           /* response NULL (is an answer) */clientSession->send(msg);
410           success = true;
411           usedClientSession = clientSession;
412         }
413         else {
414           failReason = "Reference wait step didn't store a valid client session. Unable to send the message";
415           LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
416         }
417       }
418       else {
419         if (entity) {
420           success = entity->send(msg);
421           anna::diameter::comm::Server *usedServer = entity->getLastUsedResource();
422           usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL;
423         }
424         else {
425           failReason = "There is no diameter entity currently configured. Unable to send the message";
426           LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
427         }
428       } // else (normal sending)
429
430       // Detailed log:
431       if(a_originHost->logEnabled()) {
432         if (decodeMessage(true /* trust */)) {
433           std::string detail = usedClientSession ? usedClientSession->asString() : "<null client session>"; // shouldn't happen
434           a_originHost->writeLogFile(*a_messageCodec, (success ? "sent2e" : "send2eError"), detail);
435         }
436       }
437     }
438     else if (getType() == Type::Sendxml2c) {
439       anna::diameter::comm::ServerSession *usedServerSession = NULL;
440
441       if (tsw) { // is an answer for a received request on wait condition
442         anna::diameter::comm::ServerSession *serverSession = tsw->getServerSession();
443         if (serverSession) {
444           /* response NULL (is an answer) */serverSession->send(msg);
445           success = true;
446           usedServerSession = serverSession;
447         }
448         else {
449           failReason = "Reference wait step didn't store a valid server session. Unable to send the message";
450           LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
451         }
452       }
453       else {
454         if (localServer) {
455           success = localServer->send(msg);
456           usedServerSession = localServer->getLastUsedResource();
457         }
458         else {
459           failReason = "There is no diameter local server currently configured. Unable to send the message";
460           LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
461         }
462       } // else (normal sending)
463
464       // Detailed log:
465       if(a_originHost->logEnabled()) {
466         if (decodeMessage(true /* trust */)) {
467           std::string detail = usedServerSession ? usedServerSession->asString() : "<null server session>"; // shouldn't happen
468           a_originHost->writeLogFile(*a_messageCodec, (success ? "sent2c" : "send2cError"), detail);
469         }
470       }
471     }
472
473   } catch(anna::RuntimeException &ex) {
474     failReason = ex.asString();
475   }
476
477   // release msg
478   a_originHost->releaseCommMessage(msg);
479
480   if (!success) {
481     a_testCase->addDebugSummaryHint(failReason); // before report (when set Failed state);
482     a_testCase->setState(TestCase::State::Failed);
483   }
484   else {
485     complete();
486   }
487
488   return success; // go next if sent was OK
489 }
490
491 void TestStepSendxml::do_reset() throw() {
492   a_expired = false;
493   //a_message.clear();
494   //a_messageAlreadyDecoded = false;
495 }
496
497 ////////////////////////////////////////////////////////////////////////////////////////////////////////
498 // TestStepDelay
499 ////////////////////////////////////////////////////////////////////////////////////////////////////////
500 anna::xml::Node* TestStepDelay::asXML(anna::xml::Node* parent)
501 throw() {
502   anna::xml::Node* result = TestStep::asXML(parent);
503   //parent->createChild("TestStepDelay");
504
505   result->createAttribute("Delay", ((a_delay == 0) ? "dummy step, no delay" : a_delay.asString()));
506
507   return result;
508 }
509
510 bool TestStepDelay::do_execute() throw() {
511   if (a_delay == 0) { complete(); return true; } // special case
512   try {
513     a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_delay, TestTimer::Type::Delay);
514   }
515   catch (anna::RuntimeException &ex) {
516     ex.trace();
517     a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
518     a_testCase->setState(TestCase::State::Failed);
519   }
520
521   return false; // don't go next (wait complete)
522 }
523
524 void TestStepDelay::do_complete() throw() {
525   if (a_delay == 0) return; // special case
526   a_timer = NULL;
527   next(); // next() invoked here because execute() is always false for delay and never advance the iterator
528   // TODO, avoid this recursion
529 }
530
531 void TestStepDelay::do_reset() throw() {
532   if (a_delay == 0) return; // special case
533   try {
534     TestManager::instantiate().cancelTimer(a_timer);
535   }
536   catch (anna::RuntimeException &ex) {
537     ex.trace();
538   }
539   a_timer = NULL;
540   //a_delay = 0; THIS IS CONFIGURATION INFO
541 }
542
543 ////////////////////////////////////////////////////////////////////////////////////////////////////////
544 // TestStepWait
545 ////////////////////////////////////////////////////////////////////////////////////////////////////////
546 void TestStepWait::setCondition(bool fromEntity,
547     const std::string &code, const std::string &bitR, const std::string &hopByHop, const std::string &applicationId,
548     const std::string &sessionId, const std::string &resultCode,
549     const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw() {
550
551   a_condition.setReceivedFromEntity(fromEntity);
552   a_condition.setCode(code);
553   a_condition.setBitR(bitR);
554   a_condition.setResultCode(resultCode);
555   a_condition.setSessionId(sessionId);
556   a_condition.setHopByHop(hopByHop);
557   a_condition.setMsisdn(msisdn);
558   a_condition.setImsi(imsi);
559   a_condition.setServiceContextId(serviceContextId);
560 }
561
562 void TestStepWait::setConditionRegexpHex(bool fromEntity, const std::string &regexp) throw() {
563   a_condition.setReceivedFromEntity(fromEntity);
564   a_condition.setRegexpHex(regexp);
565 }
566
567 void TestStepWait::setConditionRegexpXml(bool fromEntity, const std::string &regexp) throw() {
568   a_condition.setReceivedFromEntity(fromEntity);
569   a_condition.setRegexpXml(regexp);
570 }
571
572 anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent)
573 throw() {
574   anna::xml::Node* result = TestStep::asXML(parent);
575   //parent->createChild("TestStepWait");
576   std::string msg = "", xmlmsg = "";
577
578   // Condition
579   a_condition.asXML(result);
580
581   // Message
582   if (TestManager::instantiate().getDumpHex()) {
583     if (a_message.isEmpty()) {
584       msg = "<empty>";
585     }
586     else {
587       msg = "\n"; msg += a_message.asString(); msg += "\n";
588     }
589   }
590
591   if (a_message.isEmpty()) {
592       xmlmsg = "<empty>";
593   }
594   else {
595     if (decodeMessage()) {
596       xmlmsg = "\n";
597       xmlmsg += a_messageCodec->asXMLString();
598       xmlmsg += "\n";
599     }
600     else {
601       xmlmsg = "<unable to decode, check traces>"; 
602     }
603   }
604
605   if (msg != "") result->createAttribute("MatchedMessage", msg);
606   if (xmlmsg != "") result->createAttribute("MatchedXMLMessage", xmlmsg);
607   if (a_clientSession) result->createAttribute("ClientSessionOrigin", anna::functions::asString("%p", a_clientSession));
608   if (a_serverSession) result->createAttribute("ServerSessionOrigin", anna::functions::asString("%p", a_serverSession));
609
610   return result;
611 }
612
613 bool TestStepWait::do_execute() throw() {
614   return a_completed;
615 }
616
617 void TestStepWait::do_complete() throw() {
618   //a_testCase->process(); // next() not invoked; we only want to reactivate the test case
619   // avoid stack overflow: we will process the test case externally when incoming message is fulfilled (TestCase.cpp), and TestManager is noticed
620 }
621
622 bool TestStepWait::fulfilled(const anna::DataBlock &db/*, bool matchSessionId*/) throw() {
623   if (a_condition.comply(db/*, matchSessionId*/)) {
624     a_message = db; // store matched
625     complete();
626     return true;
627   }
628
629   return false;
630 }
631
632 void TestStepWait::do_reset() throw() {
633   a_message.clear();
634   a_clientSession = NULL;
635   a_serverSession = NULL;
636 }
637
638 ////////////////////////////////////////////////////////////////////////////////////////////////////////
639 // TestStepCmd
640 ////////////////////////////////////////////////////////////////////////////////////////////////////////
641 anna::xml::Node* TestStepCmd::asXML(anna::xml::Node* parent)
642 throw() {
643   anna::xml::Node* result = TestStep::asXML(parent);
644   //parent->createChild("TestStepCmd");
645
646   result->createAttribute("Script", (a_script != "") ? a_script:"<no script>");
647   if (a_errorMsg != "") result->createAttribute("ErrorMessage", a_errorMsg);
648   if (a_threadRunning) {
649     if (a_childPid != -1)
650       result->createAttribute("ChildPid", a_childPid);
651   }
652   else {
653     if (a_resultCode != -2) {
654       result->createAttribute("ResultCode", a_resultCode);
655       //if (a_output != "") result->createAttribute("Output", a_output);
656     }
657   }
658
659   return result;
660 }
661
662 bool TestStepCmd::do_execute() throw() {
663   if (!a_threadRunning /* || a_threadDeprecated DO NOT WANT TO OVERLAP ... */) {
664     // Special tags to replace:
665     std::string cmd = getScript();
666     size_t index;
667     while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID)) != std::string::npos)
668       cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID), anna::functions::asString(TestManager::instantiate().getPoolCycle()));
669     while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID)) != std::string::npos)
670       cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID), anna::functions::asString(a_testCase->getId()));
671     while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID)) != std::string::npos)
672       cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID), anna::functions::asString(getNumber()));
673
674     a_thread = std::thread(cmdRunOnThread, this, cmd);
675     //a_thread = std::thread(cmdRunOnThreadWithFork, this, cmd);
676
677     a_thread.detach();
678   }
679
680   return false; // don't go next (wait complete): If system function on thread stucks, then the reset test case will stuck here forever.
681   // We must implement a interrupt procedure for the thread on reset call... TODO !
682 }
683
684 void TestStepCmd::do_complete() throw() {
685
686   a_threadRunning = false;
687   if (a_threadDeprecated) {
688     a_threadDeprecated = false;
689     do_reset();
690     setErrorMsg(anna::functions::asString("Step %d deprecated due to previous reset for Test Case %llu", getNumber(), a_testCase->getId()));
691     a_testCase->setState(TestCase::State::Failed);
692     return; // ignore TODO: interrupt the thread to avoid execution of the script
693   }
694
695   if (getResultCode() != 0)
696     a_testCase->setState(TestCase::State::Failed);
697   else
698     next(); // next() invoked here because execute() is always false for delay and never advance the iterator
699   // TODO, avoid this recursion
700 }
701
702 void TestStepCmd::do_reset() throw() {
703
704     if (a_threadRunning) {
705       std::string s_warn = anna::functions::asString("Thread still in progress: deprecating step %d for Test Case %llu", getNumber(), a_testCase->getId());
706       LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
707       a_threadDeprecated = true;
708     }
709 //  if (a_threadRunning) {
710 //    std::string s_warn = anna::functions::asString("Thread still in progress: killing child pid %d within step %d for Test Case %llu", a_childPid, getNumber(), a_testCase->getId());
711 //    LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
712 //    kill (a_childPid, SIGKILL);
713 //  }
714
715   a_resultCode = -2;
716   a_errorMsg = "";
717   //a_output = "";
718   a_childPid = -1;
719 }
720