Line data Source code
1 : /*
2 : * License:
3 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
4 : *
5 : * https://snapwebsites.org/
6 : * contact@m2osw.com
7 : *
8 : * This program is free software; you can redistribute it and/or modify
9 : * it under the terms of the GNU General Public License as published by
10 : * the Free Software Foundation; either version 2 of the License, or
11 : * (at your option) any later version.
12 : *
13 : * This program is distributed in the hope that it will be useful,
14 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : * GNU General Public License for more details.
17 : *
18 : * You should have received a copy of the GNU General Public License along
19 : * with this program; if not, write to the Free Software Foundation, Inc.,
20 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 : */
22 :
23 : // self
24 : //
25 : #include "main.h"
26 :
27 :
28 : // snaplogger lib
29 : //
30 : #include <eventdispatcher/message.h>
31 :
32 :
33 : // C lib
34 : //
35 : #include <unistd.h>
36 :
37 :
38 :
39 3 : CATCH_TEST_CASE("message", "[message]")
40 : {
41 2 : CATCH_START_SECTION("Simple message fields")
42 : {
43 2 : ed::message msg;
44 :
45 : // SENT FROM SERVER
46 : //
47 1 : CATCH_REQUIRE(msg.get_sent_from_server().empty());
48 1 : msg.set_sent_from_server("remote");
49 1 : CATCH_REQUIRE(msg.get_sent_from_server() == "remote");
50 :
51 : // SENT FROM SERVICE
52 : //
53 1 : CATCH_REQUIRE(msg.get_sent_from_service().empty());
54 1 : msg.set_sent_from_service("firewall");
55 1 : CATCH_REQUIRE(msg.get_sent_from_service() == "firewall");
56 :
57 : // SERVER
58 : //
59 1 : CATCH_REQUIRE(msg.get_server().empty());
60 1 : msg.set_server("jungle");
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(msg.get_service() == "watchdog");
68 :
69 : // COMMAND
70 : //
71 1 : CATCH_REQUIRE(msg.get_command().empty());
72 1 : msg.set_command("CONNECT");
73 1 : CATCH_REQUIRE(msg.get_command() == "CONNECT");
74 :
75 : // MESSAGE VERSION
76 : //
77 1 : CATCH_REQUIRE(msg.get_message_version() == ed::MESSAGE_VERSION);
78 1 : msg.add_version_parameter();
79 1 : CATCH_REQUIRE(msg.check_version_parameter());
80 :
81 : // PARAMETER (race)
82 : //
83 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("race"));
84 1 : msg.add_parameter("race", "true");
85 1 : CATCH_REQUIRE(msg.has_parameter("race"));
86 1 : CATCH_REQUIRE(msg.get_parameter("race") == "true");
87 :
88 : // PARAMETER (speed)
89 : //
90 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("speed"));
91 1 : msg.add_parameter("speed", 1078);
92 1 : CATCH_REQUIRE(msg.has_parameter("speed"));
93 1 : CATCH_REQUIRE(msg.get_parameter("speed") == "1078");
94 1 : CATCH_REQUIRE(msg.get_integer_parameter("speed") == 1078);
95 :
96 : // PARAMETER (height)
97 : //
98 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("height"));
99 1 : msg.add_parameter("height", 27U);
100 1 : CATCH_REQUIRE(msg.has_parameter("height"));
101 1 : CATCH_REQUIRE(msg.get_parameter("height") == "27");
102 1 : CATCH_REQUIRE(msg.get_integer_parameter("height") == 27);
103 :
104 : // PARAMETER (huge)
105 : //
106 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("huge"));
107 1 : msg.add_parameter("huge", 7428447997487423361LL);
108 1 : CATCH_REQUIRE(msg.has_parameter("huge"));
109 1 : CATCH_REQUIRE(msg.get_parameter("huge") == "7428447997487423361");
110 1 : CATCH_REQUIRE(msg.get_integer_parameter("huge") == 7428447997487423361LL);
111 :
112 : // PARAMETER (huge #2)
113 : //
114 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("huge2"));
115 1 : msg.add_parameter("huge2", 7428447997487423961ULL);
116 1 : CATCH_REQUIRE(msg.has_parameter("huge2"));
117 1 : CATCH_REQUIRE(msg.get_parameter("huge2") == "7428447997487423961");
118 1 : CATCH_REQUIRE(msg.get_integer_parameter("huge2") == 7428447997487423961ULL);
119 :
120 : // PARAMETER (a64bit)
121 : //
122 1 : std::int64_t const a64bit = 7428447907487423361LL;
123 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("a64bit"));
124 1 : msg.add_parameter("a64bit", a64bit);
125 1 : CATCH_REQUIRE(msg.has_parameter("a64bit"));
126 1 : CATCH_REQUIRE(msg.get_parameter("a64bit") == "7428447907487423361");
127 1 : CATCH_REQUIRE(msg.get_integer_parameter("a64bit") == a64bit);
128 :
129 : // PARAMETER (u64bit)
130 : //
131 1 : std::uint64_t const u64bit = 428447907487423361UL;
132 1 : CATCH_REQUIRE_FALSE(msg.has_parameter("u64bit"));
133 1 : msg.add_parameter("u64bit", u64bit);
134 1 : CATCH_REQUIRE(msg.has_parameter("u64bit"));
135 1 : CATCH_REQUIRE(msg.get_parameter("u64bit") == "428447907487423361");
136 1 : CATCH_REQUIRE(msg.get_integer_parameter("u64bit") == u64bit);
137 :
138 9 : for(auto p : msg.get_all_parameters())
139 : {
140 8 : if(p.first == "huge")
141 : {
142 1 : CATCH_REQUIRE(p.second == "7428447997487423361");
143 : }
144 7 : else if(p.first == "u64bit")
145 : {
146 1 : CATCH_REQUIRE(p.second == "428447907487423361");
147 : }
148 : }
149 :
150 : // REPLY TO
151 : //
152 2 : ed::message msg2;
153 1 : msg2.reply_to(msg);
154 :
155 1 : CATCH_REQUIRE(msg2.get_sent_from_server().empty());
156 1 : CATCH_REQUIRE(msg2.get_sent_from_service().empty());
157 1 : CATCH_REQUIRE(msg2.get_server() == "remote");
158 1 : CATCH_REQUIRE(msg2.get_service() == "firewall");
159 1 : CATCH_REQUIRE(msg2.get_command().empty());
160 1 : CATCH_REQUIRE(msg2.get_message_version() == ed::MESSAGE_VERSION);
161 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("race"));
162 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("speed"));
163 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("height"));
164 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("huge"));
165 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("huge2"));
166 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("a64bit"));
167 1 : CATCH_REQUIRE_FALSE(msg2.has_parameter("u64bit"));
168 1 : CATCH_REQUIRE(msg2.get_all_parameters().empty());
169 :
170 1 : for(auto p : msg2.get_all_parameters())
171 : {
172 0 : std::cerr << "--- " << p.first << "=" << p.second << "\n";
173 : }
174 :
175 : // make sure the original wasn't modified
176 : //
177 1 : CATCH_REQUIRE(msg.get_sent_from_server() == "remote");
178 1 : CATCH_REQUIRE(msg.get_sent_from_service() == "firewall");
179 1 : CATCH_REQUIRE(msg.get_server() == "jungle");
180 1 : CATCH_REQUIRE(msg.get_service() == "watchdog");
181 : }
182 : CATCH_END_SECTION()
183 :
184 : //bool from_message(std::string const & msg);
185 : //std::string to_message() const;
186 : //void reply_to(message const & msg);
187 : //parameters_t const & get_all_parameters() const;
188 7 : }
189 :
190 :
191 : // vim: ts=4 sw=4 et
|