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 //
13 #include <anna/testing/TestDiameterCondition.hpp>
15 #include <anna/xml/Node.hpp>
16 #include <anna/xml/Compiler.hpp>
17 #include <anna/diameter/defines.hpp>
18 #include <anna/diameter/codec/functions.hpp>
19 #include <anna/diameter/codec/Message.hpp>
20 #include <anna/diameter/helpers/base/functions.hpp>
22 #include <anna/diameter/helpers/dcca/defines.hpp>
23 #include <anna/diameter/helpers/dcca/functions.hpp>
24 #include <anna/core/util/defines.hpp>
25 #include <anna/core/util/RegularExpression.hpp>
28 using namespace anna::testing;
31 const char* TestDiameterCondition::asText(const Type::_v type)
33 static const char* text [] = { "RegexpXml", "RegexpHex", "Fields" };
37 bool TestDiameterCondition::exists() const {
38 if (a_type != Type::Fields) return (getRegexp() != "");
40 return (a_code != "" || a_bitR != "" || a_hopByHop != "" || a_applicationId != "" || a_sessionId != "" || a_resultCode != "" || a_msisdn != "" || a_imsi != "" || a_serviceContextId != "");
43 bool TestDiameterCondition::comply(const anna::DataBlock &message) const {
45 if (a_type == Type::RegexpXml) {
46 anna::diameter::codec::Message codecMsg;
47 try { codecMsg.decode(message); } catch (anna::RuntimeException &ex) { ex.trace(); }
48 return codecMsg.isLike(getRegexp());
50 else if (a_type == Type::RegexpHex) {
51 anna::RegularExpression re(getRegexp());
52 return re.isLike(anna::functions::asHexString(message));
57 anna::diameter::CommandId cid;
59 if (a_code != "" || a_bitR != "") {
61 cid = anna::diameter::codec::functions::getCommandId(message);
63 catch (anna::RuntimeException &) { return false; }
67 compare = anna::functions::asString(cid.first);
68 if (a_code != compare) return false;
72 compare = (cid.second ? "1":"0");
73 if (a_bitR != compare) return false;
76 if (a_hopByHop != "") {
78 anna::diameter::HopByHop h = anna::diameter::codec::functions::getHopByHop(message);
79 compare = anna::functions::asString(h);
81 catch (anna::RuntimeException &) { return false; }
82 if (a_hopByHop != compare) return false;
85 if (a_applicationId != "") {
87 anna::diameter::ApplicationId a = anna::diameter::codec::functions::getApplicationId(message);
88 compare = anna::functions::asString(a);
90 catch (anna::RuntimeException &) { return false; }
91 if (a_applicationId != compare) return false;
94 //if (matchSessionId) {
95 if (a_sessionId != "") {
97 compare = anna::diameter::helpers::base::functions::getSessionId(message);
99 catch (anna::RuntimeException &) { return false; }
100 if (a_sessionId != compare) return false;
104 if (a_resultCode != "") {
106 anna::U32 rc = anna::diameter::helpers::base::functions::getResultCode(message);
107 compare = anna::functions::asString(rc);
109 catch (anna::RuntimeException &) { return false; }
110 if (a_resultCode != compare) return false;
113 if (a_msisdn != "") {
115 compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
117 catch (anna::RuntimeException &) { return false; }
118 if (a_msisdn != compare) return false;
123 compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
125 catch (anna::RuntimeException &) { return false; }
126 if (a_imsi != compare) return false;
129 if (a_serviceContextId != "") {
131 compare = anna::diameter::helpers::dcca::functions::getServiceContextId(message);
133 catch (anna::RuntimeException &) { return false; }
134 if (a_serviceContextId != compare) return false;
140 anna::xml::Node* TestDiameterCondition::asXML(anna::xml::Node* parent) const
142 anna::xml::Node* result = parent->createChild("TestDiameterCondition");
143 if (!exists()) return result;
145 if (a_type == Type::RegexpXml) {
146 if (getRegexp() != "") result->createAttribute("RegexpXml", getRegexp());
148 else if (a_type == Type::RegexpHex) {
149 if (getRegexp() != "") result->createAttribute("RegexpHex", getRegexp());
152 if (a_code != "") result->createAttribute("Code", atoi(a_code.c_str()));
153 if (a_bitR != "") result->createAttribute("BitR", ((a_bitR == "1") ? "yes":"no"));
154 if (a_hopByHop != "") result->createAttribute("HopByHop", atoll(a_hopByHop.c_str()));
155 if (a_applicationId != "") result->createAttribute("ApplicationId", atoll(a_applicationId.c_str()));
156 if (a_sessionId != "") result->createAttribute("SessionId", a_sessionId);
157 if (a_resultCode != "") result->createAttribute("ResultCode", atoi(a_resultCode.c_str()));
158 if (a_msisdn != "") result->createAttribute("Msisdn", a_msisdn);
159 if (a_imsi != "") result->createAttribute("Imsi", a_imsi);
160 if (a_serviceContextId != "") result->createAttribute("ServiceContextId", a_serviceContextId);
163 result->createAttribute("ExpectedSource", a_rcvFromEntity ? "entity":"client");