8#include <boost/test/unit_test.hpp>
16 unsigned char bytes[] = { 3, 4, 5, 6 };
17 std::vector<unsigned char> vch;
24 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
26 BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
30 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
32 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
37 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
39 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
44 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
46 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
51 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
53 BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
57 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
59 BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
64 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
66 BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
72 std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
117 std::vector<uint8_t> data{0x82, 0xa7, 0x31};
131 bit_writer.
Write(0, 1);
132 bit_writer.
Write(2, 2);
133 bit_writer.
Write(6, 3);
134 bit_writer.
Write(11, 4);
135 bit_writer.
Write(1, 5);
136 bit_writer.
Write(32, 6);
137 bit_writer.
Write(7, 7);
138 bit_writer.
Write(30497, 16);
142 uint32_t serialized_int1;
143 data >> serialized_int1;
145 uint16_t serialized_int2;
146 data >> serialized_int2;
163 std::vector<uint8_t> in;
164 std::vector<char> expected_xor;
165 std::vector<unsigned char> key;
170 key.push_back(
'\x00');
171 key.push_back(
'\x00');
174 std::string(expected_xor.begin(), expected_xor.end()),
177 in.push_back(
'\x0f');
178 in.push_back(
'\xf0');
179 expected_xor.push_back(
'\xf0');
180 expected_xor.push_back(
'\x0f');
188 key.push_back(
'\xff');
191 std::string(expected_xor.begin(), expected_xor.end()),
197 expected_xor.clear();
198 in.push_back(
'\xf0');
199 in.push_back(
'\x0f');
200 expected_xor.push_back(
'\x0f');
201 expected_xor.push_back(
'\x00');
207 key.push_back(
'\xff');
208 key.push_back(
'\x0f');
212 std::string(expected_xor.begin(), expected_xor.end()),
218 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
222 for (uint8_t j = 0; j < 40; ++j) {
223 fwrite(&j, 1, 1, file);
232 }
catch (
const std::exception& e) {
234 "Rewind limit must be less than buffer size") !=
nullptr);
272 }
catch (
const std::exception& e) {
274 "Read attempted past buffer limit") !=
nullptr);
282 for (uint8_t j = 3; j < 10; ++j) {
315 for (uint8_t j = 0; j <
sizeof(a); ++j) {
325 }
catch (
const std::exception& e) {
327 "CBufferedFile::Fill: end of file") !=
nullptr);
348 fs::remove(streams_test_filename);
356 fs::path streams_test_filename = m_args.GetDataDirBase() /
"streams_test_tmp";
357 for (
int rep = 0; rep < 50; ++rep) {
360 for (uint8_t i = 0; i < fileSize; ++i) {
361 fwrite(&i, 1, 1, file);
368 size_t currentPos = 0;
370 for (
int step = 0; step < 100; ++step) {
371 if (currentPos >= fileSize)
385 if (currentPos + 1 > fileSize)
389 for (uint8_t i = 0; i < 1; ++i) {
397 if (currentPos + 2 > fileSize)
401 for (uint8_t i = 0; i < 2; ++i) {
409 if (currentPos + 5 > fileSize)
413 for (uint8_t i = 0; i < 5; ++i) {
422 if (find >= fileSize)
424 bf.
FindByte(
static_cast<char>(find));
438 bool okay = bf.
SetPos(requestPos);
446 if (requestPos <= maxPos &&
447 maxPos > rewindSize &&
448 requestPos >= maxPos - rewindSize) {
455 if (maxPos < currentPos)
459 fs::remove(streams_test_filename);
uint64_t Read(int nbits)
Read the specified number of bits from the stream.
void Write(uint64_t data, int nbits)
Write the nbits least significant bits of a 64-bit int to the output stream.
void Flush()
Flush any unwritten bits to the output stream, padding with 0's to the next byte boundary.
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.
bool SetLimit(uint64_t nPos=std::numeric_limits< uint64_t >::max())
prevent reading beyond a certain position no argument removes the limit
void FindByte(char ch)
search for a given byte in the stream, and remain positioned on it
uint64_t GetPos() const
return the current reading position
bool SetPos(uint64_t nPos)
rewind to a given reading position
bool eof() const
check whether we're at the end of the source file
Double ended buffer combining vector and stream-like interfaces.
const_iterator begin() const
iterator insert(iterator it, const uint8_t x)
void Xor(const std::vector< unsigned char > &key)
XOR the contents of this stream with a certain key.
const_iterator end() const
Minimal stream for reading from an existing vector by reference.
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
BOOST_AUTO_TEST_SUITE_END()
FILE * fopen(const fs::path &p, const char *mode)
#define BOOST_CHECK_THROW(stmt, excMatch)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
@ ZEROS
Seed with a compile time constant of zeros.
static uint64_t InsecureRandRange(uint64_t range)
static void SeedInsecureRand(SeedRand seed=SeedRand::SEED)
BOOST_AUTO_TEST_CASE(streams_vector_writer)
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation