Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
timers.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014, Peter Thorson. All rights reserved.
3 *
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.
14 *
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.
25 *
26 */
27//#define BOOST_TEST_DYN_LINK
28#define BOOST_TEST_MODULE transport_asio_timers
29#include <boost/test/unit_test.hpp>
30
31#include <exception>
32#include <iostream>
33
35
38
39// Concurrency
41
42// HTTP
45
46// Loggers
48//#include <websocketpp/logger/basic.hpp>
49
50#include <boost/asio.hpp>
51
52// Accept a connection, read data, and discard until EOF
53void run_dummy_server(int port) {
54 using boost::asio::ip::tcp;
55
56 try {
57 boost::asio::io_service io_service;
58 tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v6(), port));
59 tcp::socket socket(io_service);
60
61 acceptor.accept(socket);
62 for (;;) {
63 char data[512];
64 boost::system::error_code ec;
65 socket.read_some(boost::asio::buffer(data), ec);
66 if (ec == boost::asio::error::eof) {
67 break;
68 } else if (ec) {
69 // other error
70 throw ec;
71 }
72 }
73 } catch (std::exception & e) {
74 std::cout << e.what() << std::endl;
75 } catch (boost::system::error_code & ec) {
76 std::cout << ec.message() << std::endl;
77 }
78}
79
80// Wait for the specified time period then fail the test
82 boost::asio::io_service ios;
83 boost::asio::deadline_timer t(ios,boost::posix_time::milliseconds(value));
84 boost::system::error_code ec;
85 t.wait(ec);
86 BOOST_FAIL( "Test timed out" );
87}
88
89struct config {
91 //typedef websocketpp::log::basic<concurrency_type,websocketpp::log::alevel> alog_type;
97
98 static const bool enable_multithreading = true;
99
100 static const long timeout_socket_pre_init = 1000;
101 static const long timeout_proxy = 1000;
102 static const long timeout_socket_post_init = 1000;
103 static const long timeout_dns_resolve = 1000;
104 static const long timeout_connect = 1000;
105 static const long timeout_socket_shutdown = 1000;
106};
107
108// Mock context that does no validation
109typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
111 return context_ptr(new boost::asio::ssl::context(boost::asio::ssl::context::sslv23));
112}
113
114// Mock connection
117
118 mock_con(bool a, config::alog_type& b, config::elog_type& c) : base(a,b,c) {}
119
120 void start() {
121 base::init(websocketpp::lib::bind(&mock_con::handle_start,this,
122 websocketpp::lib::placeholders::_1));
123 }
124
125 void handle_start(const websocketpp::lib::error_code& ec) {
128
129 BOOST_CHECK_EQUAL( ec, make_error_code(tls_handshake_timeout) );
130
131 base::cancel_socket();
132 }
133};
134
136typedef websocketpp::lib::shared_ptr<mock_con> connection_ptr;
137
140
146
147 void connect(std::string u) {
148 m_con.reset(new mock_con(false,alog,elog));
150
151 BOOST_CHECK( uri->get_valid() );
152 BOOST_CHECK_EQUAL( base::init(m_con), websocketpp::lib::error_code() );
153
155 m_con,
156 uri,
157 websocketpp::lib::bind(
159 this,
160 m_con,
161 websocketpp::lib::placeholders::_1
162 )
163 );
164 }
165
166 void handle_connect(connection_ptr con, websocketpp::lib::error_code const & ec)
167 {
168 BOOST_CHECK( !ec );
169 con->start();
170 }
171
173 config::alog_type alog;
174 config::elog_type elog;
175};
176
177BOOST_AUTO_TEST_CASE( tls_handshake_timeout ) {
178 websocketpp::lib::thread dummy_server(websocketpp::lib::bind(&run_dummy_server,9005));
179 websocketpp::lib::thread timer(websocketpp::lib::bind(&run_test_timer,5000));
180 dummy_server.detach();
181 timer.detach();
182
183 mock_endpoint endpoint;
184 endpoint.set_tls_init_handler(&on_tls_init);
185 endpoint.connect("wss://localhost:9005");
186 endpoint.run();
187}
static const long timeout_socket_shutdown
Definition timers.cpp:105
websocketpp::http::parser::response response_type
Definition timers.cpp:95
static const long timeout_connect
Definition timers.cpp:104
static const long timeout_socket_post_init
Definition timers.cpp:102
websocketpp::log::stub alog_type
Definition timers.cpp:92
websocketpp::log::stub elog_type
Definition timers.cpp:93
static const long timeout_proxy
Definition timers.cpp:101
static const long timeout_dns_resolve
Definition timers.cpp:103
websocketpp::concurrency::none concurrency_type
Definition timers.cpp:90
static const long timeout_socket_pre_init
Definition timers.cpp:100
static const bool enable_multithreading
Definition timers.cpp:98
websocketpp::transport::asio::tls_socket::endpoint socket_type
Definition timers.cpp:96
Stub concurrency policy that implements the interface using no-ops.
Definition none.hpp:60
Stores, parses, and manipulates HTTP requests.
Definition request.hpp:50
Stores, parses, and manipulates HTTP responses.
Definition response.hpp:57
Stub logger that ignores all input.
Definition stub.hpp:41
Asio based connection transport component.
void init(init_handler callback)
Initialize transport for reading.
Asio based endpoint transport component.
Definition endpoint.hpp:53
void async_connect(transport_con_ptr tcon, uri_ptr u, connect_handler cb)
Initiate a new connection.
Definition endpoint.hpp:821
void init_asio()
Initialize asio transport with internal io_service.
Definition endpoint.hpp:245
void init_logging(alog_type *a, elog_type *e)
Initialize logging.
Definition endpoint.hpp:795
std::size_t run()
wraps the run method of the internal io_service object
Definition endpoint.hpp:612
lib::error_code init(transport_con_ptr tcon)
Initialize a connection.
TLS enabled Asio endpoint socket component.
Definition tls.hpp:408
websocketpp::lib::shared_ptr< boost::asio::ssl::context > context_ptr
@ tls_handshake_timeout
TLS Handshake Timeout.
Definition base.hpp:93
lib::error_code make_error_code(error::value e)
Definition base.hpp:147
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition uri.hpp:351
#define value
Definition pkcs11.h:157
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
mock_con(bool a, config::alog_type &b, config::elog_type &c)
Definition timers.cpp:118
void start()
Definition timers.cpp:120
void handle_start(const websocketpp::lib::error_code &ec)
Definition timers.cpp:125
websocketpp::transport::asio::connection< config > base
Definition timers.cpp:116
config::alog_type alog
Definition timers.cpp:173
config::elog_type elog
Definition timers.cpp:174
connection_ptr m_con
Definition timers.cpp:172
void handle_connect(connection_ptr con, websocketpp::lib::error_code const &ec)
Definition timers.cpp:166
void connect(std::string u)
Definition timers.cpp:147
websocketpp::transport::asio::endpoint< config > base
Definition timers.cpp:139
static level const all
Special aggregate value representing "all levels".
Definition levels.hpp:152
void run_test_timer(long value)
Definition timers.cpp:81
context_ptr on_tls_init(websocketpp::connection_hdl)
Definition timers.cpp:110
BOOST_AUTO_TEST_CASE(tls_handshake_timeout)
Definition timers.cpp:177
websocketpp::lib::shared_ptr< boost::asio::ssl::context > context_ptr
Definition timers.cpp:109
void run_dummy_server(int port)
Definition timers.cpp:53
websocketpp::lib::shared_ptr< mock_con > connection_ptr
Definition timers.cpp:136
websocketpp::transport::asio::connection< config > con_type
Definition timers.cpp:135