2019-02-26 18:55:23 -08:00
|
|
|
|
//
|
2019-02-21 07:00:31 -08:00
|
|
|
|
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2018-05-02 08:30:29 -07:00
|
|
|
|
//
|
|
|
|
|
|
// 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.
|
2018-11-11 14:07:55 -08:00
|
|
|
|
#include <boost/beast/_experimental/test/stream.hpp>
|
2019-02-06 14:40:48 -08:00
|
|
|
|
|
|
|
|
|
|
#include <boost/beast/_experimental/unit_test/suite.hpp>
|
2019-02-26 18:55:23 -08:00
|
|
|
|
#include <boost/beast/_experimental/test/handler.hpp>
|
2019-02-06 14:40:48 -08:00
|
|
|
|
|
2020-12-01 17:22:39 +01:00
|
|
|
|
#include <boost/asio/ssl/stream.hpp>
|
2020-12-11 12:03:34 +01:00
|
|
|
|
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
|
|
|
|
|
|
#include <boost/asio/use_awaitable.hpp>
|
|
|
|
|
|
#endif
|
2020-12-01 17:22:39 +01:00
|
|
|
|
|
2020-12-11 12:03:34 +01:00
|
|
|
|
#define DEF boost::asio::use_future_t
|
2019-02-06 14:40:48 -08:00
|
|
|
|
namespace boost {
|
|
|
|
|
|
namespace beast {
|
|
|
|
|
|
|
2019-02-26 18:55:23 -08:00
|
|
|
|
class stream_test
|
2019-02-06 14:40:48 -08:00
|
|
|
|
: public unit_test::suite
|
|
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
void
|
|
|
|
|
|
testTestStream()
|
|
|
|
|
|
{
|
|
|
|
|
|
char buf[1] = {};
|
|
|
|
|
|
net::mutable_buffer m0;
|
|
|
|
|
|
net::mutable_buffer m1(buf, sizeof(buf));
|
|
|
|
|
|
|
|
|
|
|
|
{
|
2019-02-26 18:55:23 -08:00
|
|
|
|
net::io_context ioc;
|
|
|
|
|
|
|
2019-02-06 14:40:48 -08:00
|
|
|
|
{
|
|
|
|
|
|
test::stream ts(ioc);
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
test::stream ts(ioc);
|
|
|
|
|
|
ts.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
test::stream t1(ioc);
|
|
|
|
|
|
auto t2 = connect(t1);
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
test::stream t1(ioc);
|
|
|
|
|
|
auto t2 = connect(t1);
|
|
|
|
|
|
t2.close();
|
|
|
|
|
|
}
|
2019-02-26 18:55:23 -08:00
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
// abandon
|
|
|
|
|
|
net::io_context ioc;
|
|
|
|
|
|
test::stream ts(ioc);
|
|
|
|
|
|
ts.async_read_some(m1,
|
|
|
|
|
|
[](error_code, std::size_t)
|
2019-02-06 14:40:48 -08:00
|
|
|
|
{
|
2019-02-26 18:55:23 -08:00
|
|
|
|
BEAST_FAIL();
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
//---
|
|
|
|
|
|
{
|
|
|
|
|
|
net::io_context ioc;
|
2019-02-06 14:40:48 -08:00
|
|
|
|
{
|
|
|
|
|
|
test::stream ts(ioc);
|
|
|
|
|
|
ts.async_read_some(m1,
|
2019-02-26 18:55:23 -08:00
|
|
|
|
test::fail_handler(
|
|
|
|
|
|
net::error::operation_aborted));
|
2019-02-06 14:40:48 -08:00
|
|
|
|
}
|
2019-02-26 18:55:23 -08:00
|
|
|
|
test::run(ioc);
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
net::io_context ioc;
|
|
|
|
|
|
test::stream ts(ioc);
|
|
|
|
|
|
ts.async_read_some(m1,
|
|
|
|
|
|
test::fail_handler(
|
|
|
|
|
|
net::error::operation_aborted));
|
|
|
|
|
|
ts.close();
|
|
|
|
|
|
test::run(ioc);
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
net::io_context ioc;
|
|
|
|
|
|
test::stream t1(ioc);
|
|
|
|
|
|
auto t2 = connect(t1);
|
|
|
|
|
|
t1.async_read_some(m1,
|
|
|
|
|
|
test::fail_handler(
|
|
|
|
|
|
net::error::eof));
|
|
|
|
|
|
t2.close();
|
|
|
|
|
|
test::run(ioc);
|
|
|
|
|
|
}
|
|
|
|
|
|
{
|
|
|
|
|
|
net::io_context ioc;
|
|
|
|
|
|
test::stream t1(ioc);
|
|
|
|
|
|
auto t2 = connect(t1);
|
|
|
|
|
|
t1.async_read_some(m1,
|
|
|
|
|
|
test::fail_handler(
|
|
|
|
|
|
net::error::operation_aborted));
|
|
|
|
|
|
t1.close();
|
|
|
|
|
|
test::run(ioc);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
testSharedAbandon()
|
|
|
|
|
|
{
|
|
|
|
|
|
struct handler
|
|
|
|
|
|
{
|
|
|
|
|
|
std::shared_ptr<test::stream> ts_;
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
operator()(error_code, std::size_t)
|
2019-02-06 14:40:48 -08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
2019-02-26 18:55:23 -08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
char buf[1] = {};
|
|
|
|
|
|
net::mutable_buffer m1(buf, sizeof(buf));
|
|
|
|
|
|
|
|
|
|
|
|
std::weak_ptr<test::stream> wp;
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
net::io_context ioc;
|
2019-02-06 14:40:48 -08:00
|
|
|
|
{
|
2019-02-26 18:55:23 -08:00
|
|
|
|
auto sp = std::make_shared<test::stream>(ioc);
|
|
|
|
|
|
|
|
|
|
|
|
sp->async_read_some(m1, handler{sp});
|
|
|
|
|
|
wp = sp;
|
2019-02-06 14:40:48 -08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2019-02-26 18:55:23 -08:00
|
|
|
|
BEAST_EXPECT(! wp.lock());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
testLifetimeViolation()
|
|
|
|
|
|
{
|
|
|
|
|
|
// This should assert
|
|
|
|
|
|
std::shared_ptr<test::stream> sp;
|
|
|
|
|
|
{
|
|
|
|
|
|
net::io_context ioc;
|
|
|
|
|
|
sp = std::make_shared<test::stream>(ioc);
|
|
|
|
|
|
}
|
|
|
|
|
|
sp.reset();
|
2019-02-06 14:40:48 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-01 17:22:39 +01:00
|
|
|
|
void
|
|
|
|
|
|
testAsioSSLCompat()
|
|
|
|
|
|
{
|
|
|
|
|
|
BOOST_STATIC_ASSERT(
|
|
|
|
|
|
std::is_same<
|
|
|
|
|
|
boost::asio::ssl::stream<test::stream>::
|
|
|
|
|
|
lowest_layer_type,
|
|
|
|
|
|
test::stream>::value);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-11 12:03:34 +01:00
|
|
|
|
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
|
|
|
|
|
|
net::awaitable<void>
|
|
|
|
|
|
testRebind(net::mutable_buffer& b)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto ex = co_await net::this_coro::executor;
|
|
|
|
|
|
auto s1 = test::stream(ex);
|
|
|
|
|
|
auto s2 = net::use_awaitable.as_default_on(std::move(s1));
|
2021-03-17 11:25:02 +01:00
|
|
|
|
#ifdef BOOST_ASIO_HAS_DEFAULT_FUNCTION_TEMPLATE_ARGUMENTS
|
2020-12-11 12:03:34 +01:00
|
|
|
|
auto bt = co_await s2.async_read_some(b);
|
|
|
|
|
|
bt = co_await s2.async_write_some(b);
|
2021-03-17 11:25:02 +01:00
|
|
|
|
#else
|
|
|
|
|
|
auto bt = co_await s2.async_read_some(b,
|
|
|
|
|
|
net::use_awaitable);
|
|
|
|
|
|
bt = co_await s2.async_write_some(b,
|
|
|
|
|
|
net::use_awaitable);
|
|
|
|
|
|
#endif
|
2020-12-11 12:03:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2019-02-06 14:40:48 -08:00
|
|
|
|
void
|
|
|
|
|
|
run() override
|
|
|
|
|
|
{
|
|
|
|
|
|
testTestStream();
|
2019-02-26 18:55:23 -08:00
|
|
|
|
testSharedAbandon();
|
|
|
|
|
|
//testLifetimeViolation();
|
2020-12-01 17:22:39 +01:00
|
|
|
|
boost::ignore_unused(&stream_test::testAsioSSLCompat);
|
2020-12-11 12:03:34 +01:00
|
|
|
|
#if defined(BOOST_ASIO_HAS_CO_AWAIT)
|
|
|
|
|
|
boost::ignore_unused(&stream_test::testRebind);
|
|
|
|
|
|
#endif
|
2019-02-06 14:40:48 -08:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2019-02-26 18:55:23 -08:00
|
|
|
|
BEAST_DEFINE_TESTSUITE(beast,test,stream);
|
2019-02-06 14:40:48 -08:00
|
|
|
|
|
|
|
|
|
|
} // beast
|
|
|
|
|
|
} // boost
|