|
6 | 6 |
|
7 | 7 | #include <test/util/setup_common.h> |
8 | 8 | #include <util/fs.h> |
| 9 | +#include <util/translation.h> |
| 10 | +#ifdef USE_BDB |
9 | 11 | #include <wallet/bdb.h> |
| 12 | +#endif |
| 13 | +#ifdef USE_SQLITE |
| 14 | +#include <wallet/sqlite.h> |
| 15 | +#endif |
| 16 | +#include <wallet/test/util.h> |
| 17 | +#include <wallet/walletutil.h> // for WALLET_FLAG_DESCRIPTORS |
10 | 18 |
|
11 | 19 | #include <fstream> |
12 | 20 | #include <memory> |
13 | 21 | #include <string> |
14 | 22 |
|
| 23 | +inline std::ostream& operator<<(std::ostream& os, const std::pair<const SerializeData, SerializeData>& kv) |
| 24 | +{ |
| 25 | + Span key{kv.first}, value{kv.second}; |
| 26 | + os << "(\"" << std::string_view{reinterpret_cast<const char*>(key.data()), key.size()} << "\", \"" |
| 27 | + << std::string_view{reinterpret_cast<const char*>(key.data()), key.size()} << "\")"; |
| 28 | + return os; |
| 29 | +} |
| 30 | + |
15 | 31 | namespace wallet { |
| 32 | + |
| 33 | +static Span<const std::byte> StringBytes(std::string_view str) |
| 34 | +{ |
| 35 | + return AsBytes<const char>({str.data(), str.size()}); |
| 36 | +} |
| 37 | + |
| 38 | +static SerializeData StringData(std::string_view str) |
| 39 | +{ |
| 40 | + auto bytes = StringBytes(str); |
| 41 | + return SerializeData{bytes.begin(), bytes.end()}; |
| 42 | +} |
| 43 | + |
| 44 | +static void CheckPrefix(DatabaseBatch& batch, Span<const std::byte> prefix, MockableData expected) |
| 45 | +{ |
| 46 | + std::unique_ptr<DatabaseCursor> cursor = batch.GetNewPrefixCursor(prefix); |
| 47 | + MockableData actual; |
| 48 | + while (true) { |
| 49 | + DataStream key, value; |
| 50 | + DatabaseCursor::Status status = cursor->Next(key, value); |
| 51 | + if (status == DatabaseCursor::Status::DONE) break; |
| 52 | + BOOST_CHECK(status == DatabaseCursor::Status::MORE); |
| 53 | + BOOST_CHECK( |
| 54 | + actual.emplace(SerializeData(key.begin(), key.end()), SerializeData(value.begin(), value.end())).second); |
| 55 | + } |
| 56 | + BOOST_CHECK_EQUAL_COLLECTIONS(actual.begin(), actual.end(), expected.begin(), expected.end()); |
| 57 | +} |
| 58 | + |
16 | 59 | BOOST_FIXTURE_TEST_SUITE(db_tests, BasicTestingSetup) |
17 | 60 |
|
18 | 61 | static std::shared_ptr<BerkeleyEnvironment> GetWalletEnv(const fs::path& path, fs::path& database_filename) |
@@ -78,5 +121,90 @@ BOOST_AUTO_TEST_CASE(getwalletenv_g_dbenvs_free_instance) |
78 | 121 | BOOST_CHECK(env_2_a == env_2_b); |
79 | 122 | } |
80 | 123 |
|
| 124 | +static std::vector<std::unique_ptr<WalletDatabase>> TestDatabases(const fs::path& path_root) |
| 125 | +{ |
| 126 | + std::vector<std::unique_ptr<WalletDatabase>> dbs; |
| 127 | + DatabaseOptions options; |
| 128 | + DatabaseStatus status; |
| 129 | + bilingual_str error; |
| 130 | +#ifdef USE_BDB |
| 131 | + dbs.emplace_back(MakeBerkeleyDatabase(path_root / "bdb", options, status, error)); |
| 132 | +#endif |
| 133 | +#ifdef USE_SQLITE |
| 134 | + dbs.emplace_back(MakeSQLiteDatabase(path_root / "sqlite", options, status, error)); |
| 135 | +#endif |
| 136 | + dbs.emplace_back(CreateMockableWalletDatabase()); |
| 137 | + return dbs; |
| 138 | +} |
| 139 | + |
| 140 | +BOOST_AUTO_TEST_CASE(db_cursor_prefix_range_test) |
| 141 | +{ |
| 142 | + // Test each supported db |
| 143 | + for (const auto& database : TestDatabases(m_path_root)) { |
| 144 | + BOOST_ASSERT(database); |
| 145 | + |
| 146 | + std::vector<std::string> prefixes = {"", "FIRST", "SECOND", "P\xfe\xff", "P\xff\x01", "\xff\xff"}; |
| 147 | + |
| 148 | + // Write elements to it |
| 149 | + std::unique_ptr<DatabaseBatch> handler = database->MakeBatch(); |
| 150 | + for (unsigned int i = 0; i < 10; i++) { |
| 151 | + for (const auto& prefix : prefixes) { |
| 152 | + BOOST_CHECK(handler->Write(std::make_pair(prefix, i), i)); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // Now read all the items by prefix and verify that each element gets parsed correctly |
| 157 | + for (const auto& prefix : prefixes) { |
| 158 | + DataStream s_prefix; |
| 159 | + s_prefix << prefix; |
| 160 | + std::unique_ptr<DatabaseCursor> cursor = handler->GetNewPrefixCursor(s_prefix); |
| 161 | + DataStream key; |
| 162 | + DataStream value; |
| 163 | + for (int i = 0; i < 10; i++) { |
| 164 | + DatabaseCursor::Status status = cursor->Next(key, value); |
| 165 | + BOOST_ASSERT(status == DatabaseCursor::Status::MORE); |
| 166 | + |
| 167 | + std::string key_back; |
| 168 | + unsigned int i_back; |
| 169 | + key >> key_back >> i_back; |
| 170 | + BOOST_CHECK_EQUAL(key_back, prefix); |
| 171 | + |
| 172 | + unsigned int value_back; |
| 173 | + value >> value_back; |
| 174 | + BOOST_CHECK_EQUAL(value_back, i_back); |
| 175 | + } |
| 176 | + |
| 177 | + // Let's now read it once more, it should return DONE |
| 178 | + BOOST_CHECK(cursor->Next(key, value) == DatabaseCursor::Status::DONE); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +// Lower level DatabaseBase::GetNewPrefixCursor test, to cover cases that aren't |
| 184 | +// covered in the higher level test above. The higher level test uses |
| 185 | +// serialized strings which are prefixed with string length, so it doesn't test |
| 186 | +// truly empty prefixes or prefixes that begin with \xff |
| 187 | +BOOST_AUTO_TEST_CASE(db_cursor_prefix_byte_test) |
| 188 | +{ |
| 189 | + const MockableData::value_type |
| 190 | + e{StringData(""), StringData("e")}, |
| 191 | + p{StringData("prefix"), StringData("p")}, |
| 192 | + ps{StringData("prefixsuffix"), StringData("ps")}, |
| 193 | + f{StringData("\xff"), StringData("f")}, |
| 194 | + fs{StringData("\xffsuffix"), StringData("fs")}, |
| 195 | + ff{StringData("\xff\xff"), StringData("ff")}, |
| 196 | + ffs{StringData("\xff\xffsuffix"), StringData("ffs")}; |
| 197 | + for (const auto& database : TestDatabases(m_path_root)) { |
| 198 | + std::unique_ptr<DatabaseBatch> batch = database->MakeBatch(); |
| 199 | + for (const auto& [k, v] : {e, p, ps, f, fs, ff, ffs}) { |
| 200 | + batch->Write(MakeUCharSpan(k), MakeUCharSpan(v)); |
| 201 | + } |
| 202 | + CheckPrefix(*batch, StringBytes(""), {e, p, ps, f, fs, ff, ffs}); |
| 203 | + CheckPrefix(*batch, StringBytes("prefix"), {p, ps}); |
| 204 | + CheckPrefix(*batch, StringBytes("\xff"), {f, fs, ff, ffs}); |
| 205 | + CheckPrefix(*batch, StringBytes("\xff\xff"), {ff, ffs}); |
| 206 | + } |
| 207 | +} |
| 208 | + |
81 | 209 | BOOST_AUTO_TEST_SUITE_END() |
82 | 210 | } // namespace wallet |
0 commit comments