Bitcoin Core  26.1.0
P2P Digital Currency
streams_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2022 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 <streams.h>
6 #include <test/util/random.h>
8 #include <util/fs.h>
9 #include <util/strencodings.h>
10 
11 #include <boost/test/unit_test.hpp>
12 
13 using namespace std::string_literals;
14 
15 BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
16 
18 {
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}};
24  {
25  // Check errors for missing file
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"});
30  }
31  {
32 #ifdef __MINGW64__
33  // Our usage of mingw-w64 and the msvcrt runtime does not support
34  // the x modifier for the _wfopen().
35  const char* mode = "wb";
36 #else
37  const char* mode = "wbx";
38 #endif
39  AutoFile xor_file{raw_file(mode), xor_pat};
40  xor_file << test1 << test2;
41  }
42  {
43  // Read raw from disk
44  AutoFile non_xor_file{raw_file("rb")};
45  std::vector<std::byte> raw(7);
46  non_xor_file >> Span{raw};
47  BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa");
48  // Check that no padding exists
49  BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
50  }
51  {
52  AutoFile xor_file{raw_file("rb"), xor_pat};
53  std::vector<std::byte> read1, read2;
54  xor_file >> read1 >> read2;
56  BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
57  // Check that eof was reached
58  BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
59  }
60  {
61  AutoFile xor_file{raw_file("rb"), xor_pat};
62  std::vector<std::byte> read2;
63  // Check that ignore works
64  xor_file.ignore(4);
65  xor_file >> read2;
66  BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
67  // Check that ignore and read fail now
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"});
70  }
71 }
72 
73 BOOST_AUTO_TEST_CASE(streams_vector_writer)
74 {
75  unsigned char a(1);
76  unsigned char b(2);
77  unsigned char bytes[] = { 3, 4, 5, 6 };
78  std::vector<unsigned char> vch;
79 
80  // Each test runs twice. Serializing a second time at the same starting
81  // point should yield the same results, even if the first test grew the
82  // vector.
83 
84  CVectorWriter{INIT_PROTO_VERSION, vch, 0, a, b};
85  BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
86  CVectorWriter{INIT_PROTO_VERSION, vch, 0, a, b};
87  BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
88  vch.clear();
89 
90  CVectorWriter{INIT_PROTO_VERSION, vch, 2, a, b};
91  BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
92  CVectorWriter{INIT_PROTO_VERSION, vch, 2, a, b};
93  BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
94  vch.clear();
95 
96  vch.resize(5, 0);
97  CVectorWriter{INIT_PROTO_VERSION, vch, 2, a, b};
98  BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
99  CVectorWriter{INIT_PROTO_VERSION, vch, 2, a, b};
100  BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
101  vch.clear();
102 
103  vch.resize(4, 0);
104  CVectorWriter{INIT_PROTO_VERSION, vch, 3, a, b};
105  BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
106  CVectorWriter{INIT_PROTO_VERSION, vch, 3, a, b};
107  BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
108  vch.clear();
109 
110  vch.resize(4, 0);
111  CVectorWriter{INIT_PROTO_VERSION, vch, 4, a, b};
112  BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
113  CVectorWriter{INIT_PROTO_VERSION, vch, 4, a, b};
114  BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
115  vch.clear();
116 
117  CVectorWriter{INIT_PROTO_VERSION, vch, 0, bytes};
118  BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
119  CVectorWriter{INIT_PROTO_VERSION, vch, 0, bytes};
120  BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
121  vch.clear();
122 
123  vch.resize(4, 8);
124  CVectorWriter{INIT_PROTO_VERSION, vch, 2, a, bytes, b};
125  BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
126  CVectorWriter{INIT_PROTO_VERSION, vch, 2, a, bytes, b};
127  BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
128  vch.clear();
129 }
130 
131 BOOST_AUTO_TEST_CASE(streams_vector_reader)
132 {
133  std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
134 
135  SpanReader reader{INIT_PROTO_VERSION, vch};
136  BOOST_CHECK_EQUAL(reader.size(), 6U);
137  BOOST_CHECK(!reader.empty());
138 
139  // Read a single byte as an unsigned char.
140  unsigned char a;
141  reader >> a;
142  BOOST_CHECK_EQUAL(a, 1);
143  BOOST_CHECK_EQUAL(reader.size(), 5U);
144  BOOST_CHECK(!reader.empty());
145 
146  // Read a single byte as a signed char.
147  signed char b;
148  reader >> b;
149  BOOST_CHECK_EQUAL(b, -1);
150  BOOST_CHECK_EQUAL(reader.size(), 4U);
151  BOOST_CHECK(!reader.empty());
152 
153  // Read a 4 bytes as an unsigned int.
154  unsigned int c;
155  reader >> c;
156  BOOST_CHECK_EQUAL(c, 100992003U); // 3,4,5,6 in little-endian base-256
157  BOOST_CHECK_EQUAL(reader.size(), 0U);
158  BOOST_CHECK(reader.empty());
159 
160  // Reading after end of byte vector throws an error.
161  signed int d;
162  BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
163 
164  // Read a 4 bytes as a signed int from the beginning of the buffer.
165  SpanReader new_reader{INIT_PROTO_VERSION, vch};
166  new_reader >> d;
167  BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
168  BOOST_CHECK_EQUAL(new_reader.size(), 2U);
169  BOOST_CHECK(!new_reader.empty());
170 
171  // Reading after end of byte vector throws an error even if the reader is
172  // not totally empty.
173  BOOST_CHECK_THROW(new_reader >> d, std::ios_base::failure);
174 }
175 
176 BOOST_AUTO_TEST_CASE(streams_vector_reader_rvalue)
177 {
178  std::vector<uint8_t> data{0x82, 0xa7, 0x31};
179  SpanReader reader{INIT_PROTO_VERSION, data};
180  uint32_t varint = 0;
181  // Deserialize into r-value
182  reader >> VARINT(varint);
183  BOOST_CHECK_EQUAL(varint, 54321U);
184  BOOST_CHECK(reader.empty());
185 }
186 
187 BOOST_AUTO_TEST_CASE(bitstream_reader_writer)
188 {
189  DataStream data{};
190 
191  BitStreamWriter bit_writer{data};
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);
200  bit_writer.Flush();
201 
202  DataStream data_copy{data};
203  uint32_t serialized_int1;
204  data >> serialized_int1;
205  BOOST_CHECK_EQUAL(serialized_int1, uint32_t{0x7700C35A}); // NOTE: Serialized as LE
206  uint16_t serialized_int2;
207  data >> serialized_int2;
208  BOOST_CHECK_EQUAL(serialized_int2, uint16_t{0x1072}); // NOTE: Serialized as LE
209 
210  BitStreamReader bit_reader{data_copy};
211  BOOST_CHECK_EQUAL(bit_reader.Read(1), 0U);
212  BOOST_CHECK_EQUAL(bit_reader.Read(2), 2U);
213  BOOST_CHECK_EQUAL(bit_reader.Read(3), 6U);
214  BOOST_CHECK_EQUAL(bit_reader.Read(4), 11U);
215  BOOST_CHECK_EQUAL(bit_reader.Read(5), 1U);
216  BOOST_CHECK_EQUAL(bit_reader.Read(6), 32U);
217  BOOST_CHECK_EQUAL(bit_reader.Read(7), 7U);
218  BOOST_CHECK_EQUAL(bit_reader.Read(16), 30497U);
219  BOOST_CHECK_THROW(bit_reader.Read(8), std::ios_base::failure);
220 }
221 
222 BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
223 {
224  std::vector<std::byte> in;
225 
226  // Degenerate case
227  {
228  DataStream ds{in};
229  ds.Xor({0x00, 0x00});
230  BOOST_CHECK_EQUAL(""s, ds.str());
231  }
232 
233  in.push_back(std::byte{0x0f});
234  in.push_back(std::byte{0xf0});
235 
236  // Single character key
237  {
238  DataStream ds{in};
239  ds.Xor({0xff});
240  BOOST_CHECK_EQUAL("\xf0\x0f"s, ds.str());
241  }
242 
243  // Multi character key
244 
245  in.clear();
246  in.push_back(std::byte{0xf0});
247  in.push_back(std::byte{0x0f});
248 
249  {
250  DataStream ds{in};
251  ds.Xor({0xff, 0x0f});
252  BOOST_CHECK_EQUAL("\x0f\x00"s, ds.str());
253  }
254 }
255 
256 BOOST_AUTO_TEST_CASE(streams_buffered_file)
257 {
258  fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
259  CAutoFile file{fsbridge::fopen(streams_test_filename, "w+b"), 333};
260 
261  // The value at each offset is the offset.
262  for (uint8_t j = 0; j < 40; ++j) {
263  file << j;
264  }
265  std::rewind(file.Get());
266 
267  // The buffer size (second arg) must be greater than the rewind
268  // amount (third arg).
269  try {
270  BufferedFile bfbad{file, 25, 25};
271  BOOST_CHECK(false);
272  } catch (const std::exception& e) {
273  BOOST_CHECK(strstr(e.what(),
274  "Rewind limit must be less than buffer size") != nullptr);
275  }
276 
277  // The buffer is 25 bytes, allow rewinding 10 bytes.
278  BufferedFile bf{file, 25, 10};
279  BOOST_CHECK(!bf.eof());
280 
281  // This member has no functional effect.
282  BOOST_CHECK_EQUAL(bf.GetVersion(), 333);
283 
284  uint8_t i;
285  bf >> i;
286  BOOST_CHECK_EQUAL(i, 0);
287  bf >> i;
288  BOOST_CHECK_EQUAL(i, 1);
289 
290  // After reading bytes 0 and 1, we're positioned at 2.
291  BOOST_CHECK_EQUAL(bf.GetPos(), 2U);
292 
293  // Rewind to offset 0, ok (within the 10 byte window).
294  BOOST_CHECK(bf.SetPos(0));
295  bf >> i;
296  BOOST_CHECK_EQUAL(i, 0);
297 
298  // We can go forward to where we've been, but beyond may fail.
299  BOOST_CHECK(bf.SetPos(2));
300  bf >> i;
301  BOOST_CHECK_EQUAL(i, 2);
302 
303  // If you know the maximum number of bytes that should be
304  // read to deserialize the variable, you can limit the read
305  // extent. The current file offset is 3, so the following
306  // SetLimit() allows zero bytes to be read.
307  BOOST_CHECK(bf.SetLimit(3));
308  try {
309  bf >> i;
310  BOOST_CHECK(false);
311  } catch (const std::exception& e) {
312  BOOST_CHECK(strstr(e.what(),
313  "Attempt to position past buffer limit") != nullptr);
314  }
315  // The default argument removes the limit completely.
316  BOOST_CHECK(bf.SetLimit());
317  // The read position should still be at 3 (no change).
318  BOOST_CHECK_EQUAL(bf.GetPos(), 3U);
319 
320  // Read from current offset, 3, forward until position 10.
321  for (uint8_t j = 3; j < 10; ++j) {
322  bf >> i;
323  BOOST_CHECK_EQUAL(i, j);
324  }
325  BOOST_CHECK_EQUAL(bf.GetPos(), 10U);
326 
327  // We're guaranteed (just barely) to be able to rewind to zero.
328  BOOST_CHECK(bf.SetPos(0));
329  BOOST_CHECK_EQUAL(bf.GetPos(), 0U);
330  bf >> i;
331  BOOST_CHECK_EQUAL(i, 0);
332 
333  // We can set the position forward again up to the farthest
334  // into the stream we've been, but no farther. (Attempting
335  // to go farther may succeed, but it's not guaranteed.)
336  BOOST_CHECK(bf.SetPos(10));
337  bf >> i;
338  BOOST_CHECK_EQUAL(i, 10);
339  BOOST_CHECK_EQUAL(bf.GetPos(), 11U);
340 
341  // Now it's only guaranteed that we can rewind to offset 1
342  // (current read position, 11, minus rewind amount, 10).
343  BOOST_CHECK(bf.SetPos(1));
344  BOOST_CHECK_EQUAL(bf.GetPos(), 1U);
345  bf >> i;
346  BOOST_CHECK_EQUAL(i, 1);
347 
348  // We can stream into large variables, even larger than
349  // the buffer size.
350  BOOST_CHECK(bf.SetPos(11));
351  {
352  uint8_t a[40 - 11];
353  bf >> a;
354  for (uint8_t j = 0; j < sizeof(a); ++j) {
355  BOOST_CHECK_EQUAL(a[j], 11 + j);
356  }
357  }
358  BOOST_CHECK_EQUAL(bf.GetPos(), 40U);
359 
360  // We've read the entire file, the next read should throw.
361  try {
362  bf >> i;
363  BOOST_CHECK(false);
364  } catch (const std::exception& e) {
365  BOOST_CHECK(strstr(e.what(),
366  "BufferedFile::Fill: end of file") != nullptr);
367  }
368  // Attempting to read beyond the end sets the EOF indicator.
369  BOOST_CHECK(bf.eof());
370 
371  // Still at offset 40, we can go back 10, to 30.
372  BOOST_CHECK_EQUAL(bf.GetPos(), 40U);
373  BOOST_CHECK(bf.SetPos(30));
374  bf >> i;
375  BOOST_CHECK_EQUAL(i, 30);
376  BOOST_CHECK_EQUAL(bf.GetPos(), 31U);
377 
378  // We're too far to rewind to position zero.
379  BOOST_CHECK(!bf.SetPos(0));
380  // But we should now be positioned at least as far back as allowed
381  // by the rewind window (relative to our farthest read position, 40).
382  BOOST_CHECK(bf.GetPos() <= 30U);
383 
384  // We can explicitly close the file, or the destructor will do it.
385  file.fclose();
386 
387  fs::remove(streams_test_filename);
388 }
389 
390 BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
391 {
392  fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
393  CAutoFile file{fsbridge::fopen(streams_test_filename, "w+b"), 333};
394  // The value at each offset is the byte offset (e.g. byte 1 in the file has the value 0x01).
395  for (uint8_t j = 0; j < 40; ++j) {
396  file << j;
397  }
398  std::rewind(file.Get());
399 
400  // The buffer is 25 bytes, allow rewinding 10 bytes.
401  BufferedFile bf{file, 25, 10};
402 
403  uint8_t i;
404  // This is like bf >> (7-byte-variable), in that it will cause data
405  // to be read from the file into memory, but it's not copied to us.
406  bf.SkipTo(7);
407  BOOST_CHECK_EQUAL(bf.GetPos(), 7U);
408  bf >> i;
409  BOOST_CHECK_EQUAL(i, 7);
410 
411  // The bytes in the buffer up to offset 7 are valid and can be read.
412  BOOST_CHECK(bf.SetPos(0));
413  bf >> i;
414  BOOST_CHECK_EQUAL(i, 0);
415  bf >> i;
416  BOOST_CHECK_EQUAL(i, 1);
417 
418  bf.SkipTo(11);
419  bf >> i;
420  BOOST_CHECK_EQUAL(i, 11);
421 
422  // SkipTo() honors the transfer limit; we can't position beyond the limit.
423  bf.SetLimit(13);
424  try {
425  bf.SkipTo(14);
426  BOOST_CHECK(false);
427  } catch (const std::exception& e) {
428  BOOST_CHECK(strstr(e.what(), "Attempt to position past buffer limit") != nullptr);
429  }
430 
431  // We can position exactly to the transfer limit.
432  bf.SkipTo(13);
433  BOOST_CHECK_EQUAL(bf.GetPos(), 13U);
434 
435  file.fclose();
436  fs::remove(streams_test_filename);
437 }
438 
439 BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
440 {
441  // Make this test deterministic.
443 
444  fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
445  for (int rep = 0; rep < 50; ++rep) {
446  CAutoFile file{fsbridge::fopen(streams_test_filename, "w+b"), 333};
447  size_t fileSize = InsecureRandRange(256);
448  for (uint8_t i = 0; i < fileSize; ++i) {
449  file << i;
450  }
451  std::rewind(file.Get());
452 
453  size_t bufSize = InsecureRandRange(300) + 1;
454  size_t rewindSize = InsecureRandRange(bufSize);
455  BufferedFile bf{file, bufSize, rewindSize};
456  size_t currentPos = 0;
457  size_t maxPos = 0;
458  for (int step = 0; step < 100; ++step) {
459  if (currentPos >= fileSize)
460  break;
461 
462  // We haven't read to the end of the file yet.
463  BOOST_CHECK(!bf.eof());
464  BOOST_CHECK_EQUAL(bf.GetPos(), currentPos);
465 
466  // Pretend the file consists of a series of objects of varying
467  // sizes; the boundaries of the objects can interact arbitrarily
468  // with the CBufferFile's internal buffer. These first three
469  // cases simulate objects of various sizes (1, 2, 5 bytes).
470  switch (InsecureRandRange(6)) {
471  case 0: {
472  uint8_t a[1];
473  if (currentPos + 1 > fileSize)
474  continue;
475  bf.SetLimit(currentPos + 1);
476  bf >> a;
477  for (uint8_t i = 0; i < 1; ++i) {
478  BOOST_CHECK_EQUAL(a[i], currentPos);
479  currentPos++;
480  }
481  break;
482  }
483  case 1: {
484  uint8_t a[2];
485  if (currentPos + 2 > fileSize)
486  continue;
487  bf.SetLimit(currentPos + 2);
488  bf >> a;
489  for (uint8_t i = 0; i < 2; ++i) {
490  BOOST_CHECK_EQUAL(a[i], currentPos);
491  currentPos++;
492  }
493  break;
494  }
495  case 2: {
496  uint8_t a[5];
497  if (currentPos + 5 > fileSize)
498  continue;
499  bf.SetLimit(currentPos + 5);
500  bf >> a;
501  for (uint8_t i = 0; i < 5; ++i) {
502  BOOST_CHECK_EQUAL(a[i], currentPos);
503  currentPos++;
504  }
505  break;
506  }
507  case 3: {
508  // SkipTo is similar to the "read" cases above, except
509  // we don't receive the data.
510  size_t skip_length{static_cast<size_t>(InsecureRandRange(5))};
511  if (currentPos + skip_length > fileSize) continue;
512  bf.SetLimit(currentPos + skip_length);
513  bf.SkipTo(currentPos + skip_length);
514  currentPos += skip_length;
515  break;
516  }
517  case 4: {
518  // Find a byte value (that is at or ahead of the current position).
519  size_t find = currentPos + InsecureRandRange(8);
520  if (find >= fileSize)
521  find = fileSize - 1;
522  bf.FindByte(std::byte(find));
523  // The value at each offset is the offset.
524  BOOST_CHECK_EQUAL(bf.GetPos(), find);
525  currentPos = find;
526 
527  bf.SetLimit(currentPos + 1);
528  uint8_t i;
529  bf >> i;
530  BOOST_CHECK_EQUAL(i, currentPos);
531  currentPos++;
532  break;
533  }
534  case 5: {
535  size_t requestPos = InsecureRandRange(maxPos + 4);
536  bool okay = bf.SetPos(requestPos);
537  // The new position may differ from the requested position
538  // because we may not be able to rewind beyond the rewind
539  // window, and we may not be able to move forward beyond the
540  // farthest position we've reached so far.
541  currentPos = bf.GetPos();
542  BOOST_CHECK_EQUAL(okay, currentPos == requestPos);
543  // Check that we can position within the rewind window.
544  if (requestPos <= maxPos &&
545  maxPos > rewindSize &&
546  requestPos >= maxPos - rewindSize) {
547  // We requested a position within the rewind window.
548  BOOST_CHECK(okay);
549  }
550  break;
551  }
552  }
553  if (maxPos < currentPos)
554  maxPos = currentPos;
555  }
556  }
557  fs::remove(streams_test_filename);
558 }
559 
560 BOOST_AUTO_TEST_CASE(streams_hashed)
561 {
562  DataStream stream{};
563  HashedSourceWriter hash_writer{stream};
564  const std::string data{"bitcoin"};
565  hash_writer << data;
566 
567  HashVerifier hash_verifier{stream};
568  std::string result;
569  hash_verifier >> result;
570  BOOST_CHECK_EQUAL(data, result);
571  BOOST_CHECK_EQUAL(hash_writer.GetHash(), hash_verifier.GetHash());
572 }
573 
#define VARINT(obj)
Definition: serialize.h:522
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:25
const std::string test1
void SkipTo(const uint64_t file_pos)
Move the read position ahead in the stream to the given position.
Definition: streams.h:645
Wrapper around a CAutoFile& that implements a ring buffer to deserialize from.
Definition: streams.h:569
void Write(uint64_t data, int nbits)
Write the nbits least significant bits of a 64-bit int to the output stream.
Definition: streams.h:433
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:470
Basic testing setup.
Definition: setup_common.h:49
Minimal stream for reading from an existing byte array by Span.
Definition: streams.h:146
static uint64_t InsecureRandRange(uint64_t range)
Definition: random.h:60
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.
Definition: streams.h:192
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
Definition: setup_common.h:200
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
Definition: streams.h:322
Writes data to an underlying source stream, while hashing the written data.
Definition: hash.h:202
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:168
A Span is an object that can refer to a contiguous sequence of objects.
Definition: solver.h:20
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:...
Definition: fs.h:30
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
Definition: random.h:36
void ignore(size_t nSize)
Definition: streams.cpp:31
#define BOOST_CHECK(expr)
Definition: object.cpp:17