11 #include <boost/test/unit_test.hpp>
13 using namespace std::string_literals;
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"});
32 AutoFile xor_file{raw_file(
"wbx"), xor_pat};
33 xor_file <<
test1 << test2;
37 AutoFile non_xor_file{raw_file(
"rb")};
38 std::vector<std::byte> raw(7);
39 non_xor_file >>
Span{raw};
42 BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure,
HasReason{
"AutoFile::ignore: end of file"});
45 AutoFile xor_file{raw_file(
"rb"), xor_pat};
46 std::vector<std::byte> read1, read2;
47 xor_file >> read1 >> read2;
51 BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure,
HasReason{
"AutoFile::read: end of file"});
54 AutoFile xor_file{raw_file(
"rb"), xor_pat};
55 std::vector<std::byte> read2;
61 BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure,
HasReason{
"AutoFile::ignore: end of file"});
62 BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure,
HasReason{
"AutoFile::read: end of file"});
70 unsigned char bytes[] = { 3, 4, 5, 6 };
71 std::vector<unsigned char> vch;
78 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
80 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
84 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
86 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
91 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
93 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
98 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
100 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
105 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
107 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
111 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
113 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
118 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
120 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
126 std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
171 std::vector<uint8_t> data{0x82, 0xa7, 0x31};
185 bit_writer.
Write(0, 1);
186 bit_writer.Write(2, 2);
187 bit_writer.Write(6, 3);
188 bit_writer.Write(11, 4);
189 bit_writer.Write(1, 5);
190 bit_writer.Write(32, 6);
191 bit_writer.Write(7, 7);
192 bit_writer.Write(30497, 16);
196 uint32_t serialized_int1;
197 data >> serialized_int1;
199 uint16_t serialized_int2;
200 data >> serialized_int2;
217 std::vector<std::byte> in;
222 ds.
Xor({0x00, 0x00});
226 in.push_back(std::byte{0x0f});
227 in.push_back(std::byte{0xf0});
239 in.push_back(std::byte{0xf0});
240 in.push_back(std::byte{0x0f});
244 ds.
Xor({0xff, 0x0f});
251 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
255 for (uint8_t j = 0; j < 40; ++j) {
258 std::rewind(file.Get());
265 }
catch (
const std::exception& e) {
267 "Rewind limit must be less than buffer size") !=
nullptr);
301 }
catch (
const std::exception& e) {
303 "Attempt to position past buffer limit") !=
nullptr);
311 for (uint8_t j = 3; j < 10; ++j) {
344 for (uint8_t j = 0; j <
sizeof(a); ++j) {
354 }
catch (
const std::exception& e) {
356 "BufferedFile::Fill: end of file") !=
nullptr);
377 fs::remove(streams_test_filename);
382 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
385 for (uint8_t j = 0; j < 40; ++j) {
388 std::rewind(file.Get());
417 }
catch (
const std::exception& e) {
418 BOOST_CHECK(strstr(e.what(),
"Attempt to position past buffer limit") !=
nullptr);
426 fs::remove(streams_test_filename);
434 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
435 for (
int rep = 0; rep < 50; ++rep) {
438 for (uint8_t i = 0; i < fileSize; ++i) {
441 std::rewind(file.Get());
446 size_t currentPos = 0;
448 for (
int step = 0; step < 100; ++step) {
449 if (currentPos >= fileSize)
463 if (currentPos + 1 > fileSize)
465 bf.SetLimit(currentPos + 1);
467 for (uint8_t i = 0; i < 1; ++i) {
475 if (currentPos + 2 > fileSize)
477 bf.SetLimit(currentPos + 2);
479 for (uint8_t i = 0; i < 2; ++i) {
487 if (currentPos + 5 > fileSize)
489 bf.SetLimit(currentPos + 5);
491 for (uint8_t i = 0; i < 5; ++i) {
501 if (currentPos + skip_length > fileSize)
continue;
502 bf.SetLimit(currentPos + skip_length);
503 bf.SkipTo(currentPos + skip_length);
504 currentPos += skip_length;
510 if (find >= fileSize)
512 bf.FindByte(std::byte(find));
517 bf.SetLimit(currentPos + 1);
526 bool okay = bf.SetPos(requestPos);
531 currentPos = bf.GetPos();
534 if (requestPos <= maxPos &&
535 maxPos > rewindSize &&
536 requestPos >= maxPos - rewindSize) {
543 if (maxPos < currentPos)
547 fs::remove(streams_test_filename);
554 const std::string data{
"bitcoin"};
559 hash_verifier >> result;
Non-refcounted RAII wrapper for FILE*.
void ignore(size_t nSize)
void Write(uint64_t data, int nbits)
Write the nbits least significant bits of a 64-bit int to the output stream.
Wrapper around an AutoFile& that implements a ring buffer to deserialize from.
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
Double ended buffer combining vector and stream-like interfaces.
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Reads data from an underlying stream, while hashing the read data.
Writes data to an underlying source stream, while hashing the written data.
A Span is an object that can refer to a contiguous sequence of objects.
Minimal stream for reading from an existing byte array by Span.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
BOOST_AUTO_TEST_SUITE_END()
FILE * fopen(const fs::path &p, const char *mode)
#define BOOST_CHECK_THROW(stmt, excMatch)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
BOOST_AUTO_TEST_CASE(xor_file)
@ ZEROS
Seed with a compile time constant of zeros.
static uint64_t InsecureRandRange(uint64_t range)
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.