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 <memory>
14 : : #include <ranges>
15 : :
16 : : #include <boost/test/unit_test.hpp>
17 : :
18 : : using util::ToString;
19 : :
20 : : BOOST_FIXTURE_TEST_SUITE(dbwrapper_tests, BasicTestingSetup)
21 : :
22 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
23 : : {
24 : : // Perform tests both obfuscated and non-obfuscated.
25 [ + + ]: 3 : for (const bool obfuscate : {false, true}) {
26 : 2 : constexpr size_t CACHE_SIZE{1_MiB};
27 [ + - ]: 4 : const fs::path path{m_args.GetDataDirBase() / "dbwrapper"};
28 : :
29 : 2 : Obfuscation obfuscation;
30 : 2 : std::vector<std::pair<uint8_t, uint256>> key_values{};
31 : :
32 : : // Write values
33 : 2 : {
34 : 4 : CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .wipe_data = true, .obfuscate = obfuscate}};
35 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscate, !dbw.IsEmpty());
+ - ]
36 : :
37 : : // Ensure that we're doing real obfuscation when obfuscate=true
38 [ + - ]: 2 : obfuscation = dbwrapper_private::GetObfuscation(dbw);
39 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
+ - ]
40 : :
41 [ + + ]: 22 : for (uint8_t k{0}; k < 10; ++k) {
42 : 20 : uint8_t key{k};
43 : 20 : uint256 value{m_rng.rand256()};
44 [ + - ]: 20 : dbw.Write(key, value);
45 [ + - ]: 20 : key_values.emplace_back(key, value);
46 : : }
47 : 2 : }
48 : :
49 : : // Verify that the obfuscation key is never obfuscated
50 : 2 : {
51 : 0 : CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = false}};
52 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscation, dbwrapper_private::GetObfuscation(dbw));
+ - ]
53 : 2 : }
54 : :
55 : : // Read back the values
56 : 2 : {
57 : 0 : CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}};
58 : :
59 : : // Ensure obfuscation is read back correctly
60 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscation, dbwrapper_private::GetObfuscation(dbw));
+ - ]
61 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
+ - ]
62 : :
63 : : // Verify all written values
64 [ + - + + ]: 22 : for (const auto& [key, expected_value] : key_values) {
65 : 20 : uint256 read_value{};
66 [ + - + - : 40 : BOOST_CHECK(dbw.Read(key, read_value));
+ - + - ]
67 [ + - + - ]: 20 : BOOST_CHECK_EQUAL(read_value, expected_value);
68 : : }
69 : 2 : }
70 : 4 : }
71 [ + - + - : 7 : }
+ - + - +
- + - ]
72 : :
73 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
74 : : {
75 : : // Perform tests both obfuscated and non-obfuscated.
76 [ + + ]: 3 : for (bool obfuscate : {false, true}) {
77 [ + + + - ]: 5 : fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_1_obfuscate_true" : "dbwrapper_1_obfuscate_false");
78 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = false, .wipe_data = true, .obfuscate = obfuscate});
79 : :
80 : 2 : uint256 res;
81 : 2 : uint32_t res_uint_32;
82 : 2 : bool res_bool;
83 : :
84 : : // Ensure that we're doing real obfuscation when obfuscate=true
85 [ + - + - : 2 : BOOST_CHECK_EQUAL(obfuscate, dbwrapper_private::GetObfuscation(dbw));
+ - ]
86 : :
87 : : //Simulate block raw data - "b + block hash"
88 [ + - + - ]: 2 : std::string key_block = "b" + m_rng.rand256().ToString();
89 : :
90 : 2 : uint256 in_block = m_rng.rand256();
91 [ + - ]: 2 : dbw.Write(key_block, in_block);
92 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_block, res));
+ - + - ]
93 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in_block.ToString());
+ - + - ]
94 : :
95 : : //Simulate file raw data - "f + file_number"
96 [ + - ]: 2 : std::string key_file = strprintf("f%04x", m_rng.rand32());
97 : :
98 : 2 : uint256 in_file_info = m_rng.rand256();
99 [ + - ]: 2 : dbw.Write(key_file, in_file_info);
100 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_file, res));
+ - + - ]
101 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in_file_info.ToString());
+ - + - ]
102 : :
103 : : //Simulate transaction raw data - "t + transaction hash"
104 [ + - + - ]: 2 : std::string key_transaction = "t" + m_rng.rand256().ToString();
105 : :
106 : 2 : uint256 in_transaction = m_rng.rand256();
107 [ + - ]: 2 : dbw.Write(key_transaction, in_transaction);
108 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_transaction, res));
+ - + - ]
109 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in_transaction.ToString());
+ - + - ]
110 : :
111 : : //Simulate UTXO raw data - "c + transaction hash"
112 [ + - + - ]: 2 : std::string key_utxo = "c" + m_rng.rand256().ToString();
113 : :
114 : 2 : uint256 in_utxo = m_rng.rand256();
115 [ + - ]: 2 : dbw.Write(key_utxo, in_utxo);
116 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_utxo, res));
+ - + - ]
117 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in_utxo.ToString());
+ - + - ]
118 : :
119 : : //Simulate last block file number - "l"
120 : 2 : uint8_t key_last_blockfile_number{'l'};
121 : 2 : uint32_t lastblockfilenumber = m_rng.rand32();
122 [ + - ]: 2 : dbw.Write(key_last_blockfile_number, lastblockfilenumber);
123 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_last_blockfile_number, res_uint_32));
+ - + - ]
124 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(lastblockfilenumber, res_uint_32);
125 : :
126 : : //Simulate Is Reindexing - "R"
127 : 2 : uint8_t key_IsReindexing{'R'};
128 : 2 : bool isInReindexing = m_rng.randbool();
129 [ + - ]: 2 : dbw.Write(key_IsReindexing, isInReindexing);
130 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_IsReindexing, res_bool));
+ - + - ]
131 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(isInReindexing, res_bool);
132 : :
133 : : //Simulate last block hash up to which UXTO covers - 'B'
134 : 2 : uint8_t key_lastblockhash_uxto{'B'};
135 : 2 : uint256 lastblock_hash = m_rng.rand256();
136 [ + - ]: 2 : dbw.Write(key_lastblockhash_uxto, lastblock_hash);
137 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_lastblockhash_uxto, res));
+ - + - ]
138 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(lastblock_hash, res);
139 : :
140 : : //Simulate file raw data - "F + filename_number + filename"
141 [ + - ]: 2 : std::string file_option_tag = "F";
142 : 2 : uint8_t filename_length = m_rng.randbits(8);
143 [ + - ]: 2 : std::string filename = "randomfilename";
144 [ + - ]: 2 : std::string key_file_option = strprintf("%s%01x%s", file_option_tag, filename_length, filename);
145 : :
146 : 2 : bool in_file_bool = m_rng.randbool();
147 [ + - ]: 2 : dbw.Write(key_file_option, in_file_bool);
148 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key_file_option, res_bool));
+ - + - ]
149 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(res_bool, in_file_bool);
150 : 4 : }
151 [ + - + - ]: 3 : }
152 : :
153 : : // Test batch operations
154 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper_batch)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
155 : : {
156 : : // Perform tests both obfuscated and non-obfuscated.
157 [ + + ]: 3 : for (const bool obfuscate : {false, true}) {
158 [ + + + - ]: 5 : fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_batch_obfuscate_true" : "dbwrapper_batch_obfuscate_false");
159 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
160 : :
161 : 2 : uint8_t key{'i'};
162 : 2 : uint256 in = m_rng.rand256();
163 : 2 : uint8_t key2{'j'};
164 : 2 : uint256 in2 = m_rng.rand256();
165 : 2 : uint8_t key3{'k'};
166 : 2 : uint256 in3 = m_rng.rand256();
167 : :
168 : 2 : uint256 res;
169 [ + - ]: 2 : CDBBatch batch(dbw);
170 : :
171 [ + - ]: 2 : batch.Write(key, in);
172 [ + - ]: 2 : batch.Write(key2, in2);
173 [ + - ]: 2 : batch.Write(key3, in3);
174 : :
175 : : // Remove key3 before it's even been written
176 [ + - ]: 2 : batch.Erase(key3);
177 : :
178 [ + - ]: 2 : dbw.WriteBatch(batch);
179 : :
180 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key, res));
+ - + - ]
181 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
+ - + - ]
182 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key2, res));
+ - + - ]
183 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in2.ToString());
+ - + - ]
184 : :
185 : : // key3 should've never been written
186 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key3, res) == false);
+ - + - ]
187 : :
188 [ + - ]: 2 : batch.Clear();
189 [ + - ]: 2 : batch.Write(key3, in3);
190 [ + - ]: 2 : dbw.WriteBatch(batch);
191 : :
192 [ + - + - : 4 : BOOST_CHECK(dbw.Read(key3, res));
+ - + - ]
193 [ + - + - : 2 : BOOST_CHECK_EQUAL(res.ToString(), in3.ToString());
+ - + - ]
194 : 4 : }
195 [ + - + - ]: 3 : }
196 : :
197 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(dbwrapper_iterator)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
198 : : {
199 : : // Perform tests both obfuscated and non-obfuscated.
200 [ + + ]: 3 : for (const bool obfuscate : {false, true}) {
201 [ + + + - ]: 5 : fs::path ph = m_args.GetDataDirBase() / (obfuscate ? "dbwrapper_iterator_obfuscate_true" : "dbwrapper_iterator_obfuscate_false");
202 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = obfuscate});
203 : :
204 : : // The two keys are intentionally chosen for ordering
205 : 2 : uint8_t key{'j'};
206 : 2 : uint256 in = m_rng.rand256();
207 [ + - ]: 2 : dbw.Write(key, in);
208 : 2 : uint8_t key2{'k'};
209 : 2 : uint256 in2 = m_rng.rand256();
210 [ + - ]: 2 : dbw.Write(key2, in2);
211 : :
212 [ + - + - ]: 2 : std::unique_ptr<CDBIterator> it(dbw.NewIterator());
213 : :
214 : : // Be sure to seek past the obfuscation key (if it exists)
215 [ + - ]: 2 : it->Seek(key);
216 : :
217 : : // A failed key decode must not consume the current iterator entry.
218 : 2 : uint16_t key_too_large{0};
219 [ + - + - : 4 : BOOST_CHECK(!it->GetKey(key_too_large));
+ - + - ]
220 : :
221 : 2 : uint8_t key_res;
222 : :
223 [ + - + - : 4 : BOOST_REQUIRE(it->GetKey(key_res));
+ - + - ]
224 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(key_res, key);
225 : : // A failed value decode must not leave the iterator's scratch stream dirty.
226 : 2 : std::pair<uint256, uint8_t> value_too_large;
227 [ + - + - : 4 : BOOST_CHECK(!it->GetValue(value_too_large));
+ - + - ]
228 : :
229 : 2 : uint256 val_res;
230 [ + - + - : 4 : BOOST_REQUIRE(it->GetValue(val_res));
+ - + - ]
231 [ + - + - : 2 : BOOST_CHECK_EQUAL(val_res.ToString(), in.ToString());
+ - + - ]
232 : :
233 [ + - ]: 2 : it->Seek(key2);
234 : :
235 [ + - + - : 4 : BOOST_REQUIRE(it->GetKey(key_res));
+ - + - ]
236 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(key_res, key2);
237 [ + - + - : 4 : BOOST_REQUIRE(it->GetValue(val_res));
+ - + - ]
238 [ + - + - : 2 : BOOST_CHECK_EQUAL(val_res.ToString(), in2.ToString());
+ - + - ]
239 : :
240 [ + - ]: 2 : it->Seek(key);
241 : :
242 [ + - + - : 4 : BOOST_REQUIRE(it->GetKey(key_res));
+ - + - ]
243 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(key_res, key);
244 [ + - + - : 4 : BOOST_REQUIRE(it->GetValue(val_res));
+ - + - ]
245 [ + - + - : 2 : BOOST_CHECK_EQUAL(val_res.ToString(), in.ToString());
+ - + - ]
246 : :
247 [ + - ]: 2 : it->Next();
248 : :
249 [ + - + - : 4 : BOOST_REQUIRE(it->GetKey(key_res));
+ - + - ]
250 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(key_res, key2);
251 [ + - + - : 4 : BOOST_REQUIRE(it->GetValue(val_res));
+ - + - ]
252 [ + - + - : 2 : BOOST_CHECK_EQUAL(val_res.ToString(), in2.ToString());
+ - + - ]
253 : :
254 [ + - ]: 2 : it->Next();
255 [ + - + - : 2 : BOOST_CHECK_EQUAL(it->Valid(), false);
+ - ]
256 : 4 : }
257 [ + - + - ]: 3 : }
258 : :
259 : : // Test that we do not obfuscation if there is existing data.
260 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
261 : : {
262 : : // We're going to share this fs::path between two wrappers
263 [ + - ]: 2 : fs::path ph = m_args.GetDataDirBase() / "existing_data_no_obfuscate";
264 [ + - ]: 1 : fs::create_directories(ph);
265 : :
266 : : // Set up a non-obfuscated wrapper to write some initial data.
267 [ + - + - ]: 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});
268 : 1 : uint8_t key{'k'};
269 : 1 : uint256 in = m_rng.rand256();
270 : 1 : uint256 res;
271 : :
272 [ + - ]: 1 : dbw->Write(key, in);
273 [ + - + - : 2 : BOOST_CHECK(dbw->Read(key, res));
+ - + - ]
274 [ + - + - : 1 : BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
+ - + - ]
275 : :
276 : : // Call the destructor to free leveldb LOCK
277 [ + - ]: 1 : dbw.reset();
278 : :
279 : : // Now, set up another wrapper that wants to obfuscate the same directory
280 : 0 : CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = false, .obfuscate = true});
281 : :
282 : : // Check that the key/val we wrote with unobfuscated wrapper exists and
283 : : // is readable.
284 : 1 : uint256 res2;
285 [ + - + - : 2 : BOOST_CHECK(odbw.Read(key, res2));
+ - + - ]
286 [ + - + - : 1 : BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
+ - + - ]
287 : :
288 [ + - + - : 2 : BOOST_CHECK(!odbw.IsEmpty());
+ - + - ]
289 [ + - + - : 2 : BOOST_CHECK(!dbwrapper_private::GetObfuscation(odbw)); // The key should be an empty string
+ - ]
290 : :
291 : 1 : uint256 in2 = m_rng.rand256();
292 : 1 : uint256 res3;
293 : :
294 : : // Check that we can write successfully
295 [ + - ]: 1 : odbw.Write(key, in2);
296 [ + - + - : 2 : BOOST_CHECK(odbw.Read(key, res3));
+ - + - ]
297 [ + - + - : 1 : BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
+ - + - ]
298 [ + - + - ]: 3 : }
299 : :
300 : : // Ensure that we start obfuscating during a reindex.
301 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(existing_data_reindex)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
302 : : {
303 : : // We're going to share this fs::path between two wrappers
304 [ + - ]: 2 : fs::path ph = m_args.GetDataDirBase() / "existing_data_reindex";
305 [ + - ]: 1 : fs::create_directories(ph);
306 : :
307 : : // Set up a non-obfuscated wrapper to write some initial data.
308 [ + - + - ]: 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});
309 : 1 : uint8_t key{'k'};
310 : 1 : uint256 in = m_rng.rand256();
311 : 1 : uint256 res;
312 : :
313 [ + - ]: 1 : dbw->Write(key, in);
314 [ + - + - : 2 : BOOST_CHECK(dbw->Read(key, res));
+ - + - ]
315 [ + - + - : 1 : BOOST_CHECK_EQUAL(res.ToString(), in.ToString());
+ - + - ]
316 : :
317 : : // Call the destructor to free leveldb LOCK
318 [ + - ]: 1 : dbw.reset();
319 : :
320 : : // Simulate a -reindex by wiping the existing data store
321 : 0 : CDBWrapper odbw({.path = ph, .cache_bytes = 1 << 10, .memory_only = false, .wipe_data = true, .obfuscate = true});
322 : :
323 : : // Check that the key/val we wrote with unobfuscated wrapper doesn't exist
324 : 1 : uint256 res2;
325 [ + - + - : 2 : BOOST_CHECK(!odbw.Read(key, res2));
+ - + - ]
326 [ + - + - : 2 : BOOST_CHECK(dbwrapper_private::GetObfuscation(odbw));
+ - ]
327 : :
328 : 1 : uint256 in2 = m_rng.rand256();
329 : 1 : uint256 res3;
330 : :
331 : : // Check that we can write successfully
332 [ + - ]: 1 : odbw.Write(key, in2);
333 [ + - + - : 2 : BOOST_CHECK(odbw.Read(key, res3));
+ - + - ]
334 [ + - + - : 1 : BOOST_CHECK_EQUAL(res3.ToString(), in2.ToString());
+ - + - ]
335 [ + - + - ]: 3 : }
336 : :
337 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(iterator_ordering)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
338 : : {
339 [ + - ]: 2 : fs::path ph = m_args.GetDataDirBase() / "iterator_ordering";
340 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = false});
341 [ + + ]: 257 : for (int x=0x00; x<256; ++x) {
342 : 256 : uint8_t key = x;
343 : 256 : uint32_t value = x*x;
344 [ + + + - ]: 256 : if (!(x & 1)) dbw.Write(key, value);
345 : : }
346 : :
347 : : // Check that creating an iterator creates a snapshot
348 [ + - ]: 1 : std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
349 : :
350 [ + + ]: 257 : for (unsigned int x=0x00; x<256; ++x) {
351 : 256 : uint8_t key = x;
352 : 256 : uint32_t value = x*x;
353 [ + + + - ]: 256 : if (x & 1) dbw.Write(key, value);
354 : : }
355 : :
356 [ + + ]: 3 : for (const int seek_start : {0x00, 0x80}) {
357 [ + - ]: 2 : it->Seek((uint8_t)seek_start);
358 [ + + ]: 384 : for (unsigned int x=seek_start; x<255; ++x) {
359 : 382 : uint8_t key;
360 : 382 : uint32_t value;
361 [ + - + - : 764 : BOOST_CHECK(it->Valid());
+ - + - ]
362 [ + - + - ]: 382 : if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
363 : : break;
364 [ + - + - : 764 : BOOST_CHECK(it->GetKey(key));
+ - + + ]
365 [ + + ]: 382 : if (x & 1) {
366 [ + - + - ]: 190 : BOOST_CHECK_EQUAL(key, x + 1);
367 : 190 : continue;
368 : : }
369 [ + - + - : 384 : BOOST_CHECK(it->GetValue(value));
+ - + - ]
370 [ + - + - ]: 192 : BOOST_CHECK_EQUAL(key, x);
371 [ + - + - ]: 192 : BOOST_CHECK_EQUAL(value, x*x);
372 [ + - ]: 192 : it->Next();
373 : : }
374 [ + - + - : 4 : BOOST_CHECK(!it->Valid());
+ - ]
375 : : }
376 [ + - + - ]: 3 : }
377 : :
378 : 252 : struct StringContentsSerializer {
379 : : // Used to make two serialized objects the same while letting them have different lengths
380 : : // This is a terrible idea
381 : : std::string str;
382 : 150 : StringContentsSerializer() = default;
383 [ - + ]: 104 : explicit StringContentsSerializer(const std::string& inp) : str(inp) {}
384 : :
385 : : template<typename Stream>
386 : 102 : void Serialize(Stream& s) const
387 : : {
388 [ - + + + ]: 10334 : for (size_t i = 0; i < str.size(); i++) {
389 : 10232 : s << uint8_t(str[i]);
390 : : }
391 : 102 : }
392 : :
393 : : template<typename Stream>
394 : 150 : void Unserialize(Stream& s)
395 : : {
396 : 150 : str.clear();
397 : 150 : uint8_t c{0};
398 [ + + ]: 15495 : while (!s.empty()) {
399 : 15345 : s >> c;
400 : 15345 : str.push_back(c);
401 : : }
402 : 150 : }
403 : : };
404 : :
405 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(iterator_string_ordering)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
406 : : {
407 [ + - ]: 2 : fs::path ph = m_args.GetDataDirBase() / "iterator_string_ordering";
408 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB, .memory_only = true, .wipe_data = false, .obfuscate = false});
409 [ + + ]: 11 : for (int x = 0; x < 10; ++x) {
410 [ + + ]: 110 : for (int y = 0; y < 10; ++y) {
411 [ + - ]: 100 : std::string key{ToString(x)};
412 [ + + ]: 550 : for (int z = 0; z < y; ++z)
413 [ - + ]: 900 : key += key;
414 : 100 : uint32_t value = x*x;
415 [ - + + - ]: 300 : dbw.Write(StringContentsSerializer{key}, value);
416 : 100 : }
417 : : }
418 : :
419 [ + - ]: 1 : std::unique_ptr<CDBIterator> it(const_cast<CDBWrapper&>(dbw).NewIterator());
420 [ + + ]: 3 : for (const int seek_start : {0, 5}) {
421 [ + - + - ]: 6 : it->Seek(StringContentsSerializer{ToString(seek_start)});
422 [ + + ]: 17 : for (unsigned int x = seek_start; x < 10; ++x) {
423 [ + + ]: 165 : for (int y = 0; y < 10; ++y) {
424 [ + - ]: 150 : std::string exp_key{ToString(x)};
425 [ + + ]: 825 : for (int z = 0; z < y; ++z)
426 [ - + ]: 1350 : exp_key += exp_key;
427 [ + - ]: 150 : StringContentsSerializer key;
428 : 150 : uint32_t value;
429 [ + - + - : 300 : BOOST_CHECK(it->Valid());
+ - + - ]
430 [ + - + - ]: 150 : if (!it->Valid()) // Avoid spurious errors about invalid iterator's key and value in case of failure
431 : : break;
432 [ + - + - : 300 : BOOST_CHECK(it->GetKey(key));
+ - + - ]
433 [ + - + - : 300 : BOOST_CHECK(it->GetValue(value));
+ - + - ]
434 [ + - + - ]: 150 : BOOST_CHECK_EQUAL(key.str, exp_key);
435 [ + - + - ]: 150 : BOOST_CHECK_EQUAL(value, x*x);
436 [ + - ]: 150 : it->Next();
437 : 150 : }
438 : : }
439 [ + - + - : 4 : BOOST_CHECK(!it->Valid());
+ - ]
440 : : }
441 [ + - + - ]: 3 : }
442 : :
443 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(unicodepath)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
444 : : {
445 : : // Attempt to create a database with a UTF8 character in the path.
446 : : // On Windows this test will fail if the directory is created using
447 : : // the ANSI CreateDirectoryA call and the code page isn't UTF8.
448 : : // It will succeed if created with CreateDirectoryW.
449 [ + - ]: 2 : fs::path ph = m_args.GetDataDirBase() / "test_runner_₿_🏃_20191128_104644";
450 : 0 : CDBWrapper dbw({.path = ph, .cache_bytes = 1_MiB});
451 : :
452 [ + - + - ]: 2 : fs::path lockPath = ph / "LOCK";
453 [ + - + - : 2 : BOOST_CHECK(fs::exists(lockPath));
+ - ]
454 [ + - + - ]: 3 : }
455 : :
456 : :
457 : : BOOST_AUTO_TEST_SUITE_END()
|