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 //
18 #include <anna/xml/Compiler.hpp>
19 #include <anna/diameter/defines.hpp>
20 #include <anna/diameter/helpers/dcca/defines.hpp>
21 #include <anna/diameter/codec/functions.hpp>
22 #include <anna/diameter/helpers/base/functions.hpp>
23 #include <anna/diameter/helpers/dcca/functions.hpp>
24 #include <anna/core/util/Millisecond.hpp>
25 #include <anna/core/tracing/Logger.hpp>
28 #include <TestCase.hpp>
29 #include <TestManager.hpp>
32 void TestCase::DebugSummary::addHint(const std::string &hint) throw() {
34 event.Timestamp = anna::functions::millisecond();
36 a_events.push_back(event);
39 void TestCase::DebugSummary::clear() throw() {
43 anna::xml::Node* TestCase::DebugSummary::asXML(anna::xml::Node* parent) const throw() {
44 anna::xml::Node* result = parent->createChild("DebugSummary");
46 std::vector<event_t>::const_iterator it;
47 for (it = a_events.begin(); it != a_events.end(); it++) {
48 anna::xml::Node* event = result->createChild("Event");
49 event->createAttribute("Timestamp", (*it).Timestamp.asString());
50 event->createAttribute("Hint", (*it).Hint);
57 TestCase::~TestCase() {
58 reset(true); // hard reset
59 std::vector<TestStep*>::const_iterator it;
60 for (it = a_steps.begin(); it != a_steps.end(); it++) delete (*it);
63 const char* TestCase::asText(const State::_v state)
65 static const char* text [] = { "Initialized", "InProgress", "Failed", "Success" };
69 anna::xml::Node* TestCase::asXML(anna::xml::Node* parent) const
71 anna::xml::Node* result = parent->createChild("TestCase");
73 result->createAttribute("Id", a_id);
74 result->createAttribute("State", asText(a_state));
75 result->createAttribute("StartTimestamp", a_startTime.asString());
76 int steps = a_steps.size();
78 result->createAttribute("NumberOfTestSteps", steps);
79 std::vector<TestStep*>::const_iterator it;
80 for (it = a_steps.begin(); it != a_steps.end(); it++) {
85 if (a_debugSummary.events()) {
86 a_debugSummary.asXML(result);
92 std::string TestCase::asXMLString() const throw() {
93 anna::xml::Node root("root");
94 return anna::xml::Compiler().apply(asXML(&root));
97 bool TestCase::hasSameCondition(const TestCondition &condition) const throw() {
98 std::vector<TestStep*>::const_iterator it;
100 for (it = a_steps.begin(); it != a_steps.end(); it++) {
101 if ((*it)->getType() != TestStep::Type::Wait) continue;
102 step = (TestStepWait *)(*it);
103 if (step->getCondition() == condition) return true;
109 void TestCase::setState(const State::_v &state) throw() {
111 State::_v previousState = a_state;
112 if (state == previousState) return;
114 TestManager &testManager = TestManager::instantiate();
116 if (!testManager.getDumpReports()) return;
117 // report file name: cycle-<cycle id>.testcase-<test case id>.xml
119 // FORMAT: We tabulate the cycle and test case in order to ease ordering of files by mean ls:
120 int cycles = testManager.getPoolRepeats();
121 int tests = testManager.tests();
122 int cyclesWidth = (cycles<=0) ? 3 /* 1000 cycles !! */: ((int) log10 ((double) cycles) + 1);
123 int testsWidth = (tests<=0) ? 9 /* subscribers */: ((int) log10 ((double) tests) + 1);
124 std::stringstream format;
125 format << "/cycle-%0" << cyclesWidth << "d.testcase-%0" << testsWidth << "llu.xml";
128 std::string file = testManager.getReportsDirectory() + anna::functions::asString(format.str().c_str(), testManager.getPoolCycle(), a_id);
130 out.open(file.c_str(), std::ofstream::out | std::ofstream::app);
131 if(out.is_open() == false) {
132 std::string msg("Error opening '");
134 msg += "' for writting";
135 anna::Logger::error(msg, ANNA_FILE_LOCATION);
138 out << asXMLString() << std::endl;
143 // Count in-progress test cases:
145 testManager.setInProgressCountDelta(1);
147 else if (previousState == State::InProgress){
148 testManager.setInProgressCountDelta(-1);
152 bool TestCase::done() throw() {
153 if (a_stepsIt == a_steps.end()) {
154 setState(State::Success);
161 bool TestCase::process() throw() {
162 if (a_steps.size() == 0) {
163 LOGWARNING(anna::Logger::warning(anna::functions::asString("Test case %llu is empty, nothing to execute", a_id), ANNA_FILE_LOCATION));
167 LOGDEBUG(anna::Logger::debug(anna::functions::asString("Test case %llu is finished, nothing done until soft-reset", a_id), ANNA_FILE_LOCATION));
171 if (a_state == State::Initialized) {
172 a_stepsIt = a_steps.begin();
173 setState(State::InProgress);
175 // For 'wait' steps (not really useful, but better than nothing: begin timestamp on test case start timestamp...):
176 a_startTime = anna::functions::millisecond();
179 // Check end of the test case:
180 if (done()) return false;
182 bool somethingDone = false;
183 while ((*a_stepsIt)->execute()) { // executes returns 'true' if the next step must be also executed (execute until can't stand no more)
185 // Check end of the test case:
186 if (done()) return false;
187 somethingDone = true;
190 return somethingDone;
193 bool TestCase::reset(bool hard) throw() {
195 // Soft reset if finished:
196 if (!hard /* is soft reset */ && !isFinished()) return false;
198 // Clean stage ////////////////////////////
200 std::vector<TestStep*>::iterator it;
201 for (it = a_steps.begin(); it != a_steps.end(); it++)
204 a_debugSummary.clear();
207 setState(State::Initialized);
212 void TestCase::assertInitialized() const throw(anna::RuntimeException) {
213 if (a_state != State::Initialized)
214 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);
217 void TestCase::assertMessage(const anna::DataBlock &db, bool toEntity) throw(anna::RuntimeException) {
219 bool isRequest = anna::diameter::codec::functions::isRequest(db);
220 bool registerSessionId = ((isRequest && toEntity) || (!isRequest && !toEntity) /* (*) */);
221 // (*) we register answers Session-Id assuming that we will know the Session-Id values created by the client (OCS)
222 // This is another solution for TODO(***) regarding diameter server testing. No tsure about the final implementation.
226 anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db);
227 if (a_hopByHops.find(hbh) != a_hopByHops.end())
228 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);
229 a_hopByHops[hbh] = NULL; // may be assigned to a wait condition
232 if (registerSessionId)
233 TestManager::instantiate().registerSessionId(anna::diameter::helpers::base::functions::getSessionId(db), this);
236 void TestCase::addTimeout(const anna::Millisecond &timeout) throw(anna::RuntimeException) {
238 TestStepTimeout *step = new TestStepTimeout(this);
239 step->setTimeout(timeout);
240 a_steps.push_back(step);
243 void TestCase::addSendxml2e(const anna::DataBlock &db, RealmNode *realm, int stepNumber) throw(anna::RuntimeException) {
245 assertMessage(db, true /* to entity */);
247 if (stepNumber != -1) {
248 int steps = a_steps.size();
249 int stepIndx = stepNumber - 1;
250 if ((stepIndx < 0) || (stepIndx > (a_steps.size()-1)))
251 throw anna::RuntimeException(anna::functions::asString("Step number out of range (test case %llu)", a_id), ANNA_FILE_LOCATION);
253 TestStep *stepReferred = a_steps[stepIndx];
254 if (stepReferred->getType() != TestStep::Type::Wait)
255 throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'wait' step (test case %llu)", a_id), ANNA_FILE_LOCATION);
257 const TestCondition &tc = (static_cast<TestStepWait*>(stepReferred))->getCondition();
258 if (tc.getCode() == "0") { // if regexp used, is not possible to detect this kind of errors
259 throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'wait for request' step (test case %llu)", a_id), ANNA_FILE_LOCATION);
263 TestStepSendxml2e *step = new TestStepSendxml2e(this);
264 step->setMsgDataBlock(db);
265 step->setRealmNode(realm);
266 step->setWaitForRequestStepNumber(stepNumber); // -1 means, no reference
267 a_steps.push_back(step);
270 void TestCase::addSendxml2c(const anna::DataBlock &db, RealmNode *realm, int stepNumber) throw(anna::RuntimeException) {
272 assertMessage(db, false /* to client */);
274 TestStepSendxml2c *step = new TestStepSendxml2c(this);
275 step->setMsgDataBlock(db);
276 step->setRealmNode(realm);
277 a_steps.push_back(step);
280 void TestCase::addDelay(const anna::Millisecond &delay) throw(anna::RuntimeException) {
282 TestStepDelay *step = new TestStepDelay(this);
283 step->setDelay(delay);
284 a_steps.push_back(step);
287 void TestCase::addWait(bool fromEntity,
288 const std::string &code, const std::string &bitR, const std::string &resultCode, const std::string &sessionId,
289 const std::string &hopByHop, const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw(anna::RuntimeException) {
291 std::string usedHopByHop = hopByHop;
292 TestStepWait *step = NULL;
294 // Check basic conditions:
296 if (resultCode != "")
297 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);
299 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);
302 if (hopByHop != "") {
303 if (hopByHop[0] == '#') {
304 int steps = a_steps.size();
306 throw anna::RuntimeException(anna::functions::asString("No steps has been programmed, step reference is nonsense (test case %llu)", a_id), ANNA_FILE_LOCATION);
308 int stepNumber = atoi(hopByHop.substr(1).c_str());
309 int stepIndx = stepNumber - 1;
310 if ((stepIndx < 0) || (stepIndx > (steps-1)))
311 throw anna::RuntimeException(anna::functions::asString("Step reference number %d out of range [1-%d]", stepNumber, steps), ANNA_FILE_LOCATION);
313 TestStep *stepReferred = a_steps[stepIndx];
314 if (stepReferred->getType() != TestStep::Type::Sendxml2e && stepReferred->getType() != TestStep::Type::Sendxml2c)
315 throw anna::RuntimeException(anna::functions::asString("Step number must refer to a 'sendxml2e' or 'sendxml2c' step (test case %llu)", a_id), ANNA_FILE_LOCATION);
317 const anna::DataBlock &db = (static_cast<TestStepSendxml*>(stepReferred))->getMsgDataBlock();
318 bool isAnswer = anna::diameter::codec::functions::isAnswer(db);
320 throw anna::RuntimeException(anna::functions::asString("Step number must refer to a request message (test case %llu)", a_id), ANNA_FILE_LOCATION);
323 anna::diameter::HopByHop hbh = anna::diameter::codec::functions::getHopByHop(db);
324 usedHopByHop = anna::functions::asString(hbh);
325 step = new TestStepWait(this);
326 a_hopByHops[hbh /* always exists: is the info we calculated above */] = step;
331 if (!step) step = new TestStepWait(this);
332 step->setCondition(fromEntity, code, bitR, resultCode, sessionId, usedHopByHop, msisdn, imsi, serviceContextId);
335 if (hasSameCondition(step->getCondition()))
336 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);
339 a_steps.push_back(step);
342 void TestCase::addWaitRegexp(bool fromEntity, const std::string ®exp) throw(anna::RuntimeException) {
345 TestStepWait *step = new TestStepWait(this);
346 step->setCondition(fromEntity, regexp);
349 if (hasSameCondition(step->getCondition()))
350 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);
353 a_steps.push_back(step);
356 void TestCase::addCmd(const std::string &script, const std::string ¶meters) throw(anna::RuntimeException) {
359 TestStepCmd *step = new TestStepCmd(this);
360 step->setScript(script);
361 step->setParameters(parameters);
363 a_steps.push_back(step);
366 TestStepWait *TestCase::searchNextWaitConditionFulfilled(const anna::DataBlock &message, bool waitFromEntity) throw() {
368 TestStepWait *result;
369 for (std::vector<TestStep*>::const_iterator it = a_stepsIt /* current */; it != a_steps.end(); it++) {
370 if ((*it)->getType() != TestStep::Type::Wait) continue;
371 if ((*it)->isCompleted()) continue;
372 result = (TestStepWait*)(*it);
373 if ((result->getCondition().receivedFromEntity() == waitFromEntity) && (result->fulfilled(message)))
380 const TestStep *TestCase::getStep(int stepNumber) const throw() {
381 int stepIndx = stepNumber - 1;
382 if ((stepIndx < 0) || (stepIndx > (a_steps.size()-1))) return NULL;
383 std::vector<TestStep*>::const_iterator it = (a_steps.begin() + stepNumber);