2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of the WebSocket++ Project nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //#define BOOST_TEST_DYN_LINK
28 #define BOOST_TEST_MODULE transport_integration
29 #include <boost/test/unit_test.hpp>
31 #include <websocketpp/common/thread.hpp>
33 #include <websocketpp/config/core.hpp>
34 #include <websocketpp/config/core_client.hpp>
35 #include <websocketpp/config/asio.hpp>
36 #include <websocketpp/config/asio_client.hpp>
37 #include <websocketpp/config/debug_asio.hpp>
38 #include <websocketpp/server.hpp>
39 #include <websocketpp/client.hpp>
41 struct config : public websocketpp::config::asio_client {
43 typedef websocketpp::config::asio base;
45 typedef base::concurrency_type concurrency_type;
47 typedef base::request_type request_type;
48 typedef base::response_type response_type;
50 typedef base::message_type message_type;
51 typedef base::con_msg_manager_type con_msg_manager_type;
52 typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
54 typedef base::alog_type alog_type;
55 typedef base::elog_type elog_type;
57 typedef base::rng_type rng_type;
59 struct transport_config : public base::transport_config {
60 typedef type::concurrency_type concurrency_type;
61 typedef type::alog_type alog_type;
62 typedef type::elog_type elog_type;
63 typedef type::request_type request_type;
64 typedef type::response_type response_type;
65 typedef websocketpp::transport::asio::basic_socket::endpoint
69 typedef websocketpp::transport::asio::endpoint<transport_config>
72 //static const websocketpp::log::level elog_level = websocketpp::log::elevel::all;
73 //static const websocketpp::log::level alog_level = websocketpp::log::alevel::all;
75 /// Length of time before an opening handshake is aborted
76 static const long timeout_open_handshake = 500;
77 /// Length of time before a closing handshake is aborted
78 static const long timeout_close_handshake = 500;
79 /// Length of time to wait for a pong after a ping
80 static const long timeout_pong = 500;
83 struct config_tls : public websocketpp::config::asio_tls_client {
85 typedef websocketpp::config::asio base;
87 typedef base::concurrency_type concurrency_type;
89 typedef base::request_type request_type;
90 typedef base::response_type response_type;
92 typedef base::message_type message_type;
93 typedef base::con_msg_manager_type con_msg_manager_type;
94 typedef base::endpoint_msg_manager_type endpoint_msg_manager_type;
96 typedef base::alog_type alog_type;
97 typedef base::elog_type elog_type;
99 typedef base::rng_type rng_type;
101 struct transport_config : public base::transport_config {
102 typedef type::concurrency_type concurrency_type;
103 typedef type::alog_type alog_type;
104 typedef type::elog_type elog_type;
105 typedef type::request_type request_type;
106 typedef type::response_type response_type;
107 typedef websocketpp::transport::asio::basic_socket::endpoint
111 typedef websocketpp::transport::asio::endpoint<transport_config>
114 //static const websocketpp::log::level elog_level = websocketpp::log::elevel::all;
115 //static const websocketpp::log::level alog_level = websocketpp::log::alevel::all;
117 /// Length of time before an opening handshake is aborted
118 static const long timeout_open_handshake = 500;
119 /// Length of time before a closing handshake is aborted
120 static const long timeout_close_handshake = 500;
121 /// Length of time to wait for a pong after a ping
122 static const long timeout_pong = 500;
125 typedef websocketpp::server<config> server;
126 typedef websocketpp::client<config> client;
128 typedef websocketpp::server<config_tls> server_tls;
129 typedef websocketpp::client<config_tls> client_tls;
131 typedef websocketpp::server<websocketpp::config::core> iostream_server;
132 typedef websocketpp::client<websocketpp::config::core_client> iostream_client;
134 using websocketpp::lib::placeholders::_1;
135 using websocketpp::lib::placeholders::_2;
136 using websocketpp::lib::bind;
138 template <typename T>
139 void close_after_timeout(T & e, websocketpp::connection_hdl hdl, long timeout) {
142 websocketpp::lib::error_code ec;
143 e.close(hdl,websocketpp::close::status::normal,"",ec);
147 void run_server(server * s, int port, bool log = false) {
149 s->set_access_channels(websocketpp::log::alevel::all);
150 s->set_error_channels(websocketpp::log::elevel::all);
152 s->clear_access_channels(websocketpp::log::alevel::all);
153 s->clear_error_channels(websocketpp::log::elevel::all);
157 s->set_reuse_addr(true);
164 void run_client(client & c, std::string uri, bool log = false) {
166 c.set_access_channels(websocketpp::log::alevel::all);
167 c.set_error_channels(websocketpp::log::elevel::all);
169 c.clear_access_channels(websocketpp::log::alevel::all);
170 c.clear_error_channels(websocketpp::log::elevel::all);
172 websocketpp::lib::error_code ec;
174 c.set_reuse_addr(true);
177 client::connection_ptr con = c.get_connection(uri,ec);
184 void run_client_and_mark(client * c, bool * flag, websocketpp::lib::mutex * mutex) {
187 websocketpp::lib::lock_guard<websocketpp::lib::mutex> lock(*mutex);
192 void run_time_limited_client(client & c, std::string uri, long timeout,
196 c.set_access_channels(websocketpp::log::alevel::all);
197 c.set_error_channels(websocketpp::log::elevel::all);
199 c.clear_access_channels(websocketpp::log::alevel::all);
200 c.clear_error_channels(websocketpp::log::elevel::all);
204 websocketpp::lib::error_code ec;
205 client::connection_ptr con = c.get_connection(uri,ec);
209 websocketpp::lib::thread tthread(websocketpp::lib::bind(
210 &close_after_timeout<client>,
211 websocketpp::lib::ref(c),
220 void run_dummy_server(int port) {
221 using boost::asio::ip::tcp;
224 boost::asio::io_service io_service;
225 tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v6(), port));
226 tcp::socket socket(io_service);
228 acceptor.accept(socket);
231 boost::system::error_code ec;
232 socket.read_some(boost::asio::buffer(data), ec);
233 if (ec == boost::asio::error::eof) {
240 } catch (std::exception & e) {
241 std::cout << e.what() << std::endl;
242 } catch (boost::system::error_code & ec) {
243 std::cout << ec.message() << std::endl;
247 void run_dummy_client(std::string port) {
248 using boost::asio::ip::tcp;
251 boost::asio::io_service io_service;
252 tcp::resolver resolver(io_service);
253 tcp::resolver::query query("localhost", port);
254 tcp::resolver::iterator iterator = resolver.resolve(query);
255 tcp::socket socket(io_service);
257 boost::asio::connect(socket, iterator);
260 boost::system::error_code ec;
261 socket.read_some(boost::asio::buffer(data), ec);
262 if (ec == boost::asio::error::eof) {
269 } catch (std::exception & e) {
270 std::cout << e.what() << std::endl;
271 } catch (boost::system::error_code & ec) {
272 std::cout << ec.message() << std::endl;
276 bool on_ping(server * s, websocketpp::connection_hdl, std::string) {
277 s->get_alog().write(websocketpp::log::alevel::app,"got ping");
281 void cancel_on_open(server * s, websocketpp::connection_hdl) {
285 void stop_on_close(server * s, websocketpp::connection_hdl hdl) {
286 server::connection_ptr con = s->get_con_from_hdl(hdl);
287 //BOOST_CHECK_EQUAL( con->get_local_close_code(), websocketpp::close::status::normal );
288 //BOOST_CHECK_EQUAL( con->get_remote_close_code(), websocketpp::close::status::normal );
292 template <typename T>
293 void ping_on_open(T * c, std::string payload, websocketpp::connection_hdl hdl) {
294 typename T::connection_ptr con = c->get_con_from_hdl(hdl);
295 websocketpp::lib::error_code ec;
296 con->ping(payload,ec);
297 BOOST_CHECK_EQUAL(ec, websocketpp::lib::error_code());
300 void fail_on_pong(websocketpp::connection_hdl, std::string) {
301 BOOST_FAIL( "expected no pong handler" );
304 void fail_on_pong_timeout(websocketpp::connection_hdl, std::string) {
305 BOOST_FAIL( "expected no pong timeout" );
308 void req_pong(std::string expected_payload, websocketpp::connection_hdl,
311 BOOST_CHECK_EQUAL( expected_payload, payload );
314 void fail_on_open(websocketpp::connection_hdl) {
315 BOOST_FAIL( "expected no open handler" );
318 void delay(websocketpp::connection_hdl, long duration) {
322 template <typename T>
323 void check_ec(T * c, websocketpp::lib::error_code ec,
324 websocketpp::connection_hdl hdl)
326 typename T::connection_ptr con = c->get_con_from_hdl(hdl);
327 BOOST_CHECK_EQUAL( con->get_ec(), ec );
328 //BOOST_CHECK_EQUAL( con->get_local_close_code(), websocketpp::close::status::normal );
329 //BOOST_CHECK_EQUAL( con->get_remote_close_code(), websocketpp::close::status::normal );
332 template <typename T>
333 void check_ec_and_stop(T * e, websocketpp::lib::error_code ec,
334 websocketpp::connection_hdl hdl)
336 typename T::connection_ptr con = e->get_con_from_hdl(hdl);
337 BOOST_CHECK_EQUAL( con->get_ec(), ec );
338 //BOOST_CHECK_EQUAL( con->get_local_close_code(), websocketpp::close::status::normal );
339 //BOOST_CHECK_EQUAL( con->get_remote_close_code(), websocketpp::close::status::normal );
343 template <typename T>
344 void req_pong_timeout(T * c, std::string expected_payload,
345 websocketpp::connection_hdl hdl, std::string payload)
347 typename T::connection_ptr con = c->get_con_from_hdl(hdl);
348 BOOST_CHECK_EQUAL( payload, expected_payload );
349 con->close(websocketpp::close::status::normal,"");
352 template <typename T>
353 void close(T * e, websocketpp::connection_hdl hdl) {
354 e->get_con_from_hdl(hdl)->close(websocketpp::close::status::normal,"");
357 // Wait for the specified time period then fail the test
358 void run_test_timer(long value) {
360 BOOST_FAIL( "Test timed out" );
363 BOOST_AUTO_TEST_CASE( pong_no_timeout ) {
367 s.set_close_handler(bind(&stop_on_close,&s,::_1));
369 // send a ping when the connection is open
370 c.set_open_handler(bind(&ping_on_open<client>,&c,"foo",::_1));
371 // require that a pong with matching payload is received
372 c.set_pong_handler(bind(&req_pong,"foo",::_1,::_2));
373 // require that a pong timeout is NOT received
374 c.set_pong_timeout_handler(bind(&fail_on_pong_timeout,::_1,::_2));
376 websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
378 // Run a client that closes the connection after 1 seconds
379 run_time_limited_client(c, "http://localhost:9005", 1, false);
384 BOOST_AUTO_TEST_CASE( pong_timeout ) {
388 s.set_ping_handler(bind(&on_ping, &s,::_1,::_2));
389 s.set_close_handler(bind(&stop_on_close,&s,::_1));
391 c.set_fail_handler(bind(&check_ec<client>,&c,
392 websocketpp::lib::error_code(),::_1));
394 c.set_pong_handler(bind(&fail_on_pong,::_1,::_2));
395 c.set_open_handler(bind(&ping_on_open<client>,&c,"foo",::_1));
396 c.set_pong_timeout_handler(bind(&req_pong_timeout<client>,&c,"foo",::_1,::_2));
397 c.set_close_handler(bind(&check_ec<client>,&c,
398 websocketpp::lib::error_code(),::_1));
400 websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
401 websocketpp::lib::thread tthread(websocketpp::lib::bind(&run_test_timer,10));
404 run_client(c, "http://localhost:9005",false);
409 BOOST_AUTO_TEST_CASE( client_open_handshake_timeout ) {
412 // set open handler to fail test
413 c.set_open_handler(bind(&fail_on_open,::_1));
414 // set fail hander to test for the right fail error code
415 c.set_fail_handler(bind(&check_ec<client>,&c,
416 websocketpp::error::open_handshake_timeout,::_1));
418 websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_dummy_server,9005));
419 websocketpp::lib::thread tthread(websocketpp::lib::bind(&run_test_timer,10));
423 run_client(c, "http://localhost:9005");
426 BOOST_AUTO_TEST_CASE( server_open_handshake_timeout ) {
429 // set open handler to fail test
430 s.set_open_handler(bind(&fail_on_open,::_1));
431 // set fail hander to test for the right fail error code
432 s.set_fail_handler(bind(&check_ec_and_stop<server>,&s,
433 websocketpp::error::open_handshake_timeout,::_1));
435 websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
436 websocketpp::lib::thread tthread(websocketpp::lib::bind(&run_test_timer,10));
439 run_dummy_client("9005");
444 BOOST_AUTO_TEST_CASE( client_self_initiated_close_handshake_timeout ) {
448 // on open server sleeps for longer than the timeout
449 // on open client sends close handshake
450 // client handshake timer should be triggered
451 s.set_open_handler(bind(&delay,::_1,1));
452 s.set_close_handler(bind(&stop_on_close,&s,::_1));
454 c.set_open_handler(bind(&close<client>,&c,::_1));
455 c.set_close_handler(bind(&check_ec<client>,&c,
456 websocketpp::error::close_handshake_timeout,::_1));
458 websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
459 websocketpp::lib::thread tthread(websocketpp::lib::bind(&run_test_timer,10));
462 run_client(c, "http://localhost:9005", false);
467 BOOST_AUTO_TEST_CASE( client_peer_initiated_close_handshake_timeout ) {
468 // on open server sends close
469 // client should ack normally and then wait
470 // server leaves TCP connection open
471 // client handshake timer should be triggered
473 // TODO: how to make a mock server that leaves the TCP connection open?
476 BOOST_AUTO_TEST_CASE( server_self_initiated_close_handshake_timeout ) {
480 // on open server sends close
481 // on open client sleeps for longer than the timeout
482 // server handshake timer should be triggered
484 s.set_open_handler(bind(&close<server>,&s,::_1));
485 s.set_close_handler(bind(&check_ec_and_stop<server>,&s,
486 websocketpp::error::close_handshake_timeout,::_1));
488 c.set_open_handler(bind(&delay,::_1,1));
490 websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
491 websocketpp::lib::thread tthread(websocketpp::lib::bind(&run_test_timer,10));
494 run_client(c, "http://localhost:9005",false);
499 BOOST_AUTO_TEST_CASE( client_runs_out_of_work ) {
502 websocketpp::lib::thread tthread(websocketpp::lib::bind(&run_test_timer,3));
505 websocketpp::lib::error_code ec;
511 // This test checks that an io_service with no work ends immediately.
518 BOOST_AUTO_TEST_CASE( client_is_perpetual ) {
521 websocketpp::lib::mutex mutex;
523 websocketpp::lib::error_code ec;
529 websocketpp::lib::thread cthread(websocketpp::lib::bind(&run_client_and_mark,&c,&flag,&mutex));
534 // Checks that the thread hasn't exited yet
535 websocketpp::lib::lock_guard<websocketpp::lib::mutex> lock(mutex);
536 BOOST_CHECK( !flag );
544 // Checks that the thread has exited
545 websocketpp::lib::lock_guard<websocketpp::lib::mutex> lock(mutex);
552 BOOST_AUTO_TEST_CASE( client_failed_connection ) {
555 run_time_limited_client(c,"http://localhost:9005", 5, false);
558 BOOST_AUTO_TEST_CASE( stop_listening ) {
562 // the first connection stops the server from listening
563 s.set_open_handler(bind(&cancel_on_open,&s,::_1));
565 // client immediately closes after opening a connection
566 c.set_open_handler(bind(&close<client>,&c,::_1));
568 websocketpp::lib::thread sthread(websocketpp::lib::bind(&run_server,&s,9005,false));
569 websocketpp::lib::thread tthread(websocketpp::lib::bind(&run_test_timer,5));
572 run_client(c, "http://localhost:9005",false);
577 BOOST_AUTO_TEST_CASE( pause_reading ) {
579 std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
580 char buffer[2] = { char(0x81), char(0x80) };
582 // suppress output (it needs a place to go to avoid error but we don't care what it is)
583 std::stringstream null_output;
584 s.register_ostream(&null_output);
586 iostream_server::connection_ptr con = s.get_connection();
589 // read handshake, should work
590 BOOST_CHECK_EQUAL( con->read_some(handshake.data(), handshake.length()), handshake.length());
592 // pause reading and try again. The first read should work, the second should return 0
593 // the first read was queued already after the handshake so it will go through because
594 // reading wasn't paused when it was queued. The byte it reads wont be enough to
595 // complete the frame so another read will be requested. This one wont actually happen
596 // because the connection is paused now.
597 con->pause_reading();
598 BOOST_CHECK_EQUAL( con->read_some(buffer, 1), 1);
599 BOOST_CHECK_EQUAL( con->read_some(buffer+1, 1), 0);
600 // resume reading and try again. Should work this time because the resume should have
602 con->resume_reading();
603 BOOST_CHECK_EQUAL( con->read_some(buffer+1, 1), 1);
607 BOOST_AUTO_TEST_CASE( server_connection_cleanup ) {
611 #ifdef _WEBSOCKETPP_MOVE_SEMANTICS_
612 BOOST_AUTO_TEST_CASE( move_construct_transport ) {
615 server s2(std::move(s1));
617 #endif // _WEBSOCKETPP_MOVE_SEMANTICS_