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 //
19 #include <anna/xml/Compiler.hpp>
20 #include <anna/diameter/defines.hpp>
21 #include <anna/diameter/helpers/dcca/defines.hpp>
22 #include <anna/diameter/codec/functions.hpp>
23 #include <anna/diameter/helpers/base/functions.hpp>
24 #include <anna/diameter/helpers/dcca/functions.hpp>
25 #include <anna/core/util/Millisecond.hpp>
26 #include <anna/core/tracing/Logger.hpp>
29 #include <TestCase.hpp>
30 #include <TestManager.hpp>
33 void TestCase::DebugSummary::addHint(const std::string &hint) throw() {
35 event.Timestamp = anna::functions::millisecond();
37 a_events.push_back(event);
40 void TestCase::DebugSummary::clear() throw() {
44 anna::xml::Node* TestCase::DebugSummary::asXML(anna::xml::Node* parent) const throw() {
45 anna::xml::Node* result = parent->createChild("DebugSummary");
47 std::vector<event_t>::const_iterator it;
48 for (it = a_events.begin(); it != a_events.end(); it++) {
49 anna::xml::Node* event = result->createChild("Event");
50 event->createAttribute("Timestamp", (*it).Timestamp.asString());
51 event->createAttribute("Hint", (*it).Hint);
58 TestCase::~TestCase() {
59 reset(true); // hard reset
60 std::map<int/* step number*/, TestStep*>::const_iterator it;
61 for (it = a_steps.begin(); it != a_steps.end(); it++) delete (it->second);
64 const char* TestCase::asText(const State::_v state)
66 static const char* text [] = { "Initialized", "InProgress", "Failed", "Success" };
70 anna::xml::Node* TestCase::asXML(anna::xml::Node* parent) const
72 anna::xml::Node* result = parent->createChild("TestCase");
74 result->createAttribute("Id", a_id);
75 result->createAttribute("State", asText(a_state));
76 result->createAttribute("StartTimestamp", a_startTime.asString());
77 int steps = a_steps.size();
79 result->createAttribute("NumberOfTestSteps", steps);
80 std::map<int/* step number*/, TestStep*>::const_iterator it;
81 for (it = a_steps.begin(); it != a_steps.end(); it++) {
82 it->second->asXML(result);
86 if (a_debugSummary.events()) {
87 a_debugSummary.asXML(result);
90 result->createAttribute("Interactive", (a_interactiveAmount != -1) ? "yes":"no");
95 std::string TestCase::asXMLString() const throw() {
96 anna::xml::Node root("root");
97 return anna::xml::Compiler().apply(asXML(&root));
100 bool TestCase::hasSameCondition(const TestCondition &condition) const throw() {
101 std::map<int/* step number*/, TestStep*>::const_iterator it;
103 for (it = a_steps.begin(); it != a_steps.end(); it++) {
104 if (it->second->getType() != TestStep::Type::Wait) continue;
105 step = (TestStepWait *)(it->second);
106 if (step->getCondition() == condition) return true;
112 void TestCase::setState(const State::_v &state) throw() {
114 State::_v previousState = a_state;
115 if (state == previousState) return;
117 TestManager &testManager = TestManager::instantiate();
119 if (!testManager.getDumpReports()) return;
120 // report file name: cycle-<cycle id>.testcase-<test case id>.xml
122 // FORMAT: We tabulate the cycle and test case in order to ease ordering of files by mean ls:
123 int cycles = testManager.getPoolRepeats();
124 int tests = testManager.tests();
125 int cyclesWidth = (cycles<=0) ? 3 /* 1000 cycles !! */: ((int) log10 ((double) cycles) + 1);
126 int testsWidth = (tests<=0) ? 9 /* subscribers */: ((int) log10 ((double) tests) + 1);
127 std::stringstream format;
128 format << "/cycle-%0" << cyclesWidth << "d.testcase-%0" << testsWidth << "llu.xml";
131 std::string file = testManager.getReportsDirectory() + anna::functions::asString(format.str().c_str(), testManager.getPoolCycle(), a_id);
133 out.open(file.c_str(), std::ofstream::out | std::ofstream::app);
134 if(out.is_open() == false) {
135 std::string msg("Error opening '");
137 msg += "' for writting";
138 anna::Logger::error(msg, ANNA_FILE_LOCATION);
141 out << asXMLString() << std::endl;
146 // Count in-progress test cases:
148 testManager.setInProgressCountDelta(1);
150 else if (previousState == State::InProgress){
151 testManager.setInProgressCountDelta(-1);
155 bool TestCase::done() throw() {
156 if (a_stepsIt == a_steps.end()) {
157 setState(State::Success);
164 bool TestCase::process() throw() {
165 if (a_steps.size() == 0) {
166 LOGWARNING(anna::Logger::warning(anna::functions::asString("Test case %llu is empty, nothing to execute", a_id), ANNA_FILE_LOCATION));
170 LOGDEBUG(anna::Logger::debug(anna::functions::asString("Test case %llu is finished, nothing done until soft-reset", a_id), ANNA_FILE_LOCATION));
174 if (a_state == State::Initialized) {
175 a_stepsIt = a_steps.begin();
176 setState(State::InProgress);
178 // For 'wait' steps (not really useful, but better than nothing: begin timestamp on test case start timestamp...):
179 a_startTime = anna::functions::millisecond();
182 // Check end of the test case:
183 if (done()) return false;
185 bool somethingDone = false;
186 while (a_stepsIt->second->execute()) { // executes returns 'true' if the next step must be also executed (execute until can't stand no more)
188 // Check end of the test case:
189 if (done()) return false;
190 somethingDone = true;
193 return somethingDone;
196 bool TestCase::reset(bool hard) throw() {
198 // Soft reset if finished:
199 if (!hard /* is soft reset */ && !isFinished()) return false;
201 // Clean stage ////////////////////////////
203 std::map<int/* step number*/, TestStep*>::iterator it;
204 for (it = a_steps.begin(); it != a_steps.end(); it++)
207 a_debugSummary.clear();
209 a_interactiveAmount = -1;
211 setState(State::Initialized);
216 void TestCase::assertInitialized() const throw(anna::RuntimeException) {
217 if (a_state != State::Initialized)
218 throw anna::RuntimeException(anna::functions::asString("Cannot program anymore. The test case %llu was started. You must reset it to append new steps.", a_id), ANNA_FILE_LOCATION);
221 void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(anna::RuntimeException) {
223 bool isRequest = anna::diameter::codec::functions::isRequest(db);
224 bool registerSessionId = ((isRequest && toEntity) || (!isRequest && !toEntity) /* (*) */);
225 // (*) we register answers Session-Id assuming that we will know the Session-Id values created by the client (OCS)
226 // This is another solution for TODO(***) regarding diameter server testing. No tsure about the final implementation.
230 anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db);
231 if (a_hopByHops.find(hbh) != a_hopByHops.end())
232 throw anna::RuntimeException(anna::functions::asString("Another request has been programmed with the same hop-by-hop (%llu) in this test case (%llu)", hbh, a_id), ANNA_FILE_LOCATION);
233 a_hopByHops[hbh] = NULL; // may be assigned to a wait condition
236 if (registerSessionId)
237 TestManager::instantiate().registerSessionId(anna::diameter::helpers::base::functions::getSessionId(db), this);
240 void TestCase::addTimeout(const anna::Millisecond &timeout) throw(anna::RuntimeException) {
242 TestStepTimeout *step = new TestStepTimeout(this);
243 step->setTimeout(timeout);
247 void TestCase::addSendxml2e(const anna::DataBlock &db, RealmNode *realm, int stepNumber) throw(anna::RuntimeException) {
249 assertMessage(db, true /* to entity */);
251 if (stepNumber != -1) {
252 const TestStep *stepReferred = getStep(stepNumber);
254 throw anna::RuntimeException(anna::functions::asString("Step number (%d) do not exists (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
256 if (stepReferred->getType() != TestStep::Type::Wait)
257 throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait' step (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
259 const TestCondition &tc = (static_cast<const TestStepWait*>(stepReferred))->getCondition();
260 if (tc.getCode() == "0") { // if regexp used, is not possible to detect this kind of errors
261 throw anna::RuntimeException(anna::functions::asString("Step number (%d) must refer to a 'wait for request' step (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
265 TestStepSendxml2e *step = new TestStepSendxml2e(this);
266 step->setMsgDataBlock(db);
267 step->setRealmNode(realm);
268 step->setWaitForRequestStepNumber(stepNumber); // -1 means, no reference
272 void TestCase::addSendxml2c(const anna::DataBlock &db, RealmNode *realm, int stepNumber) throw(anna::RuntimeException) {
274 assertMessage(db, false /* to client */);
276 TestStepSendxml2c *step = new TestStepSendxml2c(this);
277 step->setMsgDataBlock(db);
278 step->setRealmNode(realm);
282 void TestCase::addDelay(const anna::Millisecond &delay) throw(anna::RuntimeException) {
284 TestStepDelay *step = new TestStepDelay(this);
285 step->setDelay(delay);
289 void TestCase::addWait(bool fromEntity,
290 const std::string &code, const std::string &bitR, const std::string &hopByHop, const std::string &applicationId,
291 const std::string &sessionId, const std::string &resultCode,
292 const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw(anna::RuntimeException) {
294 std::string usedHopByHop = hopByHop;
295 TestStepWait *step = NULL;
297 // Check basic conditions:
299 if (resultCode != "")
300 throw anna::RuntimeException(anna::functions::asString("You cannot specify Result-Code (%s) for a wait condition of a diameter request message (test case %llu)", resultCode.c_str(), a_id), ANNA_FILE_LOCATION);
302 throw anna::RuntimeException(anna::functions::asString("You cannot specify Hop-by-hop (%s) for a wait condition of a diameter request message (test case %llu)", hopByHop.c_str(), a_id), ANNA_FILE_LOCATION);
305 if (hopByHop != "") {
306 if (hopByHop[0] == '#') {
308 throw anna::RuntimeException(anna::functions::asString("No steps has been programmed, step reference is nonsense (test case %llu)", a_id), ANNA_FILE_LOCATION);
310 int stepNumber = atoi(hopByHop.substr(1).c_str());
312 const TestStep *stepReferred = getStep(stepNumber);
314 throw anna::RuntimeException(anna::functions::asString("Step reference number (%d) do not exists (test case %llu)", stepNumber, a_id), ANNA_FILE_LOCATION);
316 if (stepReferred->getType() != TestStep::Type::Sendxml2e && stepReferred->getType() != TestStep::Type::Sendxml2c)
317 throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'sendxml2e' or 'sendxml2c' step (test case %llu)", a_id), ANNA_FILE_LOCATION);
319 const anna::DataBlock &db = (static_cast<const TestStepSendxml*>(stepReferred))->getMsgDataBlock();
320 bool isAnswer = anna::diameter::codec::functions::isAnswer(db);
322 throw anna::RuntimeException(anna::functions::asString("Step number must refer to a request message (test case %llu)", a_id), ANNA_FILE_LOCATION);
325 anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db);
326 usedHopByHop = anna::functions::asString(hbh);
327 step = new TestStepWait(this);
328 a_hopByHops[hbh /* always exists: is the info we calculated above */] = step;
333 if (!step) step = new TestStepWait(this);
334 step->setCondition(fromEntity, code, bitR, usedHopByHop, applicationId, sessionId, resultCode, msisdn, imsi, serviceContextId);
337 if (hasSameCondition(step->getCondition()))
338 anna::Logger::warning(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu). Are you sure ?", a_id), ANNA_FILE_LOCATION);
344 void TestCase::addWaitRegexp(bool fromEntity, const std::string ®exp) throw(anna::RuntimeException) {
347 TestStepWait *step = new TestStepWait(this);
348 step->setCondition(fromEntity, regexp);
351 if (hasSameCondition(step->getCondition()))
352 anna::Logger::warning(anna::functions::asString("The same wait condition has already been programmed in this test case (%llu). Are you sure ?", a_id), ANNA_FILE_LOCATION);
358 void TestCase::addCommand(const std::string &cmd) throw(anna::RuntimeException) {
361 TestStepCmd *step = new TestStepCmd(this);
362 step->setScript(cmd);
367 TestStepWait *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) throw() {
369 TestStepWait *result;
370 for (std::map<int/* step number*/, TestStep*>::const_iterator it = a_stepsIt /* current */; it != a_steps.end(); it++) {
371 if (it->second->getType() != TestStep::Type::Wait) continue;
372 if (it->second->isCompleted()) continue;
373 result = (TestStepWait*)(it->second);
374 if ((result->getCondition().receivedFromEntity() == waitFromEntity) && (result->fulfilled(message)))
381 const TestStep *TestCase::getStep(int stepNumber) const throw() {
382 std::map<int/* step number*/, TestStep*>::const_iterator it = a_steps.find(stepNumber);
383 if (it != a_steps.end()) return it->second;