Line data Source code
1 : // Copyright (c) 2012-2021 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/eventdispatcher
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : // self
21 : //
22 : #include "catch_main.h"
23 :
24 :
25 : // eventdispatcher lib
26 : //
27 : #include <eventdispatcher/message.h>
28 :
29 :
30 : // C lib
31 : //
32 : #include <unistd.h>
33 :
34 :
35 :
36 5 : CATCH_TEST_CASE("message", "[message]")
37 : {
38 6 : CATCH_START_SECTION("Simple message fields")
39 : {
40 2 : ed::message msg;
41 :
42 : // SENT FROM SERVER
43 : //
44 1 : CATCH_REQUIRE(msg.get_sent_from_server().empty());
45 1 : msg.set_sent_from_server("remote");
46 1 : CATCH_REQUIRE_FALSE(msg.get_sent_from_server().empty());
47 1 : CATCH_REQUIRE(msg.get_sent_from_server() == "remote");
48 :
49 : // SENT FROM SERVICE
50 : //
51 1 : CATCH_REQUIRE(msg.get_sent_from_service().empty());
52 1 : msg.set_sent_from_service("firewall");
53 1 : CATCH_REQUIRE_FALSE(msg.get_sent_from_service().empty());
54 1 : CATCH_REQUIRE(msg.get_sent_from_service() == "firewall");
55 :
56 : // SERVER
57 : //
58 1 : CATCH_REQUIRE(msg.get_server().empty());
59 1 : msg.set_server("jungle");
60 1 : CATCH_REQUIRE_FALSE(msg.get_server().empty());
61 1 : CATCH_REQUIRE(msg.get_server() == "jungle");
62 :
63 : // SERVICE
64 : //
65 1 : CATCH_REQUIRE(msg.get_service().empty());
66 1 : msg.set_service("watchdog");
67 1 : CATCH_REQUIRE_FALSE(msg.get_service().empty());
68 1 : CATCH_REQUIRE(msg.get_service() == "watchdog");
69 :
70 : // COMMAND
71 : //
72 1 : CATCH_REQUIRE(msg.get_command().empty());
73 1 : msg.set_command("CONNECT");
74 1 : CATCH_REQUIRE_FALSE(msg.get_command().empty());
75 1 : CATCH_REQUIRE(msg.get_command() == "CONNECT");
76 :
77 : // MESSAGE VERSION
78 : //
79 1 : CATCH_REQUIRE(msg.get_message_version() == ed::MESSAGE_VERSION);
80 1 : msg.add_version_parameter();
81 1 : CATCH_REQUIRE(msg.check_version_parameter());
82 :
83 : // PARAMETER (race)
84 : //
85 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("race"));
86 1 : msg.add_parameter("race", "true");
87 1 : CATCH_REQUIRE(msg.has_parameter("race"));
88 1 : CATCH_REQUIRE(msg.get_parameter("race") == "true");
89 :
90 : // PARAMETER (speed)
91 : //
92 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("speed"));
93 1 : msg.add_parameter("speed", 1078);
94 1 : CATCH_REQUIRE(msg.has_parameter("speed"));
95 1 : CATCH_REQUIRE(msg.get_parameter("speed") == "1078");
96 1 : CATCH_REQUIRE(msg.get_integer_parameter("speed") == 1078);
97 :
98 : // PARAMETER (height)
99 : //
100 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("height"));
101 1 : msg.add_parameter("height", 27U);
102 1 : CATCH_REQUIRE(msg.has_parameter("height"));
103 1 : CATCH_REQUIRE(msg.get_parameter("height") == "27");
104 1 : CATCH_REQUIRE(msg.get_integer_parameter("height") == 27);
105 :
106 : // PARAMETER (huge)
107 : //
108 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("huge"));
109 1 : msg.add_parameter("huge", 7428447997487423361LL);
110 1 : CATCH_REQUIRE(msg.has_parameter("huge"));
111 1 : CATCH_REQUIRE(msg.get_parameter("huge") == "7428447997487423361");
112 1 : CATCH_REQUIRE(msg.get_integer_parameter("huge") == 7428447997487423361LL);
113 :
114 : // PARAMETER (huge #2)
115 : //
116 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("huge2"));
117 1 : msg.add_parameter("huge2", 7428447997487423961ULL);
118 1 : CATCH_REQUIRE(msg.has_parameter("huge2"));
119 1 : CATCH_REQUIRE(msg.get_parameter("huge2") == "7428447997487423961");
120 1 : CATCH_REQUIRE(msg.get_integer_parameter("huge2") == 7428447997487423961ULL);
121 :
122 : // PARAMETER (a64bit)
123 : //
124 1 : std::int64_t const a64bit = 7428447907487423361LL;
125 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("a64bit"));
126 1 : msg.add_parameter("a64bit", a64bit);
127 1 : CATCH_REQUIRE(msg.has_parameter("a64bit"));
128 1 : CATCH_REQUIRE(msg.get_parameter("a64bit") == "7428447907487423361");
129 1 : CATCH_REQUIRE(msg.get_integer_parameter("a64bit") == a64bit);
130 :
131 : // PARAMETER (u64bit)
132 : //
133 1 : std::uint64_t const u64bit = 428447907487423361UL;
134 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("u64bit"));
135 1 : msg.add_parameter("u64bit", u64bit);
136 1 : CATCH_REQUIRE(msg.has_parameter("u64bit"));
137 1 : CATCH_REQUIRE(msg.get_parameter("u64bit") == "428447907487423361");
138 1 : CATCH_REQUIRE(msg.get_integer_parameter("u64bit") == u64bit);
139 :
140 9 : for(auto p : msg.get_all_parameters())
141 : {
142 8 : if(p.first == "huge")
143 : {
144 1 : CATCH_REQUIRE(p.second == "7428447997487423361");
145 : }
146 7 : else if(p.first == "u64bit")
147 : {
148 1 : CATCH_REQUIRE(p.second == "428447907487423361");
149 : }
150 : }
151 :
152 : // REPLY TO
153 : //
154 2 : ed::message msg2;
155 1 : msg2.reply_to(msg);
156 :
157 1 : CATCH_REQUIRE(msg2.get_sent_from_server().empty());
158 1 : CATCH_REQUIRE(msg2.get_sent_from_service().empty());
159 1 : CATCH_REQUIRE(msg2.get_server() == "remote");
160 1 : CATCH_REQUIRE(msg2.get_service() == "firewall");
161 1 : CATCH_REQUIRE(msg2.get_command().empty());
162 1 : CATCH_REQUIRE(msg2.get_message_version() == ed::MESSAGE_VERSION);
163 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("race"));
164 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("speed"));
165 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("height"));
166 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("huge"));
167 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("huge2"));
168 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("a64bit"));
169 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("u64bit"));
170 1 : CATCH_REQUIRE(msg2.get_all_parameters().empty());
171 :
172 : //for(auto p : msg2.get_all_parameters())
173 : //{
174 : //std::cerr << "--- " << p.first << "=" << p.second << "\n";
175 : //}
176 :
177 : // make sure the original wasn't modified
178 : //
179 1 : CATCH_REQUIRE(msg.get_sent_from_server() == "remote");
180 1 : CATCH_REQUIRE(msg.get_sent_from_service() == "firewall");
181 1 : CATCH_REQUIRE(msg.get_server() == "jungle");
182 1 : CATCH_REQUIRE(msg.get_service() == "watchdog");
183 : }
184 : CATCH_END_SECTION()
185 :
186 6 : CATCH_START_SECTION("To & From Messages (simple)")
187 : {
188 2 : ed::message msg;
189 :
190 1 : CATCH_REQUIRE(msg.get_command().empty());
191 1 : msg.set_command("FIRE");
192 1 : CATCH_REQUIRE_FALSE(msg.get_command().empty());
193 1 : CATCH_REQUIRE(msg.get_command() == "FIRE");
194 :
195 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("name"));
196 1 : msg.add_parameter("name", "Charles");
197 1 : CATCH_REQUIRE(msg.has_parameter("name"));
198 1 : CATCH_REQUIRE(msg.get_parameter("name") == "Charles");
199 :
200 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("length"));
201 1 : msg.add_parameter("length", -35);
202 1 : CATCH_REQUIRE(msg.has_parameter("length"));
203 1 : CATCH_REQUIRE(msg.get_parameter("length") == "-35");
204 1 : CATCH_REQUIRE(msg.get_integer_parameter("length") == -35);
205 :
206 2 : std::string const m(msg.to_message());
207 :
208 2 : ed::message rcv;
209 :
210 1 : CATCH_REQUIRE(rcv.get_command().empty());
211 1 : CATCH_REQUIRE_FALSE(rcv.has_parameter("name"));
212 1 : CATCH_REQUIRE_FALSE(rcv.has_parameter("length"));
213 :
214 1 : rcv.from_message(m);
215 :
216 1 : CATCH_REQUIRE_FALSE(rcv.get_command().empty());
217 1 : CATCH_REQUIRE(rcv.get_command() == "FIRE");
218 :
219 1 : CATCH_REQUIRE(rcv.has_parameter("name"));
220 1 : CATCH_REQUIRE(rcv.get_parameter("name") == "Charles");
221 :
222 1 : CATCH_REQUIRE(rcv.has_parameter("length"));
223 1 : CATCH_REQUIRE(rcv.get_parameter("length") == "-35");
224 1 : CATCH_REQUIRE(rcv.get_integer_parameter("length") == -35);
225 : }
226 : CATCH_END_SECTION()
227 :
228 6 : CATCH_START_SECTION("To & From Messages (full message)")
229 : {
230 2 : ed::message msg;
231 :
232 1 : CATCH_REQUIRE(msg.get_command().empty());
233 1 : msg.set_command("FIRE");
234 1 : CATCH_REQUIRE_FALSE(msg.get_command().empty());
235 1 : CATCH_REQUIRE(msg.get_command() == "FIRE");
236 :
237 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("name"));
238 1 : msg.add_parameter("name", "Charles");
239 1 : CATCH_REQUIRE(msg.has_parameter("name"));
240 1 : CATCH_REQUIRE(msg.get_parameter("name") == "Charles");
241 :
242 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("length"));
243 1 : msg.add_parameter("length", -35);
244 1 : CATCH_REQUIRE(msg.has_parameter("length"));
245 1 : CATCH_REQUIRE(msg.get_parameter("length") == "-35");
246 1 : CATCH_REQUIRE(msg.get_integer_parameter("length") == -35);
247 :
248 2 : std::string const m(msg.to_message());
249 :
250 2 : ed::message rcv;
251 :
252 1 : CATCH_REQUIRE(rcv.get_command().empty());
253 1 : CATCH_REQUIRE_FALSE(rcv.has_parameter("name"));
254 1 : CATCH_REQUIRE_FALSE(rcv.has_parameter("length"));
255 :
256 1 : rcv.from_message(m);
257 :
258 1 : CATCH_REQUIRE_FALSE(rcv.get_command().empty());
259 1 : CATCH_REQUIRE(rcv.get_command() == "FIRE");
260 :
261 1 : CATCH_REQUIRE(rcv.has_parameter("name"));
262 1 : CATCH_REQUIRE(rcv.get_parameter("name") == "Charles");
263 :
264 1 : CATCH_REQUIRE(rcv.has_parameter("length"));
265 1 : CATCH_REQUIRE(rcv.get_parameter("length") == "-35");
266 1 : CATCH_REQUIRE(rcv.get_integer_parameter("length") == -35);
267 : }
268 : CATCH_END_SECTION()
269 :
270 : // TODO:
271 : //void reply_to(message const & msg);
272 : //parameters_t const & get_all_parameters() const;
273 9 : }
274 :
275 :
276 : // vim: ts=4 sw=4 et
|