Merge #10953: [Refactor] Combine scriptPubKey and amount as CTxOut in CScriptCheck
[bitcoinplatinum.git] / src / test / serialize_tests.cpp
blob9661a665140499148dc2cf8e803f2aea160a4845
1 // Copyright (c) 2012-2016 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.
5 #include "serialize.h"
6 #include "streams.h"
7 #include "hash.h"
8 #include "test/test_bitcoin.h"
10 #include <stdint.h>
12 #include <boost/test/unit_test.hpp>
14 BOOST_FIXTURE_TEST_SUITE(serialize_tests, BasicTestingSetup)
16 class CSerializeMethodsTestSingle
18 protected:
19 int intval;
20 bool boolval;
21 std::string stringval;
22 const char* charstrval;
23 CTransactionRef txval;
24 public:
25 CSerializeMethodsTestSingle() = default;
26 CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const char* charstrvalin, CTransaction txvalin) : intval(intvalin), boolval(boolvalin), stringval(std::move(stringvalin)), charstrval(charstrvalin), txval(MakeTransactionRef(txvalin)){}
27 ADD_SERIALIZE_METHODS;
29 template <typename Stream, typename Operation>
30 inline void SerializationOp(Stream& s, Operation ser_action) {
31 READWRITE(intval);
32 READWRITE(boolval);
33 READWRITE(stringval);
34 READWRITE(FLATDATA(charstrval));
35 READWRITE(txval);
38 bool operator==(const CSerializeMethodsTestSingle& rhs)
40 return intval == rhs.intval && \
41 boolval == rhs.boolval && \
42 stringval == rhs.stringval && \
43 strcmp(charstrval, rhs.charstrval) == 0 && \
44 *txval == *rhs.txval;
48 class CSerializeMethodsTestMany : public CSerializeMethodsTestSingle
50 public:
51 using CSerializeMethodsTestSingle::CSerializeMethodsTestSingle;
52 ADD_SERIALIZE_METHODS;
54 template <typename Stream, typename Operation>
55 inline void SerializationOp(Stream& s, Operation ser_action) {
56 READWRITEMANY(intval, boolval, stringval, FLATDATA(charstrval), txval);
60 BOOST_AUTO_TEST_CASE(sizes)
62 BOOST_CHECK_EQUAL(sizeof(char), GetSerializeSize(char(0), 0));
63 BOOST_CHECK_EQUAL(sizeof(int8_t), GetSerializeSize(int8_t(0), 0));
64 BOOST_CHECK_EQUAL(sizeof(uint8_t), GetSerializeSize(uint8_t(0), 0));
65 BOOST_CHECK_EQUAL(sizeof(int16_t), GetSerializeSize(int16_t(0), 0));
66 BOOST_CHECK_EQUAL(sizeof(uint16_t), GetSerializeSize(uint16_t(0), 0));
67 BOOST_CHECK_EQUAL(sizeof(int32_t), GetSerializeSize(int32_t(0), 0));
68 BOOST_CHECK_EQUAL(sizeof(uint32_t), GetSerializeSize(uint32_t(0), 0));
69 BOOST_CHECK_EQUAL(sizeof(int64_t), GetSerializeSize(int64_t(0), 0));
70 BOOST_CHECK_EQUAL(sizeof(uint64_t), GetSerializeSize(uint64_t(0), 0));
71 BOOST_CHECK_EQUAL(sizeof(float), GetSerializeSize(float(0), 0));
72 BOOST_CHECK_EQUAL(sizeof(double), GetSerializeSize(double(0), 0));
73 // Bool is serialized as char
74 BOOST_CHECK_EQUAL(sizeof(char), GetSerializeSize(bool(0), 0));
76 // Sanity-check GetSerializeSize and c++ type matching
77 BOOST_CHECK_EQUAL(GetSerializeSize(char(0), 0), 1);
78 BOOST_CHECK_EQUAL(GetSerializeSize(int8_t(0), 0), 1);
79 BOOST_CHECK_EQUAL(GetSerializeSize(uint8_t(0), 0), 1);
80 BOOST_CHECK_EQUAL(GetSerializeSize(int16_t(0), 0), 2);
81 BOOST_CHECK_EQUAL(GetSerializeSize(uint16_t(0), 0), 2);
82 BOOST_CHECK_EQUAL(GetSerializeSize(int32_t(0), 0), 4);
83 BOOST_CHECK_EQUAL(GetSerializeSize(uint32_t(0), 0), 4);
84 BOOST_CHECK_EQUAL(GetSerializeSize(int64_t(0), 0), 8);
85 BOOST_CHECK_EQUAL(GetSerializeSize(uint64_t(0), 0), 8);
86 BOOST_CHECK_EQUAL(GetSerializeSize(float(0), 0), 4);
87 BOOST_CHECK_EQUAL(GetSerializeSize(double(0), 0), 8);
88 BOOST_CHECK_EQUAL(GetSerializeSize(bool(0), 0), 1);
91 BOOST_AUTO_TEST_CASE(floats_conversion)
93 // Choose values that map unambiguously to binary floating point to avoid
94 // rounding issues at the compiler side.
95 BOOST_CHECK_EQUAL(ser_uint32_to_float(0x00000000), 0.0F);
96 BOOST_CHECK_EQUAL(ser_uint32_to_float(0x3f000000), 0.5F);
97 BOOST_CHECK_EQUAL(ser_uint32_to_float(0x3f800000), 1.0F);
98 BOOST_CHECK_EQUAL(ser_uint32_to_float(0x40000000), 2.0F);
99 BOOST_CHECK_EQUAL(ser_uint32_to_float(0x40800000), 4.0F);
100 BOOST_CHECK_EQUAL(ser_uint32_to_float(0x44444444), 785.066650390625F);
102 BOOST_CHECK_EQUAL(ser_float_to_uint32(0.0F), 0x00000000);
103 BOOST_CHECK_EQUAL(ser_float_to_uint32(0.5F), 0x3f000000);
104 BOOST_CHECK_EQUAL(ser_float_to_uint32(1.0F), 0x3f800000);
105 BOOST_CHECK_EQUAL(ser_float_to_uint32(2.0F), 0x40000000);
106 BOOST_CHECK_EQUAL(ser_float_to_uint32(4.0F), 0x40800000);
107 BOOST_CHECK_EQUAL(ser_float_to_uint32(785.066650390625F), 0x44444444);
110 BOOST_AUTO_TEST_CASE(doubles_conversion)
112 // Choose values that map unambiguously to binary floating point to avoid
113 // rounding issues at the compiler side.
114 BOOST_CHECK_EQUAL(ser_uint64_to_double(0x0000000000000000ULL), 0.0);
115 BOOST_CHECK_EQUAL(ser_uint64_to_double(0x3fe0000000000000ULL), 0.5);
116 BOOST_CHECK_EQUAL(ser_uint64_to_double(0x3ff0000000000000ULL), 1.0);
117 BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4000000000000000ULL), 2.0);
118 BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4010000000000000ULL), 4.0);
119 BOOST_CHECK_EQUAL(ser_uint64_to_double(0x4088888880000000ULL), 785.066650390625);
121 BOOST_CHECK_EQUAL(ser_double_to_uint64(0.0), 0x0000000000000000ULL);
122 BOOST_CHECK_EQUAL(ser_double_to_uint64(0.5), 0x3fe0000000000000ULL);
123 BOOST_CHECK_EQUAL(ser_double_to_uint64(1.0), 0x3ff0000000000000ULL);
124 BOOST_CHECK_EQUAL(ser_double_to_uint64(2.0), 0x4000000000000000ULL);
125 BOOST_CHECK_EQUAL(ser_double_to_uint64(4.0), 0x4010000000000000ULL);
126 BOOST_CHECK_EQUAL(ser_double_to_uint64(785.066650390625), 0x4088888880000000ULL);
129 Python code to generate the below hashes:
131 def reversed_hex(x):
132 return binascii.hexlify(''.join(reversed(x)))
133 def dsha256(x):
134 return hashlib.sha256(hashlib.sha256(x).digest()).digest()
136 reversed_hex(dsha256(''.join(struct.pack('<f', x) for x in range(0,1000)))) == '8e8b4cf3e4df8b332057e3e23af42ebc663b61e0495d5e7e32d85099d7f3fe0c'
137 reversed_hex(dsha256(''.join(struct.pack('<d', x) for x in range(0,1000)))) == '43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96'
139 BOOST_AUTO_TEST_CASE(floats)
141 CDataStream ss(SER_DISK, 0);
142 // encode
143 for (int i = 0; i < 1000; i++) {
144 ss << float(i);
146 BOOST_CHECK(Hash(ss.begin(), ss.end()) == uint256S("8e8b4cf3e4df8b332057e3e23af42ebc663b61e0495d5e7e32d85099d7f3fe0c"));
148 // decode
149 for (int i = 0; i < 1000; i++) {
150 float j;
151 ss >> j;
152 BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
156 BOOST_AUTO_TEST_CASE(doubles)
158 CDataStream ss(SER_DISK, 0);
159 // encode
160 for (int i = 0; i < 1000; i++) {
161 ss << double(i);
163 BOOST_CHECK(Hash(ss.begin(), ss.end()) == uint256S("43d0c82591953c4eafe114590d392676a01585d25b25d433557f0d7878b23f96"));
165 // decode
166 for (int i = 0; i < 1000; i++) {
167 double j;
168 ss >> j;
169 BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
173 BOOST_AUTO_TEST_CASE(varints)
175 // encode
177 CDataStream ss(SER_DISK, 0);
178 CDataStream::size_type size = 0;
179 for (int i = 0; i < 100000; i++) {
180 ss << VARINT(i);
181 size += ::GetSerializeSize(VARINT(i), 0, 0);
182 BOOST_CHECK(size == ss.size());
185 for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
186 ss << VARINT(i);
187 size += ::GetSerializeSize(VARINT(i), 0, 0);
188 BOOST_CHECK(size == ss.size());
191 // decode
192 for (int i = 0; i < 100000; i++) {
193 int j = -1;
194 ss >> VARINT(j);
195 BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
198 for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
199 uint64_t j = -1;
200 ss >> VARINT(j);
201 BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
205 BOOST_AUTO_TEST_CASE(varints_bitpatterns)
207 CDataStream ss(SER_DISK, 0);
208 ss << VARINT(0); BOOST_CHECK_EQUAL(HexStr(ss), "00"); ss.clear();
209 ss << VARINT(0x7f); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
210 ss << VARINT((int8_t)0x7f); BOOST_CHECK_EQUAL(HexStr(ss), "7f"); ss.clear();
211 ss << VARINT(0x80); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
212 ss << VARINT((uint8_t)0x80); BOOST_CHECK_EQUAL(HexStr(ss), "8000"); ss.clear();
213 ss << VARINT(0x1234); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
214 ss << VARINT((int16_t)0x1234); BOOST_CHECK_EQUAL(HexStr(ss), "a334"); ss.clear();
215 ss << VARINT(0xffff); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
216 ss << VARINT((uint16_t)0xffff); BOOST_CHECK_EQUAL(HexStr(ss), "82fe7f"); ss.clear();
217 ss << VARINT(0x123456); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
218 ss << VARINT((int32_t)0x123456); BOOST_CHECK_EQUAL(HexStr(ss), "c7e756"); ss.clear();
219 ss << VARINT(0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
220 ss << VARINT((uint32_t)0x80123456U); BOOST_CHECK_EQUAL(HexStr(ss), "86ffc7e756"); ss.clear();
221 ss << VARINT(0xffffffff); BOOST_CHECK_EQUAL(HexStr(ss), "8efefefe7f"); ss.clear();
222 ss << VARINT(0x7fffffffffffffffLL); BOOST_CHECK_EQUAL(HexStr(ss), "fefefefefefefefe7f"); ss.clear();
223 ss << VARINT(0xffffffffffffffffULL); BOOST_CHECK_EQUAL(HexStr(ss), "80fefefefefefefefe7f"); ss.clear();
226 BOOST_AUTO_TEST_CASE(compactsize)
228 CDataStream ss(SER_DISK, 0);
229 std::vector<char>::size_type i, j;
231 for (i = 1; i <= MAX_SIZE; i *= 2)
233 WriteCompactSize(ss, i-1);
234 WriteCompactSize(ss, i);
236 for (i = 1; i <= MAX_SIZE; i *= 2)
238 j = ReadCompactSize(ss);
239 BOOST_CHECK_MESSAGE((i-1) == j, "decoded:" << j << " expected:" << (i-1));
240 j = ReadCompactSize(ss);
241 BOOST_CHECK_MESSAGE(i == j, "decoded:" << j << " expected:" << i);
245 static bool isCanonicalException(const std::ios_base::failure& ex)
247 std::ios_base::failure expectedException("non-canonical ReadCompactSize()");
249 // The string returned by what() can be different for different platforms.
250 // Instead of directly comparing the ex.what() with an expected string,
251 // create an instance of exception to see if ex.what() matches
252 // the expected explanatory string returned by the exception instance.
253 return strcmp(expectedException.what(), ex.what()) == 0;
257 BOOST_AUTO_TEST_CASE(noncanonical)
259 // Write some non-canonical CompactSize encodings, and
260 // make sure an exception is thrown when read back.
261 CDataStream ss(SER_DISK, 0);
262 std::vector<char>::size_type n;
264 // zero encoded with three bytes:
265 ss.write("\xfd\x00\x00", 3);
266 BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
268 // 0xfc encoded with three bytes:
269 ss.write("\xfd\xfc\x00", 3);
270 BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
272 // 0xfd encoded with three bytes is OK:
273 ss.write("\xfd\xfd\x00", 3);
274 n = ReadCompactSize(ss);
275 BOOST_CHECK(n == 0xfd);
277 // zero encoded with five bytes:
278 ss.write("\xfe\x00\x00\x00\x00", 5);
279 BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
281 // 0xffff encoded with five bytes:
282 ss.write("\xfe\xff\xff\x00\x00", 5);
283 BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
285 // zero encoded with nine bytes:
286 ss.write("\xff\x00\x00\x00\x00\x00\x00\x00\x00", 9);
287 BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
289 // 0x01ffffff encoded with nine bytes:
290 ss.write("\xff\xff\xff\xff\x01\x00\x00\x00\x00", 9);
291 BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
294 BOOST_AUTO_TEST_CASE(insert_delete)
296 // Test inserting/deleting bytes.
297 CDataStream ss(SER_DISK, 0);
298 BOOST_CHECK_EQUAL(ss.size(), 0);
300 ss.write("\x00\x01\x02\xff", 4);
301 BOOST_CHECK_EQUAL(ss.size(), 4);
303 char c = (char)11;
305 // Inserting at beginning/end/middle:
306 ss.insert(ss.begin(), c);
307 BOOST_CHECK_EQUAL(ss.size(), 5);
308 BOOST_CHECK_EQUAL(ss[0], c);
309 BOOST_CHECK_EQUAL(ss[1], 0);
311 ss.insert(ss.end(), c);
312 BOOST_CHECK_EQUAL(ss.size(), 6);
313 BOOST_CHECK_EQUAL(ss[4], (char)0xff);
314 BOOST_CHECK_EQUAL(ss[5], c);
316 ss.insert(ss.begin()+2, c);
317 BOOST_CHECK_EQUAL(ss.size(), 7);
318 BOOST_CHECK_EQUAL(ss[2], c);
320 // Delete at beginning/end/middle
321 ss.erase(ss.begin());
322 BOOST_CHECK_EQUAL(ss.size(), 6);
323 BOOST_CHECK_EQUAL(ss[0], 0);
325 ss.erase(ss.begin()+ss.size()-1);
326 BOOST_CHECK_EQUAL(ss.size(), 5);
327 BOOST_CHECK_EQUAL(ss[4], (char)0xff);
329 ss.erase(ss.begin()+1);
330 BOOST_CHECK_EQUAL(ss.size(), 4);
331 BOOST_CHECK_EQUAL(ss[0], 0);
332 BOOST_CHECK_EQUAL(ss[1], 1);
333 BOOST_CHECK_EQUAL(ss[2], 2);
334 BOOST_CHECK_EQUAL(ss[3], (char)0xff);
336 // Make sure GetAndClear does the right thing:
337 CSerializeData d;
338 ss.GetAndClear(d);
339 BOOST_CHECK_EQUAL(ss.size(), 0);
342 BOOST_AUTO_TEST_CASE(class_methods)
344 int intval(100);
345 bool boolval(true);
346 std::string stringval("testing");
347 const char* charstrval("testing charstr");
348 CMutableTransaction txval;
349 CSerializeMethodsTestSingle methodtest1(intval, boolval, stringval, charstrval, txval);
350 CSerializeMethodsTestMany methodtest2(intval, boolval, stringval, charstrval, txval);
351 CSerializeMethodsTestSingle methodtest3;
352 CSerializeMethodsTestMany methodtest4;
353 CDataStream ss(SER_DISK, PROTOCOL_VERSION);
354 BOOST_CHECK(methodtest1 == methodtest2);
355 ss << methodtest1;
356 ss >> methodtest4;
357 ss << methodtest2;
358 ss >> methodtest3;
359 BOOST_CHECK(methodtest1 == methodtest2);
360 BOOST_CHECK(methodtest2 == methodtest3);
361 BOOST_CHECK(methodtest3 == methodtest4);
363 CDataStream ss2(SER_DISK, PROTOCOL_VERSION, intval, boolval, stringval, FLATDATA(charstrval), txval);
364 ss2 >> methodtest3;
365 BOOST_CHECK(methodtest3 == methodtest4);
368 BOOST_AUTO_TEST_SUITE_END()