Mercurial > irccd
comparison tests/src/libcommon/stream/main.cpp @ 774:bfc51f8a00f3
Tests: cleanup data structure and rename io to stream
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 26 Oct 2018 13:57:36 +0200 |
parents | tests/src/libcommon/io/main.cpp@8c44bbcbbab9 |
children | 560b62f6b0a7 |
comparison
equal
deleted
inserted
replaced
773:8c44bbcbbab9 | 774:bfc51f8a00f3 |
---|---|
1 /* | |
2 * main.cpp -- test io 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/socket_acceptor.hpp> | |
27 #include <irccd/socket_connector.hpp> | |
28 #include <irccd/socket_stream.hpp> | |
29 | |
30 #if defined(IRCCD_HAVE_SSL) | |
31 # include <irccd/tls_acceptor.hpp> | |
32 # include <irccd/tls_connector.hpp> | |
33 # include <irccd/tls_stream.hpp> | |
34 #endif // !IRCCD_HAVE_SSL | |
35 | |
36 using boost::asio::io_service; | |
37 using boost::asio::ip::tcp; | |
38 | |
39 #if defined(IRCCD_HAVE_SSL) | |
40 using boost::asio::ssl::context; | |
41 #endif | |
42 | |
43 #if !BOOST_OS_WINDOWS | |
44 using boost::asio::local::stream_protocol; | |
45 #endif | |
46 | |
47 namespace irccd { | |
48 | |
49 namespace { | |
50 | |
51 class stream_fixture { | |
52 public: | |
53 io_service service_; | |
54 | |
55 std::unique_ptr<acceptor> acceptor_; | |
56 std::unique_ptr<connector> connector_; | |
57 | |
58 std::shared_ptr<stream> stream1_; | |
59 std::shared_ptr<stream> stream2_; | |
60 | |
61 virtual auto create_acceptor() -> std::unique_ptr<acceptor> = 0; | |
62 | |
63 virtual auto create_connector() -> std::unique_ptr<connector> = 0; | |
64 | |
65 void init() | |
66 { | |
67 acceptor_ = create_acceptor(); | |
68 connector_ = create_connector(); | |
69 | |
70 acceptor_->accept([this] (auto code, auto stream) { | |
71 if (code) | |
72 throw std::system_error(code); | |
73 | |
74 stream1_ = std::move(stream); | |
75 }); | |
76 connector_->connect([this] (auto code, auto stream) { | |
77 if (code) | |
78 throw std::system_error(code); | |
79 | |
80 stream2_ = std::move(stream); | |
81 }); | |
82 | |
83 service_.run(); | |
84 service_.reset(); | |
85 } | |
86 }; | |
87 | |
88 class ip_stream_fixture : public stream_fixture { | |
89 private: | |
90 tcp::endpoint endpoint_; | |
91 | |
92 protected: | |
93 /** | |
94 * \copydoc io_fixture::create_acceptor | |
95 */ | |
96 auto create_acceptor() -> std::unique_ptr<acceptor> override | |
97 { | |
98 tcp::endpoint endpoint(tcp::v4(), 0U); | |
99 tcp::acceptor acceptor(service_, std::move(endpoint)); | |
100 | |
101 endpoint_ = acceptor.local_endpoint(); | |
102 | |
103 return std::make_unique<ip_acceptor>(std::move(acceptor)); | |
104 } | |
105 | |
106 /** | |
107 * \copydoc io_fixture::create_connector | |
108 */ | |
109 auto create_connector() -> std::unique_ptr<connector> override | |
110 { | |
111 return std::make_unique<ip_connector>(service_, endpoint_); | |
112 } | |
113 }; | |
114 | |
115 #if defined(IRCCD_HAVE_SSL) | |
116 | |
117 class ssl_stream_fixture : public stream_fixture { | |
118 private: | |
119 tcp::endpoint endpoint_; | |
120 | |
121 protected: | |
122 /** | |
123 * \copydoc io_fixture::create_acceptor | |
124 */ | |
125 auto create_acceptor() -> std::unique_ptr<acceptor> override | |
126 { | |
127 context context(context::sslv23); | |
128 | |
129 context.use_certificate_file(TESTS_SOURCE_DIR "/data/test.crt", context::pem); | |
130 context.use_private_key_file(TESTS_SOURCE_DIR "/data/test.key", context::pem); | |
131 | |
132 tcp::endpoint endpoint(tcp::v4(), 0U); | |
133 tcp::acceptor acceptor(service_, std::move(endpoint)); | |
134 | |
135 endpoint_ = acceptor.local_endpoint(); | |
136 | |
137 return std::make_unique<tls_acceptor<>>(std::move(context), std::move(acceptor)); | |
138 } | |
139 | |
140 /** | |
141 * \copydoc io_fixture::create_connector | |
142 */ | |
143 auto create_connector() -> std::unique_ptr<connector> override | |
144 { | |
145 return std::make_unique<tls_connector<>>(context(context::sslv23), service_, endpoint_); | |
146 } | |
147 }; | |
148 | |
149 #endif // !IRCCD_HAVE_SSL | |
150 | |
151 #if !BOOST_OS_WINDOWS | |
152 | |
153 class local_stream_fixture : public stream_fixture { | |
154 public: | |
155 /** | |
156 * \copydoc io_fixture::create_acceptor | |
157 */ | |
158 auto create_acceptor() -> std::unique_ptr<acceptor> override | |
159 { | |
160 std::remove(CMAKE_BINARY_DIR "/tmp/io-test.sock"); | |
161 | |
162 stream_protocol::acceptor acceptor(service_, CMAKE_BINARY_DIR "/tmp/io-test.sock"); | |
163 | |
164 return std::make_unique<local_acceptor>(std::move(acceptor)); | |
165 } | |
166 | |
167 /** | |
168 * \copydoc io_fixture::create_connector | |
169 */ | |
170 auto create_connector() -> std::unique_ptr<connector> override | |
171 { | |
172 return std::make_unique<local_connector>(service_, CMAKE_BINARY_DIR "/tmp/io-test.sock"); | |
173 } | |
174 }; | |
175 | |
176 #endif // !BOOST_OS_WINDOWS | |
177 | |
178 /** | |
179 * List of fixtures to tests. | |
180 */ | |
181 using list = boost::mpl::list< | |
182 ip_stream_fixture | |
183 #if defined(IRCCD_HAVE_SSL) | |
184 , ssl_stream_fixture | |
185 #endif | |
186 #if !BOOST_OS_WINDOWS | |
187 , local_stream_fixture | |
188 #endif | |
189 >; | |
190 | |
191 BOOST_AUTO_TEST_CASE_TEMPLATE(invalid_argument, Test, list) | |
192 { | |
193 Test fixture; | |
194 | |
195 const nlohmann::json message{ | |
196 { "abc", 123 }, | |
197 { "def", 456 } | |
198 }; | |
199 | |
200 fixture.init(); | |
201 fixture.stream1_->read([] (auto code, auto message) { | |
202 BOOST_TEST(!code); | |
203 BOOST_TEST(message.is_object()); | |
204 BOOST_TEST(message["abc"].template get<int>() == 123); | |
205 BOOST_TEST(message["def"].template get<int>() == 456); | |
206 }); | |
207 fixture.stream2_->write(message, [] (auto code) { | |
208 BOOST_TEST(!code); | |
209 }); | |
210 fixture.service_.run(); | |
211 } | |
212 | |
213 BOOST_AUTO_TEST_CASE_TEMPLATE(network_down, Test, list) | |
214 { | |
215 Test fixture; | |
216 | |
217 fixture.init(); | |
218 fixture.stream1_->read([] (auto code, auto message) { | |
219 BOOST_TEST(code.value() == static_cast<int>(std::errc::not_connected)); | |
220 BOOST_TEST(message.is_null()); | |
221 }); | |
222 fixture.stream2_ = nullptr; | |
223 fixture.service_.run(); | |
224 } | |
225 | |
226 } // !namespace | |
227 | |
228 } // !irccd |