Branch data Line data Source code
1 : : // Copyright (c) 2012-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <hash.h>
6 : : #include <serialize.h>
7 : : #include <streams.h>
8 : : #include <test/util/common.h>
9 : : #include <test/util/setup_common.h>
10 : : #include <util/strencodings.h>
11 : :
12 : : #include <cstdint>
13 : : #include <string>
14 : :
15 : : #include <boost/test/unit_test.hpp>
16 : :
17 : : BOOST_FIXTURE_TEST_SUITE(serialize_tests, BasicTestingSetup)
18 : :
19 : : // For testing move-semantics, declare a version of datastream that can be moved
20 : : // but is not copyable.
21 : 3 : class UncopyableStream : public DataStream
22 : : {
23 : : public:
24 : 1 : using DataStream::DataStream;
25 : : UncopyableStream(const UncopyableStream&) = delete;
26 : : UncopyableStream& operator=(const UncopyableStream&) = delete;
27 : 2 : UncopyableStream(UncopyableStream&&) noexcept = default;
28 : : UncopyableStream& operator=(UncopyableStream&&) noexcept = default;
29 : : };
30 : :
31 : : class CSerializeMethodsTestSingle
32 : : {
33 : : protected:
34 : : int intval;
35 : : bool boolval;
36 : : std::string stringval;
37 : : char charstrval[16];
38 : : CTransactionRef txval;
39 : : public:
40 : 1 : CSerializeMethodsTestSingle() = default;
41 [ + - ]: 2 : CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const uint8_t* charstrvalin, const CTransactionRef& txvalin) : intval(intvalin), boolval(boolvalin), stringval(std::move(stringvalin)), txval(txvalin)
42 : : {
43 : 2 : memcpy(charstrval, charstrvalin, sizeof(charstrval));
44 : 2 : }
45 : :
46 : 6 : SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj)
47 : : {
48 : 3 : READWRITE(obj.intval);
49 : 3 : READWRITE(obj.boolval);
50 : 3 : READWRITE(obj.stringval);
51 : 3 : READWRITE(obj.charstrval);
52 : 3 : READWRITE(TX_WITH_WITNESS(obj.txval));
53 : 3 : }
54 : :
55 : 5 : bool operator==(const CSerializeMethodsTestSingle& rhs) const
56 : : {
57 : 10 : return intval == rhs.intval &&
58 [ + - ]: 5 : boolval == rhs.boolval &&
59 [ + - ]: 5 : stringval == rhs.stringval &&
60 [ + - + - : 10 : strcmp(charstrval, rhs.charstrval) == 0 &&
- + ]
61 [ - + ]: 5 : *txval == *rhs.txval;
62 : : }
63 : : };
64 : :
65 : 0 : class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle
66 : : {
67 : : public:
68 : 1 : using CSerializeMethodsTestSingle::CSerializeMethodsTestSingle;
69 : :
70 : 4 : SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
71 : : {
72 : 2 : READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, TX_WITH_WITNESS(obj.txval));
73 : 2 : }
74 : : };
75 : :
76 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(sizes)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
77 : : {
78 [ + - ]: 1 : BOOST_CHECK_EQUAL(sizeof(unsigned char), GetSerializeSize((unsigned char)0));
79 [ + - ]: 1 : BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0)));
80 [ + - ]: 1 : BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0)));
81 [ + - ]: 1 : BOOST_CHECK_EQUAL(sizeof(int16_t), GetSerializeSize(int16_t(0)));
82 [ + - ]: 1 : BOOST_CHECK_EQUAL(sizeof(uint16_t), GetSerializeSize(uint16_t(0)));
83 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(sizeof(int32_t), GetSerializeSize(int32_t(0)));
84 [ + - ]: 1 : BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0)));
85 [ + - ]: 1 : BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0)));
86 [ + - ]: 1 : BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0)));
87 : : // Bool is serialized as uint8_t
88 [ + - ]: 1 : BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(bool(0)));
89 : :
90 : : // Sanity-check GetSerializeSize and c++ type matching
91 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize((unsigned char)0), 1U);
92 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(int8_t(0)), 1U);
93 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(uint8_t(0)), 1U);
94 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(int16_t(0)), 2U);
95 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(uint16_t(0)), 2U);
96 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(int32_t(0)), 4U);
97 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0)), 4U);
98 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0)), 8U);
99 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0)), 8U);
100 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(bool(0)), 1U);
101 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 1>{0}), 1U);
102 [ + - ]: 1 : BOOST_CHECK_EQUAL(GetSerializeSize(std::array<uint8_t, 2>{0, 0}), 2U);
103 : 1 : }
104 : :
105 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(varints)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
106 : : {
107 : : // encode
108 : :
109 : 1 : DataStream ss{};
110 : 1 : DataStream::size_type size = 0;
111 [ + + ]: 100001 : for (int i = 0; i < 100000; i++) {
112 [ + - ]: 100000 : ss << VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED);
113 : 100000 : size += ::GetSerializeSize(VARINT_MODE(i, VarIntMode::NONNEGATIVE_SIGNED));
114 [ + - - + : 200000 : BOOST_CHECK(size == ss.size());
+ - ]
115 : : }
116 : :
117 [ + + ]: 102 : for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
118 [ + - ]: 101 : ss << VARINT(i);
119 : 101 : size += ::GetSerializeSize(VARINT(i));
120 [ + - - + : 202 : BOOST_CHECK(size == ss.size());
+ - ]
121 : : }
122 : :
123 : : // decode
124 [ + + ]: 100001 : for (int i = 0; i < 100000; i++) {
125 : 100000 : int j = -1;
126 [ + - ]: 100000 : ss >> VARINT_MODE(j, VarIntMode::NONNEGATIVE_SIGNED);
127 [ + - + - ]: 200000 : BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
128 : : }
129 : :
130 [ + + ]: 102 : for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
131 : 101 : uint64_t j = std::numeric_limits<uint64_t>::max();
132 [ + - ]: 101 : ss >> VARINT(j);
133 [ + - + - ]: 202 : BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
134 : : }
135 : 1 : }
136 : :
137 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(varints_bitpatterns)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
138 : : {
139 : 1 : DataStream ss{};
140 [ + - + - : 2 : ss << VARINT_MODE(0, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "00"); ss.clear();
- + + - +
- + - ]
141 [ + - + - : 2 : ss << VARINT_MODE(0x7f, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
- + + - +
- + - ]
142 [ + - + - : 2 : ss << VARINT_MODE(int8_t{0x7f}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
- + + - +
- + - ]
143 [ + - + - : 2 : ss << VARINT_MODE(0x80, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
- + + - +
- + - ]
144 [ + - + - : 2 : ss << VARINT(uint8_t{0x80}); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
- + + - +
- + - ]
145 [ + - + - : 2 : ss << VARINT_MODE(0x1234, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
- + + - +
- + - ]
146 [ + - + - : 2 : ss << VARINT_MODE(int16_t{0x1234}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
- + + - +
- + - ]
147 [ + - + - : 2 : ss << VARINT_MODE(0xffff, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
- + + - +
- + - ]
148 [ + - + - : 2 : ss << VARINT(uint16_t{0xffff}); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
- + + - +
- + - ]
149 [ + - + - : 2 : ss << VARINT_MODE(0x123456, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
- + + - +
- + - ]
150 [ + - + - : 2 : ss << VARINT_MODE(int32_t{0x123456}, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
- + + - +
- + - ]
151 [ + - + - : 2 : ss << VARINT(0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
- + + - +
- + - ]
152 [ + - + - : 2 : ss << VARINT(uint32_t{0x80123456U}); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
- + + - +
- + - ]
153 [ + - + - : 2 : ss << VARINT(0xffffffff); BOOST_CHECK_EQUAL(HexStr(ss), "8efefefe7f"); ss.clear();
- + + - +
- + - ]
154 [ + - + - : 2 : ss << VARINT_MODE(0x7fffffffffffffffLL, VarIntMode::NONNEGATIVE_SIGNED); BOOST_CHECK_EQUAL(HexStr(ss), "fefefefefefefefe7f"); ss.clear();
- + + - +
- + - ]
155 [ + - + - : 3 : ss << VARINT(0xffffffffffffffffULL); BOOST_CHECK_EQUAL(HexStr(ss), "80fefefefefefefefe7f"); ss.clear();
- + + - +
- + - ]
156 : 1 : }
157 : :
158 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(compactsize)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
159 : : {
160 : 1 : DataStream ss{};
161 : 1 : std::vector<char>::size_type i, j;
162 : :
163 [ + + ]: 27 : for (i = 1; i <= MAX_SIZE; i *= 2)
164 : : {
165 [ + - ]: 26 : WriteCompactSize(ss, i-1);
166 [ + - ]: 26 : WriteCompactSize(ss, i);
167 : : }
168 [ + + ]: 27 : for (i = 1; i <= MAX_SIZE; i *= 2)
169 : : {
170 [ + - ]: 26 : j = ReadCompactSize(ss);
171 [ + - + - : 52 : BOOST_CHECK_MESSAGE((i-1) == j, "decoded:" << j << " expected:" << (i-1));
+ - ]
172 [ + - ]: 26 : j = ReadCompactSize(ss);
173 [ + - + - ]: 52 : BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
174 : : }
175 : 1 : }
176 : :
177 : 6 : static bool isCanonicalException(const std::ios_base::failure& ex)
178 : : {
179 : 6 : std::ios_base::failure expectedException("non-canonical ReadCompactSize()");
180 : :
181 : : // The string returned by what() can be different for different platforms.
182 : : // Instead of directly comparing the ex.what() with an expected string,
183 : : // create an instance of exception to see if ex.what() matches
184 : : // the expected explanatory string returned by the exception instance.
185 : 6 : return strcmp(expectedException.what(), ex.what()) == 0;
186 : 6 : }
187 : :
188 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(vector_bool)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
189 : : {
190 : 1 : std::vector<uint8_t> vec1{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};
191 [ + - ]: 1 : std::vector<bool> vec2{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};
192 : :
193 [ + - + - : 2 : BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
+ - + - ]
194 [ + - + - : 2 : BOOST_CHECK((HashWriter{} << vec1).GetHash() == (HashWriter{} << vec2).GetHash());
+ - + - +
- + - + -
+ - ]
195 : 1 : }
196 : :
197 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(array)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
198 : : {
199 : 1 : std::array<uint8_t, 32> array1{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};
200 : 1 : DataStream ds;
201 [ + - ]: 1 : ds << array1;
202 : 1 : std::array<uint8_t, 32> array2;
203 [ + - ]: 1 : ds >> array2;
204 [ + - + - ]: 2 : BOOST_CHECK(array1 == array2);
205 : 1 : }
206 : :
207 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(noncanonical)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
208 : : {
209 : : // Write some non-canonical CompactSize encodings, and
210 : : // make sure an exception is thrown when read back.
211 : 1 : DataStream ss{};
212 : 1 : std::vector<char>::size_type n;
213 : :
214 : : // zero encoded with three bytes:
215 [ + - ]: 1 : ss << std::span{"\xfd\x00\x00"}.first(3);
216 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
- - - - -
+ + - + -
+ - ]
217 : :
218 : : // 0xfc encoded with three bytes:
219 [ + - ]: 1 : ss << std::span{"\xfd\xfc\x00"}.first(3);
220 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
- - - - -
+ + - + -
+ - ]
221 : :
222 : : // 0xfd encoded with three bytes is OK:
223 [ + - ]: 1 : ss << std::span{"\xfd\xfd\x00"}.first(3);
224 [ + - ]: 1 : n = ReadCompactSize(ss);
225 [ + - + - : 2 : BOOST_CHECK(n == 0xfd);
+ - ]
226 : :
227 : : // zero encoded with five bytes:
228 [ + - ]: 1 : ss << std::span{"\xfe\x00\x00\x00\x00"}.first(5);
229 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
- - - - -
+ + - + -
+ - ]
230 : :
231 : : // 0xffff encoded with five bytes:
232 [ + - ]: 1 : ss << std::span{"\xfe\xff\xff\x00\x00"}.first(5);
233 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
- - - - -
+ + - + -
+ - ]
234 : :
235 : : // zero encoded with nine bytes:
236 [ + - ]: 1 : ss << std::span{"\xff\x00\x00\x00\x00\x00\x00\x00\x00"}.first(9);
237 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
- - - - -
+ + - + -
+ - ]
238 : :
239 : : // 0x01ffffff encoded with nine bytes:
240 [ + - ]: 1 : ss << std::span{"\xff\xff\xff\xff\x01\x00\x00\x00\x00"}.first(9);
241 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
- - - - -
+ + - + -
+ - ]
242 : 1 : }
243 : :
244 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(class_methods)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
245 : : {
246 : 1 : int intval(100);
247 : 1 : bool boolval(true);
248 : 1 : std::string stringval("testing");
249 : 1 : const uint8_t charstrval[16]{"testing charstr"};
250 [ + - ]: 1 : CMutableTransaction txval;
251 [ + - ]: 1 : CTransactionRef tx_ref{MakeTransactionRef(txval)};
252 [ - + ]: 2 : CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, tx_ref);
253 [ - + ]: 2 : CSerializeMethodsTestMany methodtest2(intval, boolval, stringval, charstrval, tx_ref);
254 [ + - ]: 1 : CSerializeMethodsTestSingle methodtest3;
255 : 1 : CSerializeMethodsTestMany methodtest4;
256 : 1 : DataStream ss;
257 [ + - + - : 2 : BOOST_CHECK(methodtest1 == methodtest2);
+ - ]
258 [ + - ]: 1 : ss << methodtest1;
259 [ + - ]: 1 : ss >> methodtest4;
260 [ + - ]: 1 : ss << methodtest2;
261 [ + - ]: 1 : ss >> methodtest3;
262 [ + - + - : 2 : BOOST_CHECK(methodtest1 == methodtest2);
+ - ]
263 [ + - + - : 2 : BOOST_CHECK(methodtest2 == methodtest3);
+ - ]
264 [ + - + - : 2 : BOOST_CHECK(methodtest3 == methodtest4);
+ - ]
265 : :
266 : 1 : DataStream ss2;
267 [ + - + - : 1 : ss2 << intval << boolval << stringval << charstrval << TX_WITH_WITNESS(txval);
+ - + - +
- ]
268 [ + - ]: 1 : ss2 >> methodtest3;
269 [ + - + - : 2 : BOOST_CHECK(methodtest3 == methodtest4);
+ - ]
270 : 1 : {
271 : 1 : DataStream ds;
272 [ + - ]: 1 : const std::string in{"ab"};
273 [ - + + - : 1 : ds << std::span{in} << std::byte{'c'};
+ - ]
274 : 1 : std::array<std::byte, 2> out;
275 : 1 : std::byte out_3;
276 [ + - + - ]: 1 : ds >> std::span{out} >> out_3;
277 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(out.at(0), std::byte{'a'});
278 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(out.at(1), std::byte{'b'});
279 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(out_3, std::byte{'c'});
280 : 1 : }
281 [ + - ]: 3 : }
282 : :
283 : : struct BaseFormat {
284 : : const enum {
285 : : RAW,
286 : : HEX,
287 : : } m_base_format;
288 : 10 : SER_PARAMS_OPFUNC
289 : : };
290 : : constexpr BaseFormat RAW{BaseFormat::RAW};
291 : : constexpr BaseFormat HEX{BaseFormat::HEX};
292 : :
293 : : /// (Un)serialize a number as raw byte or 2 hexadecimal chars.
294 : : class Base
295 : : {
296 : : public:
297 : : uint8_t m_base_data;
298 : :
299 : 4 : Base() : m_base_data(17) {}
300 : 4 : explicit Base(uint8_t data) : m_base_data(data) {}
301 : :
302 : : template <typename Stream>
303 [ + + ]: 10 : void Serialize(Stream& s) const
304 : : {
305 [ + + ]: 10 : if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
306 : 6 : s << m_base_data;
307 : : } else {
308 [ - + + - ]: 4 : s << std::span<const char>{HexStr(std::span{&m_base_data, 1})};
309 : : }
310 : 10 : }
311 : :
312 : : template <typename Stream>
313 [ + + ]: 8 : void Unserialize(Stream& s)
314 : : {
315 [ + + ]: 8 : if (s.template GetParams<BaseFormat>().m_base_format == BaseFormat::RAW) {
316 : 5 : s >> m_base_data;
317 : : } else {
318 : 3 : std::string hex{"aa"};
319 [ + - - + ]: 3 : s >> std::span{hex}.first(hex.size());
320 [ + - + - ]: 6 : m_base_data = TryParseHex<uint8_t>(hex).value().at(0);
321 : 3 : }
322 : 8 : }
323 : : };
324 : :
325 : : class DerivedAndBaseFormat
326 : : {
327 : : public:
328 : : BaseFormat m_base_format;
329 : :
330 : : enum class DerivedFormat {
331 : : LOWER,
332 : : UPPER,
333 : : } m_derived_format;
334 : :
335 : 2 : SER_PARAMS_OPFUNC
336 : : };
337 : :
338 : 0 : class Derived : public Base
339 : : {
340 : : public:
341 : : std::string m_derived_data;
342 : :
343 : 4 : SERIALIZE_METHODS(Derived, obj)
344 : : {
345 : 2 : auto& fmt = SER_PARAMS(DerivedAndBaseFormat);
346 : 2 : READWRITE(fmt.m_base_format(AsBase<Base>(obj)));
347 : :
348 : : if (ser_action.ForRead()) {
349 : : std::string str;
350 : : s >> str;
351 : : SER_READ(obj, obj.m_derived_data = str);
352 : : } else {
353 [ + + - + : 4 : s << (fmt.m_derived_format == DerivedAndBaseFormat::DerivedFormat::LOWER ?
- + ]
354 : : ToLower(obj.m_derived_data) :
355 : : ToUpper(obj.m_derived_data));
356 : : }
357 : 2 : }
358 : : };
359 : :
360 : : struct OtherParam {
361 : : uint8_t param;
362 : 2 : SER_PARAMS_OPFUNC
363 : : };
364 : :
365 : : //! Checker for value of OtherParam. When being serialized, serializes the
366 : : //! param to the stream. When being unserialized, verifies the value in the
367 : : //! stream matches the param.
368 : : class OtherParamChecker
369 : : {
370 : : public:
371 : : template <typename Stream>
372 : 2 : void Serialize(Stream& s) const
373 : : {
374 : 2 : const uint8_t param = s.template GetParams<OtherParam>().param;
375 : 2 : s << param;
376 : : }
377 : :
378 : : template <typename Stream>
379 : 2 : void Unserialize(Stream& s) const
380 : : {
381 : 2 : const uint8_t param = s.template GetParams<OtherParam>().param;
382 : : uint8_t value;
383 : 2 : s >> value;
384 [ + - ]: 2 : BOOST_CHECK_EQUAL(value, param);
385 : 2 : }
386 : : };
387 : :
388 : : //! Test creating a stream with multiple parameters and making sure
389 : : //! serialization code requiring different parameters can retrieve them. Also
390 : : //! test that earlier parameters take precedence if the same parameter type is
391 : : //! specified twice. (Choice of whether earlier or later values take precedence
392 : : //! or multiple values of the same type are allowed was arbitrary, and just
393 : : //! decided based on what would require smallest amount of ugly C++ template
394 : : //! code. Intent of the test is to just ensure there is no unexpected behavior.)
395 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(with_params_multi)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
396 : : {
397 : 1 : const OtherParam other_param_used{.param = 0x10};
398 : 1 : const OtherParam other_param_ignored{.param = 0x11};
399 : 1 : const OtherParam other_param_override{.param = 0x12};
400 : 1 : const OtherParamChecker check;
401 : 1 : DataStream stream;
402 [ + - ]: 1 : ParamsStream pstream{stream, RAW, other_param_used, other_param_ignored};
403 : :
404 : 1 : Base base1{0x20};
405 [ + - + - : 1 : pstream << base1 << check << other_param_override(check);
+ - ]
406 [ + - + - : 1 : BOOST_CHECK_EQUAL(stream.str(), "\x20\x10\x12");
+ - ]
407 : :
408 : 1 : Base base2;
409 [ + - + - : 1 : pstream >> base2 >> check >> other_param_override(check);
+ - ]
410 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(base2.m_base_data, 0x20);
411 : 1 : }
412 : :
413 : : //! Test creating a ParamsStream that moves from a stream argument.
414 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(with_params_move)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
415 : : {
416 : 1 : UncopyableStream stream{MakeByteSpan(std::string_view{"abc"})};
417 : 1 : ParamsStream pstream{std::move(stream), RAW, HEX, RAW};
418 [ + - + - : 1 : BOOST_CHECK_EQUAL(pstream.GetStream().str(), "abc");
+ - ]
419 [ + - ]: 1 : pstream.GetStream().clear();
420 : :
421 : 1 : Base base1{0x20};
422 [ + - ]: 1 : pstream << base1;
423 [ + - + - : 1 : BOOST_CHECK_EQUAL(pstream.GetStream().str(), "\x20");
+ - ]
424 : :
425 : 1 : Base base2;
426 [ + - ]: 1 : pstream >> base2;
427 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(base2.m_base_data, 0x20);
428 : 1 : }
429 : :
430 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(with_params_base)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
431 : : {
432 : 1 : Base b{0x0F};
433 : :
434 : 1 : DataStream stream;
435 : :
436 [ + - ]: 1 : stream << RAW(b);
437 [ + - + - : 1 : BOOST_CHECK_EQUAL(stream.str(), "\x0F");
+ - ]
438 : :
439 : 1 : b.m_base_data = 0;
440 [ + - ]: 1 : stream >> RAW(b);
441 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(b.m_base_data, 0x0F);
442 : :
443 [ - + ]: 1 : stream.clear();
444 : :
445 [ + - ]: 1 : stream << HEX(b);
446 [ + - + - : 1 : BOOST_CHECK_EQUAL(stream.str(), "0f");
+ - ]
447 : :
448 : 1 : b.m_base_data = 0;
449 [ + - ]: 1 : stream >> HEX(b);
450 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(b.m_base_data, 0x0F);
451 : 1 : }
452 : :
453 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(with_params_vector_of_base)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
454 : : {
455 : 1 : std::vector<Base> v{Base{0x0F}, Base{0xFF}};
456 : :
457 : 1 : DataStream stream;
458 : :
459 [ + - ]: 1 : stream << RAW(v);
460 [ + - + - : 1 : BOOST_CHECK_EQUAL(stream.str(), "\x02\x0F\xFF");
+ - ]
461 : :
462 [ + - ]: 1 : v[0].m_base_data = 0;
463 : 1 : v[1].m_base_data = 0;
464 [ + - ]: 1 : stream >> RAW(v);
465 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v[0].m_base_data, 0x0F);
466 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v[1].m_base_data, 0xFF);
467 : :
468 [ - + ]: 1 : stream.clear();
469 : :
470 [ + - ]: 1 : stream << HEX(v);
471 [ + - + - : 1 : BOOST_CHECK_EQUAL(stream.str(), "\x02"
+ - ]
472 : : "0fff");
473 : :
474 [ + - ]: 1 : v[0].m_base_data = 0;
475 : 1 : v[1].m_base_data = 0;
476 [ + - ]: 1 : stream >> HEX(v);
477 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v[0].m_base_data, 0x0F);
478 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(v[1].m_base_data, 0xFF);
479 : 1 : }
480 : :
481 : : constexpr DerivedAndBaseFormat RAW_LOWER{{BaseFormat::RAW}, DerivedAndBaseFormat::DerivedFormat::LOWER};
482 : : constexpr DerivedAndBaseFormat HEX_UPPER{{BaseFormat::HEX}, DerivedAndBaseFormat::DerivedFormat::UPPER};
483 : :
484 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(with_params_derived)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
485 : : {
486 [ + - ]: 1 : Derived d;
487 : 1 : d.m_base_data = 0x0F;
488 [ + - ]: 1 : d.m_derived_data = "xY";
489 : :
490 : 1 : DataStream stream;
491 : :
492 [ + - ]: 1 : stream << RAW_LOWER(d);
493 : :
494 [ + - ]: 1 : stream << HEX_UPPER(d);
495 : :
496 [ + - + - : 1 : BOOST_CHECK_EQUAL(stream.str(), "\x0F\x02xy"
+ - ]
497 : : "0f\x02XY");
498 : 1 : }
499 : :
500 : : BOOST_AUTO_TEST_SUITE_END()
|