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 //
15 #include <anna/xml/Compiler.hpp>
16 #include <anna/core/util/Millisecond.hpp>
17 #include <anna/diameter.comm/Message.hpp>
18 #include <anna/core/tracing/Logger.hpp>
19 #include <anna/diameter/codec/functions.hpp>
20 #include <anna/diameter/helpers/base/functions.hpp>
23 #include <RealmNode.hpp>
24 #include <MyDiameterEntity.hpp>
25 #include <MyLocalServer.hpp>
26 #include <TestStep.hpp>
27 #include <Launcher.hpp>
28 #include <TestCase.hpp>
29 #include <TestManager.hpp>
30 #include <TestTimer.hpp>
34 void cmdRunOnThread (TestStepCmd *step, const std::string &cmd) {
35 step->setThreadRunning(true);
36 int rc = system(cmd.c_str());
37 if (rc != -1) rc >>= 8; // divide by 256
38 step->setResultCode(rc);
40 // TODO: timeout the system call
45 ////////////////////////////////////////////////////////////////////////////////////////////////////////
47 ////////////////////////////////////////////////////////////////////////////////////////////////////////
48 void TestStep::initialize(TestCase *testCase) {
49 a_testCase = testCase;
51 a_type = Type::Unconfigured;
54 a_number = testCase->steps() + 1; // testCase is not NULL
57 const char* TestStep::asText(const Type::_v type)
59 static const char* text [] = { "Unconfigured", "Timeout", "Sendxml2e", "Sendxml2c", "Delay", "Wait", "Cmd" };
63 anna::xml::Node* TestStep::asXML(anna::xml::Node* parent) const
65 anna::xml::Node* result = parent->createChild("TestStep");
67 result->createAttribute("Number", a_number);
68 result->createAttribute("Type", asText(a_type));
69 result->createAttribute("Completed", (a_completed ? "yes":"no"));
72 std::string s_aux = a_beginTimestamp.asString();
73 // int deltaMs = (int)(a_beginTimestamp - a_testCase->getStartTimestamp());
74 // if (a_beginTimestamp != 0 && deltaMs > 0) s_aux += anna::functions::asString(" [%.3f]", deltaMs/1000.0);
75 result->createAttribute("BeginTimestamp", s_aux);
78 s_aux = a_endTimestamp.asString();
79 // deltaMs = (int)(a_endTimestamp - a_testCase->getStartTimestamp());
80 // if (a_endTimestamp != 0 && deltaMs > 0) s_aux += anna::functions::asString(" [%.3f]", deltaMs/1000.0);
81 result->createAttribute("EndTimestamp", s_aux);
86 std::string TestStep::asXMLString() const throw() {
87 anna::xml::Node root("root");
88 return anna::xml::Compiler().apply(asXML(&root));
91 bool TestStep::execute() throw() {
92 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));
93 setBeginTimestamp(anna::functions::millisecond());
97 void TestStep::complete() throw() {
98 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));
100 setEndTimestamp(anna::functions::millisecond());
104 void TestStep::reset() throw() {
105 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));
106 // type and testCase kept
108 a_beginTimestamp = 0;
113 void TestStep::next() throw() {
114 a_testCase->nextStep();
115 a_testCase->process();
119 ////////////////////////////////////////////////////////////////////////////////////////////////////////
121 ////////////////////////////////////////////////////////////////////////////////////////////////////////
122 anna::xml::Node* TestStepTimeout::asXML(anna::xml::Node* parent) const
124 anna::xml::Node* result = TestStep::asXML(parent); // end timestamp will be 0 if test finished OK
125 //parent->createChild("TestStepTimeout");
126 result->createAttribute("Timeout", a_timeout.asString());
131 bool TestStepTimeout::do_execute() throw() {
133 a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_timeout, TestTimer::Type::TimeLeft);
135 catch (anna::RuntimeException &ex) {
137 a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
138 a_testCase->setState(TestCase::State::Failed);
141 return true; // go next
144 void TestStepTimeout::do_complete() throw() {
145 int stepNumber = getNumber();
146 if (stepNumber == a_testCase->steps()) {
147 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)
148 a_testCase->setState(TestCase::State::Success);
150 else if (a_testCase->getState() == TestCase::State::InProgress) { // sure
151 a_testCase->addDebugSummaryHint(anna::functions::asString("Timeout expired (step number %d) before test case finished", stepNumber)); // before report (when set Failed state)
152 a_testCase->setState(TestCase::State::Failed);
158 void TestStepTimeout::do_reset() throw() {
159 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));
162 TestManager::instantiate().cancelTimer(a_timer);
164 catch (anna::RuntimeException &ex) {
168 //a_timeout = 0; THIS IS CONFIGURATION INFO
171 ////////////////////////////////////////////////////////////////////////////////////////////////////////
173 ////////////////////////////////////////////////////////////////////////////////////////////////////////
174 anna::xml::Node* TestStepSendxml::asXML(anna::xml::Node* parent) const
176 anna::xml::Node* result = TestStep::asXML(parent);
177 //parent->createChild("TestStepSendxml");
178 std::string msg = "", xmlmsg = "";
181 if (TestManager::instantiate().getDumpHex()) {
182 if (a_message.isEmpty()) {
186 msg = "\n"; msg += a_message.asString(); msg += "\n";
190 if (!a_message.isEmpty()) {
192 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
193 static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
194 codecMsg.decode(a_message);
195 xmlmsg = "\n"; xmlmsg += codecMsg.asXMLString(); xmlmsg += "\n";
197 catch (anna::RuntimeException &ex) {
202 if (msg != "") result->createAttribute("Message", msg);
203 if (xmlmsg != "") result->createAttribute("XMLMessage", xmlmsg);
204 result->createAttribute("Expired", (a_expired ? "yes":"no"));
205 if (a_waitForRequestStepNumber != -1)
206 result->createAttribute("WaitForRequestStepNumber", a_waitForRequestStepNumber);
211 bool TestStepSendxml::do_execute() throw() {
212 anna::diameter::comm::Message *msg = a_realmNode->createCommMessage();
213 bool success = false;
214 std::string failReason, s_warn;
217 // Update sequence for answers:
218 if (a_waitForRequestStepNumber != -1) { // is an answer: try to copy sequence information; alert about Session-Id discrepance
219 // Request which was received:
220 const TestStepWait *tsw = (const TestStepWait*)(a_testCase->getStep(a_waitForRequestStepNumber));
221 const anna::DataBlock &request = tsw->getMsgDataBlock();
222 anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(request);
223 anna::diameter::EndToEnd ete = anna::diameter::codec::functions::getEndToEnd(request);
225 anna::diameter::codec::functions::setHopByHop(a_message, hbh);
226 anna::diameter::codec::functions::setEndToEnd(a_message, ete);
228 // Check Session-Id for warning ...
229 std::string sessionIdAnswer = anna::diameter::helpers::base::functions::getSessionId(a_message);
230 std::string sessionIdRequest = anna::diameter::helpers::base::functions::getSessionId(request);
231 if (sessionIdRequest != sessionIdAnswer) {
232 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());
233 LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
234 a_testCase->addDebugSummaryHint(s_warn);
238 if (getType() == Type::Sendxml2e) {
239 MyDiameterEntity *entity = a_realmNode->getEntity();
242 msg->setBody(a_message);
243 /* response = NULL =*/entity->send(msg);
247 failReason = "There is no diameter entity currently configured. Unable to send the message";
248 LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
251 else if (getType() == Type::Sendxml2c) {
252 MyLocalServer *localServer = a_realmNode->getDiameterServer();
255 msg->setBody(a_message);
256 /* response = NULL =*/localServer->send(msg);
260 failReason = "There is no diameter local server currently configured. Unable to send the message";
261 LOGWARNING(anna::Logger::warning(failReason, ANNA_FILE_LOCATION));
264 } catch(anna::RuntimeException &ex) {
265 failReason = ex.asString();
269 a_realmNode->releaseCommMessage(msg);
272 a_testCase->addDebugSummaryHint(failReason); // before report (when set Failed state);
273 a_testCase->setState(TestCase::State::Failed);
279 return success; // go next if sent was OK
282 void TestStepSendxml::do_reset() throw() {
287 ////////////////////////////////////////////////////////////////////////////////////////////////////////
289 ////////////////////////////////////////////////////////////////////////////////////////////////////////
290 anna::xml::Node* TestStepDelay::asXML(anna::xml::Node* parent) const
292 anna::xml::Node* result = TestStep::asXML(parent);
293 //parent->createChild("TestStepDelay");
295 result->createAttribute("Delay", a_delay.asString());
300 bool TestStepDelay::do_execute() throw() {
302 a_timer = TestManager::instantiate().createTimer((TestCaseStep*)this, a_delay, TestTimer::Type::Delay);
304 catch (anna::RuntimeException &ex) {
306 a_testCase->addDebugSummaryHint(ex.asString()); // before report (when set Failed state)
307 a_testCase->setState(TestCase::State::Failed);
310 return false; // don't go next (wait complete)
313 void TestStepDelay::do_complete() throw() {
315 next(); // next() invoked here because execute() is always false for delay and never advance the iterator
318 void TestStepDelay::do_reset() throw() {
320 TestManager::instantiate().cancelTimer(a_timer);
322 catch (anna::RuntimeException &ex) {
326 //a_delay = 0; THIS IS CONFIGURATION INFO
329 ////////////////////////////////////////////////////////////////////////////////////////////////////////
331 ////////////////////////////////////////////////////////////////////////////////////////////////////////
332 void TestStepWait::setCondition(bool fromEntity,
333 const std::string &code, const std::string &bitR, const std::string &resultCode, const std::string &sessionId,
334 const std::string &hopByHop, const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw() {
336 a_condition.setReceivedFromEntity(fromEntity);
337 a_condition.setCode(code);
338 a_condition.setBitR(bitR);
339 a_condition.setResultCode(resultCode);
340 a_condition.setSessionId(sessionId);
341 a_condition.setHopByHop(hopByHop);
342 a_condition.setMsisdn(msisdn);
343 a_condition.setImsi(imsi);
344 a_condition.setServiceContextId(serviceContextId);
347 void TestStepWait::setCondition(bool fromEntity, const std::string ®exp) throw() {
349 a_condition.setReceivedFromEntity(fromEntity);
350 a_condition.setRegexp(regexp);
353 anna::xml::Node* TestStepWait::asXML(anna::xml::Node* parent) const
355 anna::xml::Node* result = TestStep::asXML(parent);
356 //parent->createChild("TestStepWait");
357 std::string msg = "", xmlmsg = "";
360 a_condition.asXML(result);
363 if (TestManager::instantiate().getDumpHex()) {
364 if (a_message.isEmpty()) {
368 msg = "\n"; msg += a_message.asString(); msg += "\n";
372 if (!a_message.isEmpty()) {
374 Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
375 static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
376 codecMsg.decode(a_message);
377 xmlmsg = "\n"; xmlmsg += codecMsg.asXMLString(); xmlmsg += "\n";
379 catch (anna::RuntimeException &ex) {
384 if (msg != "") result->createAttribute("MatchedMessage", msg);
385 if (xmlmsg != "") result->createAttribute("MatchedXMLMessage", xmlmsg);
390 bool TestStepWait::do_execute() throw() {
394 void TestStepWait::do_complete() throw() {
395 a_testCase->process(); // next() not invoked; we only want to reactivate the test case
398 bool TestStepWait::fulfilled(const anna::DataBlock &db/*, bool matchSessionId*/) throw() {
399 if (a_condition.comply(db/*, matchSessionId*/)) {
400 a_message = db; // store matched
408 void TestStepWait::do_reset() throw() {
410 a_clientSession = NULL;
411 a_serverSession = NULL;
414 ////////////////////////////////////////////////////////////////////////////////////////////////////////
416 ////////////////////////////////////////////////////////////////////////////////////////////////////////
417 anna::xml::Node* TestStepCmd::asXML(anna::xml::Node* parent) const
419 anna::xml::Node* result = TestStep::asXML(parent);
420 //parent->createChild("TestStepCmd");
422 result->createAttribute("Script", (a_script != "") ? a_script:"<no script>");
423 result->createAttribute("Parameters", (a_parameters != "") ? a_parameters:"<no parameters>");
424 result->createAttribute("CommandInProgress", a_threadRunning ? "yes":"no");
425 if (!a_threadRunning && a_resultCode != -2) {
426 result->createAttribute("ResultCode", a_resultCode);
427 //if (a_output != "") result->createAttribute("Output", a_output);
433 bool TestStepCmd::do_execute() throw() {
434 if (!a_threadRunning) {
435 // Special tags to replace:
436 std::string cmd = getScript();
438 cmd += getParameters();
440 while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID)) != std::string::npos)
441 cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__CYCLE_ID), anna::functions::asString(TestManager::instantiate().getPoolCycle()));
442 while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID)) != std::string::npos)
443 cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__TESTCASE_ID), anna::functions::asString(a_testCase->getId()));
444 while ((index = cmd.find(SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID)) != std::string::npos)
445 cmd.replace(index, strlen(SH_COMMAND_TAG_FOR_REPLACE__TESTSTEP_ID), anna::functions::asString(getNumber()));
447 a_thread = std::thread(cmdRunOnThread, this, cmd);
451 return false; // don't go next (wait complete)
454 void TestStepCmd::do_complete() throw() {
456 a_threadRunning = false;
457 if (a_threadDeprecated) {
458 a_threadDeprecated = false;
459 return; // ignore TODO: interrupt the thread to avoid execution of the script
462 if (getResultCode() != 0)
463 a_testCase->setState(TestCase::State::Failed);
465 next(); // next() invoked here because execute() is always false for delay and never advance the iterator
468 void TestStepCmd::do_reset() throw() {
470 if (a_threadRunning) {
471 std::string s_warn = anna::functions::asString("Thread still in progress: deprecating step %d for Test Case %llu", getNumber(), a_testCase->getId());
472 LOGWARNING(anna::Logger::warning(s_warn, ANNA_FILE_LOCATION));
473 a_threadDeprecated = true;