2019-02-13 08:00:07 -08:00
|
|
|
|
2019-02-21 07:00:31 -08:00
|
|
|
// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
|
2017-12-06 21:59:39 +01: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>
|
|
|
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
|
|
|
|
|
|
#include <boost/asio/write.hpp>
|
|
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
#include <boost/config/workaround.hpp>
|
|
|
|
|
#if BOOST_WORKAROUND(BOOST_GCC, < 80200)
|
|
|
|
|
#define BOOST_BEAST_SYMBOL_HIDDEN __attribute__ ((visibility("hidden")))
|
|
|
|
|
#else
|
|
|
|
|
#define BOOST_BEAST_SYMBOL_HIDDEN
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-12-06 21:59:39 +01:00
|
|
|
namespace boost {
|
|
|
|
|
namespace beast {
|
|
|
|
|
namespace websocket {
|
|
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
class BOOST_BEAST_SYMBOL_HIDDEN read2_test
|
|
|
|
|
: public websocket_test_suite
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2019-02-13 08:00:07 -08:00
|
|
|
template<class Wrap, bool deflateSupported>
|
2017-12-31 10:02:33 -08:00
|
|
|
void
|
2019-02-13 08:00:07 -08:00
|
|
|
doReadTest(
|
|
|
|
|
Wrap const& w,
|
|
|
|
|
ws_type_t<deflateSupported>& ws,
|
|
|
|
|
close_code code)
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
try
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
|
|
|
|
multi_buffer b;
|
2019-02-13 08:00:07 -08:00
|
|
|
w.read(ws, b);
|
|
|
|
|
fail("", __FILE__, __LINE__);
|
|
|
|
|
}
|
|
|
|
|
catch(system_error const& se)
|
|
|
|
|
{
|
|
|
|
|
if(se.code() != error::closed)
|
|
|
|
|
throw;
|
|
|
|
|
BEAST_EXPECT(
|
|
|
|
|
ws.reason().code == code);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
template<class Wrap, bool deflateSupported>
|
|
|
|
|
void
|
|
|
|
|
doFailTest(
|
|
|
|
|
Wrap const& w,
|
|
|
|
|
ws_type_t<deflateSupported>& ws,
|
|
|
|
|
error_code ev)
|
|
|
|
|
{
|
|
|
|
|
try
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
|
|
|
|
multi_buffer b;
|
2019-02-13 08:00:07 -08:00
|
|
|
w.read(ws, b);
|
|
|
|
|
fail("", __FILE__, __LINE__);
|
|
|
|
|
}
|
|
|
|
|
catch(system_error const& se)
|
|
|
|
|
{
|
|
|
|
|
if(se.code() != ev)
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
template<bool deflateSupported = true, class Wrap>
|
|
|
|
|
void
|
|
|
|
|
doTestRead(Wrap const& w)
|
|
|
|
|
{
|
|
|
|
|
permessage_deflate pmd;
|
|
|
|
|
pmd.client_enable = false;
|
|
|
|
|
pmd.server_enable = false;
|
|
|
|
|
|
|
|
|
|
// already closed
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
|
|
|
|
echo_server es{log};
|
2019-02-13 08:00:07 -08:00
|
|
|
stream<test::stream, deflateSupported> ws{ioc_};
|
2017-12-31 10:02:33 -08:00
|
|
|
ws.next_layer().connect(es.stream());
|
|
|
|
|
ws.handshake("localhost", "/");
|
2019-02-13 08:00:07 -08:00
|
|
|
ws.close({});
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
w.read(ws, b);
|
|
|
|
|
fail("", __FILE__, __LINE__);
|
|
|
|
|
}
|
|
|
|
|
catch(system_error const& se)
|
|
|
|
|
{
|
|
|
|
|
BEAST_EXPECTS(
|
|
|
|
|
se.code() == net::error::operation_aborted,
|
|
|
|
|
se.code().message());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// empty, fragmented message
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
ws.next_layer().append(
|
|
|
|
|
string_view(
|
|
|
|
|
"\x01\x00" "\x80\x00", 4));
|
2017-12-31 10:02:33 -08:00
|
|
|
multi_buffer b;
|
2019-02-13 08:00:07 -08:00
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(b.size() == 0);
|
2017-12-31 10:02:33 -08:00
|
|
|
});
|
|
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// two part message
|
|
|
|
|
// triggers "fill the read buffer first"
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
w.write_raw(ws, sbuf(
|
|
|
|
|
"\x01\x81\xff\xff\xff\xff"));
|
|
|
|
|
w.write_raw(ws, sbuf(
|
|
|
|
|
"\xd5"));
|
|
|
|
|
w.write_raw(ws, sbuf(
|
|
|
|
|
"\x80\x81\xff\xff\xff\xff\xd5"));
|
2017-12-31 10:02:33 -08:00
|
|
|
multi_buffer b;
|
2019-02-13 08:00:07 -08:00
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == "**");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ping
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
put(ws.next_layer().buffer(), cbuf(
|
|
|
|
|
0x89, 0x00));
|
|
|
|
|
bool invoked = false;
|
|
|
|
|
ws.control_callback(
|
|
|
|
|
[&](frame_type kind, string_view)
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
BEAST_EXPECT(! invoked);
|
|
|
|
|
BEAST_EXPECT(kind == frame_type::ping);
|
|
|
|
|
invoked = true;
|
2017-12-31 10:02:33 -08:00
|
|
|
});
|
2019-02-13 08:00:07 -08:00
|
|
|
w.write(ws, sbuf("Hello"));
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(invoked);
|
|
|
|
|
BEAST_EXPECT(ws.got_text());
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == "Hello");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ping
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
put(ws.next_layer().buffer(), cbuf(
|
|
|
|
|
0x88, 0x00));
|
|
|
|
|
bool invoked = false;
|
|
|
|
|
ws.control_callback(
|
|
|
|
|
[&](frame_type kind, string_view)
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
BEAST_EXPECT(! invoked);
|
|
|
|
|
BEAST_EXPECT(kind == frame_type::close);
|
|
|
|
|
invoked = true;
|
2017-12-31 10:02:33 -08:00
|
|
|
});
|
2019-02-13 08:00:07 -08:00
|
|
|
w.write(ws, sbuf("Hello"));
|
|
|
|
|
doReadTest(w, ws, close_code::none);
|
2017-12-31 10:02:33 -08:00
|
|
|
});
|
|
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// ping then message
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
bool once = false;
|
|
|
|
|
ws.control_callback(
|
|
|
|
|
[&](frame_type kind, string_view s)
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
BEAST_EXPECT(kind == frame_type::pong);
|
|
|
|
|
BEAST_EXPECT(! once);
|
|
|
|
|
once = true;
|
|
|
|
|
BEAST_EXPECT(s == "");
|
2017-12-31 10:02:33 -08:00
|
|
|
});
|
2019-02-13 08:00:07 -08:00
|
|
|
w.ping(ws, "");
|
|
|
|
|
ws.binary(true);
|
|
|
|
|
w.write(ws, sbuf("Hello"));
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(once);
|
|
|
|
|
BEAST_EXPECT(ws.got_binary());
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == "Hello");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// ping then fragmented message
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
bool once = false;
|
|
|
|
|
ws.control_callback(
|
|
|
|
|
[&](frame_type kind, string_view s)
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
BEAST_EXPECT(kind == frame_type::pong);
|
|
|
|
|
BEAST_EXPECT(! once);
|
|
|
|
|
once = true;
|
|
|
|
|
BEAST_EXPECT(s == "payload");
|
2017-12-31 10:02:33 -08:00
|
|
|
});
|
2019-02-13 08:00:07 -08:00
|
|
|
ws.ping("payload");
|
|
|
|
|
w.write_some(ws, false, sbuf("Hello, "));
|
|
|
|
|
w.write_some(ws, false, sbuf(""));
|
|
|
|
|
w.write_some(ws, true, sbuf("World!"));
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(once);
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == "Hello, World!");
|
2017-12-31 10:02:33 -08:00
|
|
|
});
|
|
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// masked message, big
|
|
|
|
|
doStreamLoop([&](test::stream& ts)
|
|
|
|
|
{
|
|
|
|
|
echo_server es{log, kind::async_client};
|
|
|
|
|
ws_type_t<deflateSupported> ws{ts};
|
|
|
|
|
ws.next_layer().connect(es.stream());
|
|
|
|
|
ws.set_option(pmd);
|
|
|
|
|
es.async_handshake();
|
|
|
|
|
try
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
w.accept(ws);
|
|
|
|
|
std::string const s(2000, '*');
|
|
|
|
|
ws.auto_fragment(false);
|
|
|
|
|
ws.binary(false);
|
|
|
|
|
w.write(ws, net::buffer(s));
|
2017-12-31 10:02:33 -08:00
|
|
|
multi_buffer b;
|
2019-02-13 08:00:07 -08:00
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(ws.got_text());
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == s);
|
|
|
|
|
ws.next_layer().close();
|
|
|
|
|
}
|
|
|
|
|
catch(...)
|
|
|
|
|
{
|
|
|
|
|
ts.close();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-07-17 19:20:23 -04:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// close
|
|
|
|
|
doFailLoop([&](test::fail_count& fc)
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
echo_server es{log, kind::async};
|
2018-11-30 14:58:38 -08:00
|
|
|
net::io_context ioc;
|
2019-02-13 08:00:07 -08:00
|
|
|
stream<test::stream, deflateSupported> ws{ioc, fc};
|
2017-12-31 10:02:33 -08:00
|
|
|
ws.next_layer().connect(es.stream());
|
|
|
|
|
ws.handshake("localhost", "/");
|
2019-02-13 08:00:07 -08:00
|
|
|
// Cause close to be received
|
|
|
|
|
es.async_close();
|
2017-12-31 10:02:33 -08:00
|
|
|
std::size_t count = 0;
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
ws.async_read(b,
|
|
|
|
|
[&](error_code ec, std::size_t)
|
|
|
|
|
{
|
|
|
|
|
++count;
|
2019-02-13 08:00:07 -08:00
|
|
|
if(ec != error::closed)
|
|
|
|
|
BOOST_THROW_EXCEPTION(
|
|
|
|
|
system_error{ec});
|
2017-12-31 10:02:33 -08:00
|
|
|
});
|
|
|
|
|
ioc.run();
|
|
|
|
|
BEAST_EXPECT(count == 1);
|
2019-02-13 08:00:07 -08:00
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// already closed
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
w.close(ws, {});
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
doFailTest(w, ws,
|
|
|
|
|
net::error::operation_aborted);
|
|
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// buffer overflow
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
std::string const s = "Hello, world!";
|
|
|
|
|
ws.auto_fragment(false);
|
|
|
|
|
ws.binary(false);
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
multi_buffer b(3);
|
|
|
|
|
w.read(ws, b);
|
|
|
|
|
fail("", __FILE__, __LINE__);
|
|
|
|
|
}
|
|
|
|
|
catch(system_error const& se)
|
|
|
|
|
{
|
|
|
|
|
if(se.code() != error::buffer_overflow)
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// bad utf8, big
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
auto const s = std::string(2000, '*') +
|
|
|
|
|
random_string();
|
|
|
|
|
ws.text(true);
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
doReadTest(w, ws, close_code::bad_payload);
|
|
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// invalid fixed frame header
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
w.write_raw(ws, cbuf(
|
|
|
|
|
0x8f, 0x80, 0xff, 0xff, 0xff, 0xff));
|
|
|
|
|
doReadTest(w, ws, close_code::protocol_error);
|
|
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// bad close
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
put(ws.next_layer().buffer(), cbuf(
|
|
|
|
|
0x88, 0x02, 0x03, 0xed));
|
|
|
|
|
doFailTest(w, ws, error::bad_close_code);
|
|
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// message size above 2^64
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
w.write_some(ws, false, sbuf("*"));
|
|
|
|
|
w.write_raw(ws, cbuf(
|
|
|
|
|
0x80, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
|
|
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff));
|
|
|
|
|
doReadTest(w, ws, close_code::too_big);
|
|
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// message size exceeds max
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
ws.read_message_max(1);
|
|
|
|
|
w.write(ws, sbuf("**"));
|
|
|
|
|
doFailTest(w, ws, error::message_too_big);
|
|
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// bad utf8
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
put(ws.next_layer().buffer(), cbuf(
|
|
|
|
|
0x81, 0x06, 0x03, 0xea, 0xf0, 0x28, 0x8c, 0xbc));
|
|
|
|
|
doFailTest(w, ws, error::bad_frame_payload);
|
|
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// incomplete utf8
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
|
|
|
|
{
|
|
|
|
|
std::string const s =
|
|
|
|
|
"Hello, world!" "\xc0";
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
doReadTest(w, ws, close_code::bad_payload);
|
|
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// incomplete utf8, big
|
|
|
|
|
doTest<deflateSupported>(pmd,
|
|
|
|
|
[&](ws_type_t<deflateSupported>& ws)
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
std::string const s =
|
|
|
|
|
"\x81\x7e\x0f\xa1" +
|
|
|
|
|
std::string(4000, '*') + "\xc0";
|
|
|
|
|
ws.next_layer().append(s);
|
2017-12-31 10:02:33 -08:00
|
|
|
multi_buffer b;
|
2019-02-13 08:00:07 -08:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
do
|
2017-12-31 10:02:33 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
b.commit(w.read_some(ws, b.prepare(4000)));
|
|
|
|
|
}
|
|
|
|
|
while(! ws.is_message_done());
|
|
|
|
|
}
|
|
|
|
|
catch(system_error const& se)
|
|
|
|
|
{
|
|
|
|
|
if(se.code() != error::bad_frame_payload)
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-12-31 10:02:33 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// close frames
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
auto const check =
|
|
|
|
|
[&](error_code ev, string_view s)
|
|
|
|
|
{
|
|
|
|
|
echo_server es{log};
|
|
|
|
|
stream<test::stream, deflateSupported> ws{ioc_};
|
|
|
|
|
ws.next_layer().connect(es.stream());
|
|
|
|
|
w.handshake(ws, "localhost", "/");
|
|
|
|
|
ws.next_layer().append(s);
|
|
|
|
|
static_buffer<1> b;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
w.read(ws, b);
|
|
|
|
|
fail("", __FILE__, __LINE__);
|
|
|
|
|
}
|
|
|
|
|
catch(system_error const& se)
|
|
|
|
|
{
|
|
|
|
|
BEAST_EXPECTS(se.code() == ev,
|
|
|
|
|
se.code().message());
|
|
|
|
|
}
|
|
|
|
|
ws.next_layer().close();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// payload length 1
|
|
|
|
|
check(error::bad_close_size,
|
|
|
|
|
"\x88\x01\x01");
|
|
|
|
|
|
|
|
|
|
// invalid close code 1005
|
|
|
|
|
check(error::bad_close_code,
|
|
|
|
|
"\x88\x02\x03\xed");
|
|
|
|
|
|
|
|
|
|
// invalid utf8
|
|
|
|
|
check(error::bad_close_payload,
|
|
|
|
|
"\x88\x06\xfc\x15\x0f\xd7\x73\x43");
|
|
|
|
|
|
|
|
|
|
// good utf8
|
|
|
|
|
check(error::closed,
|
|
|
|
|
"\x88\x06\xfc\x15utf8");
|
2017-12-06 21:59:39 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
template<class Wrap>
|
2017-12-06 21:59:39 +01:00
|
|
|
void
|
2019-02-13 08:00:07 -08:00
|
|
|
doTestReadDeflate(Wrap const& w)
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
permessage_deflate pmd;
|
|
|
|
|
pmd.client_enable = true;
|
|
|
|
|
pmd.server_enable = true;
|
|
|
|
|
pmd.client_max_window_bits = 9;
|
|
|
|
|
pmd.server_max_window_bits = 9;
|
|
|
|
|
pmd.compLevel = 1;
|
2017-12-06 21:59:39 +01:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// message size limit
|
|
|
|
|
doTest<true>(pmd,
|
|
|
|
|
[&](ws_type_t<true>& ws)
|
|
|
|
|
{
|
|
|
|
|
std::string const s = std::string(128, '*');
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
ws.read_message_max(32);
|
|
|
|
|
doFailTest(w, ws, error::message_too_big);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// invalid inflate block
|
|
|
|
|
doTest<true>(pmd,
|
|
|
|
|
[&](ws_type_t<true>& ws)
|
|
|
|
|
{
|
|
|
|
|
auto const& s = random_string();
|
|
|
|
|
ws.binary(true);
|
|
|
|
|
ws.next_layer().append(
|
|
|
|
|
"\xc2\x40" + s.substr(0, 64));
|
|
|
|
|
flat_buffer b;
|
|
|
|
|
try
|
2017-12-27 12:30:44 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
w.read(ws, b);
|
|
|
|
|
}
|
|
|
|
|
catch(system_error const& se)
|
2017-12-27 12:30:44 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
if(se.code() == test::error::test_failure)
|
|
|
|
|
throw;
|
|
|
|
|
BEAST_EXPECTS(se.code().category() ==
|
2019-02-22 13:06:43 -08:00
|
|
|
make_error_code(static_cast<
|
|
|
|
|
zlib::error>(0)).category(),
|
|
|
|
|
se.code().message());
|
2019-02-13 08:00:07 -08:00
|
|
|
}
|
|
|
|
|
catch(...)
|
2017-12-27 12:30:44 -08:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-12-27 12:30:44 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// no_context_takeover
|
|
|
|
|
pmd.server_no_context_takeover = true;
|
|
|
|
|
doTest<true>(pmd,
|
|
|
|
|
[&](ws_type_t<true>& ws)
|
|
|
|
|
{
|
|
|
|
|
auto const& s = random_string();
|
|
|
|
|
ws.binary(true);
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == s);
|
|
|
|
|
});
|
|
|
|
|
pmd.client_no_context_takeover = false;
|
|
|
|
|
}
|
2017-12-06 21:59:39 +01:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
template<class Wrap>
|
2017-12-06 21:59:39 +01:00
|
|
|
void
|
2019-02-13 08:00:07 -08:00
|
|
|
doTestRead(
|
|
|
|
|
permessage_deflate const& pmd,
|
|
|
|
|
Wrap const& w)
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
// message
|
|
|
|
|
doTest(pmd, [&](ws_type& ws)
|
|
|
|
|
{
|
|
|
|
|
std::string const s = "Hello, world!";
|
|
|
|
|
ws.auto_fragment(false);
|
|
|
|
|
ws.binary(false);
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(ws.got_text());
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == s);
|
|
|
|
|
});
|
2017-12-06 21:59:39 +01:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// masked message
|
|
|
|
|
doStreamLoop([&](test::stream& ts)
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
echo_server es{log, kind::async_client};
|
|
|
|
|
ws_type ws{ts};
|
2017-12-06 21:59:39 +01:00
|
|
|
ws.next_layer().connect(es.stream());
|
2019-02-13 08:00:07 -08:00
|
|
|
ws.set_option(pmd);
|
|
|
|
|
es.async_handshake();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
w.accept(ws);
|
|
|
|
|
std::string const s = "Hello, world!";
|
|
|
|
|
ws.auto_fragment(false);
|
|
|
|
|
ws.binary(false);
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(ws.got_text());
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == s);
|
|
|
|
|
ws.next_layer().close();
|
|
|
|
|
}
|
|
|
|
|
catch(...)
|
|
|
|
|
{
|
|
|
|
|
ts.close();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// empty message
|
|
|
|
|
doTest(pmd, [&](ws_type& ws)
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
std::string const s = "";
|
|
|
|
|
ws.text(true);
|
|
|
|
|
w.write(ws, net::buffer(s));
|
2017-12-06 21:59:39 +01:00
|
|
|
multi_buffer b;
|
2019-02-13 08:00:07 -08:00
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(ws.got_text());
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == s);
|
|
|
|
|
});
|
2017-12-06 21:59:39 +01:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// partial message
|
|
|
|
|
doTest(pmd, [&](ws_type& ws)
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
std::string const s = "Hello";
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
char buf[3];
|
|
|
|
|
auto const bytes_written =
|
|
|
|
|
w.read_some(ws, net::buffer(buf, sizeof(buf)));
|
|
|
|
|
BEAST_EXPECT(bytes_written > 0);
|
|
|
|
|
BEAST_EXPECT(
|
|
|
|
|
string_view(buf, 3).substr(0, bytes_written) ==
|
|
|
|
|
s.substr(0, bytes_written));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// partial message, dynamic buffer
|
|
|
|
|
doTest(pmd, [&](ws_type& ws)
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
std::string const s = "Hello, world!";
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
auto bytes_written =
|
|
|
|
|
w.read_some(ws, 3, b);
|
|
|
|
|
BEAST_EXPECT(bytes_written > 0);
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) ==
|
|
|
|
|
s.substr(0, b.size()));
|
|
|
|
|
w.read_some(ws, 256, b);
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == s);
|
|
|
|
|
});
|
2017-12-06 21:59:39 +01:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// big message
|
|
|
|
|
doTest(pmd, [&](ws_type& ws)
|
|
|
|
|
{
|
|
|
|
|
auto const& s = random_string();
|
|
|
|
|
ws.binary(true);
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
multi_buffer b;
|
|
|
|
|
w.read(ws, b);
|
|
|
|
|
BEAST_EXPECT(buffers_to_string(b.data()) == s);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// message, bad utf8
|
|
|
|
|
doTest(pmd, [&](ws_type& ws)
|
|
|
|
|
{
|
|
|
|
|
std::string const s = "\x03\xea\xf0\x28\x8c\xbc";
|
|
|
|
|
ws.auto_fragment(false);
|
|
|
|
|
ws.text(true);
|
|
|
|
|
w.write(ws, net::buffer(s));
|
|
|
|
|
doReadTest(w, ws, close_code::bad_payload);
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-12-06 21:59:39 +01:00
|
|
|
|
|
|
|
|
void
|
2019-02-13 08:00:07 -08:00
|
|
|
testRead()
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
doTestRead<false>(SyncClient{});
|
|
|
|
|
doTestRead<true>(SyncClient{});
|
|
|
|
|
doTestReadDeflate(SyncClient{});
|
|
|
|
|
yield_to([&](yield_context yield)
|
|
|
|
|
{
|
|
|
|
|
doTestRead<false>(AsyncClient{yield});
|
|
|
|
|
doTestRead<true>(AsyncClient{yield});
|
|
|
|
|
doTestReadDeflate(AsyncClient{yield});
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-06 21:59:39 +01:00
|
|
|
permessage_deflate pmd;
|
2019-02-13 08:00:07 -08:00
|
|
|
pmd.client_enable = false;
|
|
|
|
|
pmd.server_enable = false;
|
|
|
|
|
doTestRead(pmd, SyncClient{});
|
|
|
|
|
yield_to([&](yield_context yield)
|
|
|
|
|
{
|
|
|
|
|
doTestRead(pmd, AsyncClient{yield});
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-06 21:59:39 +01:00
|
|
|
pmd.client_enable = true;
|
|
|
|
|
pmd.server_enable = true;
|
2019-02-13 08:00:07 -08:00
|
|
|
pmd.client_max_window_bits = 9;
|
|
|
|
|
pmd.server_max_window_bits = 9;
|
|
|
|
|
pmd.compLevel = 1;
|
|
|
|
|
doTestRead(pmd, SyncClient{});
|
|
|
|
|
yield_to([&](yield_context yield)
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
doTestRead(pmd, AsyncClient{yield});
|
|
|
|
|
});
|
2017-12-06 21:59:39 +01:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// Read close frames
|
2017-12-06 21:59:39 +01:00
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
auto const check =
|
|
|
|
|
[&](error_code ev, string_view s)
|
|
|
|
|
{
|
|
|
|
|
echo_server es{log};
|
|
|
|
|
stream<test::stream> ws{ioc_};
|
|
|
|
|
ws.next_layer().connect(es.stream());
|
|
|
|
|
ws.handshake("localhost", "/");
|
|
|
|
|
ws.next_layer().append(s);
|
|
|
|
|
static_buffer<1> b;
|
|
|
|
|
error_code ec;
|
|
|
|
|
ws.read(b, ec);
|
|
|
|
|
BEAST_EXPECTS(ec == ev, ec.message());
|
|
|
|
|
ws.next_layer().close();
|
|
|
|
|
};
|
2017-12-06 21:59:39 +01:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// payload length 1
|
|
|
|
|
check(error::bad_close_size,
|
|
|
|
|
"\x88\x01\x01");
|
2018-02-28 13:36:47 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// invalid close code 1005
|
|
|
|
|
check(error::bad_close_code,
|
|
|
|
|
"\x88\x02\x03\xed");
|
2018-03-01 06:36:18 -08:00
|
|
|
|
2019-02-13 08:00:07 -08:00
|
|
|
// invalid utf8
|
|
|
|
|
check(error::bad_close_payload,
|
|
|
|
|
"\x88\x06\xfc\x15\x0f\xd7\x73\x43");
|
|
|
|
|
|
|
|
|
|
// good utf8
|
|
|
|
|
check(error::closed,
|
|
|
|
|
"\x88\x06\xfc\x15utf8");
|
2018-03-01 06:36:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-06 21:59:39 +01:00
|
|
|
void
|
|
|
|
|
run() override
|
|
|
|
|
{
|
2019-02-13 08:00:07 -08:00
|
|
|
testRead();
|
2017-12-06 21:59:39 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
BEAST_DEFINE_TESTSUITE(beast,websocket,read2);
|
|
|
|
|
|
|
|
|
|
} // websocket
|
|
|
|
|
} // beast
|
|
|
|
|
} // boost
|