Merge #12001: [RPC] Adding ::minRelayTxFee amount to getmempoolinfo and updating...
[bitcoinplatinum.git] / src / test / streams_tests.cpp
blob1108dab584b44552f86f2366df8aa1b962d3bf09
1 // Copyright (c) 2012-2017 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/test/unit_test.hpp>
12 using namespace boost::assign; // bring 'operator+=()' into scope
14 BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
16 BOOST_AUTO_TEST_CASE(streams_vector_writer)
18 unsigned char a(1);
19 unsigned char b(2);
20 unsigned char bytes[] = { 3, 4, 5, 6 };
21 std::vector<unsigned char> vch;
23 // Each test runs twice. Serializing a second time at the same starting
24 // point should yield the same results, even if the first test grew the
25 // vector.
27 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, a, b);
28 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
29 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, a, b);
30 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
31 vch.clear();
33 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b);
34 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
35 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b);
36 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
37 vch.clear();
39 vch.resize(5, 0);
40 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b);
41 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
42 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, b);
43 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
44 vch.clear();
46 vch.resize(4, 0);
47 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 3, a, b);
48 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
49 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 3, a, b);
50 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
51 vch.clear();
53 vch.resize(4, 0);
54 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 4, a, b);
55 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
56 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 4, a, b);
57 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
58 vch.clear();
60 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, FLATDATA(bytes));
61 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
62 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 0, FLATDATA(bytes));
63 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
64 vch.clear();
66 vch.resize(4, 8);
67 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, FLATDATA(bytes), b);
68 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
69 CVectorWriter(SER_NETWORK, INIT_PROTO_VERSION, vch, 2, a, FLATDATA(bytes), b);
70 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
71 vch.clear();
74 BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
76 std::vector<char> in;
77 std::vector<char> expected_xor;
78 std::vector<unsigned char> key;
79 CDataStream ds(in, 0, 0);
81 // Degenerate case
83 key += '\x00','\x00';
84 ds.Xor(key);
85 BOOST_CHECK_EQUAL(
86 std::string(expected_xor.begin(), expected_xor.end()),
87 std::string(ds.begin(), ds.end()));
89 in += '\x0f','\xf0';
90 expected_xor += '\xf0','\x0f';
92 // Single character key
94 ds.clear();
95 ds.insert(ds.begin(), in.begin(), in.end());
96 key.clear();
98 key += '\xff';
99 ds.Xor(key);
100 BOOST_CHECK_EQUAL(
101 std::string(expected_xor.begin(), expected_xor.end()),
102 std::string(ds.begin(), ds.end()));
104 // Multi character key
106 in.clear();
107 expected_xor.clear();
108 in += '\xf0','\x0f';
109 expected_xor += '\x0f','\x00';
111 ds.clear();
112 ds.insert(ds.begin(), in.begin(), in.end());
114 key.clear();
115 key += '\xff','\x0f';
117 ds.Xor(key);
118 BOOST_CHECK_EQUAL(
119 std::string(expected_xor.begin(), expected_xor.end()),
120 std::string(ds.begin(), ds.end()));
123 BOOST_AUTO_TEST_SUITE_END()