comparison tests/src/libirccd/stream/main.cpp @ 809:8460b4a34191

misc: reorganize namespaces, closes #952 @4h
author David Demelier <markand@malikania.fr>
date Fri, 16 Nov 2018 12:25:00 +0100
parents tests/src/libirccd-core/stream/main.cpp@d42e8415e477
children 06cc2f95f479
comparison
equal deleted inserted replaced
808:80bccab4a093 809:8460b4a34191
1 /*
2 * main.cpp -- test network classes
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 "stream"
20 #include <boost/test/unit_test.hpp>
21 #include <boost/mpl/list.hpp>
22 #include <boost/predef/os.h>
23
24 #include <irccd/sysconfig.hpp>
25
26 #include <irccd/acceptor.hpp>
27 #include <irccd/connector.hpp>
28 #include <irccd/stream.hpp>
29
30 using boost::asio::io_service;
31 using boost::asio::ip::tcp;
32
33 #if defined(IRCCD_HAVE_SSL)
34 using boost::asio::ssl::context;
35 #endif
36
37 namespace irccd {
38
39 namespace {
40
41 class stream_fixture {
42 public:
43 io_service service_;
44
45 std::unique_ptr<acceptor> acceptor_;
46 std::unique_ptr<connector> connector_;
47
48 std::shared_ptr<stream> stream1_;
49 std::shared_ptr<stream> stream2_;
50
51 virtual auto create_acceptor() -> std::unique_ptr<acceptor> = 0;
52
53 virtual auto create_connector() -> std::unique_ptr<connector> = 0;
54
55 void init()
56 {
57 acceptor_ = create_acceptor();
58 connector_ = create_connector();
59
60 acceptor_->accept([this] (auto code, auto stream) {
61 if (code)
62 throw std::system_error(code);
63
64 stream1_ = std::move(stream);
65 });
66 connector_->connect([this] (auto code, auto stream) {
67 if (code)
68 throw std::system_error(code);
69
70 stream2_ = std::move(stream);
71 });
72
73 service_.run();
74 service_.reset();
75 }
76 };
77
78 class ip_stream_fixture : public stream_fixture {
79 private:
80 tcp::endpoint endpoint_;
81
82 protected:
83 /**
84 * \copydoc io_fixture::create_acceptor
85 */
86 auto create_acceptor() -> std::unique_ptr<acceptor> override
87 {
88 tcp::endpoint endpoint(tcp::v4(), 0U);
89 tcp::acceptor acceptor(service_, std::move(endpoint));
90
91 endpoint_ = acceptor.local_endpoint();
92
93 return std::make_unique<ip_acceptor>(service_, std::move(acceptor));
94 }
95
96 /**
97 * \copydoc io_fixture::create_connector
98 */
99 auto create_connector() -> std::unique_ptr<connector> override
100 {
101 const auto hostname = "127.0.0.1";
102 const auto port = std::to_string(endpoint_.port());
103
104 return std::make_unique<ip_connector>(service_, hostname, port, true, false);
105 }
106 };
107
108 #if defined(IRCCD_HAVE_SSL)
109
110 class tls_ip_stream_fixture : public stream_fixture {
111 private:
112 tcp::endpoint endpoint_;
113
114 protected:
115 /**
116 * \copydoc io_fixture::create_acceptor
117 */
118 auto create_acceptor() -> std::unique_ptr<acceptor> override
119 {
120 context context(context::tlsv12);
121
122 context.use_certificate_file(TESTS_SOURCE_DIR "/data/test.crt", context::pem);
123 context.use_private_key_file(TESTS_SOURCE_DIR "/data/test.key", context::pem);
124
125 tcp::endpoint endpoint(tcp::v4(), 0U);
126 tcp::acceptor acceptor(service_, std::move(endpoint));
127
128 endpoint_ = acceptor.local_endpoint();
129
130 return std::make_unique<tls_acceptor<ip_acceptor>>(std::move(context), service_, std::move(acceptor));
131 }
132
133 /**
134 * \copydoc io_fixture::create_connector
135 */
136 auto create_connector() -> std::unique_ptr<connector> override
137 {
138 context context(context::tlsv12);
139
140 const auto hostname = "127.0.0.1";
141 const auto port = std::to_string(endpoint_.port());
142
143 return std::make_unique<tls_connector<ip_connector>>(std::move(context),
144 service_, hostname, port, true, false);
145 }
146 };
147
148 #endif // !IRCCD_HAVE_SSL
149
150 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
151
152 class local_stream_fixture : public stream_fixture {
153 private:
154 const std::string path_{CMAKE_CURRENT_BINARY_DIR "/stream-test.sock"};
155
156 public:
157
158 /**
159 * \copydoc io_fixture::create_acceptor
160 */
161 auto create_acceptor() -> std::unique_ptr<acceptor> override
162 {
163 return std::make_unique<local_acceptor>(service_, path_);
164 }
165
166 /**
167 * \copydoc io_fixture::create_connector
168 */
169 auto create_connector() -> std::unique_ptr<connector> override
170 {
171 return std::make_unique<local_connector>(service_, path_);
172 }
173 };
174
175 #if defined(IRCCD_HAVE_SSL)
176
177 class tls_local_stream_fixture : public stream_fixture {
178 private:
179 const std::string path_{CMAKE_CURRENT_BINARY_DIR "/stream-test.sock"};
180
181 public:
182
183 /**
184 * \copydoc io_fixture::create_acceptor
185 */
186 auto create_acceptor() -> std::unique_ptr<acceptor> override
187 {
188 context context(context::tlsv12);
189
190 context.use_certificate_file(TESTS_SOURCE_DIR "/data/test.crt", context::pem);
191 context.use_private_key_file(TESTS_SOURCE_DIR "/data/test.key", context::pem);
192
193 return std::make_unique<tls_acceptor<local_acceptor>>(std::move(context), service_, path_);
194 }
195
196 /**
197 * \copydoc io_fixture::create_connector
198 */
199 auto create_connector() -> std::unique_ptr<connector> override
200 {
201 return std::make_unique<tls_connector<local_connector>>(context(context::tlsv12), service_, path_);
202 }
203 };
204
205 #endif // !IRCCD_HAVE_SSL
206
207 #endif // !BOOST_ASIO_HAS_LOCAL_SOCKETS
208
209 /**
210 * List of fixtures to tests.
211 */
212 using list = boost::mpl::list<
213 ip_stream_fixture
214 #if defined(IRCCD_HAVE_SSL)
215 , tls_ip_stream_fixture
216 #endif
217 #if defined(BOOST_ASIO_HAS_LOCAL_SOCKETS)
218 , local_stream_fixture
219 # if defined(IRCCD_HAVE_SSL)
220 , tls_local_stream_fixture
221 # endif
222 #endif
223 >;
224
225 BOOST_AUTO_TEST_CASE_TEMPLATE(invalid_argument, Test, list)
226 {
227 Test fixture;
228
229 const nlohmann::json message{
230 { "abc", 123 },
231 { "def", 456 }
232 };
233
234 fixture.init();
235 fixture.stream1_->recv([] (auto code, auto message) {
236 BOOST_TEST(!code);
237 BOOST_TEST(message.is_object());
238 BOOST_TEST(message["abc"].template get<int>() == 123);
239 BOOST_TEST(message["def"].template get<int>() == 456);
240 });
241 fixture.stream2_->send(message, [] (auto code) {
242 BOOST_TEST(!code);
243 });
244 fixture.service_.run();
245 }
246
247 BOOST_AUTO_TEST_CASE_TEMPLATE(connection_reset, Test, list)
248 {
249 Test fixture;
250
251 fixture.init();
252 fixture.stream1_->recv([] (auto code, auto message) {
253 BOOST_TEST(code.value() == static_cast<int>(std::errc::connection_reset));
254 BOOST_TEST(message.is_null());
255 });
256 fixture.stream2_ = nullptr;
257 fixture.service_.run();
258 }
259
260 } // !namespace
261
262 } // !irccd