Merge #9578: Add missing mempool lock for CalculateMemPoolAncestors
[bitcoinplatinum.git] / src / test / streams_tests.cpp
blob94b5cc119b865d7e55c40ccbb3e0dd61026ca60e
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 "streams.h"
6 #include "support/allocators/zeroafterfree.h"
7 #include "test/test_bitcoin.h"
9 #include <boost/assign/std/vector.hpp> // for 'operator+=()'
10 #include <boost/assert.hpp>
11 #include <boost/test/unit_test.hpp>
13 using namespace boost::assign; // bring 'operator+=()' into scope
15 BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
17 BOOST_AUTO_TEST_CASE(streams_vector_writer)
19 unsigned char a(1);
20 unsigned char b(2);
21 unsigned char bytes[] = { 3, 4, 5, 6 };
22 std::vector<unsigned char> vch;
24 // Each test runs twice. Serializing a second time at the same starting
25 // point should yield the same results, even if the first test grew the
26 // vector.
28 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, a, b);
29 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
30 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, a, b);
31 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
32 vch.clear();
34 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b);
35 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
36 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b);
37 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
38 vch.clear();
40 vch.resize(5, 0);
41 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b);
42 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
43 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b);
44 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
45 vch.clear();
47 vch.resize(4, 0);
48 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 3, a, b);
49 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
50 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 3, a, b);
51 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
52 vch.clear();
54 vch.resize(4, 0);
55 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 4, a, b);
56 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
57 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 4, a, b);
58 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
59 vch.clear();
61 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, FLATDATA(bytes));
62 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
63 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, FLATDATA(bytes));
64 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
65 vch.clear();
67 vch.resize(4, 8);
68 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, FLATDATA(bytes), b);
69 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
70 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, FLATDATA(bytes), b);
71 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
72 vch.clear();
75 BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
77 std::vector<char> in;
78 std::vector<char> expected_xor;
79 std::vector<unsigned char> key;
80 CDataStream ds(in, 0, 0);
82 // Degenerate case
84 key += '\x00','\x00';
85 ds.Xor(key);
86 BOOST_CHECK_EQUAL(
87 std::string(expected_xor.begin(), expected_xor.end()),
88 std::string(ds.begin(), ds.end()));
90 in += '\x0f','\xf0';
91 expected_xor += '\xf0','\x0f';
93 // Single character key
95 ds.clear();
96 ds.insert(ds.begin(), in.begin(), in.end());
97 key.clear();
99 key += '\xff';
100 ds.Xor(key);
101 BOOST_CHECK_EQUAL(
102 std::string(expected_xor.begin(), expected_xor.end()),
103 std::string(ds.begin(), ds.end()));
105 // Multi character key
107 in.clear();
108 expected_xor.clear();
109 in += '\xf0','\x0f';
110 expected_xor += '\x0f','\x00';
112 ds.clear();
113 ds.insert(ds.begin(), in.begin(), in.end());
115 key.clear();
116 key += '\xff','\x0f';
118 ds.Xor(key);
119 BOOST_CHECK_EQUAL(
120 std::string(expected_xor.begin(), expected_xor.end()),
121 std::string(ds.begin(), ds.end()));
124 BOOST_AUTO_TEST_SUITE_END()