mirror of
https://github.com/XRPLF/rippled.git
synced 2026-04-29 15:37:57 +00:00
Squashed 'src/beast/' changes from 1b9a714..6d5547a
6d5547a Set version to 1.0.0-b34 6fab138 Fix and tidy up CMake build scripts: ccefa54 Set version to 1.0.0-b33 32afe41 Set internal state correctly when writing frames: fe3e20b Add write_frames unit test 578dcd0 Add decorator unit test aaa3733 Use fwrite return value in file_body df66165 Require Visual Studio 2015 Update 3 or later b8e5a21 Set version to 1.0.0-b32 ffb1758 Update CMake scripts for finding packages: b893749 Remove http Writer suspend and resume feature (API Change): 27864fb Add io_service completion invariants tests eba05a7 Set version to 1.0.0-b31 484bcef Fix badge markdown in README.md 5663bea Add missing dynabuf_readstream member 0d7a551 Tidy up build settings 0fd4030 Move the handler, don't copy it git-subtree-dir: src/beast git-subtree-split: 6d5547a32c50ec95832c4779311502555ab0ee1f
This commit is contained in:
241
test/websocket/frame.cpp
Normal file
241
test/websocket/frame.cpp
Normal file
@@ -0,0 +1,241 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 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)
|
||||
//
|
||||
|
||||
#include <beast/websocket/detail/frame.hpp>
|
||||
#include <beast/websocket/detail/stream_base.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <initializer_list>
|
||||
#include <climits>
|
||||
|
||||
namespace beast {
|
||||
namespace websocket {
|
||||
namespace detail {
|
||||
|
||||
static
|
||||
bool
|
||||
operator==(frame_header const& lhs, frame_header const& rhs)
|
||||
{
|
||||
return
|
||||
lhs.op == rhs.op &&
|
||||
lhs.fin == rhs.fin &&
|
||||
lhs.mask == rhs.mask &&
|
||||
lhs.rsv1 == rhs.rsv1 &&
|
||||
lhs.rsv2 == rhs.rsv2 &&
|
||||
lhs.rsv3 == rhs.rsv3 &&
|
||||
lhs.len == rhs.len &&
|
||||
lhs.key == rhs.key;
|
||||
}
|
||||
|
||||
class frame_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void testCloseCodes()
|
||||
{
|
||||
BEAST_EXPECT(! is_valid(0));
|
||||
BEAST_EXPECT(! is_valid(1));
|
||||
BEAST_EXPECT(! is_valid(999));
|
||||
BEAST_EXPECT(! is_valid(1004));
|
||||
BEAST_EXPECT(! is_valid(1005));
|
||||
BEAST_EXPECT(! is_valid(1006));
|
||||
BEAST_EXPECT(! is_valid(1016));
|
||||
BEAST_EXPECT(! is_valid(2000));
|
||||
BEAST_EXPECT(! is_valid(2999));
|
||||
BEAST_EXPECT(is_valid(1000));
|
||||
BEAST_EXPECT(is_valid(1002));
|
||||
BEAST_EXPECT(is_valid(3000));
|
||||
BEAST_EXPECT(is_valid(4000));
|
||||
BEAST_EXPECT(is_valid(5000));
|
||||
}
|
||||
|
||||
struct test_fh : frame_header
|
||||
{
|
||||
test_fh()
|
||||
{
|
||||
op = opcode::text;
|
||||
fin = false;
|
||||
mask = false;
|
||||
rsv1 = false;
|
||||
rsv2 = false;
|
||||
rsv3 = false;
|
||||
len = 0;
|
||||
key = 0;
|
||||
}
|
||||
};
|
||||
|
||||
void testFrameHeader()
|
||||
{
|
||||
// good frame fields
|
||||
{
|
||||
role_type role = role_type::client;
|
||||
|
||||
auto check =
|
||||
[&](frame_header const& fh)
|
||||
{
|
||||
fh_streambuf sb;
|
||||
write(sb, fh);
|
||||
close_code::value code;
|
||||
stream_base stream;
|
||||
stream.open(role);
|
||||
detail::frame_header fh1;
|
||||
auto const n =
|
||||
stream.read_fh1(fh1, sb, code);
|
||||
if(! BEAST_EXPECT(! code))
|
||||
return;
|
||||
if(! BEAST_EXPECT(sb.size() == n))
|
||||
return;
|
||||
stream.read_fh2(fh1, sb, code);
|
||||
if(! BEAST_EXPECT(! code))
|
||||
return;
|
||||
if(! BEAST_EXPECT(sb.size() == 0))
|
||||
return;
|
||||
BEAST_EXPECT(fh1 == fh);
|
||||
};
|
||||
|
||||
test_fh fh;
|
||||
|
||||
check(fh);
|
||||
|
||||
role = role_type::server;
|
||||
fh.mask = true;
|
||||
fh.key = 1;
|
||||
check(fh);
|
||||
|
||||
fh.len = 1;
|
||||
check(fh);
|
||||
|
||||
fh.len = 126;
|
||||
check(fh);
|
||||
|
||||
fh.len = 65535;
|
||||
check(fh);
|
||||
|
||||
fh.len = 65536;
|
||||
check(fh);
|
||||
|
||||
fh.len = 65537;
|
||||
check(fh);
|
||||
}
|
||||
|
||||
// bad frame fields
|
||||
{
|
||||
role_type role = role_type::client;
|
||||
|
||||
auto check =
|
||||
[&](frame_header const& fh)
|
||||
{
|
||||
fh_streambuf sb;
|
||||
write(sb, fh);
|
||||
close_code::value code;
|
||||
stream_base stream;
|
||||
stream.open(role);
|
||||
detail::frame_header fh1;
|
||||
auto const n =
|
||||
stream.read_fh1(fh1, sb, code);
|
||||
if(code)
|
||||
{
|
||||
pass();
|
||||
return;
|
||||
}
|
||||
if(! BEAST_EXPECT(sb.size() == n))
|
||||
return;
|
||||
stream.read_fh2(fh1, sb, code);
|
||||
if(! BEAST_EXPECT(code))
|
||||
return;
|
||||
if(! BEAST_EXPECT(sb.size() == 0))
|
||||
return;
|
||||
};
|
||||
|
||||
test_fh fh;
|
||||
|
||||
fh.op = opcode::close;
|
||||
fh.fin = true;
|
||||
fh.len = 126;
|
||||
check(fh);
|
||||
fh.len = 0;
|
||||
|
||||
fh.rsv1 = true;
|
||||
check(fh);
|
||||
fh.rsv1 = false;
|
||||
|
||||
fh.rsv2 = true;
|
||||
check(fh);
|
||||
fh.rsv2 = false;
|
||||
|
||||
fh.rsv3 = true;
|
||||
check(fh);
|
||||
fh.rsv3 = false;
|
||||
|
||||
fh.op = opcode::rsv3;
|
||||
check(fh);
|
||||
fh.op = opcode::text;
|
||||
|
||||
fh.op = opcode::ping;
|
||||
fh.fin = false;
|
||||
check(fh);
|
||||
fh.fin = true;
|
||||
|
||||
fh.mask = true;
|
||||
check(fh);
|
||||
|
||||
role = role_type::server;
|
||||
fh.mask = false;
|
||||
check(fh);
|
||||
}
|
||||
}
|
||||
|
||||
void bad(std::initializer_list<std::uint8_t> bs)
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
using boost::asio::buffer_copy;
|
||||
static role_type constexpr role = role_type::client;
|
||||
std::vector<std::uint8_t> v{bs};
|
||||
fh_streambuf sb;
|
||||
sb.commit(buffer_copy(sb.prepare(v.size()), buffer(v)));
|
||||
stream_base stream;
|
||||
stream.open(role);
|
||||
close_code::value code;
|
||||
detail::frame_header fh;
|
||||
auto const n =
|
||||
stream.read_fh1(fh, sb, code);
|
||||
if(code)
|
||||
{
|
||||
pass();
|
||||
return;
|
||||
}
|
||||
if(! BEAST_EXPECT(sb.size() == n))
|
||||
return;
|
||||
stream.read_fh2(fh, sb, code);
|
||||
if(! BEAST_EXPECT(code))
|
||||
return;
|
||||
if(! BEAST_EXPECT(sb.size() == 0))
|
||||
return;
|
||||
}
|
||||
|
||||
void testBadFrameHeaders()
|
||||
{
|
||||
// bad frame fields
|
||||
//
|
||||
// can't be created by the library
|
||||
// so we produce them manually.
|
||||
|
||||
bad({0, 126, 0, 125});
|
||||
bad({0, 127, 0, 0, 0, 0, 0, 0, 255, 255});
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
testCloseCodes();
|
||||
testFrameHeader();
|
||||
testBadFrameHeaders();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(frame,websocket,beast);
|
||||
|
||||
} // detail
|
||||
} // websocket
|
||||
} // beast
|
||||
Reference in New Issue
Block a user