|
2 | 2 | // Distributed under the MIT software license, see the accompanying
|
3 | 3 | // file COPYING or https://www.opensource.org/licenses/mit-license.php.
|
4 | 4 |
|
| 5 | +#include <wallet/test/util.h> |
5 | 6 | #include <wallet/wallet.h>
|
6 | 7 | #include <test/util/setup_common.h>
|
7 | 8 |
|
@@ -50,5 +51,139 @@ BOOST_FIXTURE_TEST_CASE(wallet_load_unknown_descriptor, TestingSetup)
|
50 | 51 | }
|
51 | 52 | }
|
52 | 53 |
|
| 54 | +bool HasAnyRecordOfType(WalletDatabase& db, const std::string& key) |
| 55 | +{ |
| 56 | + std::unique_ptr<DatabaseBatch> batch = db.MakeBatch(false); |
| 57 | + BOOST_CHECK(batch->StartCursor()); |
| 58 | + while (true) { |
| 59 | + CDataStream ssKey(SER_DISK, CLIENT_VERSION); |
| 60 | + CDataStream ssValue(SER_DISK, CLIENT_VERSION); |
| 61 | + bool complete; |
| 62 | + BOOST_CHECK(batch->ReadAtCursor(ssKey, ssValue, complete)); |
| 63 | + if (complete) break; |
| 64 | + std::string type; |
| 65 | + ssKey >> type; |
| 66 | + if (type == key) return true; |
| 67 | + } |
| 68 | + return false; |
| 69 | +} |
| 70 | + |
| 71 | +BOOST_FIXTURE_TEST_CASE(wallet_load_verif_crypted_key_checksum, TestingSetup) |
| 72 | +{ |
| 73 | + // The test duplicates the db so each case has its own db instance. |
| 74 | + int NUMBER_OF_TESTS = 4; |
| 75 | + std::vector<std::unique_ptr<WalletDatabase>> dbs; |
| 76 | + CKey first_key; |
| 77 | + auto get_db = [](std::vector<std::unique_ptr<WalletDatabase>>& dbs) { |
| 78 | + std::unique_ptr<WalletDatabase> db = std::move(dbs.back()); |
| 79 | + dbs.pop_back(); |
| 80 | + return db; |
| 81 | + }; |
| 82 | + |
| 83 | + { // Context setup. |
| 84 | + // Create and encrypt legacy wallet |
| 85 | + std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", m_args, CreateMockWalletDatabase())); |
| 86 | + LOCK(wallet->cs_wallet); |
| 87 | + auto legacy_spkm = wallet->GetOrCreateLegacyScriptPubKeyMan(); |
| 88 | + BOOST_CHECK(legacy_spkm->SetupGeneration(true)); |
| 89 | + |
| 90 | + // Get the first key in the wallet |
| 91 | + CTxDestination dest = *Assert(legacy_spkm->GetNewDestination(OutputType::LEGACY)); |
| 92 | + CKeyID key_id = GetKeyForDestination(*legacy_spkm, dest); |
| 93 | + BOOST_CHECK(legacy_spkm->GetKey(key_id, first_key)); |
| 94 | + |
| 95 | + // Encrypt the wallet and duplicate database |
| 96 | + BOOST_CHECK(wallet->EncryptWallet("encrypt")); |
| 97 | + wallet->Flush(); |
| 98 | + |
| 99 | + DatabaseOptions options; |
| 100 | + for (int i=0; i < NUMBER_OF_TESTS; i++) { |
| 101 | + dbs.emplace_back(DuplicateMockDatabase(wallet->GetDatabase(), options)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + { |
| 106 | + // First test case: |
| 107 | + // Erase all the crypted keys from db and unlock the wallet. |
| 108 | + // The wallet will only re-write the crypted keys to db if any checksum is missing at load time. |
| 109 | + // So, if any 'ckey' record re-appears on db, then the checksums were not properly calculated, and we are re-writing |
| 110 | + // the records every time that 'CWallet::Unlock' gets called, which is not good. |
| 111 | + |
| 112 | + // Load the wallet and check that is encrypted |
| 113 | + std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", m_args, get_db(dbs))); |
| 114 | + BOOST_CHECK_EQUAL(wallet->LoadWallet(), DBErrors::LOAD_OK); |
| 115 | + BOOST_CHECK(wallet->IsCrypted()); |
| 116 | + BOOST_CHECK(HasAnyRecordOfType(wallet->GetDatabase(), DBKeys::CRYPTED_KEY)); |
| 117 | + |
| 118 | + // Now delete all records and check that the 'Unlock' function doesn't re-write them |
| 119 | + BOOST_CHECK(wallet->GetLegacyScriptPubKeyMan()->DeleteRecords()); |
| 120 | + BOOST_CHECK(!HasAnyRecordOfType(wallet->GetDatabase(), DBKeys::CRYPTED_KEY)); |
| 121 | + BOOST_CHECK(wallet->Unlock("encrypt")); |
| 122 | + BOOST_CHECK(!HasAnyRecordOfType(wallet->GetDatabase(), DBKeys::CRYPTED_KEY)); |
| 123 | + } |
| 124 | + |
| 125 | + { |
| 126 | + // Second test case: |
| 127 | + // Verify that loading up a 'ckey' with no checksum triggers a complete re-write of the crypted keys. |
| 128 | + std::unique_ptr<WalletDatabase> db = get_db(dbs); |
| 129 | + { |
| 130 | + std::unique_ptr<DatabaseBatch> batch = db->MakeBatch(false); |
| 131 | + std::pair<std::vector<unsigned char>, uint256> value; |
| 132 | + BOOST_CHECK(batch->Read(std::make_pair(DBKeys::CRYPTED_KEY, first_key.GetPubKey()), value)); |
| 133 | + |
| 134 | + const auto key = std::make_pair(DBKeys::CRYPTED_KEY, first_key.GetPubKey()); |
| 135 | + BOOST_CHECK(batch->Write(key, value.first, /*fOverwrite=*/true)); |
| 136 | + } |
| 137 | + |
| 138 | + // Load the wallet and check that is encrypted |
| 139 | + std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", m_args, std::move(db))); |
| 140 | + BOOST_CHECK_EQUAL(wallet->LoadWallet(), DBErrors::LOAD_OK); |
| 141 | + BOOST_CHECK(wallet->IsCrypted()); |
| 142 | + BOOST_CHECK(HasAnyRecordOfType(wallet->GetDatabase(), DBKeys::CRYPTED_KEY)); |
| 143 | + |
| 144 | + // Now delete all ckey records and check that the 'Unlock' function re-writes them |
| 145 | + // (this is because the wallet, at load time, found a ckey record with no checksum) |
| 146 | + BOOST_CHECK(wallet->GetLegacyScriptPubKeyMan()->DeleteRecords()); |
| 147 | + BOOST_CHECK(!HasAnyRecordOfType(wallet->GetDatabase(), DBKeys::CRYPTED_KEY)); |
| 148 | + BOOST_CHECK(wallet->Unlock("encrypt")); |
| 149 | + BOOST_CHECK(HasAnyRecordOfType(wallet->GetDatabase(), DBKeys::CRYPTED_KEY)); |
| 150 | + } |
| 151 | + |
| 152 | + { |
| 153 | + // Third test case: |
| 154 | + // Verify that loading up a 'ckey' with an invalid checksum throws an error. |
| 155 | + std::unique_ptr<WalletDatabase> db = get_db(dbs); |
| 156 | + { |
| 157 | + std::unique_ptr<DatabaseBatch> batch = db->MakeBatch(false); |
| 158 | + std::vector<unsigned char> crypted_data; |
| 159 | + BOOST_CHECK(batch->Read(std::make_pair(DBKeys::CRYPTED_KEY, first_key.GetPubKey()), crypted_data)); |
| 160 | + |
| 161 | + // Write an invalid checksum |
| 162 | + std::pair<std::vector<unsigned char>, uint256> value = std::make_pair(crypted_data, uint256::ONE); |
| 163 | + const auto key = std::make_pair(DBKeys::CRYPTED_KEY, first_key.GetPubKey()); |
| 164 | + BOOST_CHECK(batch->Write(key, value, /*fOverwrite=*/true)); |
| 165 | + } |
| 166 | + |
| 167 | + std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", m_args, std::move(db))); |
| 168 | + BOOST_CHECK_EQUAL(wallet->LoadWallet(), DBErrors::CORRUPT); |
| 169 | + } |
| 170 | + |
| 171 | + { |
| 172 | + // Fourth test case: |
| 173 | + // Verify that loading up a 'ckey' with an invalid pubkey throws an error |
| 174 | + std::unique_ptr<WalletDatabase> db = get_db(dbs); |
| 175 | + { |
| 176 | + CPubKey invalid_key; |
| 177 | + BOOST_ASSERT(!invalid_key.IsValid()); |
| 178 | + const auto key = std::make_pair(DBKeys::CRYPTED_KEY, invalid_key); |
| 179 | + std::pair<std::vector<unsigned char>, uint256> value; |
| 180 | + BOOST_CHECK(db->MakeBatch(false)->Write(key, value, /*fOverwrite=*/true)); |
| 181 | + } |
| 182 | + |
| 183 | + std::shared_ptr<CWallet> wallet(new CWallet(m_node.chain.get(), "", m_args, std::move(db))); |
| 184 | + BOOST_CHECK_EQUAL(wallet->LoadWallet(), DBErrors::CORRUPT); |
| 185 | + } |
| 186 | +} |
| 187 | + |
53 | 188 | BOOST_AUTO_TEST_SUITE_END()
|
54 | 189 | } // namespace wallet
|
0 commit comments