System test feature
[anna.git] / example / diameter / launcher / 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
11 // Project
12 #include <anna/xml/Node.hpp>
13 #include <anna/xml/Compiler.hpp>
14 #include <anna/diameter/defines.hpp>
15 #include <anna/diameter/codec/functions.hpp>
16 #include <anna/diameter/helpers/base/functions.hpp>
17
18 #include <anna/diameter/helpers/dcca/defines.hpp>
19 #include <anna/diameter/helpers/dcca/functions.hpp>
20 #include <anna/core/util/defines.hpp>
21
22 // Process
23 #include <TestCondition.hpp>
24 #include <Launcher.hpp>
25
26
27 const char* TestCondition::asText(const Type::_v type)
28 throw() {
29   static const char* text [] = { "Generic", "Basic" };
30   return text [type];
31 }
32
33 bool TestCondition::exists() const throw() {
34   if (a_type == Type::Generic)
35     return (a_regexp != "");
36   else
37     return (a_code != "" || a_bitR != "" || a_resultCode != "" || a_sessionId != "" || a_hopByHop != "" || a_msisdn != "" || a_imsi != "" || a_serviceContextId != "");
38 }
39
40 bool operator==(const TestCondition &c1, const TestCondition &c2) throw() {
41
42   if (c1.a_type != c2.a_type) return false;
43
44   if (c1.a_type == TestCondition::Type::Generic) {
45     if (c1.a_regexp != c2.a_regexp) return false;
46   }
47   else {
48     if (c1.a_code != c2.a_code) return false;
49     if (c1.a_bitR != c2.a_bitR) return false;
50     if (c1.a_resultCode != c2.a_resultCode) return false;
51     if (c1.a_sessionId != c2.a_sessionId) return false;
52     if (c1.a_hopByHop != c2.a_hopByHop) return false;
53     if (c1.a_msisdn != c2.a_msisdn) return false;
54     if (c1.a_imsi != c2.a_imsi) return false;
55     if (c1.a_serviceContextId != c2.a_serviceContextId) return false;
56   }
57
58   return true;
59 }
60
61 bool TestCondition::comply(const anna::DataBlock &message/*, bool matchSessionId*/) const throw() {
62
63   if (a_type == Type::Generic) {
64     Launcher& my_app = static_cast <Launcher&>(anna::app::functions::getApp());
65     static anna::diameter::codec::Message codecMsg(my_app.getCodecEngine());
66     try {
67       codecMsg.decode(message);
68     }
69     catch (anna::RuntimeException &ex) {
70       ex.trace();
71     }
72
73     return codecMsg.isLike(a_regexp);
74   }
75
76   // Basic
77   std::string compare;
78   anna::diameter::CommandId cid;
79
80   if (a_code != "" || a_bitR != "") {
81     try {
82       cid = anna::diameter::codec::functions::getCommandId(message);
83     }
84     catch (anna::RuntimeException &) { return false; }
85   }
86
87   if (a_code != "") {
88     compare = anna::functions::asString(cid.first);
89     if (a_code != compare) return false;
90   }
91
92   if (a_bitR != "") {
93     compare = (cid.first ? "1":"0");
94     if (a_bitR != compare) return false;
95   }
96
97   if (a_resultCode != "") {
98     try {
99       anna::U32 rc = anna::diameter::helpers::base::functions::getResultCode(message);
100       compare = anna::functions::asString(rc);
101     }
102     catch (anna::RuntimeException &) { return false; }
103     if (a_resultCode != compare) return false;
104   }
105
106   //if (matchSessionId) {
107     if (a_sessionId != "") {
108       try {
109         compare = anna::diameter::helpers::base::functions::getSessionId(message);
110       }
111       catch (anna::RuntimeException &) { return false; }
112       if (a_sessionId != compare) return false;
113     }
114   //}
115
116   if (a_hopByHop != "") {
117     try {
118       anna::diameter::HopByHop h = anna::diameter::codec::functions::getHopByHop(message);
119       compare = anna::functions::asString(h);
120     }
121     catch (anna::RuntimeException &) { return false; }
122     if (a_hopByHop != compare) return false;
123   }
124
125   if (a_msisdn != "") {
126     try {
127       compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_E164);
128     }
129     catch (anna::RuntimeException &) { return false; }
130     if (a_msisdn != compare) return false;
131   }
132
133   if (a_imsi != "") {
134     try {
135       compare = anna::diameter::helpers::dcca::functions::getSubscriptionIdData(message, anna::diameter::helpers::dcca::AVPVALUES__Subscription_Id_Type::END_USER_IMSI);
136     }
137     catch (anna::RuntimeException &) { return false; }
138     if (a_imsi != compare) return false;
139   }
140
141   if (a_serviceContextId != "") {
142     try {
143       compare = anna::diameter::helpers::dcca::functions::getServiceContextId(message);
144     }
145     catch (anna::RuntimeException &) { return false; }
146     if (a_serviceContextId != compare) return false;
147   }
148
149   return true;
150 }
151
152 anna::xml::Node* TestCondition::asXML(anna::xml::Node* parent) const
153 throw() {
154   anna::xml::Node* result = parent->createChild("TestCondition");
155   if (!exists()) return result;
156
157   if (a_type == Type::Generic) {
158     if (a_regexp != "") result->createAttribute("Regexp", a_regexp);
159   }
160   else {
161     if (a_code != "") result->createAttribute("Code", atoi(a_code.c_str()));
162     if (a_bitR != "") result->createAttribute("BitR", ((a_bitR == "1") ? "yes":"no"));
163     if (a_sessionId != "") result->createAttribute("SessionId", a_sessionId);
164     if (a_resultCode != "") result->createAttribute("ResultCode", atoi(a_resultCode.c_str()));
165     if (a_hopByHop != "") result->createAttribute("HopByHop", atoll(a_hopByHop.c_str()));
166     if (a_msisdn != "") result->createAttribute("Msisdn", a_msisdn);
167     if (a_imsi != "") result->createAttribute("Imsi", a_imsi);
168     if (a_serviceContextId != "") result->createAttribute("ServiceContextId", a_serviceContextId);
169   }
170
171   result->createAttribute("ExpectedSource", a_rcvFromEntity ? "entity":"client");
172
173   return result;
174 }