1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
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 //
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
20 #include <sys/types.h>
25 #include <anna/testing/TestStep.hpp>
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>
47 using namespace anna::testing;
52 void handle_sigchld(int sig) {
53 while (waitpid((pid_t)(-1 /* any child (the only) */), 0, WNOHANG|WNOWAIT) > 0) {}
56 void cmdRunOnThread (TestStepCmd *step, const std::string &cmd) {
59 step->setThreadRunning(true);
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());
71 FILE *fp = popen(cmd.c_str(), "r");
73 while(fgets(readbuf, sizeof(readbuf), fp))
74 step->appendOutput("\n");
75 step->appendOutput(readbuf);
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) {
94 char const * str = strerror_r(errno, buf, 256);
95 step->setErrorMsg(anna::functions::asString("errno = %d (%s)", errno, str));
98 step->setResultCode(WEXITSTATUS(status)); // rc = status >>= 8; // divide by 256
100 // TODO: terminate thread when deprecated (RT signal ?)
101 // TODO: mutex the step while setting data here !!
104 void cmdRunOnThreadWithFork (TestStepCmd *step, const std::string &cmd) {
107 step->setThreadRunning(true);
112 if ((cpid = fork()) < 0) {
113 step->setErrorMsg("Error in fork()");
115 else if (cpid == 0) {
117 status = system(cmd.c_str());
118 _exit(WEXITSTATUS(status));
122 step->setChildPid(cpid);
124 w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
127 if (WIFEXITED(status)) {
128 step->setResultCode(WEXITSTATUS(status)); // rc = status >>= 8; // divide by 256
131 else if (WIFSIGNALED(status)) {
132 step->setErrorMsg(anna::functions::asString("killed by signal %d", WTERMSIG(status)));
133 step->setResultCode(128 + WTERMSIG(status));
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");
142 step->setErrorMsg("waitpid error");
143 step->setResultCode(-1);
146 } while (!WIFEXITED(status) && !WIFSIGNALED(status));
152 bool decodeMessage(const anna::DataBlock &message, anna::diameter::codec::Message &messageCodec) throw() {
154 if (message.isEmpty())
159 messageCodec.clear();
160 messageCodec.decode(message);
162 catch (anna::RuntimeException &ex) {
172 ////////////////////////////////////////////////////////////////////////////////////////////////////////
174 ////////////////////////////////////////////////////////////////////////////////////////////////////////
175 void TestStep::initialize(TestCase *testCase) {
176 a_testCase = testCase;
178 a_type = Type::Unconfigured;
179 a_beginTimestamp = 0;
181 a_number = testCase->steps() + 1; // testCase is not NULL
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;
189 LOGDEBUG(anna::Logger::debug("Error DECODING, but trusting it ...", ANNA_FILE_LOCATION));
193 delete a_messageCodec;
194 a_messageCodec = NULL;
198 const char* TestStep::asText(const Type::_v type)
200 static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait", "Command" };
204 anna::xml::Node* TestStep::asXML(anna::xml::Node* parent)
206 anna::xml::Node* result = parent->createChild("TestStep");
208 result->createAttribute("Number", a_number);
209 result->createAttribute("Type", asText(a_type));
210 result->createAttribute("Completed", (a_completed ? "yes":"no"));
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);
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);
227 std::string TestStep::asXMLString() throw() {
228 anna::xml::Node root("root");
229 return anna::xml::Compiler().apply(asXML(&root));
232 bool TestStep::execute() throw() {
234 int ia = a_testCase->interactiveAmount();
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)
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());
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));
251 setEndTimestamp(anna::functions::millisecond());
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
260 a_beginTimestamp = 0;
265 void TestStep::next() throw() {
266 a_testCase->nextStep();
267 a_testCase->process();
271 ////////////////////////////////////////////////////////////////////////////////////////////////////////
273 ////////////////////////////////////////////////////////////////////////////////////////////////////////
274 anna::xml::Node* TestStepTimeout::asXML(anna::xml::Node* parent)
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());
283 bool TestStepTimeout::do_execute() throw() {
285 a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_timeout, TestTimer::Type::TimeLeft);
287 catch (anna::RuntimeException &ex) {
289 a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
290 a_testCase->setState(TestCase::State::Failed);
293 return true; // go next
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);
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);
310 void TestStepTimeout::do_reset() throw() {
312 TestManager::instantiate().cancelTimer(a_timer);
314 catch (anna::RuntimeException &ex) {
318 //a_timeout = 0; THIS IS CONFIGURATION INFO
321 ////////////////////////////////////////////////////////////////////////////////////////////////////////
323 ////////////////////////////////////////////////////////////////////////////////////////////////////////
324 anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent)
326 anna::xml::Node* result = TestStep::asXML(parent);
327 //parent->createChild("TestStepSendxml");
328 std::string msg = "", xmlmsg = "";
331 if (TestManager::instantiate().getDumpHex()) {
332 if (a_message.isEmpty()) {
336 msg = "\n"; msg += a_message.asString(); msg += "\n";
340 if (decodeMessage()) {
342 xmlmsg += a_messageCodec->asXMLString();
346 xmlmsg = "<unable to decode, check traces>";
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);
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();
368 if (a_waitForRequestStepNumber != -1) {
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());
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);
381 anna::diameter::codec::functions::setHopByHop(a_message, hbh);
382 anna::diameter::codec::functions::setEndToEnd(a_message, ete);
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);
398 // Create comm message:
400 msg->setBody(a_message);
403 if (getType() == Type::Sendxml2e) {
404 anna::diameter::comm::ClientSession *usedClientSession = NULL;
406 if (tsw) { // is an answer for a received request on wait condition
407 anna::diameter::comm::ClientSession *clientSession = tsw->getClientSession();
409 /* response NULL (is an answer) */clientSession->send(msg);
411 usedClientSession = clientSession;
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));
420 success = entity->send(msg);
421 anna::diameter::comm::Server *usedServer = entity->getLastUsedResource();
422 usedClientSession = usedServer ? usedServer->getLastUsedResource() : NULL;
425 failReason = "There is no diameter entity currently configured. Unable to send the message";
426 LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
428 } // else (normal sending)
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);
438 else if (getType() == Type::Sendxml2c) {
439 anna::diameter::comm::ServerSession *usedServerSession = NULL;
441 if (tsw) { // is an answer for a received request on wait condition
442 anna::diameter::comm::ServerSession *serverSession = tsw->getServerSession();
444 /* response NULL (is an answer) */serverSession->send(msg);
446 usedServerSession = serverSession;
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));
455 success = localServer->send(msg);
456 usedServerSession = localServer->getLastUsedResource();
459 failReason = "There is no diameter local server currently configured. Unable to send the message";
460 LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
462 } // else (normal sending)
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);
473 } catch(anna::RuntimeException &ex) {
474 failReason = ex.asString();
478 a_originHost->releaseCommMessage(msg);
481 a_testCase->addDebugSummaryHint(failReason); // before report (when set Failed state);
482 a_testCase->setState(TestCase::State::Failed);
488 return success; // go next if sent was OK
491 void TestStepSendxml::do_reset() throw() {
494 //a_messageAlreadyDecoded = false;
497 ////////////////////////////////////////////////////////////////////////////////////////////////////////
499 ////////////////////////////////////////////////////////////////////////////////////////////////////////
500 anna::xml::Node* TestStepDelay::asXML(anna::xml::Node* parent)
502 anna::xml::Node* result = TestStep::asXML(parent);
503 //parent->createChild("TestStepDelay");
505 result->createAttribute("Delay", ((a_delay == 0) ? "dummy step, no delay" : a_delay.asString()));
510 bool TestStepDelay::do_execute() throw() {
511 if (a_delay == 0) { complete(); return true; } // special case
513 a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_delay, TestTimer::Type::Delay);
515 catch (anna::RuntimeException &ex) {
517 a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
518 a_testCase->setState(TestCase::State::Failed);
521 return false; // don't go next (wait complete)
524 void TestStepDelay::do_complete() throw() {
525 if (a_delay == 0) return; // special case
527 next(); // next() invoked here because execute() is always false for delay and never advance the iterator
528 // TODO, avoid this recursion
531 void TestStepDelay::do_reset() throw() {
532 if (a_delay == 0) return; // special case
534 TestManager::instantiate().cancelTimer(a_timer);
536 catch (anna::RuntimeException &ex) {
540 //a_delay = 0; THIS IS CONFIGURATION INFO
543 ////////////////////////////////////////////////////////////////////////////////////////////////////////
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() {
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);
562 void TestStepWait::setConditionRegexpHex(bool fromEntity, const std::string ®exp) throw() {
563 a_condition.setReceivedFromEntity(fromEntity);
564 a_condition.setRegexpHex(regexp);
567 void TestStepWait::setConditionRegexpXml(bool fromEntity, const std::string ®exp) throw() {
568 a_condition.setReceivedFromEntity(fromEntity);
569 a_condition.setRegexpXml(regexp);
572 anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent)
574 anna::xml::Node* result = TestStep::asXML(parent);
575 //parent->createChild("TestStepWait");
576 std::string msg = "", xmlmsg = "";
579 a_condition.asXML(result);
582 if (TestManager::instantiate().getDumpHex()) {
583 if (a_message.isEmpty()) {
587 msg = "\n"; msg += a_message.asString(); msg += "\n";
591 if (a_message.isEmpty()) {
595 if (decodeMessage()) {
597 xmlmsg += a_messageCodec->asXMLString();
601 xmlmsg = "<unable to decode, check traces>";
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));
613 bool TestStepWait::do_execute() throw() {
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
622 bool TestStepWait::fulfilled(const anna::DataBlock &db/*, bool matchSessionId*/) throw() {
623 if (a_condition.comply(db/*, matchSessionId*/)) {
624 a_message = db; // store matched
632 void TestStepWait::do_reset() throw() {
634 a_clientSession = NULL;
635 a_serverSession = NULL;
638 ////////////////////////////////////////////////////////////////////////////////////////////////////////
640 ////////////////////////////////////////////////////////////////////////////////////////////////////////
641 anna::xml::Node* TestStepCmd::asXML(anna::xml::Node* parent)
643 anna::xml::Node* result = TestStep::asXML(parent);
644 //parent->createChild("TestStepCmd");
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);
653 if (a_resultCode != -2) {
654 result->createAttribute("ResultCode", a_resultCode);
655 //if (a_output != "") result->createAttribute("Output", a_output);
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();
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()));
674 a_thread = std::thread(cmdRunOnThread, this, cmd);
675 //a_thread = std::thread(cmdRunOnThreadWithFork, this, cmd);
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 !
684 void TestStepCmd::do_complete() throw() {
686 a_threadRunning = false;
687 if (a_threadDeprecated) {
688 a_threadDeprecated = false;
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
695 if (getResultCode() != 0)
696 a_testCase->setState(TestCase::State::Failed);
698 next(); // next() invoked here because execute() is always false for delay and never advance the iterator
699 // TODO, avoid this recursion
702 void TestStepCmd::do_reset() throw() {
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;
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);