|
| 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 "leveldbwrapper.h" |
| 6 | +#include "uint256.h" |
| 7 | +#include "random.h" |
| 8 | +#include "test/test_bitcoin.h" |
| 9 | + |
| 10 | +#include <boost/assign/std/vector.hpp> // for 'operator+=()' |
| 11 | +#include <boost/assert.hpp> |
| 12 | +#include <boost/test/unit_test.hpp> |
| 13 | + |
| 14 | +using namespace std; |
| 15 | +using namespace boost::assign; // bring 'operator+=()' into scope |
| 16 | +using namespace boost::filesystem; |
| 17 | + |
| 18 | +// Test if a string consists entirely of null characters |
| 19 | +bool is_null_key(const vector<unsigned char>& key) { |
| 20 | + bool isnull = true; |
| 21 | + |
| 22 | + for (unsigned int i = 0; i < key.size(); i++) |
| 23 | + isnull &= (key[i] == '\x00'); |
| 24 | + |
| 25 | + return isnull; |
| 26 | +} |
| 27 | + |
| 28 | +BOOST_FIXTURE_TEST_SUITE(leveldbwrapper_tests, BasicTestingSetup) |
| 29 | + |
| 30 | +BOOST_AUTO_TEST_CASE(leveldbwrapper) |
| 31 | +{ |
| 32 | + // Perform tests both obfuscated and non-obfuscated. |
| 33 | + for (int i = 0; i < 2; i++) { |
| 34 | + bool obfuscate = (bool)i; |
| 35 | + path ph = temp_directory_path() / unique_path(); |
| 36 | + CLevelDBWrapper dbw(ph, (1 << 20), true, false, obfuscate); |
| 37 | + char key = 'k'; |
| 38 | + uint256 in = GetRandHash(); |
| 39 | + uint256 res; |
| 40 | + |
| 41 | + // Ensure that we're doing real obfuscation when obfuscate=true |
| 42 | + BOOST_CHECK(obfuscate != is_null_key(dbw.GetObfuscateKey())); |
| 43 | + |
| 44 | + BOOST_CHECK(dbw.Write(key, in)); |
| 45 | + BOOST_CHECK(dbw.Read(key, res)); |
| 46 | + BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +// Test that we do not obfuscation if there is existing data. |
| 51 | +BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate) |
| 52 | +{ |
| 53 | + // We're going to share this path between two wrappers |
| 54 | + path ph = temp_directory_path() / unique_path(); |
| 55 | + create_directories(ph); |
| 56 | + |
| 57 | + // Set up a non-obfuscated wrapper to write some initial data. |
| 58 | + CLevelDBWrapper* dbw = new CLevelDBWrapper(ph, (1 << 10), false, false, false); |
| 59 | + char key = 'k'; |
| 60 | + uint256 in = GetRandHash(); |
| 61 | + uint256 res; |
| 62 | + |
| 63 | + BOOST_CHECK(dbw->Write(key, in)); |
| 64 | + BOOST_CHECK(dbw->Read(key, res)); |
| 65 | + BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); |
| 66 | + |
| 67 | + // Call the destructor to free leveldb LOCK |
| 68 | + delete dbw; |
| 69 | + |
| 70 | + // Now, set up another wrapper that wants to obfuscate the same directory |
| 71 | + CLevelDBWrapper odbw(ph, (1 << 10), false, false, true); |
| 72 | + |
| 73 | + // Check that the key/val we wrote with unobfuscated wrapper exists and |
| 74 | + // is readable. |
| 75 | + uint256 res2; |
| 76 | + BOOST_CHECK(odbw.Read(key, res2)); |
| 77 | + BOOST_CHECK_EQUAL(res2.ToString(), in.ToString()); |
| 78 | + |
| 79 | + BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data |
| 80 | + BOOST_CHECK(is_null_key(odbw.GetObfuscateKey())); // The key should be an empty string |
| 81 | + |
| 82 | + uint256 in2 = GetRandHash(); |
| 83 | + uint256 res3; |
| 84 | + |
| 85 | + // Check that we can write successfully |
| 86 | + BOOST_CHECK(odbw.Write(key, in2)); |
| 87 | + BOOST_CHECK(odbw.Read(key, res3)); |
| 88 | + BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString()); |
| 89 | +} |
| 90 | + |
| 91 | +// Ensure that we start obfuscating during a reindex. |
| 92 | +BOOST_AUTO_TEST_CASE(existing_data_reindex) |
| 93 | +{ |
| 94 | + // We're going to share this path between two wrappers |
| 95 | + path ph = temp_directory_path() / unique_path(); |
| 96 | + create_directories(ph); |
| 97 | + |
| 98 | + // Set up a non-obfuscated wrapper to write some initial data. |
| 99 | + CLevelDBWrapper* dbw = new CLevelDBWrapper(ph, (1 << 10), false, false, false); |
| 100 | + char key = 'k'; |
| 101 | + uint256 in = GetRandHash(); |
| 102 | + uint256 res; |
| 103 | + |
| 104 | + BOOST_CHECK(dbw->Write(key, in)); |
| 105 | + BOOST_CHECK(dbw->Read(key, res)); |
| 106 | + BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); |
| 107 | + |
| 108 | + // Call the destructor to free leveldb LOCK |
| 109 | + delete dbw; |
| 110 | + |
| 111 | + // Simulate a -reindex by wiping the existing data store |
| 112 | + CLevelDBWrapper odbw(ph, (1 << 10), false, true, true); |
| 113 | + |
| 114 | + // Check that the key/val we wrote with unobfuscated wrapper doesn't exist |
| 115 | + uint256 res2; |
| 116 | + BOOST_CHECK(!odbw.Read(key, res2)); |
| 117 | + BOOST_CHECK(!is_null_key(odbw.GetObfuscateKey())); |
| 118 | + |
| 119 | + uint256 in2 = GetRandHash(); |
| 120 | + uint256 res3; |
| 121 | + |
| 122 | + // Check that we can write successfully |
| 123 | + BOOST_CHECK(odbw.Write(key, in2)); |
| 124 | + BOOST_CHECK(odbw.Read(key, res3)); |
| 125 | + BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString()); |
| 126 | +} |
| 127 | + |
| 128 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments