Mercurial > malikania
comparison tests/libcommon/socket/main.cpp @ 192:74afc5a41c83
Common: rewrite socket class, closes #911 @2h
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 27 Oct 2018 07:11:03 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
191:bfc6b9c9081a | 192:74afc5a41c83 |
---|---|
1 /* | |
2 * main.cpp -- test socket | |
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 "socket" | |
20 #include <boost/test/unit_test.hpp> | |
21 | |
22 #include <malikania/socket.hpp> | |
23 | |
24 using boost::asio::io_context; | |
25 using boost::asio::ip::make_address; | |
26 using boost::asio::ip::tcp; | |
27 using boost::asio::ssl::context; | |
28 using boost::asio::ssl::stream_base; | |
29 | |
30 namespace mlk { | |
31 | |
32 namespace { | |
33 | |
34 class socket_fixture { | |
35 private: | |
36 auto init() -> context; | |
37 void handshake(); | |
38 void pair(); | |
39 | |
40 protected: | |
41 // SSL and Asio context. | |
42 context ssl_context_{init()}; | |
43 io_context ctx_; | |
44 | |
45 // two connected sockets. | |
46 socket server_{ctx_, ssl_context_}; | |
47 socket client_{ctx_, ssl_context_}; | |
48 | |
49 socket_fixture(); | |
50 }; | |
51 | |
52 auto socket_fixture::init() -> context | |
53 { | |
54 context ret{context::sslv23}; | |
55 | |
56 ret.use_private_key_file(CMAKE_SOURCE_DIR "/tests/assets/test.key", context::pem); | |
57 ret.use_certificate_file(CMAKE_SOURCE_DIR "/tests/assets/test.crt", context::pem); | |
58 | |
59 return ret; | |
60 } | |
61 | |
62 void socket_fixture::handshake() | |
63 { | |
64 server_.get_socket().async_handshake(stream_base::server, [] (auto code) { | |
65 if (code) | |
66 throw std::system_error(code); | |
67 }); | |
68 client_.get_socket().async_handshake(stream_base::client, [] (auto code) { | |
69 if (code) | |
70 throw std::system_error(code); | |
71 }); | |
72 | |
73 ctx_.run(); | |
74 ctx_.reset(); | |
75 } | |
76 | |
77 void socket_fixture::pair() | |
78 { | |
79 tcp::acceptor acc(ctx_, tcp::endpoint(tcp::v4(), 0U)); | |
80 | |
81 acc.async_accept(server_.get_socket().lowest_layer(), [] (auto code) { | |
82 if (code) | |
83 throw std::system_error(code); | |
84 }); | |
85 | |
86 tcp::endpoint ep(make_address("127.0.0.1"), acc.local_endpoint().port()); | |
87 | |
88 client_.get_socket().lowest_layer().async_connect(ep, [] (auto code) { | |
89 if (code) | |
90 throw std::system_error(code); | |
91 }); | |
92 | |
93 ctx_.run(); | |
94 ctx_.reset(); | |
95 } | |
96 | |
97 socket_fixture::socket_fixture() | |
98 { | |
99 pair(); | |
100 handshake(); | |
101 } | |
102 | |
103 BOOST_FIXTURE_TEST_SUITE(socket_suite, socket_fixture) | |
104 | |
105 BOOST_AUTO_TEST_CASE(simple) | |
106 { | |
107 server_.recv([] (auto code, auto json) { | |
108 BOOST_TEST(!code); | |
109 BOOST_TEST(json["hello"].template get<std::string>() == "world"); | |
110 }); | |
111 client_.send({{ "hello", "world" }}, [] (auto code) { | |
112 BOOST_TEST(!code); | |
113 }); | |
114 ctx_.run(); | |
115 } | |
116 | |
117 BOOST_AUTO_TEST_CASE(multiple) | |
118 { | |
119 server_.recv([] (auto code, auto json) { | |
120 BOOST_TEST(!code); | |
121 BOOST_TEST(json["hello"].template get<std::string>() == "world"); | |
122 }); | |
123 client_.send({{ "hello", "world" }}, [] (auto code) { | |
124 BOOST_TEST(!code); | |
125 }); | |
126 ctx_.run(); | |
127 ctx_.reset(); | |
128 | |
129 server_.recv([] (auto code, auto json) { | |
130 BOOST_TEST(!code); | |
131 BOOST_TEST(json["hello"].template get<std::string>() == "what's up?"); | |
132 }); | |
133 client_.send({{ "hello", "what's up?" }}, [] (auto code) { | |
134 BOOST_TEST(!code); | |
135 }); | |
136 ctx_.run(); | |
137 } | |
138 | |
139 BOOST_AUTO_TEST_CASE(simple_then_too_big) | |
140 { | |
141 std::string big(102400, 'a'); | |
142 | |
143 server_.recv([] (auto code, auto json) { | |
144 BOOST_TEST(!code); | |
145 BOOST_TEST(json["hello"].template get<std::string>() == "world"); | |
146 }); | |
147 client_.send({{ "hello", "world" }}, [] (auto code) { | |
148 BOOST_TEST(!code); | |
149 }); | |
150 ctx_.run(); | |
151 ctx_.reset(); | |
152 | |
153 server_.recv([] (auto code, auto json) { | |
154 BOOST_TEST(code.value() == static_cast<int>(std::errc::argument_list_too_long)); | |
155 BOOST_TEST(json.is_null()); | |
156 }); | |
157 client_.send({{ "hello", std::move(big) }}, [] (auto code) { | |
158 BOOST_TEST(!code); | |
159 }); | |
160 ctx_.run(); | |
161 } | |
162 | |
163 BOOST_AUTO_TEST_SUITE_END() | |
164 | |
165 } // !namespace | |
166 | |
167 } // !mlk |