2019-01-10 20:25:10 -08:00
|
|
|
//
|
|
|
|
|
// Copyright (c) 2018 Vinnie Falco (vinnie dot falco at gmail 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)
|
|
|
|
|
//
|
|
|
|
|
// Official repository: https://github.com/boostorg/beast
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
// Test that header file is self-contained.
|
|
|
|
|
#include <boost/beast/core/close_socket.hpp>
|
|
|
|
|
|
|
|
|
|
#include <boost/beast/_experimental/unit_test/suite.hpp>
|
2019-01-28 22:05:01 -08:00
|
|
|
#include <boost/beast/_experimental/test/stream.hpp>
|
2019-01-10 20:25:10 -08:00
|
|
|
#include <boost/beast/core/get_lowest_layer.hpp>
|
2019-01-28 22:05:01 -08:00
|
|
|
#include <boost/beast/core/string.hpp>
|
2019-01-10 20:25:10 -08:00
|
|
|
|
|
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
2019-01-28 22:05:01 -08:00
|
|
|
#include <boost/asio/write.hpp>
|
2019-01-10 20:25:10 -08:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
|
|
namespace boost {
|
|
|
|
|
namespace beast {
|
|
|
|
|
|
|
|
|
|
class close_socket_test : public beast::unit_test::suite
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
template<class T>
|
|
|
|
|
struct layer
|
|
|
|
|
{
|
|
|
|
|
T t;
|
|
|
|
|
|
|
|
|
|
template<class U>
|
|
|
|
|
explicit
|
|
|
|
|
layer(U&& u)
|
|
|
|
|
: t(std::forward<U>(u))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T& next_layer()
|
|
|
|
|
{
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
testClose()
|
|
|
|
|
{
|
|
|
|
|
net::io_context ioc;
|
|
|
|
|
{
|
|
|
|
|
net::ip::tcp::socket sock(ioc);
|
|
|
|
|
sock.open(net::ip::tcp::v4());
|
|
|
|
|
BEAST_EXPECT(sock.is_open());
|
|
|
|
|
close_socket(get_lowest_layer(sock));
|
|
|
|
|
BEAST_EXPECT(! sock.is_open());
|
|
|
|
|
}
|
|
|
|
|
{
|
2019-01-28 22:05:01 -08:00
|
|
|
using type = layer<net::ip::tcp::socket>;
|
|
|
|
|
type layer(ioc);
|
2019-01-10 20:25:10 -08:00
|
|
|
layer.next_layer().open(net::ip::tcp::v4());
|
|
|
|
|
BEAST_EXPECT(layer.next_layer().is_open());
|
2019-01-28 22:05:01 -08:00
|
|
|
BOOST_STATIC_ASSERT(detail::has_next_layer<type>::value);
|
|
|
|
|
BOOST_STATIC_ASSERT((std::is_same<
|
|
|
|
|
typename std::decay<decltype(get_lowest_layer(layer))>::type,
|
|
|
|
|
lowest_layer_type<decltype(layer)>>::value));
|
|
|
|
|
BOOST_STATIC_ASSERT(std::is_same<net::ip::tcp::socket&,
|
|
|
|
|
decltype(get_lowest_layer(layer))>::value);
|
|
|
|
|
BOOST_STATIC_ASSERT(std::is_same<net::ip::tcp::socket,
|
|
|
|
|
lowest_layer_type<decltype(layer)>>::value);
|
2019-01-10 20:25:10 -08:00
|
|
|
close_socket(get_lowest_layer(layer));
|
|
|
|
|
BEAST_EXPECT(! layer.next_layer().is_open());
|
|
|
|
|
}
|
2019-01-28 22:05:01 -08:00
|
|
|
{
|
|
|
|
|
test::stream ts(ioc);
|
|
|
|
|
close_socket(ts);
|
|
|
|
|
}
|
2019-01-10 20:25:10 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-28 22:05:01 -08:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
template <class WriteStream>
|
|
|
|
|
void hello_and_close (WriteStream& stream)
|
|
|
|
|
{
|
|
|
|
|
net::write(stream, net::const_buffer("Hello, world!", 13));
|
|
|
|
|
close_socket(get_lowest_layer(stream));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class my_socket
|
|
|
|
|
{
|
|
|
|
|
net::ip::tcp::socket sock_;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
my_socket(net::io_context& ioc)
|
|
|
|
|
: sock_(ioc)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
friend void beast_close_socket(my_socket& s)
|
|
|
|
|
{
|
|
|
|
|
error_code ec;
|
|
|
|
|
s.sock_.close(ec);
|
|
|
|
|
// ignore the error
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
testJavadocs()
|
|
|
|
|
{
|
|
|
|
|
BEAST_EXPECT(&close_socket_test::template hello_and_close<net::ip::tcp::socket>);
|
|
|
|
|
{
|
|
|
|
|
net::io_context ioc;
|
|
|
|
|
my_socket s(ioc);
|
|
|
|
|
close_socket(s);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
2019-01-10 20:25:10 -08:00
|
|
|
void
|
|
|
|
|
run() override
|
|
|
|
|
{
|
|
|
|
|
testClose();
|
2019-01-28 22:05:01 -08:00
|
|
|
testJavadocs();
|
2019-01-10 20:25:10 -08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BEAST_DEFINE_TESTSUITE(beast,core,close_socket);
|
|
|
|
|
|
|
|
|
|
} // beast
|
|
|
|
|
} // boost
|