Line data Source code
1 : // Copyright (c) 2012-2019 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
17 : // along with this program; if not, write to the Free Software
18 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 :
20 : // self
21 : //
22 : #include "main.h"
23 :
24 :
25 : // snaplogger lib
26 : //
27 : #include <eventdispatcher/message.h>
28 :
29 :
30 : // C lib
31 : //
32 : #include <unistd.h>
33 :
34 :
35 :
36 3 : CATCH_TEST_CASE("message", "[message]")
37 : {
38 2 : 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(msg.get_sent_from_server() == "remote");
47 :
48 : // SENT FROM SERVICE
49 : //
50 1 : CATCH_REQUIRE(msg.get_sent_from_service().empty());
51 1 : msg.set_sent_from_service("firewall");
52 1 : CATCH_REQUIRE(msg.get_sent_from_service() == "firewall");
53 :
54 : // SERVER
55 : //
56 1 : CATCH_REQUIRE(msg.get_server().empty());
57 1 : msg.set_server("jungle");
58 1 : CATCH_REQUIRE(msg.get_server() == "jungle");
59 :
60 : // SERVICE
61 : //
62 1 : CATCH_REQUIRE(msg.get_service().empty());
63 1 : msg.set_service("watchdog");
64 1 : CATCH_REQUIRE(msg.get_service() == "watchdog");
65 :
66 : // COMMAND
67 : //
68 1 : CATCH_REQUIRE(msg.get_command().empty());
69 1 : msg.set_command("CONNECT");
70 1 : CATCH_REQUIRE(msg.get_command() == "CONNECT");
71 :
72 : // MESSAGE VERSION
73 : //
74 1 : CATCH_REQUIRE(msg.get_message_version() == ed::MESSAGE_VERSION);
75 1 : msg.add_version_parameter();
76 1 : CATCH_REQUIRE(msg.check_version_parameter());
77 :
78 : // PARAMETER (race)
79 : //
80 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("race"));
81 1 : msg.add_parameter("race", "true");
82 1 : CATCH_REQUIRE(msg.has_parameter("race"));
83 1 : CATCH_REQUIRE(msg.get_parameter("race") == "true");
84 :
85 : // PARAMETER (speed)
86 : //
87 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("speed"));
88 1 : msg.add_parameter("speed", 1078);
89 1 : CATCH_REQUIRE(msg.has_parameter("speed"));
90 1 : CATCH_REQUIRE(msg.get_parameter("speed") == "1078");
91 1 : CATCH_REQUIRE(msg.get_integer_parameter("speed") == 1078);
92 :
93 : // PARAMETER (height)
94 : //
95 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("height"));
96 1 : msg.add_parameter("height", 27U);
97 1 : CATCH_REQUIRE(msg.has_parameter("height"));
98 1 : CATCH_REQUIRE(msg.get_parameter("height") == "27");
99 1 : CATCH_REQUIRE(msg.get_integer_parameter("height") == 27);
100 :
101 : // PARAMETER (huge)
102 : //
103 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("huge"));
104 1 : msg.add_parameter("huge", 7428447997487423361LL);
105 1 : CATCH_REQUIRE(msg.has_parameter("huge"));
106 1 : CATCH_REQUIRE(msg.get_parameter("huge") == "7428447997487423361");
107 1 : CATCH_REQUIRE(msg.get_integer_parameter("huge") == 7428447997487423361LL);
108 :
109 : // PARAMETER (huge #2)
110 : //
111 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("huge2"));
112 1 : msg.add_parameter("huge2", 7428447997487423961ULL);
113 1 : CATCH_REQUIRE(msg.has_parameter("huge2"));
114 1 : CATCH_REQUIRE(msg.get_parameter("huge2") == "7428447997487423961");
115 1 : CATCH_REQUIRE(msg.get_integer_parameter("huge2") == 7428447997487423961ULL);
116 :
117 : // PARAMETER (a64bit)
118 : //
119 1 : std::int64_t const a64bit = 7428447907487423361LL;
120 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("a64bit"));
121 1 : msg.add_parameter("a64bit", a64bit);
122 1 : CATCH_REQUIRE(msg.has_parameter("a64bit"));
123 1 : CATCH_REQUIRE(msg.get_parameter("a64bit") == "7428447907487423361");
124 1 : CATCH_REQUIRE(msg.get_integer_parameter("a64bit") == a64bit);
125 :
126 : // PARAMETER (u64bit)
127 : //
128 1 : std::uint64_t const u64bit = 428447907487423361UL;
129 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("u64bit"));
130 1 : msg.add_parameter("u64bit", u64bit);
131 1 : CATCH_REQUIRE(msg.has_parameter("u64bit"));
132 1 : CATCH_REQUIRE(msg.get_parameter("u64bit") == "428447907487423361");
133 1 : CATCH_REQUIRE(msg.get_integer_parameter("u64bit") == u64bit);
134 :
135 9 : for(auto p : msg.get_all_parameters())
136 : {
137 8 : if(p.first == "huge")
138 : {
139 1 : CATCH_REQUIRE(p.second == "7428447997487423361");
140 : }
141 7 : else if(p.first == "u64bit")
142 : {
143 1 : CATCH_REQUIRE(p.second == "428447907487423361");
144 : }
145 : }
146 :
147 : // REPLY TO
148 : //
149 2 : ed::message msg2;
150 1 : msg2.reply_to(msg);
151 :
152 1 : CATCH_REQUIRE(msg2.get_sent_from_server().empty());
153 1 : CATCH_REQUIRE(msg2.get_sent_from_service().empty());
154 1 : CATCH_REQUIRE(msg2.get_server() == "remote");
155 1 : CATCH_REQUIRE(msg2.get_service() == "firewall");
156 1 : CATCH_REQUIRE(msg2.get_command().empty());
157 1 : CATCH_REQUIRE(msg2.get_message_version() == ed::MESSAGE_VERSION);
158 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("race"));
159 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("speed"));
160 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("height"));
161 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("huge"));
162 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("huge2"));
163 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("a64bit"));
164 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("u64bit"));
165 1 : CATCH_REQUIRE(msg2.get_all_parameters().empty());
166 :
167 : //for(auto p : msg2.get_all_parameters())
168 : //{
169 : //std::cerr << "--- " << p.first << "=" << p.second << "\n";
170 : //}
171 :
172 : // make sure the original wasn't modified
173 : //
174 1 : CATCH_REQUIRE(msg.get_sent_from_server() == "remote");
175 1 : CATCH_REQUIRE(msg.get_sent_from_service() == "firewall");
176 1 : CATCH_REQUIRE(msg.get_server() == "jungle");
177 1 : CATCH_REQUIRE(msg.get_service() == "watchdog");
178 : }
179 : CATCH_END_SECTION()
180 :
181 : // TODO:
182 : //bool from_message(std::string const & msg);
183 : //std::string to_message() const;
184 : //void reply_to(message const & msg);
185 : //parameters_t const & get_all_parameters() const;
186 7 : }
187 :
188 :
189 : // vim: ts=4 sw=4 et
|