Bitcoin Core 29.99.0
P2P Digital Currency
streams_tests.cpp
Go to the documentation of this file.
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 <flatfile.h>
6#include <node/blockstorage.h>
7#include <streams.h>
8#include <test/util/random.h>
10#include <util/fs.h>
11#include <util/strencodings.h>
12
13#include <boost/test/unit_test.hpp>
14
15using namespace std::string_literals;
16
18
20{
21 fs::path xor_path{m_args.GetDataDirBase() / "test_xor.bin"};
22 auto raw_file{[&](const auto& mode) { return fsbridge::fopen(xor_path, mode); }};
23 const std::vector<uint8_t> test1{1, 2, 3};
24 const std::vector<uint8_t> test2{4, 5};
25 const std::vector<std::byte> xor_pat{std::byte{0xff}, std::byte{0x00}};
26 {
27 // Check errors for missing file
28 AutoFile xor_file{raw_file("rb"), xor_pat};
29 BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullpt"});
30 BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullpt"});
31 BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullpt"});
32 }
33 {
34#ifdef __MINGW64__
35 // Temporary workaround for https://github.com/bitcoin/bitcoin/issues/30210
36 const char* mode = "wb";
37#else
38 const char* mode = "wbx";
39#endif
40 AutoFile xor_file{raw_file(mode), xor_pat};
41 xor_file << test1 << test2;
42 }
43 {
44 // Read raw from disk
45 AutoFile non_xor_file{raw_file("rb")};
46 std::vector<std::byte> raw(7);
47 non_xor_file >> std::span{raw};
48 BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa");
49 // Check that no padding exists
50 BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
51 }
52 {
53 AutoFile xor_file{raw_file("rb"), xor_pat};
54 std::vector<std::byte> read1, read2;
55 xor_file >> read1 >> read2;
57 BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
58 // Check that eof was reached
59 BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
60 }
61 {
62 AutoFile xor_file{raw_file("rb"), xor_pat};
63 std::vector<std::byte> read2;
64 // Check that ignore works
65 xor_file.ignore(4);
66 xor_file >> read2;
67 BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
68 // Check that ignore and read fail now
69 BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
70 BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
71 }
72}
73
74BOOST_AUTO_TEST_CASE(streams_vector_writer)
75{
76 unsigned char a(1);
77 unsigned char b(2);
78 unsigned char bytes[] = { 3, 4, 5, 6 };
79 std::vector<unsigned char> vch;
80
81 // Each test runs twice. Serializing a second time at the same starting
82 // point should yield the same results, even if the first test grew the
83 // vector.
84
85 VectorWriter{vch, 0, a, b};
86 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
87 VectorWriter{vch, 0, a, b};
88 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
89 vch.clear();
90
91 VectorWriter{vch, 2, a, b};
92 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
93 VectorWriter{vch, 2, a, b};
94 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
95 vch.clear();
96
97 vch.resize(5, 0);
98 VectorWriter{vch, 2, a, b};
99 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
100 VectorWriter{vch, 2, a, b};
101 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
102 vch.clear();
103
104 vch.resize(4, 0);
105 VectorWriter{vch, 3, a, b};
106 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
107 VectorWriter{vch, 3, a, b};
108 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
109 vch.clear();
110
111 vch.resize(4, 0);
112 VectorWriter{vch, 4, a, b};
113 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
114 VectorWriter{vch, 4, a, b};
115 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
116 vch.clear();
117
118 VectorWriter{vch, 0, bytes};
119 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
120 VectorWriter{vch, 0, bytes};
121 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
122 vch.clear();
123
124 vch.resize(4, 8);
125 VectorWriter{vch, 2, a, bytes, b};
126 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
127 VectorWriter{vch, 2, a, bytes, b};
128 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
129 vch.clear();
130}
131
132BOOST_AUTO_TEST_CASE(streams_vector_reader)
133{
134 std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
135
136 SpanReader reader{vch};
137 BOOST_CHECK_EQUAL(reader.size(), 6U);
138 BOOST_CHECK(!reader.empty());
139
140 // Read a single byte as an unsigned char.
141 unsigned char a;
142 reader >> a;
143 BOOST_CHECK_EQUAL(a, 1);
144 BOOST_CHECK_EQUAL(reader.size(), 5U);
145 BOOST_CHECK(!reader.empty());
146
147 // Read a single byte as a int8_t.
148 int8_t b;
149 reader >> b;
150 BOOST_CHECK_EQUAL(b, -1);
151 BOOST_CHECK_EQUAL(reader.size(), 4U);
152 BOOST_CHECK(!reader.empty());
153
154 // Read a 4 bytes as an unsigned int.
155 unsigned int c;
156 reader >> c;
157 BOOST_CHECK_EQUAL(c, 100992003U); // 3,4,5,6 in little-endian base-256
158 BOOST_CHECK_EQUAL(reader.size(), 0U);
159 BOOST_CHECK(reader.empty());
160
161 // Reading after end of byte vector throws an error.
162 signed int d;
163 BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
164
165 // Read a 4 bytes as a signed int from the beginning of the buffer.
166 SpanReader new_reader{vch};
167 new_reader >> d;
168 BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
169 BOOST_CHECK_EQUAL(new_reader.size(), 2U);
170 BOOST_CHECK(!new_reader.empty());
171
172 // Reading after end of byte vector throws an error even if the reader is
173 // not totally empty.
174 BOOST_CHECK_THROW(new_reader >> d, std::ios_base::failure);
175}
176
177BOOST_AUTO_TEST_CASE(streams_vector_reader_rvalue)
178{
179 std::vector<uint8_t> data{0x82, 0xa7, 0x31};
180 SpanReader reader{data};
181 uint32_t varint = 0;
182 // Deserialize into r-value
183 reader >> VARINT(varint);
184 BOOST_CHECK_EQUAL(varint, 54321U);
185 BOOST_CHECK(reader.empty());
186}
187
188BOOST_AUTO_TEST_CASE(bitstream_reader_writer)
189{
191
192 BitStreamWriter bit_writer{data};
193 bit_writer.Write(0, 1);
194 bit_writer.Write(2, 2);
195 bit_writer.Write(6, 3);
196 bit_writer.Write(11, 4);
197 bit_writer.Write(1, 5);
198 bit_writer.Write(32, 6);
199 bit_writer.Write(7, 7);
200 bit_writer.Write(30497, 16);
201 bit_writer.Flush();
202
203 DataStream data_copy{data};
204 uint32_t serialized_int1;
205 data >> serialized_int1;
206 BOOST_CHECK_EQUAL(serialized_int1, uint32_t{0x7700C35A}); // NOTE: Serialized as LE
207 uint16_t serialized_int2;
208 data >> serialized_int2;
209 BOOST_CHECK_EQUAL(serialized_int2, uint16_t{0x1072}); // NOTE: Serialized as LE
210
211 BitStreamReader bit_reader{data_copy};
212 BOOST_CHECK_EQUAL(bit_reader.Read(1), 0U);
213 BOOST_CHECK_EQUAL(bit_reader.Read(2), 2U);
214 BOOST_CHECK_EQUAL(bit_reader.Read(3), 6U);
215 BOOST_CHECK_EQUAL(bit_reader.Read(4), 11U);
216 BOOST_CHECK_EQUAL(bit_reader.Read(5), 1U);
217 BOOST_CHECK_EQUAL(bit_reader.Read(6), 32U);
218 BOOST_CHECK_EQUAL(bit_reader.Read(7), 7U);
219 BOOST_CHECK_EQUAL(bit_reader.Read(16), 30497U);
220 BOOST_CHECK_THROW(bit_reader.Read(8), std::ios_base::failure);
221}
222
223BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
224{
225 std::vector<std::byte> in;
226
227 // Degenerate case
228 {
229 DataStream ds{in};
230 ds.Xor({0x00, 0x00});
231 BOOST_CHECK_EQUAL(""s, ds.str());
232 }
233
234 in.push_back(std::byte{0x0f});
235 in.push_back(std::byte{0xf0});
236
237 // Single character key
238 {
239 DataStream ds{in};
240 ds.Xor({0xff});
241 BOOST_CHECK_EQUAL("\xf0\x0f"s, ds.str());
242 }
243
244 // Multi character key
245
246 in.clear();
247 in.push_back(std::byte{0xf0});
248 in.push_back(std::byte{0x0f});
249
250 {
251 DataStream ds{in};
252 ds.Xor({0xff, 0x0f});
253 BOOST_CHECK_EQUAL("\x0f\x00"s, ds.str());
254 }
255}
256
257BOOST_AUTO_TEST_CASE(streams_buffered_file)
258{
259 fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
260 AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
261
262 // The value at each offset is the offset.
263 for (uint8_t j = 0; j < 40; ++j) {
264 file << j;
265 }
266 file.seek(0, SEEK_SET);
267
268 // The buffer size (second arg) must be greater than the rewind
269 // amount (third arg).
270 try {
271 BufferedFile bfbad{file, 25, 25};
272 BOOST_CHECK(false);
273 } catch (const std::exception& e) {
274 BOOST_CHECK(strstr(e.what(),
275 "Rewind limit must be less than buffer size") != nullptr);
276 }
277
278 // The buffer is 25 bytes, allow rewinding 10 bytes.
279 BufferedFile bf{file, 25, 10};
280 BOOST_CHECK(!bf.eof());
281
282 uint8_t i;
283 bf >> i;
284 BOOST_CHECK_EQUAL(i, 0);
285 bf >> i;
286 BOOST_CHECK_EQUAL(i, 1);
287
288 // After reading bytes 0 and 1, we're positioned at 2.
289 BOOST_CHECK_EQUAL(bf.GetPos(), 2U);
290
291 // Rewind to offset 0, ok (within the 10 byte window).
292 BOOST_CHECK(bf.SetPos(0));
293 bf >> i;
294 BOOST_CHECK_EQUAL(i, 0);
295
296 // We can go forward to where we've been, but beyond may fail.
297 BOOST_CHECK(bf.SetPos(2));
298 bf >> i;
299 BOOST_CHECK_EQUAL(i, 2);
300
301 // If you know the maximum number of bytes that should be
302 // read to deserialize the variable, you can limit the read
303 // extent. The current file offset is 3, so the following
304 // SetLimit() allows zero bytes to be read.
305 BOOST_CHECK(bf.SetLimit(3));
306 try {
307 bf >> i;
308 BOOST_CHECK(false);
309 } catch (const std::exception& e) {
310 BOOST_CHECK(strstr(e.what(),
311 "Attempt to position past buffer limit") != nullptr);
312 }
313 // The default argument removes the limit completely.
314 BOOST_CHECK(bf.SetLimit());
315 // The read position should still be at 3 (no change).
316 BOOST_CHECK_EQUAL(bf.GetPos(), 3U);
317
318 // Read from current offset, 3, forward until position 10.
319 for (uint8_t j = 3; j < 10; ++j) {
320 bf >> i;
321 BOOST_CHECK_EQUAL(i, j);
322 }
323 BOOST_CHECK_EQUAL(bf.GetPos(), 10U);
324
325 // We're guaranteed (just barely) to be able to rewind to zero.
326 BOOST_CHECK(bf.SetPos(0));
327 BOOST_CHECK_EQUAL(bf.GetPos(), 0U);
328 bf >> i;
329 BOOST_CHECK_EQUAL(i, 0);
330
331 // We can set the position forward again up to the farthest
332 // into the stream we've been, but no farther. (Attempting
333 // to go farther may succeed, but it's not guaranteed.)
334 BOOST_CHECK(bf.SetPos(10));
335 bf >> i;
336 BOOST_CHECK_EQUAL(i, 10);
337 BOOST_CHECK_EQUAL(bf.GetPos(), 11U);
338
339 // Now it's only guaranteed that we can rewind to offset 1
340 // (current read position, 11, minus rewind amount, 10).
341 BOOST_CHECK(bf.SetPos(1));
342 BOOST_CHECK_EQUAL(bf.GetPos(), 1U);
343 bf >> i;
344 BOOST_CHECK_EQUAL(i, 1);
345
346 // We can stream into large variables, even larger than
347 // the buffer size.
348 BOOST_CHECK(bf.SetPos(11));
349 {
350 uint8_t a[40 - 11];
351 bf >> a;
352 for (uint8_t j = 0; j < sizeof(a); ++j) {
353 BOOST_CHECK_EQUAL(a[j], 11 + j);
354 }
355 }
356 BOOST_CHECK_EQUAL(bf.GetPos(), 40U);
357
358 // We've read the entire file, the next read should throw.
359 try {
360 bf >> i;
361 BOOST_CHECK(false);
362 } catch (const std::exception& e) {
363 BOOST_CHECK(strstr(e.what(),
364 "BufferedFile::Fill: end of file") != nullptr);
365 }
366 // Attempting to read beyond the end sets the EOF indicator.
367 BOOST_CHECK(bf.eof());
368
369 // Still at offset 40, we can go back 10, to 30.
370 BOOST_CHECK_EQUAL(bf.GetPos(), 40U);
371 BOOST_CHECK(bf.SetPos(30));
372 bf >> i;
373 BOOST_CHECK_EQUAL(i, 30);
374 BOOST_CHECK_EQUAL(bf.GetPos(), 31U);
375
376 // We're too far to rewind to position zero.
377 BOOST_CHECK(!bf.SetPos(0));
378 // But we should now be positioned at least as far back as allowed
379 // by the rewind window (relative to our farthest read position, 40).
380 BOOST_CHECK(bf.GetPos() <= 30U);
381
382 // We can explicitly close the file, or the destructor will do it.
383 file.fclose();
384
385 fs::remove(streams_test_filename);
386}
387
388BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
389{
390 fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
391 AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
392 // The value at each offset is the byte offset (e.g. byte 1 in the file has the value 0x01).
393 for (uint8_t j = 0; j < 40; ++j) {
394 file << j;
395 }
396 file.seek(0, SEEK_SET);
397
398 // The buffer is 25 bytes, allow rewinding 10 bytes.
399 BufferedFile bf{file, 25, 10};
400
401 uint8_t i;
402 // This is like bf >> (7-byte-variable), in that it will cause data
403 // to be read from the file into memory, but it's not copied to us.
404 bf.SkipTo(7);
405 BOOST_CHECK_EQUAL(bf.GetPos(), 7U);
406 bf >> i;
407 BOOST_CHECK_EQUAL(i, 7);
408
409 // The bytes in the buffer up to offset 7 are valid and can be read.
410 BOOST_CHECK(bf.SetPos(0));
411 bf >> i;
412 BOOST_CHECK_EQUAL(i, 0);
413 bf >> i;
414 BOOST_CHECK_EQUAL(i, 1);
415
416 bf.SkipTo(11);
417 bf >> i;
418 BOOST_CHECK_EQUAL(i, 11);
419
420 // SkipTo() honors the transfer limit; we can't position beyond the limit.
421 bf.SetLimit(13);
422 try {
423 bf.SkipTo(14);
424 BOOST_CHECK(false);
425 } catch (const std::exception& e) {
426 BOOST_CHECK(strstr(e.what(), "Attempt to position past buffer limit") != nullptr);
427 }
428
429 // We can position exactly to the transfer limit.
430 bf.SkipTo(13);
431 BOOST_CHECK_EQUAL(bf.GetPos(), 13U);
432
433 file.fclose();
434 fs::remove(streams_test_filename);
435}
436
437BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
438{
439 // Make this test deterministic.
440 SeedRandomForTest(SeedRand::ZEROS);
441
442 fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
443 for (int rep = 0; rep < 50; ++rep) {
444 AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
445 size_t fileSize = m_rng.randrange(256);
446 for (uint8_t i = 0; i < fileSize; ++i) {
447 file << i;
448 }
449 file.seek(0, SEEK_SET);
450
451 size_t bufSize = m_rng.randrange(300) + 1;
452 size_t rewindSize = m_rng.randrange(bufSize);
453 BufferedFile bf{file, bufSize, rewindSize};
454 size_t currentPos = 0;
455 size_t maxPos = 0;
456 for (int step = 0; step < 100; ++step) {
457 if (currentPos >= fileSize)
458 break;
459
460 // We haven't read to the end of the file yet.
461 BOOST_CHECK(!bf.eof());
462 BOOST_CHECK_EQUAL(bf.GetPos(), currentPos);
463
464 // Pretend the file consists of a series of objects of varying
465 // sizes; the boundaries of the objects can interact arbitrarily
466 // with the CBufferFile's internal buffer. These first three
467 // cases simulate objects of various sizes (1, 2, 5 bytes).
468 switch (m_rng.randrange(6)) {
469 case 0: {
470 uint8_t a[1];
471 if (currentPos + 1 > fileSize)
472 continue;
473 bf.SetLimit(currentPos + 1);
474 bf >> a;
475 for (uint8_t i = 0; i < 1; ++i) {
476 BOOST_CHECK_EQUAL(a[i], currentPos);
477 currentPos++;
478 }
479 break;
480 }
481 case 1: {
482 uint8_t a[2];
483 if (currentPos + 2 > fileSize)
484 continue;
485 bf.SetLimit(currentPos + 2);
486 bf >> a;
487 for (uint8_t i = 0; i < 2; ++i) {
488 BOOST_CHECK_EQUAL(a[i], currentPos);
489 currentPos++;
490 }
491 break;
492 }
493 case 2: {
494 uint8_t a[5];
495 if (currentPos + 5 > fileSize)
496 continue;
497 bf.SetLimit(currentPos + 5);
498 bf >> a;
499 for (uint8_t i = 0; i < 5; ++i) {
500 BOOST_CHECK_EQUAL(a[i], currentPos);
501 currentPos++;
502 }
503 break;
504 }
505 case 3: {
506 // SkipTo is similar to the "read" cases above, except
507 // we don't receive the data.
508 size_t skip_length{static_cast<size_t>(m_rng.randrange(5))};
509 if (currentPos + skip_length > fileSize) continue;
510 bf.SetLimit(currentPos + skip_length);
511 bf.SkipTo(currentPos + skip_length);
512 currentPos += skip_length;
513 break;
514 }
515 case 4: {
516 // Find a byte value (that is at or ahead of the current position).
517 size_t find = currentPos + m_rng.randrange(8);
518 if (find >= fileSize)
519 find = fileSize - 1;
520 bf.FindByte(std::byte(find));
521 // The value at each offset is the offset.
522 BOOST_CHECK_EQUAL(bf.GetPos(), find);
523 currentPos = find;
524
525 bf.SetLimit(currentPos + 1);
526 uint8_t i;
527 bf >> i;
528 BOOST_CHECK_EQUAL(i, currentPos);
529 currentPos++;
530 break;
531 }
532 case 5: {
533 size_t requestPos = m_rng.randrange(maxPos + 4);
534 bool okay = bf.SetPos(requestPos);
535 // The new position may differ from the requested position
536 // because we may not be able to rewind beyond the rewind
537 // window, and we may not be able to move forward beyond the
538 // farthest position we've reached so far.
539 currentPos = bf.GetPos();
540 BOOST_CHECK_EQUAL(okay, currentPos == requestPos);
541 // Check that we can position within the rewind window.
542 if (requestPos <= maxPos &&
543 maxPos > rewindSize &&
544 requestPos >= maxPos - rewindSize) {
545 // We requested a position within the rewind window.
546 BOOST_CHECK(okay);
547 }
548 break;
549 }
550 }
551 if (maxPos < currentPos)
552 maxPos = currentPos;
553 }
554 }
555 fs::remove(streams_test_filename);
556}
557
558BOOST_AUTO_TEST_CASE(buffered_reader_matches_autofile_random_content)
559{
560 const size_t file_size{1 + m_rng.randrange<size_t>(1 << 17)};
561 const size_t buf_size{1 + m_rng.randrange(file_size)};
562 const FlatFilePos pos{0, 0};
563
564 const FlatFileSeq test_file{m_args.GetDataDirBase(), "buffered_file_test_random", node::BLOCKFILE_CHUNK_SIZE};
565 const std::vector obfuscation{m_rng.randbytes<std::byte>(8)};
566
567 // Write out the file with random content
568 {
569 AutoFile{test_file.Open(pos, /*read_only=*/false), obfuscation}.write(m_rng.randbytes<std::byte>(file_size));
570 }
571 BOOST_CHECK_EQUAL(fs::file_size(test_file.FileName(pos)), file_size);
572
573 {
574 AutoFile direct_file{test_file.Open(pos, /*read_only=*/true), obfuscation};
575
576 AutoFile buffered_file{test_file.Open(pos, /*read_only=*/true), obfuscation};
577 BufferedReader buffered_reader{std::move(buffered_file), buf_size};
578
579 for (size_t total_read{0}; total_read < file_size;) {
580 const size_t read{Assert(std::min(1 + m_rng.randrange(m_rng.randbool() ? buf_size : 2 * buf_size), file_size - total_read))};
581
582 DataBuffer direct_file_buffer{read};
583 direct_file.read(direct_file_buffer);
584
585 DataBuffer buffered_buffer{read};
586 buffered_reader.read(buffered_buffer);
587
588 BOOST_CHECK_EQUAL_COLLECTIONS(
589 direct_file_buffer.begin(), direct_file_buffer.end(),
590 buffered_buffer.begin(), buffered_buffer.end()
591 );
592
593 total_read += read;
594 }
595
596 {
597 DataBuffer excess_byte{1};
598 BOOST_CHECK_EXCEPTION(direct_file.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
599 }
600
601 {
602 DataBuffer excess_byte{1};
603 BOOST_CHECK_EXCEPTION(buffered_reader.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
604 }
605 }
606
607 fs::remove(test_file.FileName(pos));
608}
609
610BOOST_AUTO_TEST_CASE(buffered_writer_matches_autofile_random_content)
611{
612 const size_t file_size{1 + m_rng.randrange<size_t>(1 << 17)};
613 const size_t buf_size{1 + m_rng.randrange(file_size)};
614 const FlatFilePos pos{0, 0};
615
616 const FlatFileSeq test_buffered{m_args.GetDataDirBase(), "buffered_write_test", node::BLOCKFILE_CHUNK_SIZE};
617 const FlatFileSeq test_direct{m_args.GetDataDirBase(), "direct_write_test", node::BLOCKFILE_CHUNK_SIZE};
618 const std::vector obfuscation{m_rng.randbytes<std::byte>(8)};
619
620 {
621 DataBuffer test_data{m_rng.randbytes<std::byte>(file_size)};
622
623 AutoFile direct_file{test_direct.Open(pos, /*read_only=*/false), obfuscation};
624
625 AutoFile buffered_file{test_buffered.Open(pos, /*read_only=*/false), obfuscation};
626 BufferedWriter buffered{buffered_file, buf_size};
627
628 for (size_t total_written{0}; total_written < file_size;) {
629 const size_t write_size{Assert(std::min(1 + m_rng.randrange(m_rng.randbool() ? buf_size : 2 * buf_size), file_size - total_written))};
630
631 auto current_span = std::span{test_data}.subspan(total_written, write_size);
632 direct_file.write(current_span);
633 buffered.write(current_span);
634
635 total_written += write_size;
636 }
637 // Destructors of AutoFile and BufferedWriter will flush/close here
638 }
639
640 // Compare the resulting files
641 DataBuffer direct_result{file_size};
642 {
643 AutoFile verify_direct{test_direct.Open(pos, /*read_only=*/true), obfuscation};
644 verify_direct.read(direct_result);
645
646 DataBuffer excess_byte{1};
647 BOOST_CHECK_EXCEPTION(verify_direct.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
648 }
649
650 DataBuffer buffered_result{file_size};
651 {
652 AutoFile verify_buffered{test_buffered.Open(pos, /*read_only=*/true), obfuscation};
653 verify_buffered.read(buffered_result);
654
655 DataBuffer excess_byte{1};
656 BOOST_CHECK_EXCEPTION(verify_buffered.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
657 }
658
659 BOOST_CHECK_EQUAL_COLLECTIONS(
660 direct_result.begin(), direct_result.end(),
661 buffered_result.begin(), buffered_result.end()
662 );
663
664 fs::remove(test_direct.FileName(pos));
665 fs::remove(test_buffered.FileName(pos));
666}
667
668BOOST_AUTO_TEST_CASE(buffered_writer_reader)
669{
670 const uint32_t v1{m_rng.rand32()}, v2{m_rng.rand32()}, v3{m_rng.rand32()};
671 const fs::path test_file{m_args.GetDataDirBase() / "test_buffered_write_read.bin"};
672
673 // Write out the values through a precisely sized BufferedWriter
674 {
675 AutoFile file{fsbridge::fopen(test_file, "w+b")};
676 BufferedWriter f(file, sizeof(v1) + sizeof(v2) + sizeof(v3));
677 f << v1 << v2;
678 f.write(std::as_bytes(std::span{&v3, 1}));
679 }
680 // Read back and verify using BufferedReader
681 {
682 uint32_t _v1{0}, _v2{0}, _v3{0};
683 AutoFile file{fsbridge::fopen(test_file, "rb")};
684 BufferedReader f(std::move(file), sizeof(v1) + sizeof(v2) + sizeof(v3));
685 f >> _v1 >> _v2;
686 f.read(std::as_writable_bytes(std::span{&_v3, 1}));
687 BOOST_CHECK_EQUAL(_v1, v1);
688 BOOST_CHECK_EQUAL(_v2, v2);
689 BOOST_CHECK_EQUAL(_v3, v3);
690
691 DataBuffer excess_byte{1};
692 BOOST_CHECK_EXCEPTION(f.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
693 }
694
695 fs::remove(test_file);
696}
697
698BOOST_AUTO_TEST_CASE(streams_hashed)
699{
700 DataStream stream{};
701 HashedSourceWriter hash_writer{stream};
702 const std::string data{"bitcoin"};
703 hash_writer << data;
704
705 HashVerifier hash_verifier{stream};
706 std::string result;
707 hash_verifier >> result;
708 BOOST_CHECK_EQUAL(data, result);
709 BOOST_CHECK_EQUAL(hash_writer.GetHash(), hash_verifier.GetHash());
710}
711
#define Assert(val)
Identity function.
Definition: check.h:106
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:392
void ignore(size_t nSize)
Definition: streams.cpp:67
void write(std::span< const std::byte > src)
Definition: streams.cpp:81
void read(std::span< std::byte > dst)
Definition: streams.cpp:60
Wrapper around an AutoFile& that implements a ring buffer to deserialize from.
Definition: streams.h:482
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
Definition: streams.h:555
Wrapper that buffers reads from an underlying stream.
Definition: streams.h:629
void read(std::span< std::byte > dst)
Definition: streams.h:640
Wrapper that buffers writes to an underlying stream.
Definition: streams.h:671
void write(std::span< const std::byte > src)
Definition: streams.h:687
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:147
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:276
FlatFileSeq represents a sequence of numbered files storing raw data.
Definition: flatfile.h:46
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:302
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:151
Writes data to an underlying source stream, while hashing the written data.
Definition: hash.h:185
Minimal stream for reading from an existing byte array by std::span.
Definition: streams.h:101
const std::string test1
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:29
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:26
static const unsigned int BLOCKFILE_CHUNK_SIZE
The pre-allocation chunk size for blk?????.dat files (since 0.8)
Definition: blockstorage.h:71
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
#define VARINT(obj)
Definition: serialize.h:500
std::vector< std::byte > DataBuffer
Definition: streams.h:473
BOOST_AUTO_TEST_CASE(xor_file)
Basic testing setup.
Definition: setup_common.h:64
@ ZEROS
Seed with a compile time constant of zeros.