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"});
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);
311 }
catch (
const std::exception& e) {
313 "Attempt to position past buffer limit") !=
nullptr);
321 for (uint8_t j = 3; j < 10; ++j) {
354 for (uint8_t j = 0; j <
sizeof(a); ++j) {
364 }
catch (
const std::exception& e) {
366 "BufferedFile::Fill: end of file") !=
nullptr);
387 fs::remove(streams_test_filename);
392 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
395 for (uint8_t j = 0; j < 40; ++j) {
398 std::rewind(file.Get());
427 }
catch (
const std::exception& e) {
428 BOOST_CHECK(strstr(e.what(),
"Attempt to position past buffer limit") !=
nullptr);
436 fs::remove(streams_test_filename);
444 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
445 for (
int rep = 0; rep < 50; ++rep) {
448 for (uint8_t i = 0; i < fileSize; ++i) {
451 std::rewind(file.Get());
456 size_t currentPos = 0;
458 for (
int step = 0; step < 100; ++step) {
459 if (currentPos >= fileSize)
473 if (currentPos + 1 > fileSize)
475 bf.SetLimit(currentPos + 1);
477 for (uint8_t i = 0; i < 1; ++i) {
485 if (currentPos + 2 > fileSize)
487 bf.SetLimit(currentPos + 2);
489 for (uint8_t i = 0; i < 2; ++i) {
497 if (currentPos + 5 > fileSize)
499 bf.SetLimit(currentPos + 5);
501 for (uint8_t i = 0; i < 5; ++i) {
511 if (currentPos + skip_length > fileSize)
continue;
512 bf.SetLimit(currentPos + skip_length);
513 bf.SkipTo(currentPos + skip_length);
514 currentPos += skip_length;
520 if (find >= fileSize)
522 bf.FindByte(std::byte(find));
527 bf.SetLimit(currentPos + 1);
536 bool okay = bf.SetPos(requestPos);
541 currentPos = bf.GetPos();
544 if (requestPos <= maxPos &&
545 maxPos > rewindSize &&
546 requestPos >= maxPos - rewindSize) {
553 if (maxPos < currentPos)
557 fs::remove(streams_test_filename);
564 const std::string data{
"bitcoin"};
569 hash_verifier >> result;
#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 a CAutoFile& that implements a ring buffer to deserialize from.
void Write(uint64_t data, int nbits)
Write the nbits least significant bits of a 64-bit int to the output stream.
Non-refcounted RAII wrapper for FILE*.
Minimal stream for reading from an existing byte array by Span.
static uint64_t InsecureRandRange(uint64_t range)
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Double ended buffer combining vector and stream-like interfaces.
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
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.
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:...
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
void ignore(size_t nSize)
#define BOOST_CHECK(expr)