mirror of
https://github.com/boostorg/mysql.git
synced 2026-02-15 13:12:21 +00:00
create_message
This commit is contained in:
70
test/common/create_message.hpp
Normal file
70
test/common/create_message.hpp
Normal file
@@ -0,0 +1,70 @@
|
||||
//
|
||||
// Copyright (c) 2019-2022 Ruben Perez Hidalgo (rubenperez038 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)
|
||||
//
|
||||
|
||||
#ifndef BOOST_MYSQL_TEST_COMMON_CREATE_MESSAGE_HPP
|
||||
#define BOOST_MYSQL_TEST_COMMON_CREATE_MESSAGE_HPP
|
||||
|
||||
#include "buffer_concat.hpp"
|
||||
#include <boost/mysql/detail/protocol/common_messages.hpp>
|
||||
#include <boost/mysql/detail/protocol/serialization.hpp>
|
||||
#include <boost/mysql/detail/protocol/serialization_context.hpp>
|
||||
#include <boost/mysql/detail/protocol/capabilities.hpp>
|
||||
#include <cstring>
|
||||
#include <cstdint>
|
||||
|
||||
namespace boost {
|
||||
namespace mysql {
|
||||
namespace test {
|
||||
|
||||
inline std::vector<std::uint8_t> create_message(
|
||||
std::uint8_t seqnum,
|
||||
std::vector<std::uint8_t> body
|
||||
)
|
||||
{
|
||||
std::uint32_t body_size = body.size();
|
||||
boost::mysql::detail::packet_header header { boost::mysql::detail::int3{body_size}, seqnum };
|
||||
body.resize(body_size + 4);
|
||||
std::memmove(body.data() + 4, body.data(), body_size);
|
||||
boost::mysql::detail::serialization_context ctx (boost::mysql::detail::capabilities(), body.data());
|
||||
boost::mysql::detail::serialize(ctx, header);
|
||||
return body;
|
||||
}
|
||||
|
||||
inline std::vector<std::uint8_t> create_message(
|
||||
std::uint8_t seqnum1,
|
||||
std::vector<std::uint8_t> body1,
|
||||
std::uint8_t seqnum2,
|
||||
std::vector<std::uint8_t> body2
|
||||
)
|
||||
{
|
||||
return concat_copy(
|
||||
create_message(seqnum1, std::move(body1)),
|
||||
create_message(seqnum2, std::move(body2))
|
||||
);
|
||||
}
|
||||
|
||||
inline std::vector<std::uint8_t> create_message(
|
||||
std::uint8_t seqnum1,
|
||||
std::vector<std::uint8_t> body1,
|
||||
std::uint8_t seqnum2,
|
||||
std::vector<std::uint8_t> body2,
|
||||
std::uint8_t seqnum3,
|
||||
std::vector<std::uint8_t> body3
|
||||
)
|
||||
{
|
||||
return concat_copy(
|
||||
create_message(seqnum1, std::move(body1)),
|
||||
create_message(seqnum2, std::move(body2)),
|
||||
create_message(seqnum3, std::move(body3))
|
||||
);
|
||||
}
|
||||
|
||||
} // detail
|
||||
} // mysql
|
||||
} // boost
|
||||
|
||||
#endif
|
||||
@@ -6,12 +6,8 @@
|
||||
//
|
||||
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/mysql/detail/protocol/capabilities.hpp>
|
||||
#include <boost/mysql/detail/protocol/serialization.hpp>
|
||||
#include <boost/mysql/detail/protocol/serialization_context.hpp>
|
||||
#include <boost/mysql/detail/channel/read_buffer.hpp>
|
||||
#include <boost/mysql/detail/channel/message_parser.hpp>
|
||||
#include <boost/mysql/detail/protocol/common_messages.hpp>
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <boost/test/unit_test_suite.hpp>
|
||||
#include <cstddef>
|
||||
@@ -20,12 +16,12 @@
|
||||
#include <vector>
|
||||
#include "assert_buffer_equals.hpp"
|
||||
#include "buffer_concat.hpp"
|
||||
#include "create_message.hpp"
|
||||
|
||||
using boost::mysql::detail::message_parser;
|
||||
using boost::mysql::detail::read_buffer;
|
||||
using boost::mysql::detail::packet_header;
|
||||
using boost::mysql::detail::int3;
|
||||
using boost::mysql::test::concat_copy;
|
||||
using boost::mysql::test::create_message;
|
||||
using boost::asio::buffer;
|
||||
|
||||
namespace
|
||||
@@ -66,17 +62,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
std::vector<std::uint8_t> create_message(std::uint8_t seqnum, std::vector<std::uint8_t> body)
|
||||
{
|
||||
std::uint32_t body_size = body.size();
|
||||
packet_header header { int3{body_size}, seqnum };
|
||||
body.resize(body_size + 4);
|
||||
std::memmove(body.data() + 4, body.data(), body_size);
|
||||
boost::mysql::detail::serialization_context ctx (boost::mysql::detail::capabilities(), body.data());
|
||||
boost::mysql::detail::serialize(ctx, header);
|
||||
return body;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(message_parser_parse_message)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(fragmented_header_and_body_multiple)
|
||||
@@ -187,12 +172,7 @@ BOOST_AUTO_TEST_CASE(two_messages_one_after_another)
|
||||
// message to be parsed
|
||||
std::vector<std::uint8_t> first_msg_body { 0x01, 0x02, 0x03 };
|
||||
std::vector<std::uint8_t> second_msg_body { 0x04, 0x05, 0x06, 0x07 };
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(0, first_msg_body),
|
||||
create_message(2, second_msg_body)
|
||||
)
|
||||
);
|
||||
parser_fixture fixture (create_message(0, first_msg_body, 2, second_msg_body));
|
||||
|
||||
// 1st message
|
||||
auto res = fixture.parse_bytes(7);
|
||||
@@ -224,12 +204,7 @@ BOOST_AUTO_TEST_CASE(two_messages_at_once)
|
||||
// message to be parsed
|
||||
std::vector<std::uint8_t> first_msg_body { 0x01, 0x02, 0x03 };
|
||||
std::vector<std::uint8_t> second_msg_body { 0x04, 0x05, 0x06, 0x07 };
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(0, first_msg_body),
|
||||
create_message(2, second_msg_body)
|
||||
)
|
||||
);
|
||||
parser_fixture fixture (create_message(0, first_msg_body, 2, second_msg_body));
|
||||
|
||||
// 1st message
|
||||
auto res = fixture.parse_bytes(15);
|
||||
@@ -263,10 +238,10 @@ BOOST_AUTO_TEST_CASE(three_messages_last_fragmented)
|
||||
std::vector<std::uint8_t> second_msg_body { 0x04, 0x05, 0x06, 0x07 };
|
||||
std::vector<std::uint8_t> third_msg_body { 0x08, 0x09 };
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(0, first_msg_body),
|
||||
create_message(2, second_msg_body),
|
||||
create_message(3, third_msg_body)
|
||||
create_message(
|
||||
0, first_msg_body,
|
||||
2, second_msg_body,
|
||||
3, third_msg_body
|
||||
)
|
||||
);
|
||||
|
||||
@@ -314,9 +289,9 @@ BOOST_AUTO_TEST_CASE(two_frame_message)
|
||||
{
|
||||
// message to be parsed
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(0, std::vector<std::uint8_t>(64, 0x04)),
|
||||
create_message(1, { 0x05, 0x06, 0x07 })
|
||||
create_message(
|
||||
0, std::vector<std::uint8_t>(64, 0x04),
|
||||
1, { 0x05, 0x06, 0x07 }
|
||||
),
|
||||
64 + 16
|
||||
);
|
||||
@@ -355,10 +330,10 @@ BOOST_AUTO_TEST_CASE(two_frame_message_with_reserved_area)
|
||||
// message to be parsed
|
||||
std::vector<std::uint8_t> first_msg_body { 0x01, 0x02, 0x03 };
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(0, first_msg_body),
|
||||
create_message(4, std::vector<std::uint8_t>(64, 0x04)),
|
||||
create_message(5, { 0x05, 0x06, 0x07 })
|
||||
create_message(
|
||||
0, first_msg_body,
|
||||
4, std::vector<std::uint8_t>(64, 0x04),
|
||||
5, { 0x05, 0x06, 0x07 }
|
||||
),
|
||||
64 + 64
|
||||
);
|
||||
@@ -393,9 +368,9 @@ BOOST_AUTO_TEST_CASE(two_frame_message_fragmented)
|
||||
{
|
||||
// message to be parsed
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(0, std::vector<std::uint8_t>(64, 0x04)),
|
||||
create_message(1, { 0x05, 0x06, 0x07 })
|
||||
create_message(
|
||||
0, std::vector<std::uint8_t>(64, 0x04),
|
||||
1, { 0x05, 0x06, 0x07 }
|
||||
),
|
||||
64 + 16
|
||||
);
|
||||
@@ -463,10 +438,10 @@ BOOST_AUTO_TEST_CASE(three_frame_message)
|
||||
{
|
||||
// message to be parsed
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(2, std::vector<std::uint8_t>(64, 0x04)),
|
||||
create_message(3, std::vector<std::uint8_t>(64, 0x05)),
|
||||
create_message(4, { 0x05, 0x06, 0x07 })
|
||||
create_message(
|
||||
2, std::vector<std::uint8_t>(64, 0x04),
|
||||
3, std::vector<std::uint8_t>(64, 0x05),
|
||||
4, { 0x05, 0x06, 0x07 }
|
||||
),
|
||||
64 * 2 + 64
|
||||
);
|
||||
@@ -508,9 +483,9 @@ BOOST_AUTO_TEST_CASE(two_frame_message_mismatched_seqnums)
|
||||
{
|
||||
// message to be parsed
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(1, std::vector<std::uint8_t>(64, 0x04)),
|
||||
create_message(3, { 0x05, 0x06, 0x07 })
|
||||
create_message(
|
||||
1, std::vector<std::uint8_t>(64, 0x04),
|
||||
3, { 0x05, 0x06, 0x07 }
|
||||
),
|
||||
64 + 16
|
||||
);
|
||||
@@ -532,10 +507,10 @@ BOOST_AUTO_TEST_CASE(three_frame_message_mismatched_seqnums)
|
||||
{
|
||||
// message to be parsed
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(1, std::vector<std::uint8_t>(64, 0x04)),
|
||||
create_message(2, std::vector<std::uint8_t>(64, 0x05)),
|
||||
create_message(0, { 0x05, 0x06, 0x07 })
|
||||
create_message(
|
||||
1, std::vector<std::uint8_t>(64, 0x04),
|
||||
2, std::vector<std::uint8_t>(64, 0x05),
|
||||
0, { 0x05, 0x06, 0x07 }
|
||||
),
|
||||
64 * 2 + 64
|
||||
);
|
||||
@@ -561,9 +536,9 @@ BOOST_AUTO_TEST_CASE(two_frame_seqnum_overflow)
|
||||
{
|
||||
// message to be parsed
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(255, std::vector<std::uint8_t>(64, 0x04)),
|
||||
create_message(0, { 0x05, 0x06, 0x07 })
|
||||
create_message(
|
||||
255, std::vector<std::uint8_t>(64, 0x04),
|
||||
0, { 0x05, 0x06, 0x07 }
|
||||
),
|
||||
64 + 16
|
||||
);
|
||||
@@ -587,10 +562,10 @@ BOOST_AUTO_TEST_CASE(two_frame_max_size)
|
||||
// message to be parsed. The two frames have size == max_frame_size,
|
||||
// so a third, empty header is received
|
||||
parser_fixture fixture (
|
||||
concat_copy(
|
||||
create_message(1, std::vector<std::uint8_t>(64, 0x04)),
|
||||
create_message(2, std::vector<std::uint8_t>(64, 0x05)),
|
||||
create_message(3, {})
|
||||
create_message(
|
||||
1, std::vector<std::uint8_t>(64, 0x04),
|
||||
2, std::vector<std::uint8_t>(64, 0x05),
|
||||
3, {}
|
||||
),
|
||||
64*3
|
||||
);
|
||||
|
||||
@@ -18,12 +18,14 @@
|
||||
#include "boost/mysql/error.hpp"
|
||||
#include "assert_buffer_equals.hpp"
|
||||
#include "test_stream.hpp"
|
||||
#include "create_message.hpp"
|
||||
#include "buffer_concat.hpp"
|
||||
|
||||
using boost::mysql::detail::message_reader;
|
||||
using boost::mysql::test::test_stream;
|
||||
using boost::mysql::test::fail_count;
|
||||
using boost::mysql::test::concat_copy;
|
||||
using boost::mysql::test::create_message;
|
||||
using boost::mysql::error_code;
|
||||
using boost::mysql::errc;
|
||||
using boost::asio::buffer;
|
||||
@@ -31,18 +33,6 @@ using boost::asio::buffer;
|
||||
namespace
|
||||
{
|
||||
|
||||
// TODO: duplicated
|
||||
std::vector<std::uint8_t> create_frame(std::uint8_t seqnum, std::vector<std::uint8_t> body)
|
||||
{
|
||||
std::uint32_t body_size = body.size();
|
||||
boost::mysql::detail::packet_header header { boost::mysql::detail::int3{body_size}, seqnum };
|
||||
body.resize(body_size + 4);
|
||||
std::memmove(body.data() + 4, body.data(), body_size);
|
||||
boost::mysql::detail::serialization_context ctx (boost::mysql::detail::capabilities(), body.data());
|
||||
boost::mysql::detail::serialize(ctx, header);
|
||||
return body;
|
||||
}
|
||||
|
||||
// Machinery to be able to cover the sync and async
|
||||
// functions with the same test code
|
||||
class reader_fns
|
||||
@@ -154,7 +144,7 @@ BOOST_AUTO_TEST_CASE(message_fits_in_buffer)
|
||||
message_reader reader (512);
|
||||
std::uint8_t seqnum = 2;
|
||||
std::vector<std::uint8_t> msg_body {0x01, 0x02, 0x03};
|
||||
test_stream stream (create_frame(seqnum, msg_body));
|
||||
test_stream stream (create_message(seqnum, msg_body));
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
|
||||
// Doesn't have a message initially
|
||||
@@ -188,7 +178,7 @@ BOOST_AUTO_TEST_CASE(fragmented_message_fits_in_buffer)
|
||||
std::uint8_t seqnum = 2;
|
||||
std::vector<std::uint8_t> msg_body {0x01, 0x02, 0x03};
|
||||
test_stream stream (test_stream::read_behavior(
|
||||
create_frame(seqnum, msg_body),
|
||||
create_message(seqnum, msg_body),
|
||||
{ 3, 5 } // break the message at bytes 3 and 5
|
||||
));
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
@@ -223,7 +213,7 @@ BOOST_AUTO_TEST_CASE(message_doesnt_fit_in_buffer)
|
||||
message_reader reader (0);
|
||||
std::uint8_t seqnum = 2;
|
||||
std::vector<std::uint8_t> msg_body {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
|
||||
test_stream stream (create_frame(seqnum, msg_body));
|
||||
test_stream stream (create_message(seqnum, msg_body));
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
|
||||
// Doesn't have a message initially
|
||||
@@ -259,12 +249,7 @@ BOOST_AUTO_TEST_CASE(two_messages)
|
||||
std::uint8_t seqnum2 = 5;
|
||||
std::vector<std::uint8_t> msg1_body {0x01, 0x02, 0x03};
|
||||
std::vector<std::uint8_t> msg2_body {0x05, 0x06, 0x07, 0x08};
|
||||
test_stream stream (
|
||||
concat_copy(
|
||||
create_frame(seqnum1, msg1_body),
|
||||
create_frame(seqnum2, msg2_body)
|
||||
)
|
||||
);
|
||||
test_stream stream (create_message(seqnum1, msg1_body, seqnum2, msg2_body));
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
|
||||
// Doesn't have a message initially
|
||||
@@ -310,8 +295,8 @@ BOOST_AUTO_TEST_CASE(previous_message_keep_messages_false)
|
||||
std::vector<std::uint8_t> msg1_body {0x01, 0x02, 0x03};
|
||||
std::vector<std::uint8_t> msg2_body {0x05, 0x06, 0x07};
|
||||
test_stream stream;
|
||||
stream.add_message(create_frame(seqnum1, msg1_body));
|
||||
stream.add_message(create_frame(seqnum2, msg2_body));
|
||||
stream.add_message(create_message(seqnum1, msg1_body));
|
||||
stream.add_message(create_message(seqnum2, msg2_body));
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
|
||||
// Read and get 1st message
|
||||
@@ -350,8 +335,8 @@ BOOST_AUTO_TEST_CASE(previous_message_keep_messages_true)
|
||||
std::vector<std::uint8_t> msg1_body {0x01, 0x02, 0x03};
|
||||
std::vector<std::uint8_t> msg2_body {0x05, 0x06, 0x07};
|
||||
test_stream stream;
|
||||
stream.add_message(create_frame(seqnum1, msg1_body));
|
||||
stream.add_message(create_frame(seqnum2, msg2_body));
|
||||
stream.add_message(create_message(seqnum1, msg1_body));
|
||||
stream.add_message(create_message(seqnum2, msg2_body));
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
|
||||
// Read and get 1st message
|
||||
@@ -406,9 +391,9 @@ BOOST_AUTO_TEST_CASE(multiframe_message)
|
||||
message_reader reader (512, 8);
|
||||
std::uint8_t seqnum = 2;
|
||||
test_stream stream (
|
||||
concat_copy(
|
||||
create_frame(seqnum, { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }),
|
||||
create_frame(3, { 0x09, 0x0a })
|
||||
create_message(
|
||||
seqnum, { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 },
|
||||
3, { 0x09, 0x0a }
|
||||
)
|
||||
);
|
||||
std::vector<std::uint8_t> expected_msg { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a };
|
||||
@@ -435,7 +420,7 @@ BOOST_AUTO_TEST_CASE(seqnum_overflow)
|
||||
message_reader reader (512);
|
||||
std::uint8_t seqnum = 0xff;
|
||||
std::vector<std::uint8_t> msg_body { 0x01, 0x02, 0x03 };
|
||||
test_stream stream (create_frame(seqnum, msg_body));
|
||||
test_stream stream (create_message(seqnum, msg_body));
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
|
||||
// Read succesfully
|
||||
@@ -454,7 +439,7 @@ BOOST_AUTO_TEST_CASE(seqnum_overflow)
|
||||
BOOST_AUTO_TEST_CASE(error_passed_seqnum_mismatch)
|
||||
{
|
||||
message_reader reader (512);
|
||||
test_stream stream (create_frame(2, {0x01, 0x02, 0x03}));
|
||||
test_stream stream (create_message(2, {0x01, 0x02, 0x03}));
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
|
||||
// Read succesfully
|
||||
@@ -476,9 +461,9 @@ BOOST_AUTO_TEST_CASE(error_intermediate_frame_seqnum_mismatch)
|
||||
std::vector<std::uint8_t> msg_body {0x01, 0x02, 0x03};
|
||||
std::uint8_t seqnum = 2;
|
||||
test_stream stream (
|
||||
concat_copy(
|
||||
create_frame(seqnum, { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 }),
|
||||
create_frame(4, { 0x11, 0x12, 0x13, 0x14 }) // the right seqnum would be 3
|
||||
create_message(
|
||||
seqnum, { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 },
|
||||
4, { 0x11, 0x12, 0x13, 0x14 } // the right seqnum would be 3
|
||||
)
|
||||
);
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
@@ -508,7 +493,7 @@ BOOST_AUTO_TEST_CASE(success)
|
||||
message_reader reader (512);
|
||||
std::uint8_t seqnum = 2;
|
||||
std::vector<std::uint8_t> msg_body {0x01, 0x02, 0x03};
|
||||
test_stream stream (create_frame(seqnum, msg_body));
|
||||
test_stream stream (create_message(seqnum, msg_body));
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
|
||||
// Read succesfully
|
||||
@@ -534,12 +519,7 @@ BOOST_AUTO_TEST_CASE(cached_message)
|
||||
std::uint8_t seqnum2 = 8;
|
||||
std::vector<std::uint8_t> msg1_body {0x01, 0x02, 0x03};
|
||||
std::vector<std::uint8_t> msg2_body {0x04, 0x05};
|
||||
test_stream stream (
|
||||
concat_copy(
|
||||
create_frame(seqnum1, msg1_body),
|
||||
create_frame(seqnum2, msg2_body)
|
||||
)
|
||||
);
|
||||
test_stream stream (create_message(seqnum1, msg1_body, seqnum2, msg2_body));
|
||||
error_code err = boost::mysql::make_error_code(errc::no);
|
||||
|
||||
// Read succesfully
|
||||
@@ -569,7 +549,7 @@ BOOST_AUTO_TEST_CASE(error_in_read)
|
||||
message_reader reader (512);
|
||||
std::uint8_t seqnum = 2;
|
||||
std::vector<std::uint8_t> msg_body {0x01, 0x02, 0x03};
|
||||
test_stream stream (create_frame(seqnum, msg_body), fail_count(0, error_code(errc::base64_decode_error)));
|
||||
test_stream stream (create_message(seqnum, msg_body), fail_count(0, error_code(errc::base64_decode_error)));
|
||||
error_code err (errc::no);
|
||||
|
||||
fns->read_one(reader, stream, seqnum, err);
|
||||
@@ -586,7 +566,7 @@ BOOST_AUTO_TEST_CASE(seqnum_mismatch)
|
||||
BOOST_TEST_CONTEXT(fns->name())
|
||||
{
|
||||
message_reader reader (512);
|
||||
test_stream stream (create_frame(2, {0x01, 0x02, 0x03}));
|
||||
test_stream stream (create_message(2, {0x01, 0x02, 0x03}));
|
||||
error_code err (errc::no);
|
||||
|
||||
std::uint8_t bad_seqnum = 42;
|
||||
|
||||
Reference in New Issue
Block a user