11 #include <boost/test/unit_test.hpp> 19 fs::path xor_path{m_args.GetDataDirBase() /
"test_xor.bin"};
20 auto raw_file{[&](
const auto& mode) {
return fsbridge::fopen(xor_path, mode); }};
21 const std::vector<uint8_t>
test1{1, 2, 3};
22 const std::vector<uint8_t> test2{4, 5};
23 const std::vector<std::byte> xor_pat{std::byte{0xff}, std::byte{0x00}};
26 AutoFile xor_file{raw_file(
"rb"), xor_pat};
27 BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure,
HasReason{
"AutoFile::write: file handle is nullpt"});
28 BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure,
HasReason{
"AutoFile::read: file handle is nullpt"});
29 BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure,
HasReason{
"AutoFile::ignore: file handle is nullpt"});
34 const char* mode =
"wb";
36 const char* mode =
"wbx";
38 AutoFile xor_file{raw_file(mode), xor_pat};
39 xor_file <<
test1 << test2;
43 AutoFile non_xor_file{raw_file(
"rb")};
44 std::vector<std::byte> raw(7);
45 non_xor_file >>
Span{raw};
48 BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure,
HasReason{
"AutoFile::ignore: end of file"});
51 AutoFile xor_file{raw_file(
"rb"), xor_pat};
52 std::vector<std::byte> read1, read2;
53 xor_file >> read1 >> read2;
57 BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure,
HasReason{
"AutoFile::read: end of file"});
60 AutoFile xor_file{raw_file(
"rb"), xor_pat};
61 std::vector<std::byte> read2;
67 BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure,
HasReason{
"AutoFile::ignore: end of file"});
68 BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure,
HasReason{
"AutoFile::read: end of file"});
76 unsigned char bytes[] = { 3, 4, 5, 6 };
77 std::vector<unsigned char> vch;
84 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
86 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
90 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
92 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
97 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
99 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
104 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
106 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
111 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
113 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
117 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
119 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
124 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
126 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
132 std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
177 std::vector<uint8_t>
data{0x82, 0xa7, 0x31};
191 bit_writer.Write(0, 1);
192 bit_writer.Write(2, 2);
193 bit_writer.Write(6, 3);
194 bit_writer.Write(11, 4);
195 bit_writer.Write(1, 5);
196 bit_writer.Write(32, 6);
197 bit_writer.Write(7, 7);
198 bit_writer.Write(30497, 16);
202 uint32_t serialized_int1;
203 data >> serialized_int1;
205 uint16_t serialized_int2;
206 data >> serialized_int2;
223 std::vector<std::byte> in;
228 ds.
Xor({0x00, 0x00});
232 in.push_back(std::byte{0x0f});
233 in.push_back(std::byte{0xf0});
245 in.push_back(std::byte{0xf0});
246 in.push_back(std::byte{0x0f});
250 ds.
Xor({0xff, 0x0f});
257 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
261 for (uint8_t j = 0; j < 40; ++j) {
264 file.seek(0, SEEK_SET);
271 }
catch (
const std::exception& e) {
273 "Rewind limit must be less than buffer size") !=
nullptr);
307 }
catch (
const std::exception& e) {
309 "Attempt to position past buffer limit") !=
nullptr);
317 for (uint8_t j = 3; j < 10; ++j) {
350 for (uint8_t j = 0; j <
sizeof(a); ++j) {
360 }
catch (
const std::exception& e) {
362 "BufferedFile::Fill: end of file") !=
nullptr);
383 fs::remove(streams_test_filename);
388 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
391 for (uint8_t j = 0; j < 40; ++j) {
394 file.seek(0, SEEK_SET);
423 }
catch (
const std::exception& e) {
424 BOOST_CHECK(strstr(e.what(),
"Attempt to position past buffer limit") !=
nullptr);
432 fs::remove(streams_test_filename);
440 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
441 for (
int rep = 0; rep < 50; ++rep) {
443 size_t fileSize = m_rng.randrange(256);
444 for (uint8_t i = 0; i < fileSize; ++i) {
447 file.seek(0, SEEK_SET);
449 size_t bufSize = m_rng.randrange(300) + 1;
450 size_t rewindSize = m_rng.randrange(bufSize);
452 size_t currentPos = 0;
454 for (
int step = 0; step < 100; ++step) {
455 if (currentPos >= fileSize)
466 switch (m_rng.randrange(6)) {
469 if (currentPos + 1 > fileSize)
471 bf.SetLimit(currentPos + 1);
473 for (uint8_t i = 0; i < 1; ++i) {
481 if (currentPos + 2 > fileSize)
483 bf.SetLimit(currentPos + 2);
485 for (uint8_t i = 0; i < 2; ++i) {
493 if (currentPos + 5 > fileSize)
495 bf.SetLimit(currentPos + 5);
497 for (uint8_t i = 0; i < 5; ++i) {
506 size_t skip_length{
static_cast<size_t>(m_rng.randrange(5))};
507 if (currentPos + skip_length > fileSize)
continue;
508 bf.SetLimit(currentPos + skip_length);
509 bf.SkipTo(currentPos + skip_length);
510 currentPos += skip_length;
515 size_t find = currentPos + m_rng.randrange(8);
516 if (find >= fileSize)
518 bf.FindByte(std::byte(find));
523 bf.SetLimit(currentPos + 1);
531 size_t requestPos = m_rng.randrange(maxPos + 4);
532 bool okay = bf.SetPos(requestPos);
537 currentPos = bf.GetPos();
540 if (requestPos <= maxPos &&
541 maxPos > rewindSize &&
542 requestPos >= maxPos - rewindSize) {
549 if (maxPos < currentPos)
553 fs::remove(streams_test_filename);
560 const std::string
data{
"bitcoin"};
#define BOOST_CHECK_THROW(stmt, excMatch)
FILE * fopen(const fs::path &p, const char *mode)
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
Wrapper around an AutoFile& that implements a ring buffer to deserialize from.
Non-refcounted RAII wrapper for FILE*.
Minimal stream for reading from an existing byte array by Span.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
Double ended buffer combining vector and stream-like interfaces.
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
#define BOOST_CHECK_EQUAL(v1, v2)
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Writes data to an underlying source stream, while hashing the written data.
Reads data from an underlying stream, while hashing the read data.
A Span is an object that can refer to a contiguous sequence of objects.
BOOST_AUTO_TEST_CASE(xor_file)
Seed with a compile time constant of zeros.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
void ignore(size_t nSize)
#define BOOST_CHECK(expr)