1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
//
// tcp.cpp
// ~~~~~~~
//
// Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)
// Test that header file is self-contained.
#include <boost/asio/ip/tcp.hpp>
#include <boost/bind.hpp>
#include <cstring>
#include <boost/asio/io_service.hpp>
#include "../unit_test.hpp"
#include "../archetypes/io_control_command.hpp"
//------------------------------------------------------------------------------
// ip_tcp_compile test
// ~~~~~~~~~~~~~~~~~~~
// The following test checks that all nested classes, enums and constants in
// ip::tcp compile and link correctly. Runtime failures are ignored.
namespace ip_tcp_compile {
void test()
{
using namespace boost::asio;
namespace ip = boost::asio::ip;
try
{
io_service ios;
ip::tcp::socket sock(ios);
// no_delay class.
ip::tcp::no_delay no_delay1(true);
sock.set_option(no_delay1);
ip::tcp::no_delay no_delay2;
sock.get_option(no_delay2);
no_delay1 = true;
static_cast<bool>(no_delay1);
static_cast<bool>(!no_delay1);
static_cast<bool>(no_delay1.value());
}
catch (std::exception&)
{
}
}
} // namespace ip_tcp_compile
//------------------------------------------------------------------------------
// ip_tcp_runtime test
// ~~~~~~~~~~~~~~~~~~~
// The following test checks the runtime operation of the ip::tcp class.
namespace ip_tcp_runtime {
void test()
{
using namespace boost::asio;
namespace ip = boost::asio::ip;
io_service ios;
ip::tcp::socket sock(ios, ip::tcp::v4());
boost::system::error_code ec;
// no_delay class.
ip::tcp::no_delay no_delay1(true);
BOOST_CHECK(no_delay1.value());
BOOST_CHECK(static_cast<bool>(no_delay1));
BOOST_CHECK(!!no_delay1);
sock.set_option(no_delay1, ec);
BOOST_CHECK(!ec);
ip::tcp::no_delay no_delay2;
sock.get_option(no_delay2, ec);
BOOST_CHECK(!ec);
BOOST_CHECK(no_delay2.value());
BOOST_CHECK(static_cast<bool>(no_delay2));
BOOST_CHECK(!!no_delay2);
ip::tcp::no_delay no_delay3(false);
BOOST_CHECK(!no_delay3.value());
BOOST_CHECK(!static_cast<bool>(no_delay3));
BOOST_CHECK(!no_delay3);
sock.set_option(no_delay3, ec);
BOOST_CHECK(!ec);
ip::tcp::no_delay no_delay4;
sock.get_option(no_delay4, ec);
BOOST_CHECK(!ec);
BOOST_CHECK(!no_delay4.value());
BOOST_CHECK(!static_cast<bool>(no_delay4));
BOOST_CHECK(!no_delay4);
}
} // namespace ip_tcp_runtime
//------------------------------------------------------------------------------
// ip_tcp_socket_compile test
// ~~~~~~~~~~~~~~~~~~~~~~~~~~
// The following test checks that all public member functions on the class
// ip::tcp::socket compile and link correctly. Runtime failures are ignored.
namespace ip_tcp_socket_compile {
void connect_handler(const boost::system::error_code&)
{
}
void send_handler(const boost::system::error_code&, std::size_t)
{
}
void receive_handler(const boost::system::error_code&, std::size_t)
{
}
void write_some_handler(const boost::system::error_code&, std::size_t)
{
}
void read_some_handler(const boost::system::error_code&, std::size_t)
{
}
void test()
{
using namespace boost::asio;
namespace ip = boost::asio::ip;
try
{
io_service ios;
char mutable_char_buffer[128] = "";
const char const_char_buffer[128] = "";
socket_base::message_flags in_flags = 0;
socket_base::keep_alive socket_option;
archetypes::io_control_command io_control_command;
boost::system::error_code ec;
// basic_stream_socket constructors.
ip::tcp::socket socket1(ios);
ip::tcp::socket socket2(ios, ip::tcp::v4());
ip::tcp::socket socket3(ios, ip::tcp::v6());
ip::tcp::socket socket4(ios, ip::tcp::endpoint(ip::tcp::v4(), 0));
ip::tcp::socket socket5(ios, ip::tcp::endpoint(ip::tcp::v6(), 0));
int native_socket1 = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
ip::tcp::socket socket6(ios, ip::tcp::v4(), native_socket1);
// basic_io_object functions.
io_service& ios_ref = socket1.io_service();
(void)ios_ref;
// basic_socket functions.
ip::tcp::socket::lowest_layer_type& lowest_layer = socket1.lowest_layer();
(void)lowest_layer;
const ip::tcp::socket& socket7 = socket1;
const ip::tcp::socket::lowest_layer_type& lowest_layer2
= socket7.lowest_layer();
(void)lowest_layer2;
socket1.open(ip::tcp::v4());
socket1.open(ip::tcp::v6());
socket1.open(ip::tcp::v4(), ec);
socket1.open(ip::tcp::v6(), ec);
int native_socket2 = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
socket1.assign(ip::tcp::v4(), native_socket2);
int native_socket3 = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
socket1.assign(ip::tcp::v4(), native_socket3, ec);
bool is_open = socket1.is_open();
(void)is_open;
socket1.close();
socket1.close(ec);
ip::tcp::socket::native_type native_socket4 = socket1.native();
(void)native_socket4;
socket1.cancel();
socket1.cancel(ec);
bool at_mark1 = socket1.at_mark();
(void)at_mark1;
bool at_mark2 = socket1.at_mark(ec);
(void)at_mark2;
std::size_t available1 = socket1.available();
(void)available1;
std::size_t available2 = socket1.available(ec);
(void)available2;
socket1.bind(ip::tcp::endpoint(ip::tcp::v4(), 0));
socket1.bind(ip::tcp::endpoint(ip::tcp::v6(), 0));
socket1.bind(ip::tcp::endpoint(ip::tcp::v4(), 0), ec);
socket1.bind(ip::tcp::endpoint(ip::tcp::v6(), 0), ec);
socket1.connect(ip::tcp::endpoint(ip::tcp::v4(), 0));
socket1.connect(ip::tcp::endpoint(ip::tcp::v6(), 0));
socket1.connect(ip::tcp::endpoint(ip::tcp::v4(), 0), ec);
socket1.connect(ip::tcp::endpoint(ip::tcp::v6(), 0), ec);
socket1.async_connect(ip::tcp::endpoint(ip::tcp::v4(), 0), connect_handler);
socket1.async_connect(ip::tcp::endpoint(ip::tcp::v6(), 0), connect_handler);
socket1.set_option(socket_option);
socket1.set_option(socket_option, ec);
socket1.get_option(socket_option);
socket1.get_option(socket_option, ec);
socket1.io_control(io_control_command);
socket1.io_control(io_control_command, ec);
ip::tcp::endpoint endpoint1 = socket1.local_endpoint();
ip::tcp::endpoint endpoint2 = socket1.local_endpoint(ec);
ip::tcp::endpoint endpoint3 = socket1.remote_endpoint();
ip::tcp::endpoint endpoint4 = socket1.remote_endpoint(ec);
socket1.shutdown(socket_base::shutdown_both);
socket1.shutdown(socket_base::shutdown_both, ec);
// basic_stream_socket functions.
socket1.send(buffer(mutable_char_buffer));
socket1.send(buffer(const_char_buffer));
socket1.send(null_buffers());
socket1.send(buffer(mutable_char_buffer), in_flags);
socket1.send(buffer(const_char_buffer), in_flags);
socket1.send(null_buffers(), in_flags);
socket1.send(buffer(mutable_char_buffer), in_flags, ec);
socket1.send(buffer(const_char_buffer), in_flags, ec);
socket1.send(null_buffers(), in_flags, ec);
socket1.async_send(buffer(mutable_char_buffer), send_handler);
socket1.async_send(buffer(const_char_buffer), send_handler);
socket1.async_send(null_buffers(), send_handler);
socket1.async_send(buffer(mutable_char_buffer), in_flags, send_handler);
socket1.async_send(buffer(const_char_buffer), in_flags, send_handler);
socket1.async_send(null_buffers(), in_flags, send_handler);
socket1.receive(buffer(mutable_char_buffer));
socket1.receive(null_buffers());
socket1.receive(buffer(mutable_char_buffer), in_flags);
socket1.receive(null_buffers(), in_flags);
socket1.receive(buffer(mutable_char_buffer), in_flags, ec);
socket1.receive(null_buffers(), in_flags, ec);
socket1.async_receive(buffer(mutable_char_buffer), receive_handler);
socket1.async_receive(null_buffers(), receive_handler);
socket1.async_receive(buffer(mutable_char_buffer), in_flags,
receive_handler);
socket1.async_receive(null_buffers(), in_flags, receive_handler);
socket1.write_some(buffer(mutable_char_buffer));
socket1.write_some(buffer(const_char_buffer));
socket1.write_some(null_buffers());
socket1.write_some(buffer(mutable_char_buffer), ec);
socket1.write_some(buffer(const_char_buffer), ec);
socket1.write_some(null_buffers(), ec);
socket1.async_write_some(buffer(mutable_char_buffer), write_some_handler);
socket1.async_write_some(buffer(const_char_buffer), write_some_handler);
socket1.async_write_some(null_buffers(), write_some_handler);
socket1.read_some(buffer(mutable_char_buffer));
socket1.read_some(buffer(mutable_char_buffer), ec);
socket1.read_some(null_buffers(), ec);
socket1.async_read_some(buffer(mutable_char_buffer), read_some_handler);
socket1.async_read_some(null_buffers(), read_some_handler);
}
catch (std::exception&)
{
}
}
} // namespace ip_tcp_socket_compile
//------------------------------------------------------------------------------
// ip_tcp_acceptor_runtime test
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// The following test checks the runtime operation of the ip::tcp::acceptor
// class.
namespace ip_tcp_acceptor_runtime {
void handle_accept(const boost::system::error_code& err)
{
BOOST_CHECK(!err);
}
void handle_connect(const boost::system::error_code& err)
{
BOOST_CHECK(!err);
}
void test()
{
using namespace boost::asio;
namespace ip = boost::asio::ip;
io_service ios;
ip::tcp::acceptor acceptor(ios, ip::tcp::endpoint(ip::tcp::v4(), 0));
ip::tcp::endpoint server_endpoint = acceptor.local_endpoint();
server_endpoint.address(ip::address_v4::loopback());
ip::tcp::socket client_side_socket(ios);
ip::tcp::socket server_side_socket(ios);
client_side_socket.connect(server_endpoint);
acceptor.accept(server_side_socket);
client_side_socket.close();
server_side_socket.close();
client_side_socket.connect(server_endpoint);
ip::tcp::endpoint client_endpoint;
acceptor.accept(server_side_socket, client_endpoint);
ip::tcp::endpoint client_side_local_endpoint
= client_side_socket.local_endpoint();
BOOST_CHECK(client_side_local_endpoint.port() == client_endpoint.port());
ip::tcp::endpoint server_side_remote_endpoint
= server_side_socket.remote_endpoint();
BOOST_CHECK(server_side_remote_endpoint.port() == client_endpoint.port());
client_side_socket.close();
server_side_socket.close();
acceptor.async_accept(server_side_socket, handle_accept);
client_side_socket.async_connect(server_endpoint, handle_connect);
ios.run();
client_side_socket.close();
server_side_socket.close();
acceptor.async_accept(server_side_socket, client_endpoint, handle_accept);
client_side_socket.async_connect(server_endpoint, handle_connect);
ios.reset();
ios.run();
client_side_local_endpoint = client_side_socket.local_endpoint();
BOOST_CHECK(client_side_local_endpoint.port() == client_endpoint.port());
server_side_remote_endpoint = server_side_socket.remote_endpoint();
BOOST_CHECK(server_side_remote_endpoint.port() == client_endpoint.port());
}
} // namespace ip_tcp_acceptor_runtime
//------------------------------------------------------------------------------
test_suite* init_unit_test_suite(int, char*[])
{
test_suite* test = BOOST_TEST_SUITE("ip/tcp");
test->add(BOOST_TEST_CASE(&ip_tcp_compile::test));
test->add(BOOST_TEST_CASE(&ip_tcp_runtime::test));
test->add(BOOST_TEST_CASE(&ip_tcp_socket_compile::test));
test->add(BOOST_TEST_CASE(&ip_tcp_acceptor_runtime::test));
return test;
}
|