Changeset View
Changeset View
Standalone View
Standalone View
src/test/streams_tests.cpp
// Copyright (c) 2012-2019 The Bitcoin Core developers | // Copyright (c) 2012-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <streams.h> | #include <streams.h> | ||||
#include <test/setup_common.h> | #include <test/setup_common.h> | ||||
#include <boost/assign/std/vector.hpp> // for 'operator+=()' | |||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
using namespace boost::assign; // bring 'operator+=()' into scope | |||||
BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup) | BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup) | ||||
BOOST_AUTO_TEST_CASE(streams_vector_writer) { | BOOST_AUTO_TEST_CASE(streams_vector_writer) { | ||||
uint8_t a(1); | uint8_t a(1); | ||||
uint8_t b(2); | uint8_t b(2); | ||||
uint8_t bytes[] = {3, 4, 5, 6}; | uint8_t bytes[] = {3, 4, 5, 6}; | ||||
std::vector<uint8_t> vch; | std::vector<uint8_t> vch; | ||||
▲ Show 20 Lines • Show All 133 Lines • ▼ Show 20 Lines | |||||
BOOST_AUTO_TEST_CASE(streams_serializedata_xor) { | BOOST_AUTO_TEST_CASE(streams_serializedata_xor) { | ||||
std::vector<char> in; | std::vector<char> in; | ||||
std::vector<char> expected_xor; | std::vector<char> expected_xor; | ||||
std::vector<uint8_t> key; | std::vector<uint8_t> key; | ||||
CDataStream ds(in, 0, 0); | CDataStream ds(in, 0, 0); | ||||
// Degenerate case | // Degenerate case | ||||
key += '\x00', '\x00'; | key.push_back('\x00'); | ||||
key.push_back('\x00'); | |||||
ds.Xor(key); | ds.Xor(key); | ||||
BOOST_CHECK_EQUAL(std::string(expected_xor.begin(), expected_xor.end()), | BOOST_CHECK_EQUAL(std::string(expected_xor.begin(), expected_xor.end()), | ||||
std::string(ds.begin(), ds.end())); | std::string(ds.begin(), ds.end())); | ||||
in += '\x0f', '\xf0'; | in.push_back('\x0f'); | ||||
expected_xor += '\xf0', '\x0f'; | in.push_back('\xf0'); | ||||
expected_xor.push_back('\xf0'); | |||||
expected_xor.push_back('\x0f'); | |||||
// Single character key | // Single character key | ||||
ds.clear(); | ds.clear(); | ||||
ds.insert(ds.begin(), in.begin(), in.end()); | ds.insert(ds.begin(), in.begin(), in.end()); | ||||
key.clear(); | key.clear(); | ||||
key += '\xff'; | key.push_back('\xff'); | ||||
ds.Xor(key); | ds.Xor(key); | ||||
BOOST_CHECK_EQUAL(std::string(expected_xor.begin(), expected_xor.end()), | BOOST_CHECK_EQUAL(std::string(expected_xor.begin(), expected_xor.end()), | ||||
std::string(ds.begin(), ds.end())); | std::string(ds.begin(), ds.end())); | ||||
// Multi character key | // Multi character key | ||||
in.clear(); | in.clear(); | ||||
expected_xor.clear(); | expected_xor.clear(); | ||||
in += '\xf0', '\x0f'; | in.push_back('\xf0'); | ||||
expected_xor += '\x0f', '\x00'; | in.push_back('\x0f'); | ||||
expected_xor.push_back('\x0f'); | |||||
expected_xor.push_back('\x00'); | |||||
ds.clear(); | ds.clear(); | ||||
ds.insert(ds.begin(), in.begin(), in.end()); | ds.insert(ds.begin(), in.begin(), in.end()); | ||||
key.clear(); | key.clear(); | ||||
key += '\xff', '\x0f'; | key.push_back('\xff'); | ||||
key.push_back('\x0f'); | |||||
ds.Xor(key); | ds.Xor(key); | ||||
BOOST_CHECK_EQUAL(std::string(expected_xor.begin(), expected_xor.end()), | BOOST_CHECK_EQUAL(std::string(expected_xor.begin(), expected_xor.end()), | ||||
std::string(ds.begin(), ds.end())); | std::string(ds.begin(), ds.end())); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(streams_empty_vector) { | BOOST_AUTO_TEST_CASE(streams_empty_vector) { | ||||
std::vector<char> in; | std::vector<char> in; | ||||
Show All 17 Lines |