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