]>
Commit | Line | Data |
---|---|---|
1 | // | |
2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | // Official repository: https://github.com/boostorg/beast | |
8 | // | |
9 | ||
10 | // Test that header file is self-contained. | |
11 | #include <boost/beast/websocket/stream.hpp> | |
12 | ||
13 | #include <boost/beast/_experimental/test/tcp.hpp> | |
14 | ||
15 | #include "test.hpp" | |
16 | ||
17 | #include <boost/asio/ip/tcp.hpp> | |
18 | #include <boost/asio/io_context.hpp> | |
19 | #include <boost/asio/strand.hpp> | |
20 | ||
21 | namespace boost { | |
22 | namespace beast { | |
23 | namespace websocket { | |
24 | ||
25 | class ping_test : public websocket_test_suite | |
26 | { | |
27 | public: | |
28 | template<class Wrap> | |
29 | void | |
30 | doTestPing(Wrap const& w) | |
31 | { | |
32 | permessage_deflate pmd; | |
33 | pmd.client_enable = false; | |
34 | pmd.server_enable = false; | |
35 | ||
36 | // ping | |
37 | doTest(pmd, [&](ws_type& ws) | |
38 | { | |
39 | w.ping(ws, {}); | |
40 | }); | |
41 | ||
42 | // pong | |
43 | doTest(pmd, [&](ws_type& ws) | |
44 | { | |
45 | w.pong(ws, {}); | |
46 | }); | |
47 | ||
48 | // ping, already closed | |
49 | { | |
50 | echo_server es{log}; | |
51 | stream<test::stream> ws{ioc_}; | |
52 | ws.next_layer().connect(es.stream()); | |
53 | ws.handshake("localhost", "/"); | |
54 | ws.close({}); | |
55 | try | |
56 | { | |
57 | w.ping(ws, {}); | |
58 | fail("", __FILE__, __LINE__); | |
59 | } | |
60 | catch(system_error const& se) | |
61 | { | |
62 | BEAST_EXPECTS( | |
63 | se.code() == net::error::operation_aborted, | |
64 | se.code().message()); | |
65 | } | |
66 | } | |
67 | ||
68 | // pong, already closed | |
69 | { | |
70 | echo_server es{log}; | |
71 | stream<test::stream> ws{ioc_}; | |
72 | ws.next_layer().connect(es.stream()); | |
73 | ws.handshake("localhost", "/"); | |
74 | ws.close({}); | |
75 | try | |
76 | { | |
77 | w.pong(ws, {}); | |
78 | fail("", __FILE__, __LINE__); | |
79 | } | |
80 | catch(system_error const& se) | |
81 | { | |
82 | BEAST_EXPECTS( | |
83 | se.code() == net::error::operation_aborted, | |
84 | se.code().message()); | |
85 | } | |
86 | } | |
87 | } | |
88 | ||
89 | void | |
90 | testPing() | |
91 | { | |
92 | doTestPing(SyncClient{}); | |
93 | ||
94 | yield_to([&](yield_context yield) | |
95 | { | |
96 | doTestPing(AsyncClient{yield}); | |
97 | }); | |
98 | } | |
99 | ||
100 | void | |
101 | testSuspend() | |
102 | { | |
103 | // suspend on write | |
104 | doFailLoop([&](test::fail_count& fc) | |
105 | { | |
106 | echo_server es{log}; | |
107 | net::io_context ioc; | |
108 | stream<test::stream> ws{ioc, fc}; | |
109 | ws.next_layer().connect(es.stream()); | |
110 | ws.handshake("localhost", "/"); | |
111 | std::size_t count = 0; | |
112 | ws.async_write(sbuf("Hello, world"), | |
113 | [&](error_code ec, std::size_t n) | |
114 | { | |
115 | ++count; | |
116 | if(ec) | |
117 | BOOST_THROW_EXCEPTION( | |
118 | system_error{ec}); | |
119 | BEAST_EXPECT(n == 12); | |
120 | }); | |
121 | BEAST_EXPECT(ws.impl_->wr_block.is_locked()); | |
122 | BEAST_EXPECT(count == 0); | |
123 | ws.async_ping({}, | |
124 | [&](error_code ec) | |
125 | { | |
126 | ++count; | |
127 | if(ec) | |
128 | BOOST_THROW_EXCEPTION( | |
129 | system_error{ec}); | |
130 | }); | |
131 | BEAST_EXPECT(count == 0); | |
132 | ioc.run(); | |
133 | BEAST_EXPECT(count == 2); | |
134 | }); | |
135 | ||
136 | // suspend on close | |
137 | doFailLoop([&](test::fail_count& fc) | |
138 | { | |
139 | echo_server es{log}; | |
140 | net::io_context ioc; | |
141 | stream<test::stream> ws{ioc, fc}; | |
142 | ws.next_layer().connect(es.stream()); | |
143 | ws.handshake("localhost", "/"); | |
144 | std::size_t count = 0; | |
145 | ws.async_close({}, | |
146 | [&](error_code ec) | |
147 | { | |
148 | ++count; | |
149 | if(ec) | |
150 | BOOST_THROW_EXCEPTION( | |
151 | system_error{ec}); | |
152 | }); | |
153 | BEAST_EXPECT(ws.impl_->wr_block.is_locked()); | |
154 | BEAST_EXPECT(count == 0); | |
155 | ws.async_ping({}, | |
156 | [&](error_code ec) | |
157 | { | |
158 | ++count; | |
159 | if(ec != net::error::operation_aborted) | |
160 | BOOST_THROW_EXCEPTION( | |
161 | system_error{ec}); | |
162 | }); | |
163 | BEAST_EXPECT(count == 0); | |
164 | ioc.run(); | |
165 | BEAST_EXPECT(count == 2); | |
166 | }); | |
167 | ||
168 | // suspend on read ping + message | |
169 | doFailLoop([&](test::fail_count& fc) | |
170 | { | |
171 | echo_server es{log}; | |
172 | net::io_context ioc; | |
173 | stream<test::stream> ws{ioc, fc}; | |
174 | ws.next_layer().connect(es.stream()); | |
175 | ws.handshake("localhost", "/"); | |
176 | // add a ping and message to the input | |
177 | ws.next_layer().append(string_view{ | |
178 | "\x89\x00" "\x81\x01*", 5}); | |
179 | std::size_t count = 0; | |
180 | multi_buffer b; | |
181 | ws.async_read(b, | |
182 | [&](error_code ec, std::size_t) | |
183 | { | |
184 | ++count; | |
185 | if(ec) | |
186 | BOOST_THROW_EXCEPTION( | |
187 | system_error{ec}); | |
188 | }); | |
189 | while(! ws.impl_->wr_block.is_locked()) | |
190 | { | |
191 | ioc.run_one(); | |
192 | if(! BEAST_EXPECT(! ioc.stopped())) | |
193 | break; | |
194 | } | |
195 | BEAST_EXPECT(count == 0); | |
196 | ws.async_ping({}, | |
197 | [&](error_code ec) | |
198 | { | |
199 | ++count; | |
200 | if(ec) | |
201 | BOOST_THROW_EXCEPTION( | |
202 | system_error{ec}); | |
203 | }); | |
204 | BEAST_EXPECT(count == 0); | |
205 | ioc.run(); | |
206 | BEAST_EXPECT(count == 2); | |
207 | }); | |
208 | ||
209 | // suspend on read bad message | |
210 | doFailLoop([&](test::fail_count& fc) | |
211 | { | |
212 | echo_server es{log}; | |
213 | net::io_context ioc; | |
214 | stream<test::stream> ws{ioc, fc}; | |
215 | ws.next_layer().connect(es.stream()); | |
216 | ws.handshake("localhost", "/"); | |
217 | // add an invalid frame to the input | |
218 | ws.next_layer().append(string_view{ | |
219 | "\x09\x00", 2}); | |
220 | ||
221 | std::size_t count = 0; | |
222 | multi_buffer b; | |
223 | ws.async_read(b, | |
224 | [&](error_code ec, std::size_t) | |
225 | { | |
226 | ++count; | |
227 | if(ec != error::bad_control_fragment) | |
228 | BOOST_THROW_EXCEPTION( | |
229 | system_error{ec}); | |
230 | }); | |
231 | while(! ws.impl_->wr_block.is_locked()) | |
232 | { | |
233 | ioc.run_one(); | |
234 | if(! BEAST_EXPECT(! ioc.stopped())) | |
235 | break; | |
236 | } | |
237 | BEAST_EXPECT(count == 0); | |
238 | ws.async_ping({}, | |
239 | [&](error_code ec) | |
240 | { | |
241 | ++count; | |
242 | if(ec != net::error::operation_aborted) | |
243 | BOOST_THROW_EXCEPTION( | |
244 | system_error{ec}); | |
245 | }); | |
246 | BEAST_EXPECT(count == 0); | |
247 | ioc.run(); | |
248 | BEAST_EXPECT(count == 2); | |
249 | }); | |
250 | ||
251 | // suspend on read close #1 | |
252 | doFailLoop([&](test::fail_count& fc) | |
253 | { | |
254 | echo_server es{log}; | |
255 | net::io_context ioc; | |
256 | stream<test::stream> ws{ioc, fc}; | |
257 | ws.next_layer().connect(es.stream()); | |
258 | ws.handshake("localhost", "/"); | |
259 | // add a close frame to the input | |
260 | ws.next_layer().append(string_view{ | |
261 | "\x88\x00", 2}); | |
262 | std::size_t count = 0; | |
263 | multi_buffer b; | |
264 | ws.async_read(b, | |
265 | [&](error_code ec, std::size_t) | |
266 | { | |
267 | ++count; | |
268 | if(ec != error::closed) | |
269 | BOOST_THROW_EXCEPTION( | |
270 | system_error{ec}); | |
271 | }); | |
272 | while(! ws.impl_->wr_block.is_locked()) | |
273 | { | |
274 | ioc.run_one(); | |
275 | if(! BEAST_EXPECT(! ioc.stopped())) | |
276 | break; | |
277 | } | |
278 | BEAST_EXPECT(count == 0); | |
279 | ws.async_ping({}, | |
280 | [&](error_code ec) | |
281 | { | |
282 | ++count; | |
283 | if(ec != net::error::operation_aborted) | |
284 | BOOST_THROW_EXCEPTION( | |
285 | system_error{ec}); | |
286 | }); | |
287 | BEAST_EXPECT(count == 0); | |
288 | ioc.run(); | |
289 | BEAST_EXPECT(count == 2); | |
290 | }); | |
291 | ||
292 | // suspend on read close #2 | |
293 | doFailLoop([&](test::fail_count& fc) | |
294 | { | |
295 | echo_server es{log, kind::async}; | |
296 | net::io_context ioc; | |
297 | stream<test::stream> ws{ioc, fc}; | |
298 | ws.next_layer().connect(es.stream()); | |
299 | ws.handshake("localhost", "/"); | |
300 | // Cause close to be received | |
301 | es.async_close(); | |
302 | std::size_t count = 0; | |
303 | multi_buffer b; | |
304 | ws.async_read(b, | |
305 | [&](error_code ec, std::size_t) | |
306 | { | |
307 | ++count; | |
308 | if(ec != error::closed) | |
309 | BOOST_THROW_EXCEPTION( | |
310 | system_error{ec}); | |
311 | }); | |
312 | while(! ws.impl_->wr_block.is_locked()) | |
313 | { | |
314 | ioc.run_one(); | |
315 | if(! BEAST_EXPECT(! ioc.stopped())) | |
316 | break; | |
317 | } | |
318 | BEAST_EXPECT(count == 0); | |
319 | ws.async_ping({}, | |
320 | [&](error_code ec) | |
321 | { | |
322 | ++count; | |
323 | if(ec != net::error::operation_aborted) | |
324 | BOOST_THROW_EXCEPTION( | |
325 | system_error{ec}); | |
326 | }); | |
327 | BEAST_EXPECT(count == 0); | |
328 | ioc.run(); | |
329 | BEAST_EXPECT(count == 2); | |
330 | }); | |
331 | ||
332 | // don't ping on close | |
333 | doFailLoop([&](test::fail_count& fc) | |
334 | { | |
335 | echo_server es{log}; | |
336 | error_code ec; | |
337 | net::io_context ioc; | |
338 | stream<test::stream> ws{ioc, fc}; | |
339 | ws.next_layer().connect(es.stream()); | |
340 | ws.handshake("localhost", "/"); | |
341 | std::size_t count = 0; | |
342 | ws.async_write(sbuf("*"), | |
343 | [&](error_code ec, std::size_t n) | |
344 | { | |
345 | ++count; | |
346 | if(ec) | |
347 | BOOST_THROW_EXCEPTION( | |
348 | system_error{ec}); | |
349 | BEAST_EXPECT(n == 1); | |
350 | }); | |
351 | BEAST_EXPECT(ws.impl_->wr_block.is_locked()); | |
352 | ws.async_ping("", | |
353 | [&](error_code ec) | |
354 | { | |
355 | ++count; | |
356 | if(ec != net::error::operation_aborted) | |
357 | BOOST_THROW_EXCEPTION( | |
358 | system_error{ec}); | |
359 | }); | |
360 | ws.async_close({}, | |
361 | [&](error_code) | |
362 | { | |
363 | ++count; | |
364 | if(ec) | |
365 | BOOST_THROW_EXCEPTION( | |
366 | system_error{ec}); | |
367 | }); | |
368 | ioc.run(); | |
369 | BEAST_EXPECT(count == 3); | |
370 | }); | |
371 | ||
372 | // suspend idle ping | |
373 | { | |
374 | using socket_type = | |
375 | net::basic_stream_socket< | |
376 | net::ip::tcp, | |
377 | net::executor>; | |
378 | net::io_context ioc; | |
379 | stream<socket_type> ws1(ioc); | |
380 | stream<socket_type> ws2(ioc); | |
381 | ws1.set_option(stream_base::timeout{ | |
382 | stream_base::none(), | |
383 | std::chrono::seconds(0), | |
384 | true}); | |
385 | test::connect( | |
386 | ws1.next_layer(), | |
387 | ws2.next_layer()); | |
388 | ws1.async_handshake("localhost", "/", | |
389 | [](error_code){}); | |
390 | ws2.async_accept([](error_code){}); | |
391 | ioc.run(); | |
392 | ioc.restart(); | |
393 | flat_buffer b1; | |
394 | auto mb = b1.prepare(65536); | |
395 | std::memset(mb.data(), 0, mb.size()); | |
396 | b1.commit(65536); | |
397 | ws1.async_write(b1.data(), | |
398 | [&](error_code, std::size_t){}); | |
399 | BEAST_EXPECT( | |
400 | ws1.impl_->wr_block.is_locked()); | |
401 | ws1.async_read_some(net::mutable_buffer{}, | |
402 | [&](error_code, std::size_t){}); | |
403 | ioc.run(); | |
404 | ioc.restart(); | |
405 | flat_buffer b2; | |
406 | ws2.async_read(b2, | |
407 | [&](error_code, std::size_t){}); | |
408 | ioc.run(); | |
409 | } | |
410 | //); | |
411 | ||
412 | { | |
413 | echo_server es{log, kind::async}; | |
414 | net::io_context ioc; | |
415 | stream<test::stream> ws{ioc}; | |
416 | ws.next_layer().connect(es.stream()); | |
417 | ws.handshake("localhost", "/"); | |
418 | ||
419 | // Cause close to be received | |
420 | es.async_close(); | |
421 | ||
422 | multi_buffer b; | |
423 | std::size_t count = 0; | |
424 | // Read a close frame. | |
425 | // Sends a close frame, blocking writes. | |
426 | ws.async_read(b, | |
427 | [&](error_code ec, std::size_t) | |
428 | { | |
429 | // Read should complete with error::closed | |
430 | ++count; | |
431 | BEAST_EXPECTS(ec == error::closed, | |
432 | ec.message()); | |
433 | }); | |
434 | if(! BEAST_EXPECT(run_until(ioc, 100, | |
435 | [&]{ return ws.impl_->wr_close; }))) | |
436 | return; | |
437 | // Try to ping | |
438 | ws.async_ping("payload", | |
439 | [&](error_code ec) | |
440 | { | |
441 | // Pings after a close are aborted | |
442 | ++count; | |
443 | BEAST_EXPECTS(ec == net:: | |
444 | error::operation_aborted, | |
445 | ec.message()); | |
446 | // Subsequent calls to close are aborted | |
447 | ws.async_close({}, | |
448 | [&](error_code ec) | |
449 | { | |
450 | ++count; | |
451 | BEAST_EXPECTS(ec == net:: | |
452 | error::operation_aborted, | |
453 | ec.message()); | |
454 | }); | |
455 | }); | |
456 | static std::size_t constexpr limit = 100; | |
457 | std::size_t n; | |
458 | for(n = 0; n < limit; ++n) | |
459 | { | |
460 | if(count >= 3) | |
461 | break; | |
462 | ioc.run_one(); | |
463 | } | |
464 | BEAST_EXPECT(n < limit); | |
465 | ioc.run(); | |
466 | } | |
467 | } | |
468 | ||
469 | void | |
470 | testMoveOnly() | |
471 | { | |
472 | net::io_context ioc; | |
473 | stream<test::stream> ws{ioc}; | |
474 | ws.async_ping({}, move_only_handler{}); | |
475 | } | |
476 | ||
477 | struct copyable_handler | |
478 | { | |
479 | template<class... Args> | |
480 | void | |
481 | operator()(Args&&...) const | |
482 | { | |
483 | } | |
484 | }; | |
485 | ||
486 | void | |
487 | run() override | |
488 | { | |
489 | testPing(); | |
490 | testSuspend(); | |
491 | testMoveOnly(); | |
492 | } | |
493 | }; | |
494 | ||
495 | BEAST_DEFINE_TESTSUITE(beast,websocket,ping); | |
496 | ||
497 | } // websocket | |
498 | } // beast | |
499 | } // boost |