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 //
9 #ifndef example_diameter_launcher_TestStep_hpp
10 #define example_diameter_launcher_TestStep_hpp
18 #include <anna/core/DataBlock.hpp>
19 #include <anna/xml/Node.hpp>
20 #include <anna/diameter/codec/Message.hpp>
23 #include <TestCondition.hpp>
51 int a_number; // step number used for xml (informational)
52 anna::Millisecond a_beginTimestamp; // unix time
53 anna::Millisecond a_endTimestamp; // unix time
54 bool a_executed; // used for interactive mode in order to not repeat a execution step if before completing, the user add interactive amount
56 void setBeginTimestamp(const anna::Millisecond &t) throw() { a_beginTimestamp = t; }
57 const anna::Millisecond &getBeginTimestamp() const throw() { return a_beginTimestamp; }
58 void setEndTimestamp(const anna::Millisecond &t) throw() { a_endTimestamp = t; }
59 const anna::Millisecond &getEndTimestamp() const throw() { return a_endTimestamp; }
61 void initialize(TestCase *testCase);
64 struct Type { enum _v { Unconfigured, Timeout, Sendxml2e, Sendxml2c, Delay, Wait, Cmd }; };
65 static const char* asText(const Type::_v type) throw();
67 TestStep(TestCase *testCase) : a_message(true), a_messageCodec(NULL), a_executed(false) { initialize(testCase); }
68 virtual ~TestStep() {;}
71 const Type::_v &getType() const throw() { return a_type; }
72 const int &getNumber() const throw() { return a_number; }
73 bool isCompleted() const throw() { return a_completed; }
75 bool execute() throw();
76 void complete() throw();
79 virtual anna::xml::Node* asXML(anna::xml::Node* parent) throw();
80 std::string asXMLString() throw();
87 // Message (not for all step types)
88 anna::DataBlock a_message;
89 anna::diameter::codec::Message *a_messageCodec; // used as helper and for traffic logs
90 bool decodeMessage(bool trust = false) throw(); // If trust=true: decoding the previously encoded message (sendxml sentences).
91 // The only error would be validation ones, and we are going to ignore them here.
93 virtual bool do_execute() throw() = 0; // returns true if next step must be executed
94 virtual void do_complete() throw() = 0; // end of transaction (delay/timeout expired, wait condition fulfilled, sending done)
95 // In all cases, the next step will be executed except 'timeout' which is asynchronous
96 // and will move to the next step just after timer creation (no complete waited)
97 virtual void do_reset() throw() = 0;
101 class TestStepTimeout : public TestStep {
103 anna::Millisecond a_timeout;
104 TestTimer *a_timer; // just in case i would need to cancel
107 TestStepTimeout(TestCase *testCase) : TestStep(testCase), a_timeout(0), a_timer(NULL) { a_type = Type::Timeout; }
110 void setTimeout(const anna::Millisecond &t) throw() { a_timeout = t; }
111 const anna::Millisecond &getTimeout() const throw() { return a_timeout; }
114 bool do_execute() throw();
115 void do_complete() throw(); // timeout reached, test case failed
116 void do_reset() throw();
117 anna::xml::Node* asXML(anna::xml::Node* parent) throw();
121 class TestStepSendxml : public TestStep {
124 // possible end points:
125 OriginHost *a_originHost;
127 // Step number reference ('wait for request' step)
128 int a_waitForRequestStepNumber;
131 bool a_expired; // a_endTimestamp will be the expiration reception timestamp
134 TestStepSendxml(TestCase *testCase) : TestStep(testCase),
137 a_waitForRequestStepNumber(-1) {;}
138 ~TestStepSendxml() {;}
141 void setOriginHost(OriginHost *host) throw() { a_originHost = host; }
142 OriginHost *getOriginHost() const throw() { return a_originHost; }
143 void setWaitForRequestStepNumber(int stepNumber) throw() { a_waitForRequestStepNumber = stepNumber; }
144 int getWaitForRequestStepNumber() const throw() { return a_waitForRequestStepNumber; }
145 void setMsgDataBlock(const anna::DataBlock &db) throw() { a_message = db; }
146 const anna::DataBlock &getMsgDataBlock() const throw() { return a_message; }
149 bool do_execute() throw();
150 void do_complete() throw() {;}
151 void do_reset() throw();
152 anna::xml::Node* asXML(anna::xml::Node* parent) throw();
155 class TestStepSendxml2e : public TestStepSendxml {
157 TestStepSendxml2e(TestCase *testCase) : TestStepSendxml(testCase) { a_type = Type::Sendxml2e; }
160 class TestStepSendxml2c : public TestStepSendxml {
162 TestStepSendxml2c(TestCase *testCase) : TestStepSendxml(testCase) { a_type = Type::Sendxml2c; }
166 class TestStepDelay : public TestStep {
167 anna::Millisecond a_delay;
168 TestTimer *a_timer; // just in case i would need to cancel
171 TestStepDelay(TestCase *testCase) : TestStep(testCase), a_delay(0), a_timer(NULL) { a_type = Type::Delay; }
174 void setDelay(const anna::Millisecond &d) throw() { a_delay = d; }
175 const anna::Millisecond &getDelay() const throw() { return a_delay; }
178 bool do_execute() throw();
179 void do_complete() throw(); // delay reached
180 void do_reset() throw();
181 anna::xml::Node* asXML(anna::xml::Node* parent) throw();
185 class TestStepWait : public TestStep {
187 TestCondition a_condition;
188 anna::diameter::comm::ClientSession *a_clientSession;
189 anna::diameter::comm::ServerSession *a_serverSession;
192 TestStepWait(TestCase *testCase) : TestStep(testCase) {
194 a_clientSession = NULL;
195 a_serverSession = NULL;
200 void setCondition(bool fromEntity,
201 const std::string &code, const std::string &bitR, const std::string &hopByHop, const std::string &applicationId,
202 const std::string &sessionId, const std::string &resultCode,
203 const std::string &msisdn, const std::string &imsi, const std::string &serviceContextId) throw();
204 void setCondition(bool fromEntity, const std::string ®exp) throw();
206 void setClientSession(anna::diameter::comm::ClientSession *cs) throw() { a_clientSession = cs; }
207 void setServerSession(anna::diameter::comm::ServerSession *ss) throw() { a_serverSession = ss; }
208 anna::diameter::comm::ClientSession *getClientSession() const throw() { return a_clientSession; }
209 anna::diameter::comm::ServerSession *getServerSession() const throw() { return a_serverSession; }
211 const TestCondition &getCondition() const throw() { return a_condition; }
212 //void setMsgDataBlock(const anna::DataBlock &db) throw() { a_message = db; }
213 bool fulfilled(const anna::DataBlock &db/*, bool matchSessionId = true*/) throw();
214 const anna::DataBlock &getMsgDataBlock() const throw() { return a_message; }
218 bool do_execute() throw(); // this will be executed when test case starts (at least we could measure the time until condition is fulfilled)
219 void do_complete() throw(); // condition fulfilled
220 void do_reset() throw();
221 anna::xml::Node* asXML(anna::xml::Node* parent) throw();
225 class TestStepCmd : public TestStep {
227 std::string a_script;
228 std::thread a_thread;
229 bool a_threadRunning;
230 bool a_threadDeprecated;
232 std::string a_errorMsg;
233 //std::string a_output; // for POPEN
238 TestStepCmd(TestCase *testCase) : TestStep(testCase), a_threadRunning(false), a_threadDeprecated(false), a_resultCode(-2)/*, a_output("")*/, a_errorMsg(""), a_childPid(-1) { a_type = Type::Cmd; }
241 void setThreadRunning(bool running) throw() { a_threadRunning = running; }
243 void setResultCode(int rc) throw() { a_resultCode = rc; }
244 int getResultCode() const throw() { return a_resultCode; }
245 void setErrorMsg(const std::string &em) throw() { a_errorMsg = em; }
246 const std::string &getErrorMsg() const throw() { return a_errorMsg; }
247 //void appendOutput(const std::string &output) throw() { a_output += output; }
248 //const std::string &getOutput() const throw() { return a_output; }
249 void setChildPid(pid_t pid) throw() { a_childPid = pid; }
250 const pid_t &getChildPid() const throw() { return a_childPid; }
252 void setScript(const std::string &script) throw() { a_script = script; }
253 const std::string &getScript() const throw() { return a_script; }
256 bool do_execute() throw();
257 void do_complete() throw();
258 void do_reset() throw();
259 anna::xml::Node* asXML(anna::xml::Node* parent) throw();