Files
boost_beast/test/beast/websocket/accept.cpp

815 lines
25 KiB
C++
Raw Normal View History

2017-08-24 06:21:30 -07:00
//
2019-02-21 07:00:31 -08:00
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-08-24 06:21:30 -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.
#include <boost/beast/websocket/stream.hpp>
2019-02-17 05:23:40 -08:00
#include <boost/beast/_experimental/test/stream.hpp>
2019-02-13 08:00:07 -08:00
#include <boost/beast/_experimental/test/tcp.hpp>
2019-02-17 05:23:40 -08:00
#include <boost/beast/_experimental/unit_test/suite.hpp>
2017-08-24 06:21:30 -07:00
#include "test.hpp"
namespace boost {
namespace beast {
namespace websocket {
2019-02-17 05:23:40 -08:00
class accept_test : public unit_test::suite //: public websocket_test_suite
2017-08-24 06:21:30 -07:00
{
public:
2019-02-17 05:23:40 -08:00
class res_decorator
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
bool& b_;
public:
res_decorator(res_decorator const&) = default;
explicit
res_decorator(bool& b)
: b_(b)
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
}
2017-08-24 06:21:30 -07:00
2019-02-17 05:23:40 -08:00
void
operator()(response_type&) const
{
this->b_ = true;
}
};
template<std::size_t N>
static
net::const_buffer
sbuf(const char (&s)[N])
{
return net::const_buffer(&s[0], N-1);
}
2017-08-24 06:21:30 -07:00
2019-02-17 05:23:40 -08:00
static
void
fail_loop(
std::function<void(stream<test::stream>&)> f,
std::chrono::steady_clock::duration amount =
std::chrono::seconds(5))
{
using clock_type = std::chrono::steady_clock;
auto const expires_at =
clock_type::now() + amount;
net::io_context ioc;
for(std::size_t n = 0;;++n)
{
test::fail_count fc(n);
try
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
stream<test::stream> ws(ioc, fc);
auto tr = connect(ws.next_layer());
f(ws);
break;
2017-08-24 06:21:30 -07:00
}
2019-02-17 05:23:40 -08:00
catch(system_error const& se)
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
if(! BEAST_EXPECTS(
se.code() == test::error::test_failure,
se.code().message()))
throw;
if(! BEAST_EXPECTS(
clock_type::now() < expires_at,
"a test timeout occurred"))
break;
2017-08-24 06:21:30 -07:00
}
2019-02-17 05:23:40 -08:00
}
}
2017-08-24 06:21:30 -07:00
2019-02-17 05:23:40 -08:00
template<class Api>
void
testMatrix(Api api)
{
net::io_context ioc;
2017-08-24 06:21:30 -07:00
// request in stream
2019-02-17 05:23:40 -08:00
fail_loop([&](stream<test::stream>& ws)
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
ws.next_layer().append(
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n");
2019-02-17 05:23:40 -08:00
ws.next_layer().read_size(20);
api.accept(ws);
2017-08-24 06:21:30 -07:00
});
2019-02-17 05:23:40 -08:00
// request in stream, decorator
fail_loop([&](stream<test::stream>& ws)
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
ws.next_layer().append(
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
2019-02-17 05:23:40 -08:00
"\r\n");
ws.next_layer().read_size(20);
bool called = false;
api.accept_ex(ws, res_decorator{called});
BEAST_EXPECT(called);
});
2017-08-24 06:21:30 -07:00
2019-02-17 05:23:40 -08:00
// request in buffers
fail_loop([&](stream<test::stream>& ws)
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
api.accept(ws, sbuf(
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n"
));
});
// request in buffers, decorator
fail_loop([&](stream<test::stream>& ws)
{
bool called = false;
api.accept_ex(ws, sbuf(
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n"),
res_decorator{called});
BEAST_EXPECT(called);
});
// request in buffers and stream
fail_loop([&](stream<test::stream>& ws)
{
ws.next_layer().append(
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n");
ws.next_layer().read_size(16);
api.accept(ws, sbuf(
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
2019-02-17 05:23:40 -08:00
));
// VFALCO validate contents of ws.next_layer().str?
});
// request in buffers and stream, decorator
fail_loop([&](stream<test::stream>& ws)
{
ws.next_layer().append(
2017-08-24 06:21:30 -07:00
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n");
2019-02-17 05:23:40 -08:00
ws.next_layer().read_size(16);
2017-08-24 06:21:30 -07:00
bool called = false;
2019-02-17 05:23:40 -08:00
api.accept_ex(ws, sbuf(
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"),
res_decorator{called});
2017-08-24 06:21:30 -07:00
BEAST_EXPECT(called);
});
2019-02-17 05:23:40 -08:00
// request in message
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
request_type req;
req.method(http::verb::get);
req.target("/");
req.version(11);
req.insert(http::field::host, "localhost");
req.insert(http::field::upgrade, "websocket");
req.insert(http::field::connection, "upgrade");
req.insert(http::field::sec_websocket_key, "dGhlIHNhbXBsZSBub25jZQ==");
req.insert(http::field::sec_websocket_version, "13");
fail_loop([&](stream<test::stream>& ws)
{
api.accept(ws, req);
});
}
// request in message, decorator
{
request_type req;
req.method(http::verb::get);
req.target("/");
req.version(11);
req.insert(http::field::host, "localhost");
req.insert(http::field::upgrade, "websocket");
req.insert(http::field::connection, "upgrade");
req.insert(http::field::sec_websocket_key, "dGhlIHNhbXBsZSBub25jZQ==");
req.insert(http::field::sec_websocket_version, "13");
fail_loop([&](stream<test::stream>& ws)
{
bool called = false;
api.accept_ex(ws, req,
res_decorator{called});
BEAST_EXPECT(called);
});
}
// request in message, close frame in stream
{
request_type req;
req.method(http::verb::get);
req.target("/");
req.version(11);
req.insert(http::field::host, "localhost");
req.insert(http::field::upgrade, "websocket");
req.insert(http::field::connection, "upgrade");
req.insert(http::field::sec_websocket_key, "dGhlIHNhbXBsZSBub25jZQ==");
req.insert(http::field::sec_websocket_version, "13");
fail_loop([&](stream<test::stream>& ws)
{
ws.next_layer().append("\x88\x82\xff\xff\xff\xff\xfc\x17");
api.accept(ws, req);
try
{
static_buffer<1> b;
api.read(ws, b);
fail("success", __FILE__, __LINE__);
}
catch(system_error const& e)
{
if(e.code() != websocket::error::closed)
throw;
}
});
}
// failed handshake (missing Sec-WebSocket-Key)
fail_loop([&](stream<test::stream>& ws)
{
ws.next_layer().append(
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n");
ws.next_layer().read_size(20);
try
{
api.accept(ws);
BEAST_FAIL();
}
catch(system_error const& e)
{
if( e.code() != websocket::error::no_sec_key &&
e.code() != net::error::eof)
throw;
}
});
}
template<class Api>
void
testOversized(Api const& api)
{
net::io_context ioc;
auto const big = []
{
std::string s;
s += "X1: " + std::string(2000, '*') + "\r\n";
return s;
}();
// request in stream
{
stream<test::stream> ws{ioc,
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
+ big +
"\r\n"};
auto tr = connect(ws.next_layer());
try
{
2019-02-17 05:23:40 -08:00
api.accept(ws);
BEAST_FAIL();
2017-08-24 06:21:30 -07:00
}
catch(system_error const& se)
{
// VFALCO Its the http error category...
BEAST_EXPECTS(
se.code() == http::error::buffer_overflow,
se.code().message());
}
}
2019-02-17 05:23:40 -08:00
// request in stream, decorator
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
stream<test::stream> ws{ioc,
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
2019-02-17 05:23:40 -08:00
+ big +
"\r\n"};
auto tr = connect(ws.next_layer());
try
{
bool called = false;
api.accept_ex(ws, res_decorator{called});
BEAST_FAIL();
}
catch(system_error const& se)
{
// VFALCO Its the http error category...
BEAST_EXPECTS(
se.code() == http::error::buffer_overflow,
se.code().message());
}
}
2017-08-24 06:21:30 -07:00
2019-02-17 05:23:40 -08:00
// request in buffers
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
stream<test::stream> ws{ioc};
2017-08-24 06:21:30 -07:00
auto tr = connect(ws.next_layer());
try
{
2019-02-17 05:23:40 -08:00
api.accept(ws, net::buffer(
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
+ big +
"\r\n"
));
2019-02-17 05:23:40 -08:00
BEAST_FAIL();
2017-08-24 06:21:30 -07:00
}
catch(system_error const& se)
{
BEAST_EXPECTS(
se.code() == error::buffer_overflow,
se.code().message());
}
}
// request in buffers, decorator
{
2019-02-17 05:23:40 -08:00
stream<test::stream> ws{ioc};
2017-08-24 06:21:30 -07:00
auto tr = connect(ws.next_layer());
try
{
bool called = false;
2019-02-17 05:23:40 -08:00
api.accept_ex(ws, net::buffer(
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
+ big +
"\r\n"),
res_decorator{called});
2019-02-17 05:23:40 -08:00
BEAST_FAIL();
2017-08-24 06:21:30 -07:00
}
catch(system_error const& se)
{
BEAST_EXPECTS(
se.code() == error::buffer_overflow,
se.code().message());
}
}
// request in buffers and stream
{
2019-02-17 05:23:40 -08:00
stream<test::stream> ws{ioc,
2017-08-24 06:21:30 -07:00
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
+ big +
"\r\n"};
auto tr = connect(ws.next_layer());
try
{
2019-02-17 05:23:40 -08:00
api.accept(ws, websocket_test_suite::sbuf(
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
));
2019-02-17 05:23:40 -08:00
BEAST_FAIL();
2017-08-24 06:21:30 -07:00
}
catch(system_error const& se)
{
BEAST_EXPECTS(
se.code() == http::error::buffer_overflow,
se.code().message());
}
}
// request in buffers and stream, decorator
{
2019-02-17 05:23:40 -08:00
stream<test::stream> ws{ioc,
2017-08-24 06:21:30 -07:00
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
+ big +
"\r\n"};
auto tr = connect(ws.next_layer());
try
{
bool called = false;
2019-02-17 05:23:40 -08:00
api.accept_ex(ws, websocket_test_suite::sbuf(
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"),
res_decorator{called});
2019-02-17 05:23:40 -08:00
BEAST_FAIL();
2017-08-24 06:21:30 -07:00
}
catch(system_error const& se)
{
BEAST_EXPECTS(
se.code() == http::error::buffer_overflow,
se.code().message());
}
}
}
void
2019-02-17 05:23:40 -08:00
testInvalidInputs()
2017-08-24 06:21:30 -07:00
{
2019-02-17 05:23:40 -08:00
net::io_context ioc;
2017-08-24 06:21:30 -07:00
auto const check =
2019-02-17 05:23:40 -08:00
[&](error_code ev, string_view s)
2017-08-24 06:21:30 -07:00
{
for(int i = 0; i < 3; ++i)
{
std::size_t n;
switch(i)
{
default:
case 0:
n = 1;
break;
case 1:
n = s.size() / 2;
break;
case 2:
n = s.size() - 1;
break;
}
2019-02-17 05:23:40 -08:00
stream<test::stream> ws(ioc);
2017-08-24 06:21:30 -07:00
auto tr = connect(ws.next_layer());
ws.next_layer().append(
s.substr(n, s.size() - n));
2019-02-17 05:23:40 -08:00
tr.close();
2017-08-24 06:21:30 -07:00
try
{
2019-02-17 05:23:40 -08:00
ws.accept(net::buffer(s.data(), n));
2017-08-24 06:21:30 -07:00
BEAST_EXPECTS(! ev, ev.message());
}
catch(system_error const& se)
{
BEAST_EXPECTS(se.code() == ev, se.what());
}
}
};
// bad version
check(error::bad_http_version,
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.0\r\n"
"Host: localhost:80\r\n"
"Upgrade: WebSocket\r\n"
"Connection: keep-alive,upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n"
);
2019-02-17 05:23:40 -08:00
// bad method
check(error::bad_method,
2017-08-24 06:21:30 -07:00
"POST / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Upgrade: WebSocket\r\n"
"Connection: keep-alive,upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n"
);
2019-02-17 05:23:40 -08:00
// no Host
check(error::no_host,
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Upgrade: WebSocket\r\n"
"Connection: keep-alive,upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n"
);
2019-02-17 05:23:40 -08:00
// no Connection
check(error::no_connection,
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Upgrade: WebSocket\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
2017-08-24 06:21:30 -07:00
"Sec-WebSocket-Version: 13\r\n"
"\r\n"
);
2019-02-17 05:23:40 -08:00
// no Connection upgrade
check(error::no_connection_upgrade,
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Upgrade: WebSocket\r\n"
"Connection: keep-alive\r\n"
2017-08-24 06:21:30 -07:00
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
2017-08-24 06:21:30 -07:00
"\r\n"
);
2019-02-17 05:23:40 -08:00
// no Upgrade
check(error::no_upgrade,
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Connection: upgrade\r\n"
2017-08-24 06:21:30 -07:00
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
2017-08-24 06:21:30 -07:00
"\r\n"
);
2019-02-17 05:23:40 -08:00
// no Upgrade websocket
check(error::no_upgrade_websocket,
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Upgrade: HTTP/2\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n"
);
2019-02-17 05:23:40 -08:00
// no Sec-WebSocket-Key
check(error::no_sec_key,
2017-08-24 06:21:30 -07:00
"GET / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Upgrade: WebSocket\r\n"
"Connection: keep-alive,upgrade\r\n"
2017-08-24 06:21:30 -07:00
"Sec-WebSocket-Version: 13\r\n"
"\r\n"
);
2019-02-17 05:23:40 -08:00
// bad Sec-WebSocket-Key
check(error::bad_sec_key,
"GET / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Upgrade: WebSocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQdGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n"
);
2019-02-17 05:23:40 -08:00
// no Sec-WebSocket-Version
check(error::no_sec_version,
"GET / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Upgrade: WebSocket\r\n"
"Connection: keep-alive,upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"\r\n"
);
2019-02-17 05:23:40 -08:00
// bad Sec-WebSocket-Version
check(error::bad_sec_version,
"GET / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Upgrade: WebSocket\r\n"
"Connection: keep-alive,upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 1\r\n"
"\r\n"
);
2019-02-17 05:23:40 -08:00
// bad Sec-WebSocket-Version
check(error::bad_sec_version,
"GET / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Upgrade: WebSocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 12\r\n"
"\r\n"
);
2019-02-17 05:23:40 -08:00
2017-08-24 06:21:30 -07:00
// valid request
check({},
"GET / HTTP/1.1\r\n"
"Host: localhost:80\r\n"
"Upgrade: WebSocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n"
);
}
2019-02-13 08:00:07 -08:00
void
2019-02-17 05:23:40 -08:00
testEndOfStream()
{
net::io_context ioc;
{
stream<test::stream> ws(ioc);
auto tr = connect(ws.next_layer());
tr.close();
try
{
test_sync_api api;
api.accept(ws, net::const_buffer{});
BEAST_FAIL();
}
catch(system_error const& se)
{
BEAST_EXPECTS(
se.code() == error::closed,
se.code().message());
}
}
{
stream<test::stream> ws(ioc);
auto tr = connect(ws.next_layer());
tr.close();
try
{
test_async_api api;
api.accept(ws, net::const_buffer{});
BEAST_FAIL();
}
catch(system_error const& se)
{
BEAST_EXPECTS(
se.code() == error::closed,
se.code().message());
}
}
}
void
testAsync()
2019-02-13 08:00:07 -08:00
{
using tcp = net::ip::tcp;
net::io_context ioc;
2019-02-17 05:23:40 -08:00
// success, no timeout
2019-02-13 08:00:07 -08:00
{
stream<tcp::socket> ws1(ioc);
stream<tcp::socket> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.async_handshake("test", "/", test::success_handler());
ws2.async_accept(test::success_handler());
test::run_for(ioc, std::chrono::seconds(1));
}
{
stream<test::stream> ws1(ioc);
stream<test::stream> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.async_handshake("test", "/", test::success_handler());
ws2.async_accept(test::success_handler());
test::run_for(ioc, std::chrono::seconds(1));
}
// success, timeout enabled
{
stream<tcp::socket> ws1(ioc);
stream<tcp::socket> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.set_option(stream_base::timeout{
std::chrono::milliseconds(50),
stream_base::none(),
false});
ws1.async_accept(test::success_handler());
ws2.async_handshake("test", "/", test::success_handler());
test::run_for(ioc, std::chrono::seconds(1));
}
{
stream<test::stream> ws1(ioc);
stream<test::stream> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.set_option(stream_base::timeout{
std::chrono::milliseconds(50),
stream_base::none(),
false});
ws1.async_accept(test::success_handler());
ws2.async_handshake("test", "/", test::success_handler());
test::run_for(ioc, std::chrono::seconds(1));
}
// timeout
{
stream<tcp::socket> ws1(ioc);
stream<tcp::socket> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.set_option(stream_base::timeout{
std::chrono::milliseconds(50),
stream_base::none(),
false});
ws1.async_accept(test::fail_handler(beast::error::timeout));
test::run_for(ioc, std::chrono::seconds(1));
}
{
stream<test::stream> ws1(ioc);
stream<test::stream> ws2(ioc);
test::connect(ws1.next_layer(), ws2.next_layer());
ws1.set_option(stream_base::timeout{
std::chrono::milliseconds(50),
stream_base::none(),
false});
ws1.async_accept(test::fail_handler(beast::error::timeout));
test::run_for(ioc, std::chrono::seconds(1));
}
2019-02-17 05:23:40 -08:00
// abandoned operation
2018-02-28 13:36:47 -08:00
{
2019-02-17 05:23:40 -08:00
{
stream<tcp::socket> ws1(ioc);
ws1.async_accept(test::fail_handler(
net::error::operation_aborted));
}
test::run(ioc);
}
2019-02-17 05:23:40 -08:00
{
{
stream<tcp::socket> ws1(ioc);
string_view s =
"GET / HTTP/1.1\r\n"
"Host: localhost\r\n"
"Upgrade: websocket\r\n"
"Connection: upgrade\r\n"
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
"Sec-WebSocket-Version: 13\r\n"
"\r\n";
error_code ec;
http::request_parser<http::empty_body> p;
p.put(net::const_buffer(s.data(), s.size()), ec);
ws1.async_accept(p.get(), test::fail_handler(
net::error::operation_aborted));
}
test::run(ioc);
}
}
2017-08-24 06:21:30 -07:00
void
run() override
{
2019-02-17 05:23:40 -08:00
testMatrix(test_sync_api{});
testMatrix(test_async_api{});
testOversized(test_sync_api{});
testOversized(test_async_api{});
testInvalidInputs();
testEndOfStream();
testAsync();
2017-08-24 06:21:30 -07:00
}
};
2017-08-26 06:13:11 -07:00
BEAST_DEFINE_TESTSUITE(beast,websocket,accept);
2017-08-24 06:21:30 -07:00
} // websocket
} // beast
} // boost