NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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, const websocketpp::lib::shared_ptr<config::alog_type>& b,
119  const websocketpp::lib::shared_ptr<config::elog_type>& c)
120  : base(a,b,c) {}
121 
122  void start() {
123  base::init(websocketpp::lib::bind(&mock_con::handle_start,this,
124  websocketpp::lib::placeholders::_1));
125  }
126 
127  void handle_start(const websocketpp::lib::error_code& ec) {
130 
131  BOOST_CHECK_EQUAL( ec, make_error_code(tls_handshake_timeout) );
132 
133  base::cancel_socket();
134  }
135 };
136 
138 typedef websocketpp::lib::shared_ptr<mock_con> connection_ptr;
139 
142 
144  : alog(websocketpp::lib::make_shared<config::alog_type>())
145  , elog(websocketpp::lib::make_shared<config::elog_type>())
146  {
147  alog->set_channels(websocketpp::log::alevel::all);
148  base::init_logging(alog,elog);
149  init_asio();
150  }
151 
152  void connect(std::string u) {
153  m_con.reset(new mock_con(false,alog,elog));
155 
156  BOOST_CHECK( uri->get_valid() );
157  BOOST_CHECK_EQUAL( base::init(m_con), websocketpp::lib::error_code() );
158 
159  base::async_connect(
160  m_con,
161  uri,
162  websocketpp::lib::bind(
164  this,
165  m_con,
166  websocketpp::lib::placeholders::_1
167  )
168  );
169  }
170 
171  void handle_connect(connection_ptr con, websocketpp::lib::error_code const & ec)
172  {
173  BOOST_CHECK( !ec );
174  con->start();
175  }
176 
178  websocketpp::lib::shared_ptr<config::alog_type> alog;
179  websocketpp::lib::shared_ptr<config::elog_type> elog;
180 };
181 
183  websocketpp::lib::thread dummy_server(websocketpp::lib::bind(&run_dummy_server,9005));
184  websocketpp::lib::thread timer(websocketpp::lib::bind(&run_test_timer,5000));
185  dummy_server.detach();
186  timer.detach();
187 
188  sleep(1); // give the server thread some time to start
189 
190  mock_endpoint endpoint;
192  endpoint.connect("wss://localhost:9005");
193  endpoint.run();
194 }
static level const all
Special aggregate value representing "all levels".
Definition: levels.hpp:152
websocketpp::transport::asio::connection< config > con_type
Definition: timers.cpp:137
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
websocketpp::lib::shared_ptr< config::elog_type > elog
Definition: timers.cpp:179
void handle_connect(connection_ptr con, websocketpp::lib::error_code const &ec)
Definition: timers.cpp:171
void start()
Definition: timers.cpp:122
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.
websocketpp::http::parser::request request_type
Definition: timers.cpp:94
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:446
websocketpp::transport::asio::endpoint< config > base
Definition: timers.cpp:141
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:177
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
mock_con(bool a, const websocketpp::lib::shared_ptr< config::alog_type > &b, const websocketpp::lib::shared_ptr< config::elog_type > &c)
Definition: timers.cpp:118
static const long timeout_dns_resolve
Definition: timers.cpp:103
Namespace for the WebSocket++ project.
Definition: base64.hpp:41
void handle_start(const websocketpp::lib::error_code &ec)
Definition: timers.cpp:127
websocketpp::lib::shared_ptr< mock_con > connection_ptr
Definition: timers.cpp:138
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:352
BOOST_AUTO_TEST_CASE(tls_handshake_timeout)
Definition: timers.cpp:182
void connect(std::string u)
Definition: timers.cpp:152
TLS enabled Asio endpoint socket component.
Definition: tls.hpp:404
Stores, parses, and manipulates HTTP requests.
Definition: request.hpp:50
websocketpp::lib::shared_ptr< boost::asio::ssl::context > context_ptr
Definition: timers.cpp:109
client::connection_ptr connection_ptr
std::size_t run()
wraps the run method of the internal io_service object
Definition: endpoint.hpp:641
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
websocketpp::lib::shared_ptr< config::alog_type > alog
Definition: timers.cpp:178
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
boost::chrono::milliseconds milliseconds
Definition: time.hpp:48