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 //
16 #include <anna/xml/Compiler.hpp>
17 #include <anna/core/util/Millisecond.hpp>
18 #include <anna/diameter.comm/Message.hpp>
19 #include <anna/core/tracing/Logger.hpp>
20 #include <anna/diameter/codec/functions.hpp>
21 #include <anna/diameter/helpers/base/functions.hpp>
24 #include <RealmNode.hpp>
25 #include <MyDiameterEntity.hpp>
26 #include <MyLocalServer.hpp>
27 #include <TestStep.hpp>
28 #include <Launcher.hpp>
29 #include <TestCase.hpp>
30 #include <TestManager.hpp>
31 #include <TestTimer.hpp>
35 void cmdRunOnThread (TestStepCmd *step, const std::string &cmd) {
36 step->setThreadRunning(true);
37 int rc = system(cmd.c_str());
38 if (rc < 0 && errno == ECHILD) rc = 0; // ignore, it could happens
39 // I know one reason for this is that SICCHLD is set to SIG_IGN but this
40 // should not be the case here. SIGCHLD is explicity set to SIG_DFL
41 // using a sigaction before the call to system(). (Although it is
42 // normally set to SIG_IGN). There should not be any other threads
43 // messing about with SIGCHLD.
46 step->setErrorMsg(anna::functions::asString("errno = %d", errno));
50 rc >>= 8; // divide by 256
53 step->setResultCode(rc);
55 // TODO: timeout the system call
60 ////////////////////////////////////////////////////////////////////////////////////////////////////////
62 ////////////////////////////////////////////////////////////////////////////////////////////////////////
63 void TestStep::initialize(TestCase *testCase) {
64 a_testCase = testCase;
66 a_type = Type::Unconfigured;
69 a_number = testCase->steps() + 1; // testCase is not NULL
72 const char* TestStep::asText(const Type::_v type)
74 static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait", "Cmd" };
78 anna::xml::Node* TestStep::asXML(anna::xml::Node* parent) const
80 anna::xml::Node* result = parent->createChild("TestStep");
82 result->createAttribute("Number", a_number);
83 result->createAttribute("Type", asText(a_type));
84 result->createAttribute("Completed", (a_completed ? "yes":"no"));
87 std::string s_aux = a_beginTimestamp.asString();
88 // int deltaMs = (int)(a_beginTimestamp - a_testCase->getStartTimestamp());
89 // if (a_beginTimestamp != 0 && deltaMs > 0) s_aux += anna::functions::asString(" [%.3f]", deltaMs/1000.0);
90 result->createAttribute("BeginTimestamp", s_aux);
93 s_aux = a_endTimestamp.asString();
94 // deltaMs = (int)(a_endTimestamp - a_testCase->getStartTimestamp());
95 // if (a_endTimestamp != 0 && deltaMs > 0) s_aux += anna::functions::asString(" [%.3f]", deltaMs/1000.0);
96 result->createAttribute("EndTimestamp", s_aux);
101 std::string TestStep::asXMLString() const throw() {
102 anna::xml::Node root("root");
103 return anna::xml::Compiler().apply(asXML(&root));
106 bool TestStep::execute() throw() {
107 LOGDEBUG(anna::Logger::debug(anna::functions::asString("EXECUTING %s (step number %d) for Test Case %llu (%p) (%p)", asText(a_type), a_number, a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
108 setBeginTimestamp(anna::functions::millisecond());
112 void TestStep::complete() throw() {
113 LOGDEBUG(anna::Logger::debug(anna::functions::asString("COMPLETE %s (step number %d) for Test Case %llu (%p) (%p)", asText(a_type), a_number, a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
115 setEndTimestamp(anna::functions::millisecond());
119 void TestStep::reset() throw() {
120 LOGDEBUG(anna::Logger::debug(anna::functions::asString("RESET %s (step number %d) for Test Case %llu (%p) (%p)", asText(a_type), a_number, a_testCase->getId(), (TestCaseStep*)this, this), ANNA_FILE_LOCATION));
121 // type and testCase kept
123 a_beginTimestamp = 0;
128 void TestStep::next() throw() {
129 a_testCase->nextStep();
130 a_testCase->process();
134 ////////////////////////////////////////////////////////////////////////////////////////////////////////
136 ////////////////////////////////////////////////////////////////////////////////////////////////////////
137 anna::xml::Node* TestStepTimeout::asXML(anna::xml::Node* parent) const
139 anna::xml::Node* result = TestStep::asXML(parent); // end timestamp will be 0 if test finished OK
140 //parent->createChild("TestStepTimeout");
141 result->createAttribute("Timeout", a_timeout.asString());
146 bool TestStepTimeout::do_execute() throw() {
148 a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_timeout, TestTimer::Type::TimeLeft);
150 catch (anna::RuntimeException &ex) {
152 a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
153 a_testCase->setState(TestCase::State::Failed);
156 return true; // go next
159 void TestStepTimeout::do_complete() throw() {
160 int stepNumber = getNumber();
161 if (stepNumber == a_testCase->steps()) {
162 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)
163 a_testCase->setState(TestCase::State::Success);
165 else if (a_testCase->getState() == TestCase::State::InProgress) { // sure
166 a_testCase->addDebugSummaryHint(anna::functions::asString("Timeout expired (step number %d) before test case finished", stepNumber)); // before report (when set Failed state)
167 a_testCase->setState(TestCase::State::Failed);
173 void TestStepTimeout::do_reset() throw() {
174 LOGDEBUG(anna::Logger::debug(anna::functions::asString("REXXXXXET %s for Test Case %llu (%p) (%p) (a_timer %p)", asText(a_type), a_testCase->getId(), (TestCaseStep*)this, this, a_timer), ANNA_FILE_LOCATION));
177 TestManager::instantiate().cancelTimer(a_timer);
179 catch (anna::RuntimeException &ex) {
183 //a_timeout = 0; THIS IS CONFIGURATION INFO
186 ////////////////////////////////////////////////////////////////////////////////////////////////////////
188 ////////////////////////////////////////////////////////////////////////////////////////////////////////
189 anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent) const
191 anna::xml::Node* result = TestStep::asXML(parent);
192 //parent->createChild("TestStepSendxml");
193 std::string msg = "", xmlmsg = "";
196 if (TestManager::instantiate().getDumpHex()) {
197 if (a_message.isEmpty()) {
201 msg = "\n"; msg += a_message.asString(); msg += "\n";
205 if (!a_message.isEmpty()) {
207 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
208 static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
209 codecMsg.decode(a_message);
210 xmlmsg = "\n"; xmlmsg += codecMsg.asXMLString(); xmlmsg += "\n";
212 catch (anna::RuntimeException &ex) {
217 if (msg != "") result->createAttribute("Message", msg);
218 if (xmlmsg != "") result->createAttribute("XMLMessage", xmlmsg);
219 result->createAttribute("Expired", (a_expired ? "yes":"no"));
220 if (a_waitForRequestStepNumber != -1)
221 result->createAttribute("WaitForRequestStepNumber", a_waitForRequestStepNumber);
226 bool TestStepSendxml::do_execute() throw() {
227 anna::diameter::comm::Message *msg = a_realmNode->createCommMessage();
228 bool success = false;
229 std::string failReason, s_warn;
232 // Update sequence for answers:
233 if (a_waitForRequestStepNumber != -1) { // is an answer: try to copy sequence information; alert about Session-Id discrepance
234 // Request which was received:
235 const TestStepWait *tsw = (const TestStepWait*)(a_testCase->getStep(a_waitForRequestStepNumber));
236 const anna::DataBlock &request = tsw->getMsgDataBlock();
237 anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(request);
238 anna::diameter::EndToEnd ete = anna::diameter::codec::functions::getEndToEnd(request);
240 anna::diameter::codec::functions::setHopByHop(a_message, hbh);
241 anna::diameter::codec::functions::setEndToEnd(a_message, ete);
243 // Check Session-Id for warning ...
244 std::string sessionIdAnswer = anna::diameter::helpers::base::functions::getSessionId(a_message);
245 std::string sessionIdRequest = anna::diameter::helpers::base::functions::getSessionId(request);
246 if (sessionIdRequest != sessionIdAnswer) {
247 s_warn = anna::functions::asString("Sending an answer which Session-Id (%s) is different than supposed corresponding request (%s)", sessionIdAnswer.c_str(), sessionIdRequest.c_str());
248 LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
249 a_testCase->addDebugSummaryHint(s_warn);
253 if (getType() == Type::Sendxml2e) {
254 MyDiameterEntity *entity = a_realmNode->getEntity();
257 msg->setBody(a_message);
258 /* response = NULL =*/entity->send(msg);
262 failReason = "There is no diameter entity currently configured. Unable to send the message";
263 LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
266 else if (getType() == Type::Sendxml2c) {
267 MyLocalServer *localServer = a_realmNode->getDiameterServer();
270 msg->setBody(a_message);
271 /* response = NULL =*/localServer->send(msg);
275 failReason = "There is no diameter local server currently configured. Unable to send the message";
276 LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
279 } catch(anna::RuntimeException &ex) {
280 failReason = ex.asString();
284 a_realmNode->releaseCommMessage(msg);
287 a_testCase->addDebugSummaryHint(failReason); // before report (when set Failed state);
288 a_testCase->setState(TestCase::State::Failed);
294 return success; // go next if sent was OK
297 void TestStepSendxml::do_reset() throw() {
302 ////////////////////////////////////////////////////////////////////////////////////////////////////////
304 ////////////////////////////////////////////////////////////////////////////////////////////////////////
305 anna::xml::Node* TestStepDelay::asXML(anna::xml::Node* parent) const
307 anna::xml::Node* result = TestStep::asXML(parent);
308 //parent->createChild("TestStepDelay");
310 result->createAttribute("Delay", a_delay.asString());
315 bool TestStepDelay::do_execute() throw() {
317 a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_delay, TestTimer::Type::Delay);
319 catch (anna::RuntimeException &ex) {
321 a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
322 a_testCase->setState(TestCase::State::Failed);
325 return false; // don't go next (wait complete)
328 void TestStepDelay::do_complete() throw() {
330 next(); // next() invoked here because execute() is always false for delay and never advance the iterator
333 void TestStepDelay::do_reset() throw() {
335 TestManager::instantiate().cancelTimer(a_timer);
337 catch (anna::RuntimeException &ex) {
341 //a_delay = 0; THIS IS CONFIGURATION INFO
344 ////////////////////////////////////////////////////////////////////////////////////////////////////////
346 ////////////////////////////////////////////////////////////////////////////////////////////////////////
347 void TestStepWait::setCondition(bool fromEntity,
348 const std::string &code, const std::string &bitR, const std::string &resultCode, const std::string &sessionId,
349 const std::string &hopByHop, const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw() {
351 a_condition.setReceivedFromEntity(fromEntity);
352 a_condition.setCode(code);
353 a_condition.setBitR(bitR);
354 a_condition.setResultCode(resultCode);
355 a_condition.setSessionId(sessionId);
356 a_condition.setHopByHop(hopByHop);
357 a_condition.setMsisdn(msisdn);
358 a_condition.setImsi(imsi);
359 a_condition.setServiceContextId(serviceContextId);
362 void TestStepWait::setCondition(bool fromEntity, const std::string ®exp) throw() {
364 a_condition.setReceivedFromEntity(fromEntity);
365 a_condition.setRegexp(regexp);
368 anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent) const
370 anna::xml::Node* result = TestStep::asXML(parent);
371 //parent->createChild("TestStepWait");
372 std::string msg = "", xmlmsg = "";
375 a_condition.asXML(result);
378 if (TestManager::instantiate().getDumpHex()) {
379 if (a_message.isEmpty()) {
383 msg = "\n"; msg += a_message.asString(); msg += "\n";
387 if (!a_message.isEmpty()) {
389 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
390 static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
391 codecMsg.decode(a_message);
392 xmlmsg = "\n"; xmlmsg += codecMsg.asXMLString(); xmlmsg += "\n";
394 catch (anna::RuntimeException &ex) {
399 if (msg != "") result->createAttribute("MatchedMessage", msg);
400 if (xmlmsg != "") result->createAttribute("MatchedXMLMessage", xmlmsg);
405 bool TestStepWait::do_execute() throw() {
409 void TestStepWait::do_complete() throw() {
410 a_testCase->process(); // next() not invoked; we only want to reactivate the test case
413 bool TestStepWait::fulfilled(const anna::DataBlock &db/*, bool matchSessionId*/) throw() {
414 if (a_condition.comply(db/*, matchSessionId*/)) {
415 a_message = db; // store matched
423 void TestStepWait::do_reset() throw() {
425 a_clientSession = NULL;
426 a_serverSession = NULL;
429 ////////////////////////////////////////////////////////////////////////////////////////////////////////
431 ////////////////////////////////////////////////////////////////////////////////////////////////////////
432 anna::xml::Node* TestStepCmd::asXML(anna::xml::Node* parent) const
434 anna::xml::Node* result = TestStep::asXML(parent);
435 //parent->createChild("TestStepCmd");
437 result->createAttribute("Script", (a_script != "") ? a_script:"<no script>");
438 result->createAttribute("Parameters", (a_parameters != "") ? a_parameters:"<no parameters>");
439 result->createAttribute("CommandInProgress", a_threadRunning ? "yes":"no");
440 if (a_errorMsg != "") result->createAttribute("ErrorMessage", a_errorMsg);
441 if (!a_threadRunning && a_resultCode != -2) {
442 result->createAttribute("ResultCode", a_resultCode);
443 //if (a_output != "") result->createAttribute("Output", a_output);
449 bool TestStepCmd::do_execute() throw() {
450 if (!a_threadRunning) {
451 // Special tags to replace:
452 std::string cmd = getScript();
454 cmd += getParameters();
456 while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID)) != std::string::npos)
457 cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID), anna::functions::asString(TestManager::instantiate().getPoolCycle()));
458 while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID)) != std::string::npos)
459 cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID), anna::functions::asString(a_testCase->getId()));
460 while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID)) != std::string::npos)
461 cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID), anna::functions::asString(getNumber()));
463 a_thread = std::thread(cmdRunOnThread, this, cmd);
467 return false; // don't go next (wait complete)
470 void TestStepCmd::do_complete() throw() {
472 a_threadRunning = false;
473 if (a_threadDeprecated) {
474 a_threadDeprecated = false;
475 return; // ignore TODO: interrupt the thread to avoid execution of the script
478 if (getResultCode() != 0)
479 a_testCase->setState(TestCase::State::Failed);
481 next(); // next() invoked here because execute() is always false for delay and never advance the iterator
484 void TestStepCmd::do_reset() throw() {
486 if (a_threadRunning) {
487 std::string s_warn = anna::functions::asString("Thread still in progress: deprecating step %d for Test Case %llu", getNumber(), a_testCase->getId());
488 LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
489 a_threadDeprecated = true;