Ensures normalization on waitfe/fc-xml operations
[anna.git] / source / testing / TestCondition.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 // Standard
10 #include <algorithm>
11
12 // Project
13 #include <anna/testing/TestDiameterCondition.hpp>
14
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>
21
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>
26
27
28 using namespace anna::testing;
29
30
31 const char* TestDiameterCondition::asText(const Type::_v type)
32 throw() {
33   static const char* text [] = { "RegexpXml", "RegexpHex", "Fields" };
34   return text [type];
35 }
36
37 bool TestDiameterCondition::exists() const throw() {
38   if (a_type != Type::Fields) return (getRegexp() != "");
39
40   return (a_code != "" || a_bitR != "" || a_hopByHop != "" || a_applicationId != "" || a_sessionId != "" || a_resultCode != "" || a_msisdn != "" || a_imsi != "" || a_serviceContextId != "");
41 }
42
43 bool TestDiameterCondition::comply(const anna::DataBlock &message) const throw() {
44
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());
49   }
50   else if (a_type == Type::RegexpHex) {
51     anna::RegularExpression re(getRegexp());
52     return re.isLike(anna::functions::asHexString(message));
53   }
54
55   // Basic
56   std::string compare;
57   anna::diameter::CommandId cid;
58
59   if (a_code != "" || a_bitR != "") {
60     try {
61       cid = anna::diameter::codec::functions::getCommandId(message);
62     }
63     catch (anna::RuntimeException &) { return false; }
64   }
65
66   if (a_code != "") {
67     compare = anna::functions::asString(cid.first);
68     if (a_code != compare) return false;
69   }
70
71   if (a_bitR != "") {
72     compare = (cid.second ? "1":"0");
73     if (a_bitR != compare) return false;
74   }
75
76   if (a_hopByHop != "") {
77     try {
78       anna::diameter::HopByHop h = anna::diameter::codec::functions::getHopByHop(message);
79       compare = anna::functions::asString(h);
80     }
81     catch (anna::RuntimeException &) { return false; }
82     if (a_hopByHop != compare) return false;
83   }
84
85   if (a_applicationId != "") {
86     try {
87       anna::diameter::ApplicationId a = anna::diameter::codec::functions::getApplicationId(message);
88       compare = anna::functions::asString(a);
89     }
90     catch (anna::RuntimeException &) { return false; }
91     if (a_applicationId != compare) return false;
92   }
93
94   //if (matchSessionId) {
95   if (a_sessionId != "") {
96     try {
97       compare = anna::diameter::helpers::base::functions::getSessionId(message);
98     }
99     catch (anna::RuntimeException &) { return false; }
100     if (a_sessionId != compare) return false;
101   }
102   //}
103
104   if (a_resultCode != "") {
105     try {
106       anna::U32 rc = anna::diameter::helpers::base::functions::getResultCode(message);
107       compare = anna::functions::asString(rc);
108     }
109     catch (anna::RuntimeException &) { return false; }
110     if (a_resultCode != compare) return false;
111   }
112
113   if (a_msisdn != "") {
114     try {
115       compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
116     }
117     catch (anna::RuntimeException &) { return false; }
118     if (a_msisdn != compare) return false;
119   }
120
121   if (a_imsi != "") {
122     try {
123       compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
124     }
125     catch (anna::RuntimeException &) { return false; }
126     if (a_imsi != compare) return false;
127   }
128
129   if (a_serviceContextId != "") {
130     try {
131       compare = anna::diameter::helpers::dcca::functions::getServiceContextId(message);
132     }
133     catch (anna::RuntimeException &) { return false; }
134     if (a_serviceContextId != compare) return false;
135   }
136
137   return true;
138 }
139
140 anna::xml::Node* TestDiameterCondition::asXML(anna::xml::Node* parent) const
141 throw() {
142   anna::xml::Node* result = parent->createChild("TestDiameterCondition");
143   if (!exists()) return result;
144
145   if (a_type == Type::RegexpXml) {
146     if (getRegexp() != "") result->createAttribute("RegexpXml", getRegexp());
147   }
148   else if (a_type == Type::RegexpHex) {
149     if (getRegexp() != "") result->createAttribute("RegexpHex", getRegexp());
150   }
151   else {
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);
161   }
162
163   result->createAttribute("ExpectedSource", a_rcvFromEntity ? "entity":"client");
164
165   return result;
166 }