LCOV - code coverage report
Current view: top level - src/wallet/test - util.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 71.4 % 28 20
Test Date: 2024-08-28 04:44:32 Functions: 60.7 % 28 17
Branches: 50.0 % 2 1

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2021-2022 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                 :             : #ifndef BITCOIN_WALLET_TEST_UTIL_H
       6                 :             : #define BITCOIN_WALLET_TEST_UTIL_H
       7                 :             : 
       8                 :             : #include <config/bitcoin-config.h> // IWYU pragma: keep
       9                 :             : 
      10                 :             : #include <addresstype.h>
      11                 :             : #include <wallet/db.h>
      12                 :             : 
      13                 :             : #include <memory>
      14                 :             : 
      15                 :             : class ArgsManager;
      16                 :             : class CChain;
      17                 :             : class CKey;
      18                 :             : enum class OutputType;
      19                 :             : namespace interfaces {
      20                 :             : class Chain;
      21                 :             : } // namespace interfaces
      22                 :             : 
      23                 :             : namespace wallet {
      24                 :             : class CWallet;
      25                 :             : class WalletDatabase;
      26                 :             : struct WalletContext;
      27                 :             : 
      28                 :             : static const DatabaseFormat DATABASE_FORMATS[] = {
      29                 :             : #ifdef USE_SQLITE
      30                 :             :        DatabaseFormat::SQLITE,
      31                 :             : #endif
      32                 :             : #ifdef USE_BDB
      33                 :             :        DatabaseFormat::BERKELEY,
      34                 :             : #endif
      35                 :             : };
      36                 :             : 
      37                 :             : const std::string ADDRESS_BCRT1_UNSPENDABLE = "bcrt1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq3xueyj";
      38                 :             : 
      39                 :             : std::unique_ptr<CWallet> CreateSyncedWallet(interfaces::Chain& chain, CChain& cchain, const CKey& key);
      40                 :             : 
      41                 :             : std::shared_ptr<CWallet> TestLoadWallet(WalletContext& context);
      42                 :             : std::shared_ptr<CWallet> TestLoadWallet(std::unique_ptr<WalletDatabase> database, WalletContext& context, uint64_t create_flags);
      43                 :             : void TestUnloadWallet(std::shared_ptr<CWallet>&& wallet);
      44                 :             : 
      45                 :             : // Creates a copy of the provided database
      46                 :             : std::unique_ptr<WalletDatabase> DuplicateMockDatabase(WalletDatabase& database);
      47                 :             : 
      48                 :             : /** Returns a new encoded destination from the wallet (hardcoded to BECH32) */
      49                 :             : std::string getnewaddress(CWallet& w);
      50                 :             : /** Returns a new destination, of an specific type, from the wallet */
      51                 :             : CTxDestination getNewDestination(CWallet& w, OutputType output_type);
      52                 :             : 
      53                 :             : using MockableData = std::map<SerializeData, SerializeData, std::less<>>;
      54                 :             : 
      55                 :             : class MockableCursor: public DatabaseCursor
      56                 :             : {
      57                 :             : public:
      58                 :             :     MockableData::const_iterator m_cursor;
      59                 :             :     MockableData::const_iterator m_cursor_end;
      60                 :             :     bool m_pass;
      61                 :             : 
      62                 :           6 :     explicit MockableCursor(const MockableData& records, bool pass) : m_cursor(records.begin()), m_cursor_end(records.end()), m_pass(pass) {}
      63                 :             :     MockableCursor(const MockableData& records, bool pass, Span<const std::byte> prefix);
      64                 :         987 :     ~MockableCursor() = default;
      65                 :             : 
      66                 :             :     Status Next(DataStream& key, DataStream& value) override;
      67                 :             : };
      68                 :             : 
      69                 :             : class MockableBatch : public DatabaseBatch
      70                 :             : {
      71                 :             : private:
      72                 :             :     MockableData& m_records;
      73                 :             :     bool m_pass;
      74                 :             : 
      75                 :             :     bool ReadKey(DataStream&& key, DataStream& value) override;
      76                 :             :     bool WriteKey(DataStream&& key, DataStream&& value, bool overwrite=true) override;
      77                 :             :     bool EraseKey(DataStream&& key) override;
      78                 :             :     bool HasKey(DataStream&& key) override;
      79                 :             :     bool ErasePrefix(Span<const std::byte> prefix) override;
      80                 :             : 
      81                 :             : public:
      82                 :       19738 :     explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
      83                 :       19738 :     ~MockableBatch() = default;
      84                 :             : 
      85                 :       50107 :     void Flush() override {}
      86                 :           0 :     void Close() override {}
      87                 :             : 
      88                 :           6 :     std::unique_ptr<DatabaseCursor> GetNewCursor() override
      89                 :             :     {
      90                 :           6 :         return std::make_unique<MockableCursor>(m_records, m_pass);
      91                 :             :     }
      92                 :         981 :     std::unique_ptr<DatabaseCursor> GetNewPrefixCursor(Span<const std::byte> prefix) override {
      93                 :         981 :         return std::make_unique<MockableCursor>(m_records, m_pass, prefix);
      94                 :             :     }
      95                 :        6659 :     bool TxnBegin() override { return m_pass; }
      96                 :        6658 :     bool TxnCommit() override { return m_pass; }
      97                 :           0 :     bool TxnAbort() override { return m_pass; }
      98                 :             : };
      99                 :             : 
     100                 :             : /** A WalletDatabase whose contents and return values can be modified as needed for testing
     101                 :             :  **/
     102                 :             : class MockableDatabase : public WalletDatabase
     103                 :             : {
     104                 :             : public:
     105                 :             :     MockableData m_records;
     106                 :             :     bool m_pass{true};
     107                 :             : 
     108         [ +  - ]:         108 :     MockableDatabase(MockableData records = {}) : WalletDatabase(), m_records(records) {}
     109                 :         108 :     ~MockableDatabase() = default;
     110                 :             : 
     111                 :           0 :     void Open() override {}
     112                 :           0 :     void AddRef() override {}
     113                 :           0 :     void RemoveRef() override {}
     114                 :             : 
     115                 :           1 :     bool Rewrite(const char* pszSkip=nullptr) override { return m_pass; }
     116                 :           0 :     bool Backup(const std::string& strDest) const override { return m_pass; }
     117                 :           1 :     void Flush() override {}
     118                 :           2 :     void Close() override {}
     119                 :           0 :     bool PeriodicFlush() override { return m_pass; }
     120                 :       50107 :     void IncrementUpdateCounter() override {}
     121                 :           1 :     void ReloadDbEnv() override {}
     122                 :             : 
     123                 :           0 :     std::string Filename() override { return "mockable"; }
     124                 :          31 :     std::string Format() override { return "mock"; }
     125                 :       19738 :     std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<MockableBatch>(m_records, m_pass); }
     126                 :             : };
     127                 :             : 
     128                 :             : std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});
     129                 :             : 
     130                 :             : MockableDatabase& GetMockableDatabase(CWallet& wallet);
     131                 :             : } // namespace wallet
     132                 :             : 
     133                 :             : #endif // BITCOIN_WALLET_TEST_UTIL_H
        

Generated by: LCOV version 2.0-1