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"});
35 const char* mode =
"wb";
37 const char* mode =
"wbx";
39 AutoFile xor_file{raw_file(mode), xor_pat};
40 xor_file <<
test1 << test2;
44 AutoFile non_xor_file{raw_file(
"rb")};
45 std::vector<std::byte> raw(7);
46 non_xor_file >>
Span{raw};
49 BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure,
HasReason{
"AutoFile::ignore: end of file"});
52 AutoFile xor_file{raw_file(
"rb"), xor_pat};
53 std::vector<std::byte> read1, read2;
54 xor_file >> read1 >> read2;
58 BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure,
HasReason{
"AutoFile::read: end of file"});
61 AutoFile xor_file{raw_file(
"rb"), xor_pat};
62 std::vector<std::byte> read2;
68 BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure,
HasReason{
"AutoFile::ignore: end of file"});
69 BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure,
HasReason{
"AutoFile::read: end of file"});
77 unsigned char bytes[] = { 3, 4, 5, 6 };
78 std::vector<unsigned char> vch;
85 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
87 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
91 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
93 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
98 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
100 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
105 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
107 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
112 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
114 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
118 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
120 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
125 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
127 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
133 std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
178 std::vector<uint8_t> data{0x82, 0xa7, 0x31};
192 bit_writer.
Write(0, 1);
193 bit_writer.Write(2, 2);
194 bit_writer.Write(6, 3);
195 bit_writer.Write(11, 4);
196 bit_writer.Write(1, 5);
197 bit_writer.Write(32, 6);
198 bit_writer.Write(7, 7);
199 bit_writer.Write(30497, 16);
203 uint32_t serialized_int1;
204 data >> serialized_int1;
206 uint16_t serialized_int2;
207 data >> serialized_int2;
224 std::vector<std::byte> in;
229 ds.
Xor({0x00, 0x00});
233 in.push_back(std::byte{0x0f});
234 in.push_back(std::byte{0xf0});
246 in.push_back(std::byte{0xf0});
247 in.push_back(std::byte{0x0f});
251 ds.
Xor({0xff, 0x0f});
258 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
262 for (uint8_t j = 0; j < 40; ++j) {
265 std::rewind(file.Get());
272 }
catch (
const std::exception& e) {
274 "Rewind limit must be less than buffer size") !=
nullptr);
308 }
catch (
const std::exception& e) {
310 "Attempt to position past buffer limit") !=
nullptr);
318 for (uint8_t j = 3; j < 10; ++j) {
351 for (uint8_t j = 0; j <
sizeof(a); ++j) {
361 }
catch (
const std::exception& e) {
363 "BufferedFile::Fill: end of file") !=
nullptr);
384 fs::remove(streams_test_filename);
389 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
392 for (uint8_t j = 0; j < 40; ++j) {
395 std::rewind(file.Get());
424 }
catch (
const std::exception& e) {
425 BOOST_CHECK(strstr(e.what(),
"Attempt to position past buffer limit") !=
nullptr);
433 fs::remove(streams_test_filename);
441 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
442 for (
int rep = 0; rep < 50; ++rep) {
445 for (uint8_t i = 0; i < fileSize; ++i) {
448 std::rewind(file.Get());
453 size_t currentPos = 0;
455 for (
int step = 0; step < 100; ++step) {
456 if (currentPos >= fileSize)
470 if (currentPos + 1 > fileSize)
472 bf.SetLimit(currentPos + 1);
474 for (uint8_t i = 0; i < 1; ++i) {
482 if (currentPos + 2 > fileSize)
484 bf.SetLimit(currentPos + 2);
486 for (uint8_t i = 0; i < 2; ++i) {
494 if (currentPos + 5 > fileSize)
496 bf.SetLimit(currentPos + 5);
498 for (uint8_t i = 0; i < 5; ++i) {
508 if (currentPos + skip_length > fileSize)
continue;
509 bf.SetLimit(currentPos + skip_length);
510 bf.SkipTo(currentPos + skip_length);
511 currentPos += skip_length;
517 if (find >= fileSize)
519 bf.FindByte(std::byte(find));
524 bf.SetLimit(currentPos + 1);
533 bool okay = bf.SetPos(requestPos);
538 currentPos = bf.GetPos();
541 if (requestPos <= maxPos &&
542 maxPos > rewindSize &&
543 requestPos >= maxPos - rewindSize) {
550 if (maxPos < currentPos)
554 fs::remove(streams_test_filename);
561 const std::string data{
"bitcoin"};
566 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.