Branch data Line data Source code
1 : : // Copyright (c) 2012-2021 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 <test/util/setup_common.h> 6 : : #include <clientversion.h> 7 : : #include <streams.h> 8 : : #include <uint256.h> 9 : : #include <wallet/test/util.h> 10 : : #include <wallet/wallet.h> 11 : : 12 : : #include <boost/test/unit_test.hpp> 13 : : 14 : : namespace wallet { 15 : 0 : BOOST_FIXTURE_TEST_SUITE(walletdb_tests, BasicTestingSetup) 16 : : 17 : 0 : BOOST_AUTO_TEST_CASE(walletdb_readkeyvalue) 18 : 0 : { 19 : : /** 20 : : * When ReadKeyValue() reads from either a "key" or "wkey" it first reads the CDataStream steam into a 21 : : * CPrivKey or CWalletKey respectively and then reads a hash of the pubkey and privkey into a uint256. 22 : : * Wallets from 0.8 or before do not store the pubkey/privkey hash, trying to read the hash from old 23 : : * wallets throws an exception, for backwards compatibility this read is wrapped in a try block to 24 : : * silently fail. The test here makes sure the type of exception thrown from CDataStream::read() 25 : : * matches the type we expect, otherwise we need to update the "key"/"wkey" exception type caught. 26 : : */ 27 : 0 : CDataStream ssValue(SER_DISK, CLIENT_VERSION); 28 : 0 : uint256 dummy; 29 : 0 : BOOST_CHECK_THROW(ssValue >> dummy, std::ios_base::failure); 30 : 0 : } 31 : : 32 : 0 : BOOST_AUTO_TEST_CASE(walletdb_read_write_deadlock) 33 : : { 34 : : // Exercises a db read write operation that shouldn't deadlock. 35 : 0 : for (const DatabaseFormat& db_format : DATABASE_FORMATS) { 36 : : // Context setup 37 : 0 : DatabaseOptions options; 38 : 0 : options.require_format = db_format; 39 : : DatabaseStatus status; 40 : 0 : bilingual_str error_string; 41 : 0 : std::unique_ptr<WalletDatabase> db = MakeDatabase(m_path_root / strprintf("wallet_%d_.dat", db_format).c_str(), options, status, error_string); 42 : 0 : BOOST_CHECK_EQUAL(status, DatabaseStatus::SUCCESS); 43 : : 44 : 0 : std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", std::move(db))); 45 : 0 : wallet->m_keypool_size = 4; 46 : : 47 : : // Create legacy spkm 48 : 0 : LOCK(wallet->cs_wallet); 49 : 0 : auto legacy_spkm = wallet->GetOrCreateLegacyScriptPubKeyMan(); 50 : 0 : BOOST_CHECK(legacy_spkm->SetupGeneration(true)); 51 : 0 : wallet->Flush(); 52 : : 53 : 0 : // Now delete all records, which performs a read write operation. 54 : 0 : BOOST_CHECK(wallet->GetLegacyScriptPubKeyMan()->DeleteRecords()); 55 : 0 : } 56 : 0 : } 57 : : 58 : 0 : BOOST_AUTO_TEST_SUITE_END() 59 : : } // namespace wallet