LCOV - code coverage report
Current view: top level - src/test - leveldbwrapper_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 53 55 96.4 %
Date: 2015-10-12 22:39:14 Functions: 8 15 53.3 %
Legend: Lines: hit not hit

          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 "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           0 : bool is_null_key(const vector<unsigned char>& key) {
      20           0 :     bool isnull = true;
      21             : 
      22          68 :     for (unsigned int i = 0; i < key.size(); i++)
      23          64 :         isnull &= (key[i] == '\x00');
      24             : 
      25           4 :     return isnull;
      26             : }
      27             :  
      28           1 : BOOST_FIXTURE_TEST_SUITE(leveldbwrapper_tests, BasicTestingSetup)
      29             :                        
      30           6 : BOOST_AUTO_TEST_CASE(leveldbwrapper)
      31             : {
      32             :     // Perform tests both obfuscated and non-obfuscated.
      33           3 :     for (int i = 0; i < 2; i++) {
      34           2 :         bool obfuscate = (bool)i;
      35           8 :         path ph = temp_directory_path() / unique_path();
      36           4 :         CLevelDBWrapper dbw(ph, (1 << 20), true, false, obfuscate);
      37           2 :         char key = 'k';
      38           2 :         uint256 in = GetRandHash();
      39             :         uint256 res;
      40             : 
      41             :         // Ensure that we're doing real obfuscation when obfuscate=true
      42          16 :         BOOST_CHECK(obfuscate != is_null_key(dbw.GetObfuscateKey()));
      43             : 
      44          16 :         BOOST_CHECK(dbw.Write(key, in));
      45          16 :         BOOST_CHECK(dbw.Read(key, res));
      46          14 :         BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
      47             :     }
      48           1 : }
      49             :                        
      50             : // Test that we do not obfuscation if there is existing data.
      51           6 : BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
      52             : {
      53             :     // We're going to share this path between two wrappers
      54           4 :     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           1 :     CLevelDBWrapper* dbw = new CLevelDBWrapper(ph, (1 << 10), false, false, false);
      59           1 :     char key = 'k';
      60           1 :     uint256 in = GetRandHash();
      61             :     uint256 res;
      62             : 
      63           8 :     BOOST_CHECK(dbw->Write(key, in));
      64           7 :     BOOST_CHECK(dbw->Read(key, res));
      65           7 :     BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
      66             : 
      67             :     // Call the destructor to free leveldb LOCK
      68           1 :     delete dbw;
      69             : 
      70             :     // Now, set up another wrapper that wants to obfuscate the same directory
      71           2 :     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           8 :     BOOST_CHECK(odbw.Read(key, res2));
      77           7 :     BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
      78             : 
      79           8 :     BOOST_CHECK(!odbw.IsEmpty()); // There should be existing data
      80           9 :     BOOST_CHECK(is_null_key(odbw.GetObfuscateKey())); // The key should be an empty string
      81             : 
      82           1 :     uint256 in2 = GetRandHash();
      83             :     uint256 res3;
      84             :  
      85             :     // Check that we can write successfully
      86           8 :     BOOST_CHECK(odbw.Write(key, in2));
      87           8 :     BOOST_CHECK(odbw.Read(key, res3));
      88           7 :     BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
      89           1 : }
      90             :                         
      91             : // Ensure that we start obfuscating during a reindex.
      92           6 : BOOST_AUTO_TEST_CASE(existing_data_reindex)
      93             : {
      94             :     // We're going to share this path between two wrappers
      95           4 :     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           1 :     CLevelDBWrapper* dbw = new CLevelDBWrapper(ph, (1 << 10), false, false, false);
     100           1 :     char key = 'k';
     101           1 :     uint256 in = GetRandHash();
     102             :     uint256 res;
     103             : 
     104           8 :     BOOST_CHECK(dbw->Write(key, in));
     105           8 :     BOOST_CHECK(dbw->Read(key, res));
     106           7 :     BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
     107             : 
     108             :     // Call the destructor to free leveldb LOCK
     109           1 :     delete dbw;
     110             : 
     111             :     // Simulate a -reindex by wiping the existing data store
     112           2 :     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           8 :     BOOST_CHECK(!odbw.Read(key, res2));
     117           9 :     BOOST_CHECK(!is_null_key(odbw.GetObfuscateKey()));
     118             : 
     119           1 :     uint256 in2 = GetRandHash();
     120             :     uint256 res3;
     121             :  
     122             :     // Check that we can write successfully
     123           8 :     BOOST_CHECK(odbw.Write(key, in2));
     124           8 :     BOOST_CHECK(odbw.Read(key, res3));
     125           7 :     BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
     126           1 : }
     127             :  
     128           3 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.11