Branch data Line data Source code
1 : : // Copyright (c) 2012-present 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 <dbwrapper.h>
6 : : #include <test/util/common.h>
7 : : #include <test/util/random.h>
8 : : #include <test/util/setup_common.h>
9 : : #include <uint256.h>
10 : : #include <util/byte_units.h>
11 : : #include <util/string.h>
12 : :
13 : : #include <fstream>
14 : : #include <memory>
15 : : #include <ranges>
16 : :
17 : : #include <boost/test/unit_test.hpp>
18 : :
19 : : using util::ToString;
20 : :
21 : : BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup)
22 : :
23 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
24 : : {
25 : : // Perform tests both obfuscated and non-obfuscated.
26 [ + + ]: 3 : for (const bool obfuscate : {false, true}) {
27 : 2 : constexpr size_t CACHE_SIZE{1_MiB};
28 [ + - ]: 4 : const fs::path path{m_args.GetDataDirBase() / "dbwrapper"};
29 : :
30 : 2 : Obfuscation obfuscation;
31 : 2 : std::vector<std::pair<uint8_t, uint256>> key_values{};
32 : :
33 : : // Write values
34 : 2 : {
35 : 0 : CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .wipe_data = true, .obfuscate = obfuscate}};
36 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscate, !dbw.IsEmpty());
+ - ]
37 : :
38 : : // Ensure that we're doing real obfuscation when obfuscate=true
39 [ + - ]: 2 : obfuscation = dbwrapper_private::GetObfuscation(dbw);
40 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
+ - ]
41 : :
42 [ + + ]: 22 : for (uint8_t k{0}; k < 10; ++k) {
43 : 20 : uint8_t key{k};
44 : 20 : uint256 value{m_rng.rand256()};
45 [ + - ]: 20 : dbw.Write(key, value);
46 [ + - ]: 20 : key_values.emplace_back(key, value);
47 : : }
48 : 2 : }
49 : :
50 : : // Verify that the obfuscation key is never obfuscated
51 : 2 : {
52 : 0 : CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = false}};
53 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscation, dbwrapper_private::GetObfuscation(dbw));
+ - ]
54 : 2 : }
55 : :
56 : : // Read back the values
57 : 2 : {
58 : 0 : CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}};
59 : :
60 : : // Ensure obfuscation is read back correctly
61 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscation, dbwrapper_private::GetObfuscation(dbw));
+ - ]
62 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
+ - ]
63 : :
64 : : // Verify all written values
65 [ + - + + ]: 22 : for (const auto& [key, expected_value] : key_values) {
66 : 20 : uint256 read_value{};
67 [ + - + - : 40 : BOOST_CHECK(dbw.Read(key, read_value));
+ - + - ]
68 [ + - + - ]: 20 : BOOST_CHECK_EQUAL(read_value, expected_value);
69 : : }
70 : 2 : }
71 : 4 : }
72 [ + - + - : 7 : }
+ - + - +
- + - ]
73 : :
74 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
75 : : {
76 : : // Perform tests both obfuscated and non-obfuscated.
77 [ + + ]: 3 : for (bool obfuscate : {false, true}) {
78 [ + + + - ]: 5 : fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_1_obfuscate_true" : "dbwrapper_1_obfuscate_false");
79 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = false, .wipe_data = true, .obfuscate = obfuscate});
80 : :
81 : 2 : uint256 res;
82 : 2 : uint32_t res_uint_32;
83 : 2 : bool res_bool;
84 : :
85 : : // Ensure that we're doing real obfuscation when obfuscate=true
86 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
+ - ]
87 : :
88 : : //Simulate block raw data - "b + block hash"
89 [ + - + - ]: 2 : std::string key_block = "b" + m_rng.rand256().ToString();
90 : :
91 : 2 : uint256 in_block = m_rng.rand256();
92 [ + - ]: 2 : dbw.Write(key_block, in_block);
93 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_block, res));
+ - + - ]
94 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in_block.ToString());
+ - + - ]
95 : :
96 : : //Simulate file raw data - "f + file_number"
97 [ + - ]: 2 : std::string key_file = strprintf("f%04x", m_rng.rand32());
98 : :
99 : 2 : uint256 in_file_info = m_rng.rand256();
100 [ + - ]: 2 : dbw.Write(key_file, in_file_info);
101 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_file, res));
+ - + - ]
102 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in_file_info.ToString());
+ - + - ]
103 : :
104 : : //Simulate transaction raw data - "t + transaction hash"
105 [ + - + - ]: 2 : std::string key_transaction = "t" + m_rng.rand256().ToString();
106 : :
107 : 2 : uint256 in_transaction = m_rng.rand256();
108 [ + - ]: 2 : dbw.Write(key_transaction, in_transaction);
109 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_transaction, res));
+ - + - ]
110 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in_transaction.ToString());
+ - + - ]
111 : :
112 : : //Simulate UTXO raw data - "c + transaction hash"
113 [ + - + - ]: 2 : std::string key_utxo = "c" + m_rng.rand256().ToString();
114 : :
115 : 2 : uint256 in_utxo = m_rng.rand256();
116 [ + - ]: 2 : dbw.Write(key_utxo, in_utxo);
117 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_utxo, res));
+ - + - ]
118 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in_utxo.ToString());
+ - + - ]
119 : :
120 : : //Simulate last block file number - "l"
121 : 2 : uint8_t key_last_blockfile_number{'l'};
122 : 2 : uint32_t lastblockfilenumber = m_rng.rand32();
123 [ + - ]: 2 : dbw.Write(key_last_blockfile_number, lastblockfilenumber);
124 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_last_blockfile_number, res_uint_32));
+ - + - ]
125 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(lastblockfilenumber, res_uint_32);
126 : :
127 : : //Simulate Is Reindexing - "R"
128 : 2 : uint8_t key_IsReindexing{'R'};
129 : 2 : bool isInReindexing = m_rng.randbool();
130 [ + - ]: 2 : dbw.Write(key_IsReindexing, isInReindexing);
131 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_IsReindexing, res_bool));
+ - + - ]
132 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(isInReindexing, res_bool);
133 : :
134 : : //Simulate last block hash up to which UXTO covers - 'B'
135 : 2 : uint8_t key_lastblockhash_uxto{'B'};
136 : 2 : uint256 lastblock_hash = m_rng.rand256();
137 [ + - ]: 2 : dbw.Write(key_lastblockhash_uxto, lastblock_hash);
138 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_lastblockhash_uxto, res));
+ - + - ]
139 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(lastblock_hash, res);
140 : :
141 : : //Simulate file raw data - "F + filename_number + filename"
142 [ + - ]: 2 : std::string file_option_tag = "F";
143 : 2 : uint8_t filename_length = m_rng.randbits(8);
144 [ + - ]: 2 : std::string filename = "randomfilename";
145 [ + - ]: 2 : std::string key_file_option = strprintf("%s%01x%s", file_option_tag, filename_length, filename);
146 : :
147 : 2 : bool in_file_bool = m_rng.randbool();
148 [ + - ]: 2 : dbw.Write(key_file_option, in_file_bool);
149 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_file_option, res_bool));
+ - + - ]
150 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(res_bool, in_file_bool);
151 : 4 : }
152 [ + - + - ]: 3 : }
153 : :
154 : : // Test batch operations
155 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper_batch)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
156 : : {
157 : : // Perform tests both obfuscated and non-obfuscated.
158 [ + + ]: 3 : for (const bool obfuscate : {false, true}) {
159 [ + + + - ]: 5 : fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_batch_obfuscate_true" : "dbwrapper_batch_obfuscate_false");
160 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
161 : :
162 : 2 : uint8_t key{'i'};
163 : 2 : uint256 in = m_rng.rand256();
164 : 2 : uint8_t key2{'j'};
165 : 2 : uint256 in2 = m_rng.rand256();
166 : 2 : uint8_t key3{'k'};
167 : 2 : uint256 in3 = m_rng.rand256();
168 : :
169 : 2 : uint256 res;
170 [ + - ]: 2 : CDBBatch batch(dbw);
171 : :
172 [ + - ]: 2 : batch.Write(key, in);
173 [ + - ]: 2 : batch.Write(key2, in2);
174 [ + - ]: 2 : batch.Write(key3, in3);
175 : :
176 : : // Remove key3 before it's even been written
177 [ + - ]: 2 : batch.Erase(key3);
178 : :
179 [ + - ]: 2 : dbw.WriteBatch(batch);
180 : :
181 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key, res));
+ - + - ]
182 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
+ - + - ]
183 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key2, res));
+ - + - ]
184 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in2.ToString());
+ - + - ]
185 : :
186 : : // key3 should've never been written
187 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key3, res) == false);
+ - + - ]
188 : :
189 [ + - ]: 2 : batch.Clear();
190 [ + - ]: 2 : batch.Write(key3, in3);
191 [ + - ]: 2 : dbw.WriteBatch(batch);
192 : :
193 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key3, res));
+ - + - ]
194 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in3.ToString());
+ - + - ]
195 : 4 : }
196 [ + - + - ]: 3 : }
197 : :
198 : : // Verify that Read() returns false (without throwing) when the stored value
199 : : // fails to be deserialized
200 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper_read_returns_false_on_deserialization_error)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
201 : : {
202 [ + + ]: 3 : for (const bool obfuscate : {false, true}) {
203 [ + + + - ]: 5 : const fs::path path{m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_deser_obf" : "dbwrapper_deser_noobf")};
204 : 0 : CDBWrapper dbw({.path = path, .cache_bytes = 1 << 20, .wipe_data = true, .obfuscate = obfuscate});
205 : :
206 : 2 : constexpr uint8_t key{'X'};
207 : :
208 : : // Write a single byte. uint256 requires 32 bytes, so reading this key
209 : : // as uint256 must trigger a deserialization error inside Read()
210 [ + - ]: 2 : dbw.Write(key, uint8_t{0xFF});
211 [ + - + - : 4 : BOOST_CHECK(dbw.Exists(key));
+ - + - ]
212 : :
213 : : // Read() must catch the deserialization exception and return false,
214 : : // the same as if the key were absent
215 : 2 : uint256 result;
216 [ + - + - : 4 : BOOST_CHECK(!dbw.Read(key, result));
+ - ]
217 : 4 : }
218 [ + - + - ]: 3 : }
219 : :
220 : : // Verify Read() throws dbwrapper_error due to an internal db error
221 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper_read_throws_on_db_error)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
222 : : {
223 [ + - ]: 2 : const fs::path path{m_args.GetDataDirBase() / "dbwrapper_db_error"};
224 : 1 : constexpr uint8_t key{'Y'};
225 : :
226 : 4 : const auto make_db = [] (const fs::path& path, const bool force_compact) {
227 : 3 : return CDBWrapper({.path = path, .cache_bytes = 1 << 20, .obfuscate = false,
228 : 3 : .options = {.force_compact = force_compact}});
229 [ + - ]: 3 : };
230 : :
231 : : // Write a value and close the database
232 [ + - + - ]: 1 : make_db(path, /*force_compact=*/false).Write(key, m_rng.rand256());
233 : :
234 : : // Force compaction to ensure the data is written into the .ldb files
235 : : // rather than left in the WAL.
236 [ + - ]: 1 : (void)make_db(path, /*force_compact=*/true);
237 : :
238 : : // Corrupt every table so any subsequent Read() fails
239 [ + - + - : 9 : for (const auto& entry : fs::directory_iterator(path)) {
+ - + - +
- + + -
+ ]
240 [ + - + - : 20 : if (entry.path().extension() == ".ldb") {
+ + ]
241 [ + - ]: 2 : std::ofstream{entry.path(), std::ios::binary | std::ios::trunc}
242 [ + - ]: 1 : .write("\xff", 1);
243 : : }
244 [ - + + - : 1 : }
- - ]
245 : :
246 : : // Read() should detect the issue now and throw
247 [ + - ]: 1 : const auto db{make_db(path, /*force_compact=*/false)};
248 : 1 : uint256 result;
249 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(db.Read(key, result), dbwrapper_error, HasReason("Fatal LevelDB error"));
- - - - -
+ + - + -
+ - + - ]
250 : :
251 : : // TryRead() must return DatabaseError (without throwing).
252 [ + - ]: 1 : CDBWrapper::ReadStatus status = db.TryRead(key, result);
253 [ + - + - : 2 : BOOST_REQUIRE(!status);
+ - ]
254 [ + - + - : 2 : BOOST_CHECK(status.error().status == CDBWrapper::ReadFailure::Code::DatabaseError);
+ - ]
255 [ + - + - ]: 2 : BOOST_CHECK(status.error().err_msg.find("Fatal LevelDB error") != std::string::npos);
256 : 2 : }
257 : :
258 : : // Exercise TryRead() return values directly: found, absent and DeserializationError.
259 : : // DatabaseError is tested inside 'dbwrapper_read_throws_on_db_error' test
260 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper_tryread)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
261 : : {
262 [ + + ]: 3 : for (const bool obfuscate : {false, true}) {
263 [ + + + - ]: 5 : const fs::path path{m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_tryread_obf" : "dbwrapper_tryread_noobf")};
264 : 0 : CDBWrapper dbw({.path = path, .cache_bytes = 1 << 20, .wipe_data = true, .obfuscate = obfuscate});
265 : :
266 : 2 : constexpr uint8_t key_ok{'A'};
267 : 2 : constexpr uint8_t key_missing{'B'};
268 : 2 : constexpr uint8_t key_bad{'C'};
269 : :
270 : 2 : uint256 written_value{m_rng.rand256()};
271 [ + - ]: 2 : dbw.Write(key_ok, written_value);
272 [ + - ]: 2 : dbw.Write(key_bad, uint8_t{0xFF});
273 : :
274 : : // Found: key exists, value deserializes correctly
275 : 2 : {
276 : 2 : uint256 read_value;
277 [ + - ]: 2 : CDBWrapper::ReadStatus status = dbw.TryRead(key_ok, read_value);
278 [ + - + - : 4 : BOOST_REQUIRE(status);
+ - ]
279 [ + - + - : 4 : BOOST_CHECK(status.value());
+ - + - ]
280 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(read_value, written_value);
281 : 0 : }
282 : :
283 : : // Absent: key does not exist
284 : 2 : {
285 : 2 : uint256 read_value;
286 [ + - ]: 2 : CDBWrapper::ReadStatus status = dbw.TryRead(key_missing, read_value);
287 [ + - + - : 4 : BOOST_REQUIRE(status);
+ - ]
288 [ + - + - : 4 : BOOST_CHECK(!status.value());
+ - ]
289 : 0 : }
290 : :
291 : : // DeserializationError: key exists but stored value is too short
292 : 2 : {
293 : 2 : uint256 read_value;
294 [ + - ]: 2 : CDBWrapper::ReadStatus status = dbw.TryRead(key_bad, read_value);
295 [ + - + - : 4 : BOOST_REQUIRE(!status);
+ - ]
296 [ + - + - : 4 : BOOST_CHECK(status.error().status == CDBWrapper::ReadFailure::Code::DeserializationError);
+ - ]
297 [ + - + - ]: 4 : BOOST_CHECK(!status.error().err_msg.empty());
298 : 0 : }
299 : 4 : }
300 [ + - + - ]: 3 : }
301 : :
302 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
303 : : {
304 : : // Perform tests both obfuscated and non-obfuscated.
305 [ + + ]: 3 : for (const bool obfuscate : {false, true}) {
306 [ + + + - ]: 5 : fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_iterator_obfuscate_true" : "dbwrapper_iterator_obfuscate_false");
307 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
308 : :
309 : : // The two keys are intentionally chosen for ordering
310 : 2 : uint8_t key{'j'};
311 : 2 : uint256 in = m_rng.rand256();
312 [ + - ]: 2 : dbw.Write(key, in);
313 : 2 : uint8_t key2{'k'};
314 : 2 : uint256 in2 = m_rng.rand256();
315 [ + - ]: 2 : dbw.Write(key2, in2);
316 : :
317 [ + - + - ]: 2 : std::unique_ptr<CDBIterator> it(dbw.NewIterator());
318 : :
319 : : // Be sure to seek past the obfuscation key (if it exists)
320 [ + - ]: 2 : it->Seek(key);
321 : :
322 : : // A failed key decode must not consume the current iterator entry.
323 : 2 : uint16_t key_too_large{0};
324 [ + - + - : 4 : BOOST_CHECK(!it->GetKey(key_too_large));
+ - + - ]
325 : :
326 : 2 : uint8_t key_res;
327 : :
328 [ + - + - : 4 : BOOST_REQUIRE(it->GetKey(key_res));
+ - + - ]
329 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(key_res, key);
330 : : // A failed value decode must not leave the iterator's scratch stream dirty.
331 : 2 : std::pair<uint256, uint8_t> value_too_large;
332 [ + - + - : 4 : BOOST_CHECK(!it->GetValue(value_too_large));
+ - + - ]
333 : :
334 : 2 : uint256 val_res;
335 [ + - + - : 4 : BOOST_REQUIRE(it->GetValue(val_res));
+ - + - ]
336 [ + - + - : 2 : BOOST_CHECK_EQUAL(val_res.ToString(), in.ToString());
+ - + - ]
337 : :
338 [ + - ]: 2 : it->Seek(key2);
339 : :
340 [ + - + - : 4 : BOOST_REQUIRE(it->GetKey(key_res));
+ - + - ]
341 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(key_res, key2);
342 [ + - + - : 4 : BOOST_REQUIRE(it->GetValue(val_res));
+ - + - ]
343 [ + - + - : 2 : BOOST_CHECK_EQUAL(val_res.ToString(), in2.ToString());
+ - + - ]
344 : :
345 [ + - ]: 2 : it->Seek(key);
346 : :
347 [ + - + - : 4 : BOOST_REQUIRE(it->GetKey(key_res));
+ - + - ]
348 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(key_res, key);
349 [ + - + - : 4 : BOOST_REQUIRE(it->GetValue(val_res));
+ - + - ]
350 [ + - + - : 2 : BOOST_CHECK_EQUAL(val_res.ToString(), in.ToString());
+ - + - ]
351 : :
352 [ + - ]: 2 : it->Next();
353 : :
354 [ + - + - : 4 : BOOST_REQUIRE(it->GetKey(key_res));
+ - + - ]
355 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(key_res, key2);
356 [ + - + - : 4 : BOOST_REQUIRE(it->GetValue(val_res));
+ - + - ]
357 [ + - + - : 2 : BOOST_CHECK_EQUAL(val_res.ToString(), in2.ToString());
+ - + - ]
358 : :
359 [ + - ]: 2 : it->Next();
360 [ + - + - : 2 : BOOST_CHECK_EQUAL(it->Valid(), false);
+ - ]
361 : 4 : }
362 [ + - + - ]: 3 : }
363 : :
364 : : // Test that we do not obfuscation if there is existing data.
365 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
366 : : {
367 : : // We're going to share this fs::path between two wrappers
368 [ + - ]: 2 : fs::path ph = m_args.GetDataDirBase() / "existing_data_no_obfuscate";
369 [ + - ]: 1 : fs::create_directories(ph);
370 : :
371 : : // Set up a non-obfuscated wrapper to write some initial data.
372 [ + - + - ]: 1 : std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(DBParams{.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = false});
373 : 1 : uint8_t key{'k'};
374 : 1 : uint256 in = m_rng.rand256();
375 : 1 : uint256 res;
376 : :
377 [ + - ]: 1 : dbw->Write(key, in);
378 [ + - + - : 2 : BOOST_CHECK(dbw->Read(key, res));
+ - + - ]
379 [ + - + - : 1 : BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
+ - + - ]
380 : :
381 : : // Call the destructor to free leveldb LOCK
382 [ + - ]: 1 : dbw.reset();
383 : :
384 : : // Now, set up another wrapper that wants to obfuscate the same directory
385 : 0 : CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = true});
386 : :
387 : : // Check that the key/val we wrote with unobfuscated wrapper exists and
388 : : // is readable.
389 : 1 : uint256 res2;
390 [ + - + - : 2 : BOOST_CHECK(odbw.Read(key, res2));
+ - + - ]
391 [ + - + - : 1 : BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
+ - + - ]
392 : :
393 [ + - + - : 2 : BOOST_CHECK(!odbw.IsEmpty());
+ - + - ]
394 [ + - + - : 2 : BOOST_CHECK(!dbwrapper_private::GetObfuscation(odbw)); // The key should be an empty string
+ - ]
395 : :
396 : 1 : uint256 in2 = m_rng.rand256();
397 : 1 : uint256 res3;
398 : :
399 : : // Check that we can write successfully
400 [ + - ]: 1 : odbw.Write(key, in2);
401 [ + - + - : 2 : BOOST_CHECK(odbw.Read(key, res3));
+ - + - ]
402 [ + - + - : 1 : BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
+ - + - ]
403 [ + - + - ]: 3 : }
404 : :
405 : : // Ensure that we start obfuscating during a reindex.
406 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(existing_data_reindex)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
407 : : {
408 : : // We're going to share this fs::path between two wrappers
409 [ + - ]: 2 : fs::path ph = m_args.GetDataDirBase() / "existing_data_reindex";
410 [ + - ]: 1 : fs::create_directories(ph);
411 : :
412 : : // Set up a non-obfuscated wrapper to write some initial data.
413 [ + - + - ]: 1 : std::unique_ptr<CDBWrapper> dbw = std::make_unique<CDBWrapper>(DBParams{.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = false});
414 : 1 : uint8_t key{'k'};
415 : 1 : uint256 in = m_rng.rand256();
416 : 1 : uint256 res;
417 : :
418 [ + - ]: 1 : dbw->Write(key, in);
419 [ + - + - : 2 : BOOST_CHECK(dbw->Read(key, res));
+ - + - ]
420 [ + - + - : 1 : BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
+ - + - ]
421 : :
422 : : // Call the destructor to free leveldb LOCK
423 [ + - ]: 1 : dbw.reset();
424 : :
425 : : // Simulate a -reindex by wiping the existing data store
426 : 0 : CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = true, .obfuscate = true});
427 : :
428 : : // Check that the key/val we wrote with unobfuscated wrapper doesn't exist
429 : 1 : uint256 res2;
430 [ + - + - : 2 : BOOST_CHECK(!odbw.Read(key, res2));
+ - + - ]
431 [ + - + - : 2 : BOOST_CHECK(dbwrapper_private::GetObfuscation(odbw));
+ - ]
432 : :
433 : 1 : uint256 in2 = m_rng.rand256();
434 : 1 : uint256 res3;
435 : :
436 : : // Check that we can write successfully
437 [ + - ]: 1 : odbw.Write(key, in2);
438 [ + - + - : 2 : BOOST_CHECK(odbw.Read(key, res3));
+ - + - ]
439 [ + - + - : 1 : BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
+ - + - ]
440 [ + - + - ]: 3 : }
441 : :
442 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(iterator_ordering)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
443 : : {
444 [ + - ]: 2 : fs::path ph = m_args.GetDataDirBase() / "iterator_ordering";
445 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = false});
446 [ + + ]: 257 : for (int x=0x00; x<256; ++x) {
447 : 256 : uint8_t key = x;
448 : 256 : uint32_t value = x*x;
449 [ + + + - ]: 256 : if (!(x & 1)) dbw.Write(key, value);
450 : : }
451 : :
452 : : // Check that creating an iterator creates a snapshot
453 [ + - ]: 1 : std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
454 : :
455 [ + + ]: 257 : for (unsigned int x=0x00; x<256; ++x) {
456 : 256 : uint8_t key = x;
457 : 256 : uint32_t value = x*x;
458 [ + + + - ]: 256 : if (x & 1) dbw.Write(key, value);
459 : : }
460 : :
461 [ + + ]: 3 : for (const int seek_start : {0x00, 0x80}) {
462 [ + - ]: 2 : it->Seek((uint8_t)seek_start);
463 [ + + ]: 384 : for (unsigned int x=seek_start; x<255; ++x) {
464 : 382 : uint8_t key;
465 : 382 : uint32_t value;
466 [ + - + - : 764 : BOOST_CHECK(it->Valid());
+ - + - ]
467 [ + - + - ]: 382 : if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
468 : : break;
469 [ + - + - : 764 : BOOST_CHECK(it->GetKey(key));
+ - + + ]
470 [ + + ]: 382 : if (x & 1) {
471 [ + - + - ]: 190 : BOOST_CHECK_EQUAL(key, x + 1);
472 : 190 : continue;
473 : : }
474 [ + - + - : 384 : BOOST_CHECK(it->GetValue(value));
+ - + - ]
475 [ + - + - ]: 192 : BOOST_CHECK_EQUAL(key, x);
476 [ + - + - ]: 192 : BOOST_CHECK_EQUAL(value, x*x);
477 [ + - ]: 192 : it->Next();
478 : : }
479 [ + - + - : 4 : BOOST_CHECK(!it->Valid());
+ - ]
480 : : }
481 [ + - + - ]: 3 : }
482 : :
483 : 252 : struct StringContentsSerializer {
484 : : // Used to make two serialized objects the same while letting them have different lengths
485 : : // This is a terrible idea
486 : : std::string str;
487 : 150 : StringContentsSerializer() = default;
488 [ - + ]: 104 : explicit StringContentsSerializer(const std::string& inp) : str(inp) {}
489 : :
490 : : template<typename Stream>
491 : 102 : void Serialize(Stream& s) const
492 : : {
493 [ - + + + ]: 10334 : for (size_t i = 0; i < str.size(); i++) {
494 : 10232 : s << uint8_t(str[i]);
495 : : }
496 : 102 : }
497 : :
498 : : template<typename Stream>
499 : 150 : void Unserialize(Stream& s)
500 : : {
501 : 150 : str.clear();
502 : 150 : uint8_t c{0};
503 [ + + ]: 15495 : while (!s.empty()) {
504 : 15345 : s >> c;
505 : 15345 : str.push_back(c);
506 : : }
507 : 150 : }
508 : : };
509 : :
510 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(iterator_string_ordering)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
511 : : {
512 [ + - ]: 2 : fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering";
513 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = false});
514 [ + + ]: 11 : for (int x = 0; x < 10; ++x) {
515 [ + + ]: 110 : for (int y = 0; y < 10; ++y) {
516 [ + - ]: 100 : std::string key{ToString(x)};
517 [ + + ]: 550 : for (int z = 0; z < y; ++z)
518 [ - + ]: 900 : key += key;
519 : 100 : uint32_t value = x*x;
520 [ - + + - ]: 300 : dbw.Write(StringContentsSerializer{key}, value);
521 : 100 : }
522 : : }
523 : :
524 [ + - ]: 1 : std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
525 [ + + ]: 3 : for (const int seek_start : {0, 5}) {
526 [ + - + - ]: 6 : it->Seek(StringContentsSerializer{ToString(seek_start)});
527 [ + + ]: 17 : for (unsigned int x = seek_start; x < 10; ++x) {
528 [ + + ]: 165 : for (int y = 0; y < 10; ++y) {
529 [ + - ]: 150 : std::string exp_key{ToString(x)};
530 [ + + ]: 825 : for (int z = 0; z < y; ++z)
531 [ - + ]: 1350 : exp_key += exp_key;
532 [ + - ]: 150 : StringContentsSerializer key;
533 : 150 : uint32_t value;
534 [ + - + - : 300 : BOOST_CHECK(it->Valid());
+ - + - ]
535 [ + - + - ]: 150 : if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
536 : : break;
537 [ + - + - : 300 : BOOST_CHECK(it->GetKey(key));
+ - + - ]
538 [ + - + - : 300 : BOOST_CHECK(it->GetValue(value));
+ - + - ]
539 [ + - + - ]: 150 : BOOST_CHECK_EQUAL(key.str, exp_key);
540 [ + - + - ]: 150 : BOOST_CHECK_EQUAL(value, x*x);
541 [ + - ]: 150 : it->Next();
542 : 150 : }
543 : : }
544 [ + - + - : 4 : BOOST_CHECK(!it->Valid());
+ - ]
545 : : }
546 [ + - + - ]: 3 : }
547 : :
548 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(unicodepath)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
549 : : {
550 : : // Attempt to create a database with a UTF8 character in the path.
551 : : // On Windows this test will fail if the directory is created using
552 : : // the ANSI CreateDirectoryA call and the code page isn't UTF8.
553 : : // It will succeed if created with CreateDirectoryW.
554 [ + - ]: 2 : fs::path ph = m_args.GetDataDirBase() / "test_runner_₿_🏃_20191128_104644";
555 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB});
556 : :
557 [ + - + - ]: 2 : fs::path lockPath = ph / "LOCK";
558 [ + - + - : 2 : BOOST_CHECK(fs::exists(lockPath));
+ - ]
559 [ + - + - ]: 3 : }
560 : :
561 : :
562 : : BOOST_AUTO_TEST_SUITE_END()
|