NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
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
53 void 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
81 void run_test_timer(long value) {
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 
89 struct 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
109 typedef 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 
136 typedef websocketpp::lib::shared_ptr<mock_con> connection_ptr;
137 
140 
142  alog.set_channels(websocketpp::log::alevel::all);
143  base::init_logging(&alog,&elog);
144  init_asio();
145  }
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 
154  base::async_connect(
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 
175 };
176 
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;
185  endpoint.connect("wss://localhost:9005");
186  endpoint.run();
187 }
static level const all
Special aggregate value representing "all levels".
Definition: levels.hpp:152
websocketpp::transport::asio::connection< config > con_type
Definition: timers.cpp:135
static const bool enable_multithreading
Definition: timers.cpp:98
Asio based endpoint transport component.
Definition: base.hpp:143
static const long timeout_socket_shutdown
Definition: timers.cpp:105
lib::error_code make_error_code(error::value e)
Definition: error.hpp:235
void handle_connect(connection_ptr con, websocketpp::lib::error_code const &ec)
Definition: timers.cpp:166
void start()
Definition: timers.cpp:120
boost::posix_time::time_duration milliseconds(long duration)
Definition: asio.hpp:117
websocketpp::transport::asio::tls_socket::endpoint socket_type
Definition: timers.cpp:96
websocketpp::http::parser::response response_type
Definition: timers.cpp:95
lib::weak_ptr< void > connection_hdl
A handle to uniquely identify a connection.
config::elog_type elog
Definition: timers.cpp:174
websocketpp::http::parser::request request_type
Definition: timers.cpp:94
mock_con(bool a, config::alog_type &b, config::elog_type &c)
Definition: timers.cpp:118
Stores, parses, and manipulates HTTP responses.
Definition: response.hpp:57
void run_test_timer(long value)
Definition: timers.cpp:81
void set_tls_init_handler(tls_init_handler h)
Set TLS init handler.
Definition: tls.hpp:455
websocketpp::transport::asio::endpoint< config > base
Definition: timers.cpp:139
websocketpp::log::stub alog_type
Definition: timers.cpp:92
static const long timeout_connect
Definition: timers.cpp:104
connection_ptr m_con
Definition: timers.cpp:172
websocketpp::concurrency::none concurrency_type
Definition: timers.cpp:90
static const long timeout_socket_pre_init
Definition: timers.cpp:100
websocketpp::transport::asio::connection< config > base
Definition: timers.cpp:116
static const long timeout_dns_resolve
Definition: timers.cpp:103
void handle_start(const websocketpp::lib::error_code &ec)
Definition: timers.cpp:125
websocketpp::lib::shared_ptr< mock_con > connection_ptr
Definition: timers.cpp:136
Stub concurrency policy that implements the interface using no-ops.
Definition: none.hpp:60
static const long timeout_proxy
Definition: timers.cpp:101
Asio based connection transport component.
Definition: connection.hpp:67
void run_dummy_server(int port)
Definition: timers.cpp:53
static const long timeout_socket_post_init
Definition: timers.cpp:102
lib::shared_ptr< uri > uri_ptr
Pointer to a URI.
Definition: uri.hpp:351
BOOST_AUTO_TEST_CASE(tls_handshake_timeout)
Definition: timers.cpp:177
void connect(std::string u)
Definition: timers.cpp:147
TLS enabled Asio endpoint socket component.
Definition: tls.hpp:413
Stores, parses, and manipulates HTTP requests.
Definition: request.hpp:50
websocketpp::lib::shared_ptr< boost::asio::ssl::context > context_ptr
Definition: timers.cpp:109
config::alog_type alog
Definition: timers.cpp:173
client::connection_ptr connection_ptr
std::size_t run()
wraps the run method of the internal io_service object
Definition: endpoint.hpp:613
Catch-all error for socket component errors that don&#39;t fit in other categories.
Definition: base.hpp:83
websocketpp::lib::shared_ptr< boost::asio::ssl::context > context_ptr
Stub logger that ignores all input.
Definition: stub.hpp:41
lib::error_code make_error_code(error::value e)
Definition: base.hpp:147
websocketpp::log::stub elog_type
Definition: timers.cpp:93
context_ptr on_tls_init(websocketpp::connection_hdl)
Definition: timers.cpp:110