| 1 | // |
| 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // Official repository: https://github.com/boostorg/beast |
| 8 | // |
| 9 | |
| 10 | // Test that header file is self-contained. |
| 11 | #include <boost/beast/_experimental/test/stream.hpp> |
| 12 | |
| 13 | #include <boost/beast/_experimental/unit_test/suite.hpp> |
| 14 | #include <boost/beast/_experimental/test/handler.hpp> |
| 15 | |
| 16 | #include <boost/asio/ssl/stream.hpp> |
| 17 | #if defined(BOOST_ASIO_HAS_CO_AWAIT) |
| 18 | #include <boost/asio/use_awaitable.hpp> |
| 19 | #endif |
| 20 | |
| 21 | #define DEF boost::asio::use_future_t |
| 22 | namespace boost { |
| 23 | namespace beast { |
| 24 | |
| 25 | class stream_test |
| 26 | : public unit_test::suite |
| 27 | { |
| 28 | public: |
| 29 | void |
| 30 | testTestStream() |
| 31 | { |
| 32 | char buf[1] = {}; |
| 33 | net::mutable_buffer m0; |
| 34 | net::mutable_buffer m1(buf, sizeof(buf)); |
| 35 | |
| 36 | { |
| 37 | net::io_context ioc; |
| 38 | |
| 39 | { |
| 40 | test::stream ts(ioc); |
| 41 | } |
| 42 | { |
| 43 | test::stream ts(ioc); |
| 44 | ts.close(); |
| 45 | } |
| 46 | { |
| 47 | test::stream t1(ioc); |
| 48 | auto t2 = connect(to&: t1); |
| 49 | } |
| 50 | { |
| 51 | test::stream t1(ioc); |
| 52 | auto t2 = connect(to&: t1); |
| 53 | t2.close(); |
| 54 | } |
| 55 | } |
| 56 | { |
| 57 | // abandon |
| 58 | net::io_context ioc; |
| 59 | test::stream ts(ioc); |
| 60 | ts.async_read_some(buffers: m1, |
| 61 | handler: [](error_code, std::size_t) |
| 62 | { |
| 63 | BEAST_FAIL(); |
| 64 | }); |
| 65 | } |
| 66 | //--- |
| 67 | { |
| 68 | net::io_context ioc; |
| 69 | { |
| 70 | test::stream ts(ioc); |
| 71 | ts.async_read_some(buffers: m1, |
| 72 | handler: test::fail_handler( |
| 73 | ec: net::error::operation_aborted)); |
| 74 | } |
| 75 | test::run(ioc); |
| 76 | } |
| 77 | { |
| 78 | net::io_context ioc; |
| 79 | test::stream ts(ioc); |
| 80 | ts.async_read_some(buffers: m1, |
| 81 | handler: test::fail_handler( |
| 82 | ec: net::error::operation_aborted)); |
| 83 | ts.close(); |
| 84 | test::run(ioc); |
| 85 | } |
| 86 | { |
| 87 | net::io_context ioc; |
| 88 | test::stream t1(ioc); |
| 89 | auto t2 = connect(to&: t1); |
| 90 | t1.async_read_some(buffers: m1, |
| 91 | handler: test::fail_handler( |
| 92 | ec: net::error::eof)); |
| 93 | t2.close(); |
| 94 | test::run(ioc); |
| 95 | } |
| 96 | { |
| 97 | net::io_context ioc; |
| 98 | test::stream t1(ioc); |
| 99 | auto t2 = connect(to&: t1); |
| 100 | t1.async_read_some(buffers: m1, |
| 101 | handler: test::fail_handler( |
| 102 | ec: net::error::operation_aborted)); |
| 103 | t1.close(); |
| 104 | test::run(ioc); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | testSharedAbandon() |
| 110 | { |
| 111 | struct handler |
| 112 | { |
| 113 | std::shared_ptr<test::stream> ts_; |
| 114 | |
| 115 | void |
| 116 | operator()(error_code, std::size_t) |
| 117 | { |
| 118 | } |
| 119 | }; |
| 120 | |
| 121 | char buf[1] = {}; |
| 122 | net::mutable_buffer m1(buf, sizeof(buf)); |
| 123 | |
| 124 | std::weak_ptr<test::stream> wp; |
| 125 | |
| 126 | { |
| 127 | net::io_context ioc; |
| 128 | { |
| 129 | auto sp = std::make_shared<test::stream>(args&: ioc); |
| 130 | |
| 131 | sp->async_read_some(buffers: m1, handler: handler{.ts_: sp}); |
| 132 | wp = sp; |
| 133 | } |
| 134 | } |
| 135 | BEAST_EXPECT(! wp.lock()); |
| 136 | } |
| 137 | |
| 138 | void |
| 139 | testLifetimeViolation() |
| 140 | { |
| 141 | // This should assert |
| 142 | std::shared_ptr<test::stream> sp; |
| 143 | { |
| 144 | net::io_context ioc; |
| 145 | sp = std::make_shared<test::stream>(args&: ioc); |
| 146 | } |
| 147 | sp.reset(); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | testAsioSSLCompat() |
| 152 | { |
| 153 | BOOST_STATIC_ASSERT( |
| 154 | std::is_same< |
| 155 | boost::asio::ssl::stream<test::stream>:: |
| 156 | lowest_layer_type, |
| 157 | test::stream>::value); |
| 158 | } |
| 159 | |
| 160 | #if defined(BOOST_ASIO_HAS_CO_AWAIT) |
| 161 | net::awaitable<void> |
| 162 | testRebind(net::mutable_buffer& b) |
| 163 | { |
| 164 | auto ex = co_await net::this_coro::executor; |
| 165 | auto s1 = test::stream(ex); |
| 166 | auto s2 = net::use_awaitable.as_default_on(std::move(s1)); |
| 167 | #ifdef BOOST_ASIO_HAS_DEFAULT_FUNCTION_TEMPLATE_ARGUMENTS |
| 168 | auto bt = co_await s2.async_read_some(b); |
| 169 | ignore_unused(bt); |
| 170 | bt = co_await s2.async_write_some(b); |
| 171 | #else |
| 172 | auto bt = co_await s2.async_read_some(b, |
| 173 | net::use_awaitable); |
| 174 | bt = co_await s2.async_write_some(b, |
| 175 | net::use_awaitable); |
| 176 | #endif |
| 177 | } |
| 178 | #endif |
| 179 | |
| 180 | void |
| 181 | run() override |
| 182 | { |
| 183 | testTestStream(); |
| 184 | testSharedAbandon(); |
| 185 | //testLifetimeViolation(); |
| 186 | boost::ignore_unused(&stream_test::testAsioSSLCompat); |
| 187 | #if defined(BOOST_ASIO_HAS_CO_AWAIT) |
| 188 | boost::ignore_unused(&stream_test::testRebind); |
| 189 | #endif |
| 190 | } |
| 191 | }; |
| 192 | |
| 193 | BEAST_DEFINE_TESTSUITE(beast,test,stream); |
| 194 | |
| 195 | } // beast |
| 196 | } // boost |
| 197 | |