1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
#define BOOST_TEST_MODULE RawDataReader
#include <boost/mpl/list.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test.hpp>
#include "bitset.h"
#include "helpers.h"
#include "mariadb_repl.h"
#include <cstddef>
#include <cstdint>
#include <mysql.h>
#include <mysql_types.h>
#include <rawDataReader.h>
#include <stdexcept>
#include <streamSupport.h>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>
using namespace MyGrate;
using Bytes = std::vector<uint8_t>;
template<typename T> using BytesTo = std::tuple<Bytes, T>;
BOOST_DATA_TEST_CASE(read_packedinteger,
boost::unit_test::data::make<BytesTo<uint64_t>>({
{{0x00, 0xb2, 0x2, 0}, 0},
{{0x01, 0xb2, 0x2, 0}, 1},
{{0xfa, 0xb2, 0x2, 0}, 0xfa},
{{0xfc, 0xb2, 0x2, 0}, 0x02b2},
{{0xfd, 0xb2, 0x2, 0}, 690},
{{0xfe, 0xb2, 0x2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 690},
}),
bytes, out)
{
RawDataReader rdr {bytes.data(), bytes.size()};
BOOST_CHECK_EQUAL((rdr.readValue<PackedInteger>()), out);
}
BOOST_DATA_TEST_CASE(invalid_packed_ints,
boost::unit_test::data::make<Bytes>({
{0xFF, 0x78, 0x38, 0x1a, 0x0b},
{0xFB, 0x78, 0x38, 0x1a, 0x0b},
}),
bytes)
{
RawDataReader rdr {bytes.data(), bytes.size()};
BOOST_CHECK_THROW(rdr.readValue<PackedInteger>(), std::domain_error);
}
BOOST_DATA_TEST_CASE(read_overflow,
boost::unit_test::data::make<Bytes>({
{0x00, 0xFB, 0x12, 0x00},
}),
bytes)
{
RawDataReader rdr {bytes.data(), bytes.size()};
rdr.discard(1);
BOOST_CHECK_EQUAL(rdr.readValue<uint16_t>(), 0x12FB);
BOOST_CHECK_THROW(rdr.readValue<uint16_t>(), std::range_error);
}
/*
BOOST_DATA_TEST_CASE(read_overflow_string,
boost::unit_test::data::make<Bytes>({
{0x04, 'a', 'b', 'c'},
}),
bytes)
{
RawDataReader rdr {bytes.data(), bytes.size()};
// BOOST_CHECK_THROW(rdr.readValue<VarChar<1>>(), std::range_error);
}
BOOST_DATA_TEST_CASE(read_datetime2,
boost::unit_test::data::make<BytesTo<struct tm>>({
{{0x99, 0x78, 0x38, 0x1a, 0x0b}, make_tm(2006, 2, 28, 1, 40, 11)},
}),
bytes, exp)
{
RawDataReader rdr {bytes.data(), bytes.size()};
(void)exp;
// BOOST_CHECK_EQUAL((rdr.readValue<DateTime2>()), out);
}
BOOST_DATA_TEST_CASE(read_varchars1,
boost::unit_test::data::make<BytesTo<std::string_view>>({
{{0x00}, ""},
{{0x01, 'A'}, "A"},
{{0x03, 'a', 'b', 'c'}, "abc"},
{{0x10, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'},
"0123456789abcdef"},
}),
bytes, exp)
{
RawDataReader rdr {bytes.data(), bytes.size()};
(void)exp;
// BOOST_CHECK_EQUAL((rdr.readValue<VarChar<1>>()), exp);
}
BOOST_DATA_TEST_CASE(read_varchars2,
boost::unit_test::data::make<BytesTo<std::string_view>>({
{{0x00, 0x00}, ""},
{{0x01, 0x00, 'A'}, "A"},
{{0x03, 0x00, 'a', 'b', 'c'}, "abc"},
{{0x10, 0x00, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'},
"0123456789abcdef"},
}),
bytes, exp)
{
RawDataReader rdr {bytes.data(), bytes.size()};
(void)exp;
// BOOST_CHECK_EQUAL((rdr.readValue<VarChar<2>>()), exp);
}
*/
BOOST_DATA_TEST_CASE(read_bytes,
boost::unit_test::data::make<Bytes>({
{},
{0x01},
{0x00, 0x01, 0x02},
{0xFF, 0xFE, 0xFD},
{0xFF, 0xFE, 0xFD, 0x00, 0x01, 0x02},
}),
bytes)
{
RawDataReader rdr {bytes.data(), bytes.size()};
const auto out {rdr.viewValue<MyGrate::MySQL::Blob>(bytes.size())};
BOOST_CHECK_EQUAL_COLLECTIONS(out.begin(), out.end(), bytes.begin(), bytes.end());
}
BOOST_DATA_TEST_CASE(read_bytes_overflow,
boost::unit_test::data::make<Bytes>({
{},
{0x01},
{0x00, 0x01, 0x02},
{0xFF, 0xFE, 0xFD},
{0xFF, 0xFE, 0xFD, 0x00, 0x01, 0x02},
}),
bytes)
{
RawDataReader rdr {bytes.data(), bytes.size()};
BOOST_CHECK_THROW(rdr.viewValue<MyGrate::MySQL::Blob>(bytes.size() + 1), std::range_error);
}
BOOST_DATA_TEST_CASE(read_field_type,
boost::unit_test::data::make<BytesTo<enum_field_types>>({
{{0x00}, MYSQL_TYPE_DECIMAL},
{{0x04}, MYSQL_TYPE_FLOAT},
{{0xFF}, MYSQL_TYPE_GEOMETRY},
}),
bytes, exp)
{
RawDataReader rdr {bytes.data(), bytes.size()};
BOOST_CHECK_EQUAL(rdr.readValue<enum_field_types>(1), exp);
}
BOOST_AUTO_TEST_CASE(rdr_from_MARIADB_STRING)
{
std::string buf {"test"};
MARIADB_STRING str {buf.data(), buf.length()};
RawDataReader rdr {str};
BOOST_CHECK_EQUAL(rdr.viewValue<std::string_view>(buf.length()), buf);
}
using SimpleTypes = boost::mpl::list<std::byte, int8_t, int16_t, int32_t, int64_t, uint8_t, uint16_t, uint32_t,
uint64_t, float, double>;
BOOST_AUTO_TEST_CASE_TEMPLATE(rdr_read_simple, T, SimpleTypes)
{
RawDataReader rdr {"don't care, some bytes", 20};
rdr.readValue<T>();
}
|