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