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 //
12 #include <anna/testing/TestCondition.hpp>
14 #include <anna/xml/Node.hpp>
15 #include <anna/xml/Compiler.hpp>
16 #include <anna/diameter/defines.hpp>
17 #include <anna/diameter/codec/functions.hpp>
18 #include <anna/diameter/codec/Message.hpp>
19 #include <anna/diameter/helpers/base/functions.hpp>
21 #include <anna/diameter/helpers/dcca/defines.hpp>
22 #include <anna/diameter/helpers/dcca/functions.hpp>
23 #include <anna/core/util/defines.hpp>
26 using namespace anna::testing;
29 const char* TestCondition::asText(const Type::_v type)
31 static const char* text [] = { "Generic", "Basic" };
35 bool TestCondition::exists() const throw() {
36 if (a_type == Type::Generic)
37 return (a_regexp != "");
39 return (a_code != "" || a_bitR != "" || a_hopByHop != "" || a_applicationId != "" || a_sessionId != "" || a_resultCode != "" || a_msisdn != "" || a_imsi != "" || a_serviceContextId != "");
43 bool anna::testing::operator==(const TestCondition &c1, const TestCondition &c2) throw() {
45 if (c1.getType() != c2.getType()) return false;
47 if (c1.getType() == TestCondition::Type::Generic) {
48 if (c1.getRegexp() != c2.getRegexp()) return false;
51 if (c1.getCode() != c2.getCode()) return false;
52 if (c1.getBitR() != c2.getBitR()) return false;
53 if (c1.getHopByHop() != c2.getHopByHop()) return false;
54 if (c1.getApplicationId() != c2.getApplicationId()) return false;
55 if (c1.getSessionId() != c2.getSessionId()) return false;
56 if (c1.getResultCode() != c2.getResultCode()) return false;
57 if (c1.getMsisdn() != c2.getMsisdn()) return false;
58 if (c1.getImsi() != c2.getImsi()) return false;
59 if (c1.getServiceContextId() != c2.getServiceContextId()) return false;
66 bool TestCondition::comply(const anna::DataBlock &message/*, bool matchSessionId*/) const throw() {
68 if (a_type == Type::Generic) {
69 anna::diameter::codec::Message codecMsg;
70 try { codecMsg.decode(message); } catch (anna::RuntimeException &ex) { ex.trace(); }
71 return codecMsg.isLike(a_regexp);
76 anna::diameter::CommandId cid;
78 if (a_code != "" || a_bitR != "") {
80 cid = anna::diameter::codec::functions::getCommandId(message);
82 catch (anna::RuntimeException &) { return false; }
86 compare = anna::functions::asString(cid.first);
87 if (a_code != compare) return false;
91 compare = (cid.second ? "1":"0");
92 if (a_bitR != compare) return false;
95 if (a_hopByHop != "") {
97 anna::diameter::HopByHop h = anna::diameter::codec::functions::getHopByHop(message);
98 compare = anna::functions::asString(h);
100 catch (anna::RuntimeException &) { return false; }
101 if (a_hopByHop != compare) return false;
104 if (a_applicationId != "") {
106 anna::diameter::ApplicationId a = anna::diameter::codec::functions::getApplicationId(message);
107 compare = anna::functions::asString(a);
109 catch (anna::RuntimeException &) { return false; }
110 if (a_applicationId != compare) return false;
113 //if (matchSessionId) {
114 if (a_sessionId != "") {
116 compare = anna::diameter::helpers::base::functions::getSessionId(message);
118 catch (anna::RuntimeException &) { return false; }
119 if (a_sessionId != compare) return false;
123 if (a_resultCode != "") {
125 anna::U32 rc = anna::diameter::helpers::base::functions::getResultCode(message);
126 compare = anna::functions::asString(rc);
128 catch (anna::RuntimeException &) { return false; }
129 if (a_resultCode != compare) return false;
132 if (a_msisdn != "") {
134 compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
136 catch (anna::RuntimeException &) { return false; }
137 if (a_msisdn != compare) return false;
142 compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
144 catch (anna::RuntimeException &) { return false; }
145 if (a_imsi != compare) return false;
148 if (a_serviceContextId != "") {
150 compare = anna::diameter::helpers::dcca::functions::getServiceContextId(message);
152 catch (anna::RuntimeException &) { return false; }
153 if (a_serviceContextId != compare) return false;
159 anna::xml::Node* TestCondition::asXML(anna::xml::Node* parent) const
161 anna::xml::Node* result = parent->createChild("TestCondition");
162 if (!exists()) return result;
164 if (a_type == Type::Generic) {
165 if (a_regexp != "") result->createAttribute("Regexp", a_regexp);
168 if (a_code != "") result->createAttribute("Code", atoi(a_code.c_str()));
169 if (a_bitR != "") result->createAttribute("BitR", ((a_bitR == "1") ? "yes":"no"));
170 if (a_hopByHop != "") result->createAttribute("HopByHop", atoll(a_hopByHop.c_str()));
171 if (a_applicationId != "") result->createAttribute("ApplicationId", atoll(a_applicationId.c_str()));
172 if (a_sessionId != "") result->createAttribute("SessionId", a_sessionId);
173 if (a_resultCode != "") result->createAttribute("ResultCode", atoi(a_resultCode.c_str()));
174 if (a_msisdn != "") result->createAttribute("Msisdn", a_msisdn);
175 if (a_imsi != "") result->createAttribute("Imsi", a_imsi);
176 if (a_serviceContextId != "") result->createAttribute("ServiceContextId", a_serviceContextId);
179 result->createAttribute("ExpectedSource", a_rcvFromEntity ? "entity":"client");