comparison tests/src/libirccd/server-util/main.cpp @ 779:317c66a131be

Irccd: merge [identity] with [server], closes #905 @2h While here, also add some tests for server_util, #779
author David Demelier <markand@malikania.fr>
date Thu, 01 Nov 2018 10:34:21 +0100
parents
children 7145a3df4cb7
comparison
equal deleted inserted replaced
778:3ff081c72250 779:317c66a131be
1 /*
2 * main.cpp -- test server_util functions
3 *
4 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #define BOOST_TEST_MODULE "server_util"
20 #include <boost/test/unit_test.hpp>
21 #include <boost/filesystem.hpp>
22
23 #include <irccd/ini.hpp>
24
25 #include <irccd/daemon/server.hpp>
26 #include <irccd/daemon/server_util.hpp>
27
28 using nlohmann::json;
29
30 using irccd::server_util::from_config;
31 using irccd::server_util::from_json;
32 using irccd::server_util::message_type;
33
34 namespace irccd {
35
36 namespace server_util {
37
38 auto operator<<(std::ostream& out, message_type::kind kind) -> std::ostream&
39 {
40 if (kind == message_type::is_command)
41 out << "command";
42 else
43 out << "message";
44
45 return out;
46 }
47
48 } // !server_util
49
50 namespace {
51
52 class fixture {
53 protected:
54 boost::asio::io_service ctx_;
55 };
56
57 auto open_config(const std::string& config) -> ini::document
58 {
59 boost::filesystem::path path;
60
61 path /= CMAKE_CURRENT_SOURCE_DIR;
62 path /= config;
63
64 return ini::read_file(path.string());
65 }
66
67 auto open_json(const std::string& file) -> json
68 {
69 boost::filesystem::path path;
70
71 path /= CMAKE_CURRENT_SOURCE_DIR;
72 path /= file;
73
74 std::ifstream input(path.string());
75
76 if (!input)
77 throw std::runtime_error(std::strerror(errno));
78
79 return json::parse(std::string(std::istreambuf_iterator<char>(input.rdbuf()), {}));
80 }
81
82 BOOST_FIXTURE_TEST_SUITE(from_config, fixture)
83
84 BOOST_AUTO_TEST_SUITE(valid)
85
86 BOOST_AUTO_TEST_CASE(full)
87 {
88 const auto sv = server_util::from_config(ctx_, open_config("full.conf")[0]);
89
90 BOOST_TEST(sv->get_id() == "localhost");
91 BOOST_TEST(sv->get_host() == "irc.localhost");
92 BOOST_TEST(sv->get_port() == 3344U);
93 BOOST_TEST(sv->get_password() == "secret");
94 BOOST_TEST(sv->get_nickname() == "superbot");
95 BOOST_TEST(sv->get_username() == "sp");
96 BOOST_TEST(sv->get_realname() == "SuperBot 2000 NT");
97 BOOST_TEST(static_cast<bool>(sv->get_options() & server::options::join_invite));
98 BOOST_TEST(static_cast<bool>(sv->get_options() & server::options::auto_rejoin));
99 BOOST_TEST(static_cast<bool>(sv->get_options() & server::options::auto_reconnect));
100 }
101
102 #if defined(IRCCD_HAVE_SSL)
103
104 BOOST_AUTO_TEST_CASE(ssl)
105 {
106 const auto sv = server_util::from_config(ctx_, open_config("ssl.conf")[0]);
107
108 BOOST_TEST(sv->get_id() == "localhost");
109 BOOST_TEST(sv->get_host() == "irc.localhost");
110 BOOST_TEST(sv->get_port() == 6697U);
111 BOOST_TEST(sv->get_password() == "secret");
112 BOOST_TEST(sv->get_nickname() == "secure");
113 BOOST_TEST(sv->get_username() == "sc");
114 BOOST_TEST(sv->get_realname() == "SuperBot 2000 NT SSL");
115 BOOST_TEST(static_cast<bool>(sv->get_options() & server::options::ssl));
116 BOOST_TEST(static_cast<bool>(sv->get_options() & server::options::join_invite));
117 BOOST_TEST(static_cast<bool>(sv->get_options() & server::options::auto_rejoin));
118 BOOST_TEST(static_cast<bool>(sv->get_options() & server::options::auto_reconnect));
119 }
120
121 #endif // !IRCCD_HAVE_SSL
122
123 BOOST_AUTO_TEST_SUITE_END()
124
125 BOOST_AUTO_TEST_SUITE_END()
126
127 BOOST_FIXTURE_TEST_SUITE(from_json, fixture)
128
129 BOOST_AUTO_TEST_SUITE(valid)
130
131 BOOST_AUTO_TEST_CASE(full)
132 {
133 const auto sv = server_util::from_json(ctx_, open_json("full.json"));
134
135 BOOST_TEST(sv->get_id() == "localhost");
136 BOOST_TEST(sv->get_host() == "irc.localhost");
137 BOOST_TEST(sv->get_port() == 3344U);
138 BOOST_TEST(sv->get_password() == "secret");
139 BOOST_TEST(sv->get_nickname() == "superbot");
140 BOOST_TEST(sv->get_username() == "sp");
141 BOOST_TEST(sv->get_realname() == "SuperBot 2000 NT");
142 }
143
144 BOOST_AUTO_TEST_SUITE_END()
145
146 BOOST_AUTO_TEST_SUITE_END()
147
148 BOOST_AUTO_TEST_SUITE(message)
149
150 BOOST_AUTO_TEST_CASE(valid_short)
151 {
152 const auto m = message_type::parse("!hello", "!", "hello");
153
154 BOOST_TEST(m.type == message_type::is_command);
155 BOOST_TEST(m.message == "");
156 }
157
158 BOOST_AUTO_TEST_CASE(valid_arguments)
159 {
160 const auto m = message_type::parse("!hello world", "!", "hello");
161
162 BOOST_TEST(m.type == message_type::is_command);
163 BOOST_TEST(m.message == "world");
164 }
165
166 BOOST_AUTO_TEST_CASE(cchar_with_message_short)
167 {
168 const auto m = message_type::parse("!hello", "!", "hangman");
169
170 BOOST_TEST(m.type == message_type::is_message);
171 BOOST_TEST(m.message == "!hello");
172 }
173
174 BOOST_AUTO_TEST_CASE(cchar_with_message_arguments)
175 {
176 const auto m = message_type::parse("!hello world", "!", "hangman");
177
178 BOOST_TEST(m.type == message_type::is_message);
179 BOOST_TEST(m.message == "!hello world");
180 }
181
182 BOOST_AUTO_TEST_CASE(command_with_different_cchar_short)
183 {
184 const auto m = message_type::parse("!hello", ">", "hello");
185
186 BOOST_TEST(m.type == message_type::is_message);
187 BOOST_TEST(m.message == "!hello");
188 }
189
190 BOOST_AUTO_TEST_CASE(command_with_different_cchar_arguments)
191 {
192 const auto m = message_type::parse("!hello", ">", "hello");
193
194 BOOST_TEST(m.type == message_type::is_message);
195 BOOST_TEST(m.message == "!hello");
196 }
197
198 BOOST_AUTO_TEST_SUITE_END()
199
200 } // !namespace
201
202 } // !irccd