Line data Source code
1 : // Copyright (c) 2012-2013 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 "streams.h"
6 : #include "support/allocators/zeroafterfree.h"
7 : #include "test/test_bitcoin.h"
8 :
9 : #include <boost/assign/std/vector.hpp> // for 'operator+=()'
10 : #include <boost/assert.hpp>
11 : #include <boost/test/unit_test.hpp>
12 :
13 : using namespace std;
14 : using namespace boost::assign; // bring 'operator+=()' into scope
15 :
16 1 : BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
17 :
18 6 : BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
19 : {
20 : std::vector<char> in;
21 : std::vector<char> expected_xor;
22 : std::vector<unsigned char> key;
23 : CDataStream ds(in, 0, 0);
24 :
25 : // Degenerate case
26 :
27 2 : key += '\x00','\x00';
28 1 : ds.Xor(key);
29 11 : BOOST_CHECK_EQUAL(
30 : std::string(expected_xor.begin(), expected_xor.end()),
31 : std::string(ds.begin(), ds.end()));
32 :
33 2 : in += '\x0f','\xf0';
34 3 : expected_xor += '\xf0','\x0f';
35 :
36 : // Single character key
37 :
38 : ds.clear();
39 3 : ds.insert(ds.begin(), in.begin(), in.end());
40 : key.clear();
41 :
42 : key += '\xff';
43 1 : ds.Xor(key);
44 11 : BOOST_CHECK_EQUAL(
45 : std::string(expected_xor.begin(), expected_xor.end()),
46 : std::string(ds.begin(), ds.end()));
47 :
48 : // Multi character key
49 :
50 : in.clear();
51 : expected_xor.clear();
52 2 : in += '\xf0','\x0f';
53 3 : expected_xor += '\x0f','\x00';
54 :
55 : ds.clear();
56 3 : ds.insert(ds.begin(), in.begin(), in.end());
57 :
58 : key.clear();
59 2 : key += '\xff','\x0f';
60 :
61 1 : ds.Xor(key);
62 11 : BOOST_CHECK_EQUAL(
63 : std::string(expected_xor.begin(), expected_xor.end()),
64 : std::string(ds.begin(), ds.end()));
65 1 : }
66 :
67 3 : BOOST_AUTO_TEST_SUITE_END()
|