Branch data Line data Source code
1 : : // Copyright (c) 2012-present 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 <flatfile.h>
6 : : #include <node/blockstorage.h>
7 : : #include <streams.h>
8 : : #include <test/util/common.h>
9 : : #include <test/util/random.h>
10 : : #include <test/util/setup_common.h>
11 : : #include <util/fs.h>
12 : : #include <util/obfuscation.h>
13 : : #include <util/strencodings.h>
14 : :
15 : : #include <boost/test/unit_test.hpp>
16 : :
17 : : using namespace std::string_literals;
18 : : using namespace util::hex_literals;
19 : :
20 : : BOOST_FIXTURE_TEST_SUITE(streams_tests, BasicTestingSetup)
21 : :
22 : : // Check optimized obfuscation with random offsets and sizes to ensure proper
23 : : // handling of key wrapping. Also verify it roundtrips.
24 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(xor_random_chunks)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
25 : : {
26 : 201 : auto apply_random_xor_chunks{[&](std::span<std::byte> target, const Obfuscation& obfuscation) {
27 [ + + ]: 1077 : for (size_t offset{0}; offset < target.size();) {
28 : 877 : const size_t chunk_size{1 + m_rng.randrange(target.size() - offset)};
29 [ - + ]: 877 : obfuscation(target.subspan(offset, chunk_size), offset);
30 : 877 : offset += chunk_size;
31 : : }
32 : 201 : }};
33 : :
34 [ + + ]: 101 : for (size_t test{0}; test < 100; ++test) {
35 : 100 : const size_t write_size{1 + m_rng.randrange(100U)};
36 : 100 : const std::vector original{m_rng.randbytes<std::byte>(write_size)};
37 [ + - ]: 100 : std::vector roundtrip{original};
38 : :
39 [ + + ]: 100 : const auto key_bytes{m_rng.randbool() ? m_rng.randbytes<Obfuscation::KEY_SIZE>() : std::array<std::byte, Obfuscation::KEY_SIZE>{}};
40 : 100 : const Obfuscation obfuscation{key_bytes};
41 [ - + ]: 100 : apply_random_xor_chunks(roundtrip, obfuscation);
42 [ + - - + : 100 : BOOST_CHECK_EQUAL(roundtrip.size(), original.size());
- + + - ]
43 [ - + + + ]: 5247 : for (size_t i{0}; i < original.size(); ++i) {
44 [ + - + - ]: 5147 : BOOST_CHECK_EQUAL(roundtrip[i], original[i] ^ key_bytes[i % Obfuscation::KEY_SIZE]);
45 : : }
46 : :
47 [ - + ]: 100 : apply_random_xor_chunks(roundtrip, obfuscation);
48 [ + - + - : 200 : BOOST_CHECK_EQUAL_COLLECTIONS(roundtrip.begin(), roundtrip.end(), original.begin(), original.end());
+ - ]
49 : 100 : }
50 : 1 : }
51 : :
52 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(obfuscation_hexkey)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
53 : : {
54 : 1 : const auto key_bytes{m_rng.randbytes<Obfuscation::KEY_SIZE>()};
55 : :
56 : 1 : const Obfuscation obfuscation{key_bytes};
57 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(obfuscation.HexKey(), HexStr(key_bytes));
58 : 1 : }
59 : :
60 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(obfuscation_serialize)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
61 : : {
62 : 1 : Obfuscation obfuscation{};
63 [ + - ]: 2 : BOOST_CHECK(!obfuscation);
64 : :
65 : : // Test loading a key.
66 : 1 : std::vector key_in{m_rng.randbytes<std::byte>(Obfuscation::KEY_SIZE)};
67 : 1 : DataStream ds_in;
68 [ + - ]: 1 : ds_in << key_in;
69 [ + - - + : 1 : BOOST_CHECK_EQUAL(ds_in.size(), 1 + Obfuscation::KEY_SIZE); // serialized as a vector
+ - ]
70 [ + - ]: 1 : ds_in >> obfuscation;
71 : :
72 : : // Test saving the key.
73 : 1 : std::vector<std::byte> key_out;
74 : 1 : DataStream ds_out;
75 [ + - ]: 1 : ds_out << obfuscation;
76 [ + - ]: 1 : ds_out >> key_out;
77 : :
78 : : // Make sure saved key is the same.
79 [ + - + - : 2 : BOOST_CHECK_EQUAL_COLLECTIONS(key_in.begin(), key_in.end(), key_out.begin(), key_out.end());
+ - ]
80 : 1 : }
81 : :
82 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(obfuscation_empty)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
83 : : {
84 : 1 : const Obfuscation null_obf{};
85 [ + - ]: 2 : BOOST_CHECK(!null_obf);
86 : :
87 : 1 : const Obfuscation non_null_obf{"ff00ff00ff00ff00"_hex};
88 [ + - ]: 2 : BOOST_CHECK(non_null_obf);
89 : 1 : }
90 : :
91 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(streams_scoped_data_stream_usage)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
92 : : {
93 : 1 : DataStream stream{};
94 : 1 : {
95 : 1 : ScopedDataStreamUsage usage{stream};
96 [ + - ]: 1 : stream << uint8_t{42};
97 [ + - - + : 1 : BOOST_CHECK_GT(stream.size(), 0U);
+ - ]
98 : 1 : }
99 [ + - - + : 2 : BOOST_CHECK(stream.empty());
+ - ]
100 : :
101 : 1 : {
102 : 1 : ScopedDataStreamUsage usage{stream};
103 [ + - ]: 1 : stream << uint16_t{42};
104 [ + - - + : 1 : BOOST_CHECK_GT(stream.size(), 0U);
+ - ]
105 : 1 : }
106 [ + - - + : 2 : BOOST_CHECK(stream.empty());
+ - ]
107 : 1 : }
108 : :
109 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(xor_file)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
110 : : {
111 [ + - ]: 2 : fs::path xor_path{m_args.GetDataDirBase() / "test_xor.bin"};
112 : 6 : auto raw_file{[&](const auto& mode) { return fsbridge::fopen(xor_path, mode); }};
113 [ + - ]: 1 : const std::vector<uint8_t> test1{1, 2, 3};
114 [ + - ]: 1 : const std::vector<uint8_t> test2{4, 5};
115 : 1 : const Obfuscation obfuscation{"ff00ff00ff00ff00"_hex};
116 : :
117 : 1 : {
118 : : // Check errors for missing file
119 [ + - + - ]: 1 : AutoFile xor_file{raw_file("rb"), obfuscation};
120 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullptr"});
- - - - -
+ + - + -
+ - ]
121 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullptr"});
- - - - -
+ + - + -
+ - ]
122 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullptr"});
- - - - -
+ + - + -
+ - ]
123 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(xor_file.size(), std::ios_base::failure, HasReason{"AutoFile::size: file handle is nullptr"});
- - - - -
+ + - + -
+ - ]
124 : 1 : }
125 : 1 : {
126 : : #ifdef __MINGW64__
127 : : // Temporary workaround for https://github.com/bitcoin/bitcoin/issues/30210
128 : : const char* mode = "wb";
129 : : #else
130 : 1 : const char* mode = "wbx";
131 : : #endif
132 [ + - + - ]: 1 : AutoFile xor_file{raw_file(mode), obfuscation};
133 [ + - + - ]: 1 : xor_file << test1 << test2;
134 [ + - + - : 1 : BOOST_CHECK_EQUAL(xor_file.size(), 7);
+ - ]
135 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(xor_file.fclose(), 0);
+ - ]
136 : 1 : }
137 : 1 : {
138 : : // Read raw from disk
139 [ + - + - ]: 2 : AutoFile non_xor_file{raw_file("rb")};
140 [ + - ]: 1 : std::vector<std::byte> raw(7);
141 [ - + + - ]: 1 : non_xor_file >> std::span{raw};
142 [ + - - + : 1 : BOOST_CHECK_EQUAL(HexStr(raw), "fc01fd03fd04fa");
+ - + - ]
143 : : // Check that no padding exists
144 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
- - - - -
+ + - + -
+ - ]
145 [ + - + - : 1 : BOOST_CHECK_EQUAL(non_xor_file.size(), 7);
+ - ]
146 : 1 : }
147 : 1 : {
148 [ + - + - ]: 1 : AutoFile xor_file{raw_file("rb"), obfuscation};
149 : 1 : std::vector<std::byte> read1, read2;
150 [ + - + - ]: 1 : xor_file >> read1 >> read2;
151 [ + - - + : 2 : BOOST_CHECK_EQUAL(HexStr(read1), HexStr(test1));
+ - + - +
- ]
152 [ + - - + : 2 : BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
+ - + - +
- ]
153 : : // Check that eof was reached
154 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
- - - - -
+ + - + -
+ - ]
155 [ + - + - : 1 : BOOST_CHECK_EQUAL(xor_file.size(), 7);
+ - ]
156 : 1 : }
157 : 1 : {
158 [ + - + - ]: 1 : AutoFile xor_file{raw_file("rb"), obfuscation};
159 : 1 : std::vector<std::byte> read2;
160 : : // Check that ignore works
161 [ + - ]: 1 : xor_file.ignore(4);
162 [ + - ]: 1 : xor_file >> read2;
163 [ + - - + : 2 : BOOST_CHECK_EQUAL(HexStr(read2), HexStr(test2));
+ - + - +
- ]
164 : : // Check that ignore and read fail now
165 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"});
- - - - -
+ + - + -
+ - ]
166 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
- - - - -
+ + - + -
+ - ]
167 [ + - + - : 1 : BOOST_CHECK_EQUAL(xor_file.size(), 7);
+ - ]
168 : 1 : }
169 : 2 : }
170 : :
171 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(streams_vector_writer)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
172 : : {
173 : 1 : unsigned char a(1);
174 : 1 : unsigned char b(2);
175 : 1 : unsigned char bytes[] = {3, 4, 5, 6};
176 : 1 : std::vector<unsigned char> vch;
177 : :
178 : : // Each test runs twice. Serializing a second time at the same starting
179 : : // point should yield the same results, even if the first test grew the
180 : : // vector.
181 : :
182 [ + - ]: 1 : VectorWriter{vch, 0, a, b};
183 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
+ - + - ]
184 [ + - ]: 1 : VectorWriter{vch, 0, a, b};
185 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{1, 2}}));
+ - + - ]
186 [ + - ]: 1 : vch.clear();
187 : :
188 [ + - ]: 1 : VectorWriter{vch, 2, a, b};
189 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
+ - + - +
- ]
190 [ + - ]: 1 : VectorWriter{vch, 2, a, b};
191 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2}}));
+ - + - ]
192 [ + - ]: 1 : vch.clear();
193 : :
194 [ + - ]: 1 : vch.resize(5, 0);
195 [ + - ]: 1 : VectorWriter{vch, 2, a, b};
196 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
+ - + - ]
197 [ + - ]: 1 : VectorWriter{vch, 2, a, b};
198 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 1, 2, 0}}));
+ - + - ]
199 [ + - ]: 1 : vch.clear();
200 : :
201 [ + - ]: 1 : vch.resize(4, 0);
202 [ + - ]: 1 : VectorWriter{vch, 3, a, b};
203 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
+ - + - ]
204 [ + - ]: 1 : VectorWriter{vch, 3, a, b};
205 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 1, 2}}));
+ - + - ]
206 [ + - ]: 1 : vch.clear();
207 : :
208 [ + - ]: 1 : vch.resize(4, 0);
209 [ + - ]: 1 : VectorWriter{vch, 4, a, b};
210 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
+ - + - ]
211 [ + - ]: 1 : VectorWriter{vch, 4, a, b};
212 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{0, 0, 0, 0, 1, 2}}));
+ - + - ]
213 [ + - ]: 1 : vch.clear();
214 : :
215 [ + - ]: 1 : VectorWriter{vch, 0, bytes};
216 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
+ - + - ]
217 [ + - ]: 1 : VectorWriter{vch, 0, bytes};
218 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{3, 4, 5, 6}}));
+ - + - ]
219 [ + - ]: 1 : vch.clear();
220 : :
221 [ + - ]: 1 : vch.resize(4, 8);
222 [ + - ]: 1 : VectorWriter{vch, 2, a, bytes, b};
223 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
+ - + - ]
224 [ + - ]: 1 : VectorWriter{vch, 2, a, bytes, b};
225 [ + - + - : 2 : BOOST_CHECK((vch == std::vector<unsigned char>{{8, 8, 1, 3, 4, 5, 6, 2}}));
+ - + - ]
226 [ + - ]: 2 : vch.clear();
227 : 1 : }
228 : :
229 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(streams_span_writer)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
230 : : {
231 : 1 : unsigned char a(1);
232 : 1 : unsigned char b(2);
233 : 1 : unsigned char bytes[] = {3, 4, 5, 6};
234 : 1 : std::array<std::byte, 8> arr{};
235 : :
236 : : // Test operator<<
237 : 1 : SpanWriter writer{arr};
238 : 1 : writer << a << b;
239 [ + - ]: 1 : BOOST_CHECK_EQUAL(HexStr(arr), "0102000000000000");
240 : :
241 : : // Use variadic constructor and write to subspan.
242 : 1 : SpanWriter{std::span{arr}.subspan(2), a, bytes, b};
243 [ + - ]: 1 : BOOST_CHECK_EQUAL(HexStr(arr), "0102010304050602");
244 : :
245 : : // Writing past the end throws
246 : 1 : std::array<std::byte, 1> small{};
247 [ + - - + : 2 : BOOST_CHECK_THROW(SpanWriter(std::span{small}, a, b), std::ios_base::failure);
- - - - -
+ + - +
- ]
248 [ + - + - : 2 : BOOST_CHECK_THROW(SpanWriter(std::span{small}) << a << b, std::ios_base::failure);
- + - - -
- - + + -
+ - ]
249 : 1 : }
250 : :
251 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(streams_vector_reader)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
252 : : {
253 : 1 : std::vector<unsigned char> vch = {1, 255, 3, 4, 5, 6};
254 : :
255 [ - + ]: 1 : SpanReader reader{vch};
256 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(reader.size(), 6U);
257 [ + - + - : 2 : BOOST_CHECK(!reader.empty());
+ - ]
258 : :
259 : : // Read a single byte as an unsigned char.
260 : 1 : unsigned char a;
261 [ + - ]: 1 : reader >> a;
262 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(a, 1);
263 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(reader.size(), 5U);
264 [ + - + - : 2 : BOOST_CHECK(!reader.empty());
+ - ]
265 : :
266 : : // Read a single byte as a int8_t.
267 : 1 : int8_t b;
268 [ + - ]: 1 : reader >> b;
269 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(b, -1);
270 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(reader.size(), 4U);
271 [ + - + - : 2 : BOOST_CHECK(!reader.empty());
+ - ]
272 : :
273 : : // Read a 4 bytes as an unsigned int.
274 : 1 : unsigned int c;
275 [ + - ]: 1 : reader >> c;
276 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(c, 100992003U); // 3,4,5,6 in little-endian base-256
277 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(reader.size(), 0U);
278 [ + - + - : 2 : BOOST_CHECK(reader.empty());
+ - ]
279 : :
280 : : // Reading after end of byte vector throws an error.
281 : 1 : signed int d;
282 [ + - - + : 2 : BOOST_CHECK_THROW(reader >> d, std::ios_base::failure);
- - - - -
+ + - +
- ]
283 : :
284 : : // Read a 4 bytes as a signed int from the beginning of the buffer.
285 [ - + ]: 1 : SpanReader new_reader{vch};
286 [ + - ]: 1 : new_reader >> d;
287 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(d, 67370753); // 1,255,3,4 in little-endian base-256
288 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(new_reader.size(), 2U);
289 [ + - + - : 2 : BOOST_CHECK(!new_reader.empty());
+ - ]
290 : :
291 : : // Reading after end of byte vector throws an error even if the reader is
292 : : // not totally empty.
293 [ + - - + : 2 : BOOST_CHECK_THROW(new_reader >> d, std::ios_base::failure);
- - - - -
+ + - +
- ]
294 : 1 : }
295 : :
296 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(streams_vector_reader_rvalue)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
297 : : {
298 : 1 : std::vector<uint8_t> data{0x82, 0xa7, 0x31};
299 [ - + ]: 1 : SpanReader reader{data};
300 : 1 : uint32_t varint = 0;
301 : : // Deserialize into r-value
302 [ + - ]: 1 : reader >> VARINT(varint);
303 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(varint, 54321U);
304 [ + - + - ]: 2 : BOOST_CHECK(reader.empty());
305 : 1 : }
306 : :
307 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(bitstream_reader_writer)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
308 : : {
309 : 1 : DataStream data{};
310 : :
311 [ + - ]: 1 : BitStreamWriter bit_writer{data};
312 [ + - ]: 1 : bit_writer.Write(0, 1);
313 [ + - ]: 1 : bit_writer.Write(2, 2);
314 [ + - ]: 1 : bit_writer.Write(6, 3);
315 [ + - ]: 1 : bit_writer.Write(11, 4);
316 [ + - ]: 1 : bit_writer.Write(1, 5);
317 [ + - ]: 1 : bit_writer.Write(32, 6);
318 [ + - ]: 1 : bit_writer.Write(7, 7);
319 [ + - ]: 1 : bit_writer.Write(30497, 16);
320 [ + - ]: 1 : bit_writer.Flush();
321 : :
322 [ + - ]: 1 : DataStream data_copy{data};
323 : 1 : uint32_t serialized_int1;
324 [ + - ]: 1 : data >> serialized_int1;
325 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(serialized_int1, uint32_t{0x7700C35A}); // NOTE: Serialized as LE
326 : 1 : uint16_t serialized_int2;
327 [ + - ]: 1 : data >> serialized_int2;
328 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(serialized_int2, uint16_t{0x1072}); // NOTE: Serialized as LE
329 : :
330 [ + - ]: 1 : BitStreamReader bit_reader{data_copy};
331 [ + - + - : 1 : BOOST_CHECK_EQUAL(bit_reader.Read(1), 0U);
+ - ]
332 [ + - + - : 1 : BOOST_CHECK_EQUAL(bit_reader.Read(2), 2U);
+ - ]
333 [ + - + - : 1 : BOOST_CHECK_EQUAL(bit_reader.Read(3), 6U);
+ - ]
334 [ + - + - : 1 : BOOST_CHECK_EQUAL(bit_reader.Read(4), 11U);
+ - ]
335 [ + - + - : 1 : BOOST_CHECK_EQUAL(bit_reader.Read(5), 1U);
+ - ]
336 [ + - + - : 1 : BOOST_CHECK_EQUAL(bit_reader.Read(6), 32U);
+ - ]
337 [ + - + - : 1 : BOOST_CHECK_EQUAL(bit_reader.Read(7), 7U);
+ - ]
338 [ + - + - : 1 : BOOST_CHECK_EQUAL(bit_reader.Read(16), 30497U);
+ - ]
339 [ + - - + : 2 : BOOST_CHECK_THROW(bit_reader.Read(8), std::ios_base::failure);
- - - - -
+ + - +
- ]
340 : 1 : }
341 : :
342 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(streams_serializedata_xor)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
343 : : {
344 : : // Degenerate case
345 : 1 : {
346 : 1 : DataStream ds{};
347 : 2 : Obfuscation{}(ds);
348 [ + - + - : 2 : BOOST_CHECK_EQUAL(""s, ds.str());
+ - ]
349 : 0 : }
350 : :
351 : 1 : {
352 : 1 : const Obfuscation obfuscation{"ffffffffffffffff"_hex};
353 : :
354 : 1 : DataStream ds{"0ff0"_hex};
355 [ - + ]: 1 : obfuscation(ds);
356 [ + - + - : 2 : BOOST_CHECK_EQUAL("\xf0\x0f"s, ds.str());
+ - ]
357 : 0 : }
358 : :
359 : 1 : {
360 : 1 : const Obfuscation obfuscation{"ff0fff0fff0fff0f"_hex};
361 : :
362 : 1 : DataStream ds{"f00f"_hex};
363 [ - + ]: 1 : obfuscation(ds);
364 [ + - + - : 2 : BOOST_CHECK_EQUAL("\x0f\x00"s, ds.str());
+ - ]
365 : 1 : }
366 : 1 : }
367 : :
368 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(streams_buffered_file)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
369 : : {
370 [ + - ]: 2 : fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
371 [ + - + - ]: 2 : AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
372 : :
373 : : // The value at each offset is the offset.
374 [ + + ]: 41 : for (uint8_t j = 0; j < 40; ++j) {
375 [ + - ]: 80 : file << j;
376 : : }
377 [ + - ]: 1 : file.seek(0, SEEK_SET);
378 : :
379 : : // The buffer size must be greater than the rewind amount.
380 [ + - - + : 2 : BOOST_CHECK_EXCEPTION((BufferedFile{file, /*nBufSize=*/25, /*nRewindIn=*/25}), std::ios_base::failure, HasReason{"Rewind limit must be less than buffer size"});
- - - - -
+ + - + -
+ - ]
381 : :
382 : : // The buffer is 25 bytes, allow rewinding 10 bytes.
383 [ + - ]: 1 : BufferedFile bf{file, 25, 10};
384 [ + - + - : 2 : BOOST_CHECK(!bf.eof());
+ - ]
385 : :
386 : 1 : uint8_t i;
387 [ + - ]: 1 : bf >> i;
388 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 0);
389 [ + - ]: 1 : bf >> i;
390 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 1);
391 : :
392 : : // After reading bytes 0 and 1, we're positioned at 2.
393 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 2U);
394 : :
395 : : // Rewind to offset 0, ok (within the 10 byte window).
396 [ + - - + : 3 : BOOST_CHECK(bf.SetPos(0));
+ - + - ]
397 [ + - ]: 1 : bf >> i;
398 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 0);
399 : :
400 : : // We can go forward to where we've been, but beyond may fail.
401 [ + - - + : 3 : BOOST_CHECK(bf.SetPos(2));
+ - + - ]
402 [ + - ]: 1 : bf >> i;
403 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 2);
404 : :
405 : : // If you know the maximum number of bytes that should be
406 : : // read to deserialize the variable, you can limit the read
407 : : // extent. The current file offset is 3, so the following
408 : : // SetLimit() allows zero bytes to be read.
409 [ + - + - : 3 : BOOST_CHECK(bf.SetLimit(3));
+ - + - ]
410 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(bf >> i, std::ios_base::failure, HasReason{"Attempt to position past buffer limit"});
- - - - -
+ + - + -
+ - ]
411 : : // The default argument removes the limit completely.
412 [ + - + - : 2 : BOOST_CHECK(bf.SetLimit());
+ - ]
413 : : // The read position should still be at 3 (no change).
414 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 3U);
415 : :
416 : : // Read from current offset, 3, forward until position 10.
417 [ + + ]: 8 : for (uint8_t j = 3; j < 10; ++j) {
418 [ + - ]: 7 : bf >> i;
419 [ + - + - ]: 7 : BOOST_CHECK_EQUAL(i, j);
420 : : }
421 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 10U);
422 : :
423 : : // We're guaranteed (just barely) to be able to rewind to zero.
424 [ + - - + : 3 : BOOST_CHECK(bf.SetPos(0));
+ - + - ]
425 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 0U);
426 [ + - ]: 1 : bf >> i;
427 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 0);
428 : :
429 : : // We can set the position forward again up to the farthest
430 : : // into the stream we've been, but no farther. (Attempting
431 : : // to go farther may succeed, but it's not guaranteed.)
432 [ + - - + : 3 : BOOST_CHECK(bf.SetPos(10));
+ - + - ]
433 [ + - ]: 1 : bf >> i;
434 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 10);
435 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 11U);
436 : :
437 : : // Now it's only guaranteed that we can rewind to offset 1
438 : : // (current read position, 11, minus rewind amount, 10).
439 [ + - - + : 3 : BOOST_CHECK(bf.SetPos(1));
+ - + - ]
440 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 1U);
441 [ + - ]: 1 : bf >> i;
442 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 1);
443 : :
444 : : // We can stream into large variables, even larger than
445 : : // the buffer size.
446 [ + - - + : 3 : BOOST_CHECK(bf.SetPos(11));
+ - + - ]
447 : 1 : {
448 : 1 : uint8_t a[40 - 11];
449 [ + - ]: 1 : bf >> a;
450 [ + + ]: 30 : for (uint8_t j = 0; j < sizeof(a); ++j) {
451 [ + - + - ]: 29 : BOOST_CHECK_EQUAL(a[j], 11 + j);
452 : : }
453 : : }
454 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 40U);
455 : :
456 : : // We've read the entire file, the next read should throw.
457 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(bf >> i, std::ios_base::failure, HasReason{"BufferedFile::Fill: end of file"});
- - - - -
+ + - + -
+ - ]
458 : : // Attempting to read beyond the end sets the EOF indicator.
459 [ + - + - : 2 : BOOST_CHECK(bf.eof());
+ - ]
460 : :
461 : : // Still at offset 40, we can go back 10, to 30.
462 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 40U);
463 [ + - - + : 3 : BOOST_CHECK(bf.SetPos(30));
+ - + - ]
464 [ + - ]: 1 : bf >> i;
465 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 30);
466 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 31U);
467 : :
468 : : // We're too far to rewind to position zero.
469 [ + - - + : 3 : BOOST_CHECK(!bf.SetPos(0));
+ - + - ]
470 : : // But we should now be positioned at least as far back as allowed
471 : : // by the rewind window (relative to our farthest read position, 40).
472 [ + - + - : 2 : BOOST_CHECK(bf.GetPos() <= 30U);
+ - ]
473 : :
474 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(file.fclose(), 0);
+ - ]
475 : :
476 [ + - ]: 1 : fs::remove(streams_test_filename);
477 : 2 : }
478 : :
479 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(streams_buffered_file_skip)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
480 : : {
481 [ + - ]: 2 : fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
482 [ + - + - ]: 2 : AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
483 : : // The value at each offset is the byte offset (e.g. byte 1 in the file has the value 0x01).
484 [ + + ]: 41 : for (uint8_t j = 0; j < 40; ++j) {
485 [ + - ]: 80 : file << j;
486 : : }
487 [ + - ]: 1 : file.seek(0, SEEK_SET);
488 : :
489 : : // The buffer is 25 bytes, allow rewinding 10 bytes.
490 [ + - ]: 1 : BufferedFile bf{file, 25, 10};
491 : :
492 : 1 : uint8_t i;
493 : : // This is like bf >> (7-byte-variable), in that it will cause data
494 : : // to be read from the file into memory, but it's not copied to us.
495 [ + - ]: 1 : bf.SkipTo(7);
496 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 7U);
497 [ + - ]: 1 : bf >> i;
498 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 7);
499 : :
500 : : // The bytes in the buffer up to offset 7 are valid and can be read.
501 [ + - - + : 3 : BOOST_CHECK(bf.SetPos(0));
+ - + - ]
502 [ + - ]: 1 : bf >> i;
503 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 0);
504 [ + - ]: 1 : bf >> i;
505 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 1);
506 : :
507 [ + - ]: 1 : bf.SkipTo(11);
508 [ + - ]: 1 : bf >> i;
509 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(i, 11);
510 : :
511 : : // SkipTo() honors the transfer limit; we can't position beyond the limit.
512 [ + - ]: 1 : bf.SetLimit(13);
513 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(bf.SkipTo(14), std::ios_base::failure, HasReason{"Attempt to position past buffer limit"});
- - - - -
+ + - + -
+ - ]
514 : :
515 : : // We can position exactly to the transfer limit.
516 [ + - ]: 1 : bf.SkipTo(13);
517 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(bf.GetPos(), 13U);
518 : :
519 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(file.fclose(), 0);
+ - ]
520 [ + - ]: 1 : fs::remove(streams_test_filename);
521 : 2 : }
522 : :
523 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
524 : : {
525 : : // Make this test deterministic.
526 : 1 : SeedRandomForTest(SeedRand::ZEROS);
527 : :
528 [ + - ]: 2 : fs::path streams_test_filename = m_args.GetDataDirBase() / "streams_test_tmp";
529 [ + + ]: 51 : for (int rep = 0; rep < 50; ++rep) {
530 [ + - + - ]: 100 : AutoFile file{fsbridge::fopen(streams_test_filename, "w+b")};
531 : 50 : size_t fileSize = m_rng.randrange(256);
532 [ + + ]: 6363 : for (uint8_t i = 0; i < fileSize; ++i) {
533 [ + - ]: 12626 : file << i;
534 : : }
535 [ + - ]: 50 : file.seek(0, SEEK_SET);
536 : :
537 : 50 : size_t bufSize = m_rng.randrange(300) + 1;
538 : 50 : size_t rewindSize = m_rng.randrange(bufSize);
539 [ + - ]: 50 : BufferedFile bf{file, bufSize, rewindSize};
540 : 50 : size_t currentPos = 0;
541 : 50 : size_t maxPos = 0;
542 [ + + ]: 3930 : for (int step = 0; step < 100; ++step) {
543 [ + + ]: 3901 : if (currentPos >= fileSize)
544 : : break;
545 : :
546 : : // We haven't read to the end of the file yet.
547 [ + - + - : 7760 : BOOST_CHECK(!bf.eof());
+ - ]
548 [ + - + - ]: 3880 : BOOST_CHECK_EQUAL(bf.GetPos(), currentPos);
549 : :
550 : : // Pretend the file consists of a series of objects of varying
551 : : // sizes; the boundaries of the objects can interact arbitrarily
552 : : // with the CBufferFile's internal buffer. These first three
553 : : // cases simulate objects of various sizes (1, 2, 5 bytes).
554 [ + + + + : 3880 : switch (m_rng.randrange(6)) {
+ + - ]
555 : 644 : case 0: {
556 : 644 : uint8_t a[1];
557 [ - + ]: 644 : if (currentPos + 1 > fileSize)
558 : 0 : continue;
559 [ + - ]: 644 : bf.SetLimit(currentPos + 1);
560 [ + - ]: 644 : bf >> a;
561 [ + + ]: 1288 : for (uint8_t i = 0; i < 1; ++i) {
562 [ + - + - ]: 644 : BOOST_CHECK_EQUAL(a[i], currentPos);
563 : 644 : currentPos++;
564 : : }
565 : : break;
566 : : }
567 : 641 : case 1: {
568 : 641 : uint8_t a[2];
569 [ + + ]: 641 : if (currentPos + 2 > fileSize)
570 : 6 : continue;
571 [ + - ]: 635 : bf.SetLimit(currentPos + 2);
572 [ + - ]: 635 : bf >> a;
573 [ + + ]: 1905 : for (uint8_t i = 0; i < 2; ++i) {
574 [ + - + - ]: 1270 : BOOST_CHECK_EQUAL(a[i], currentPos);
575 : 1270 : currentPos++;
576 : : }
577 : : break;
578 : : }
579 : 666 : case 2: {
580 : 666 : uint8_t a[5];
581 [ + + ]: 666 : if (currentPos + 5 > fileSize)
582 : 19 : continue;
583 [ + - ]: 647 : bf.SetLimit(currentPos + 5);
584 [ + - ]: 647 : bf >> a;
585 [ + + ]: 3882 : for (uint8_t i = 0; i < 5; ++i) {
586 [ + - + - ]: 3235 : BOOST_CHECK_EQUAL(a[i], currentPos);
587 : 3235 : currentPos++;
588 : : }
589 : : break;
590 : : }
591 : 620 : case 3: {
592 : : // SkipTo is similar to the "read" cases above, except
593 : : // we don't receive the data.
594 : 620 : size_t skip_length{static_cast<size_t>(m_rng.randrange(5))};
595 [ + + ]: 620 : if (currentPos + skip_length > fileSize) continue;
596 [ + - ]: 616 : bf.SetLimit(currentPos + skip_length);
597 [ + - ]: 616 : bf.SkipTo(currentPos + skip_length);
598 : 616 : currentPos += skip_length;
599 : 616 : break;
600 : : }
601 : 650 : case 4: {
602 : : // Find a byte value (that is at or ahead of the current position).
603 : 650 : size_t find = currentPos + m_rng.randrange(8);
604 [ + + ]: 650 : if (find >= fileSize)
605 : 8 : find = fileSize - 1;
606 [ + - ]: 650 : bf.FindByte(std::byte(find));
607 : : // The value at each offset is the offset.
608 [ + - + - ]: 650 : BOOST_CHECK_EQUAL(bf.GetPos(), find);
609 : 650 : currentPos = find;
610 : :
611 [ + - ]: 650 : bf.SetLimit(currentPos + 1);
612 : 650 : uint8_t i;
613 [ + - ]: 650 : bf >> i;
614 [ + - + - ]: 650 : BOOST_CHECK_EQUAL(i, currentPos);
615 : 650 : currentPos++;
616 : 650 : break;
617 : : }
618 : 659 : case 5: {
619 : 659 : size_t requestPos = m_rng.randrange(maxPos + 4);
620 [ - + ]: 659 : bool okay = bf.SetPos(requestPos);
621 : : // The new position may differ from the requested position
622 : : // because we may not be able to rewind beyond the rewind
623 : : // window, and we may not be able to move forward beyond the
624 : : // farthest position we've reached so far.
625 [ + - ]: 659 : currentPos = bf.GetPos();
626 [ + - + - ]: 659 : BOOST_CHECK_EQUAL(okay, currentPos == requestPos);
627 : : // Check that we can position within the rewind window.
628 : 659 : if (requestPos <= maxPos &&
629 [ + + ]: 659 : maxPos > rewindSize &&
630 [ + + ]: 327 : requestPos >= maxPos - rewindSize) {
631 : : // We requested a position within the rewind window.
632 [ + - + - ]: 288 : BOOST_CHECK(okay);
633 : : }
634 : : break;
635 : : }
636 : : }
637 [ + + ]: 3851 : if (maxPos < currentPos)
638 : 1413 : maxPos = currentPos;
639 : : }
640 [ + - + - : 100 : BOOST_REQUIRE_EQUAL(file.fclose(), 0);
+ - ]
641 : 50 : }
642 [ + - ]: 1 : fs::remove(streams_test_filename);
643 : 1 : }
644 : :
645 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(buffered_reader_matches_autofile_random_content)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
646 : : {
647 : 1 : const size_t file_size{1 + m_rng.randrange<size_t>(1 << 17)};
648 : 1 : const size_t buf_size{1 + m_rng.randrange(file_size)};
649 : 1 : const FlatFilePos pos{0, 0};
650 : :
651 [ + - ]: 1 : const FlatFileSeq test_file{m_args.GetDataDirBase(), "buffered_file_test_random", node::BLOCKFILE_CHUNK_SIZE};
652 : 1 : const Obfuscation obfuscation{m_rng.randbytes<Obfuscation::KEY_SIZE>()};
653 : :
654 : : // Write out the file with random content
655 : 1 : {
656 [ + - + - ]: 1 : AutoFile f{test_file.Open(pos, /*read_only=*/false), obfuscation};
657 [ - + + - ]: 1 : f.write(m_rng.randbytes<std::byte>(file_size));
658 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(f.fclose(), 0);
+ - ]
659 : 1 : }
660 [ + - + - : 1 : BOOST_CHECK_EQUAL(fs::file_size(test_file.FileName(pos)), file_size);
+ - + - ]
661 : :
662 : 1 : {
663 [ + - + - ]: 1 : AutoFile direct_file{test_file.Open(pos, /*read_only=*/true), obfuscation};
664 : :
665 [ + - + - ]: 1 : AutoFile buffered_file{test_file.Open(pos, /*read_only=*/true), obfuscation};
666 [ + - ]: 1 : BufferedReader buffered_reader{std::move(buffered_file), buf_size};
667 : :
668 [ + + ]: 4 : for (size_t total_read{0}; total_read < file_size;) {
669 [ + + + + : 4 : const size_t read{Assert(std::min(1 + m_rng.randrange(m_rng.randbool() ? buf_size : 2 * buf_size), file_size - total_read))};
- + ]
670 : :
671 [ + - ]: 3 : DataBuffer direct_file_buffer{read};
672 [ - + + - ]: 3 : direct_file.read(direct_file_buffer);
673 : :
674 [ + - ]: 3 : DataBuffer buffered_buffer{read};
675 [ - + + - ]: 3 : buffered_reader.read(buffered_buffer);
676 : :
677 [ + - + - : 6 : BOOST_CHECK_EQUAL_COLLECTIONS(
+ - ]
678 : : direct_file_buffer.begin(), direct_file_buffer.end(),
679 : : buffered_buffer.begin(), buffered_buffer.end()
680 : : );
681 : :
682 : 3 : total_read += read;
683 : 3 : }
684 : :
685 : 1 : {
686 [ + - ]: 1 : DataBuffer excess_byte{1};
687 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(direct_file.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
- + - - -
- - + + -
+ - + - ]
688 : 0 : }
689 : :
690 : 1 : {
691 [ + - ]: 1 : DataBuffer excess_byte{1};
692 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(buffered_reader.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
- + - - -
- - + + -
+ - + - ]
693 : 1 : }
694 : 1 : }
695 : :
696 [ + - + - ]: 2 : fs::remove(test_file.FileName(pos));
697 : 1 : }
698 : :
699 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(buffered_writer_matches_autofile_random_content)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
700 : : {
701 : 1 : const size_t file_size{1 + m_rng.randrange<size_t>(1 << 17)};
702 : 1 : const size_t buf_size{1 + m_rng.randrange(file_size)};
703 : 1 : const FlatFilePos pos{0, 0};
704 : :
705 [ + - ]: 1 : const FlatFileSeq test_buffered{m_args.GetDataDirBase(), "buffered_write_test", node::BLOCKFILE_CHUNK_SIZE};
706 [ + - + - ]: 1 : const FlatFileSeq test_direct{m_args.GetDataDirBase(), "direct_write_test", node::BLOCKFILE_CHUNK_SIZE};
707 : 1 : const Obfuscation obfuscation{m_rng.randbytes<Obfuscation::KEY_SIZE>()};
708 : :
709 : 1 : {
710 : 1 : DataBuffer test_data{m_rng.randbytes<std::byte>(file_size)};
711 : :
712 [ + - + - ]: 1 : AutoFile direct_file{test_direct.Open(pos, /*read_only=*/false), obfuscation};
713 : :
714 [ + - + - ]: 1 : AutoFile buffered_file{test_buffered.Open(pos, /*read_only=*/false), obfuscation};
715 : 1 : {
716 [ + - ]: 1 : BufferedWriter buffered{buffered_file, buf_size};
717 : :
718 [ + + ]: 4 : for (size_t total_written{0}; total_written < file_size;) {
719 [ + + + + : 4 : const size_t write_size{Assert(std::min(1 + m_rng.randrange(m_rng.randbool() ? buf_size : 2 * buf_size), file_size - total_written))};
- + ]
720 : :
721 [ - + - + ]: 3 : auto current_span = std::span{test_data}.subspan(total_written, write_size);
722 [ + - ]: 3 : direct_file.write(current_span);
723 [ + - ]: 3 : buffered.write(current_span);
724 : :
725 : 3 : total_written += write_size;
726 : : }
727 : 0 : }
728 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(buffered_file.fclose(), 0);
+ - ]
729 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(direct_file.fclose(), 0);
+ - ]
730 : 1 : }
731 : :
732 : : // Compare the resulting files
733 [ + - ]: 1 : DataBuffer direct_result{file_size};
734 : 1 : {
735 [ + - + - ]: 1 : AutoFile verify_direct{test_direct.Open(pos, /*read_only=*/true), obfuscation};
736 [ - + + - ]: 1 : verify_direct.read(direct_result);
737 : :
738 [ + - ]: 1 : DataBuffer excess_byte{1};
739 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(verify_direct.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
- + - - -
- - + + -
+ - + - ]
740 : 1 : }
741 : :
742 [ + - ]: 1 : DataBuffer buffered_result{file_size};
743 : 1 : {
744 [ + - + - ]: 1 : AutoFile verify_buffered{test_buffered.Open(pos, /*read_only=*/true), obfuscation};
745 [ - + + - ]: 1 : verify_buffered.read(buffered_result);
746 : :
747 [ + - ]: 1 : DataBuffer excess_byte{1};
748 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(verify_buffered.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
- + - - -
- - + + -
+ - + - ]
749 : 1 : }
750 : :
751 [ + - + - : 2 : BOOST_CHECK_EQUAL_COLLECTIONS(
+ - + - ]
752 : : direct_result.begin(), direct_result.end(),
753 : : buffered_result.begin(), buffered_result.end()
754 : : );
755 : :
756 [ + - + - ]: 1 : fs::remove(test_direct.FileName(pos));
757 [ + - + - ]: 2 : fs::remove(test_buffered.FileName(pos));
758 : 3 : }
759 : :
760 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(buffered_writer_reader)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
761 : : {
762 : 1 : const uint32_t v1{m_rng.rand32()}, v2{m_rng.rand32()}, v3{m_rng.rand32()};
763 [ + - ]: 2 : const fs::path test_file{m_args.GetDataDirBase() / "test_buffered_write_read.bin"};
764 : :
765 : : // Write out the values through a precisely sized BufferedWriter
766 [ + - + - ]: 2 : AutoFile file{fsbridge::fopen(test_file, "w+b")};
767 : 1 : {
768 [ + - ]: 1 : BufferedWriter f(file, sizeof(v1) + sizeof(v2) + sizeof(v3));
769 [ + - + - ]: 1 : f << v1 << v2;
770 [ + - ]: 1 : f.write(std::as_bytes(std::span{&v3, 1}));
771 : 0 : }
772 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(file.fclose(), 0);
+ - ]
773 : :
774 : : // Read back and verify using BufferedReader
775 : 1 : {
776 : 1 : uint32_t _v1{0}, _v2{0}, _v3{0};
777 [ + - + - ]: 2 : AutoFile file{fsbridge::fopen(test_file, "rb")};
778 [ + - ]: 1 : BufferedReader f(std::move(file), sizeof(v1) + sizeof(v2) + sizeof(v3));
779 [ + - + - ]: 1 : f >> _v1 >> _v2;
780 [ + - ]: 1 : f.read(std::as_writable_bytes(std::span{&_v3, 1}));
781 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(_v1, v1);
782 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(_v2, v2);
783 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(_v3, v3);
784 : :
785 [ + - ]: 1 : DataBuffer excess_byte{1};
786 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(f.read(excess_byte), std::ios_base::failure, HasReason{"end of file"});
- + - - -
- - + + -
+ - + - ]
787 : 1 : }
788 : :
789 [ + - ]: 1 : fs::remove(test_file);
790 : 2 : }
791 : :
792 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(streams_hashed)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
793 : : {
794 : 1 : DataStream stream{};
795 [ + - ]: 1 : HashedSourceWriter hash_writer{stream};
796 [ + - ]: 1 : const std::string data{"bitcoin"};
797 [ + - ]: 1 : hash_writer << data;
798 : :
799 [ + - ]: 1 : HashVerifier hash_verifier{stream};
800 [ + - ]: 1 : std::string result;
801 [ + - ]: 1 : hash_verifier >> result;
802 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(data, result);
803 [ + - + - : 1 : BOOST_CHECK_EQUAL(hash_writer.GetHash(), hash_verifier.GetHash());
+ - + - ]
804 : 1 : }
805 : :
806 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(size_preserves_position)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
807 : : {
808 [ + - ]: 2 : const fs::path path = m_args.GetDataDirBase() / "size_pos_test.bin";
809 [ + - + - ]: 2 : AutoFile f{fsbridge::fopen(path, "w+b")};
810 [ + + ]: 11 : for (uint8_t j = 0; j < 10; ++j) {
811 [ + - ]: 20 : f << j;
812 : : }
813 : :
814 : : // Test that usage of size() does not change the current position
815 : : //
816 : : // Case: Pos at beginning of the file
817 [ + - ]: 1 : f.seek(0, SEEK_SET);
818 [ + - ]: 1 : (void)f.size();
819 : 1 : uint8_t first{};
820 [ + - ]: 1 : f >> first;
821 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(first, 0);
822 : :
823 : : // Case: Pos at middle of the file
824 [ + - ]: 1 : f.seek(0, SEEK_SET);
825 : : // Move pos to middle
826 [ + - ]: 1 : f.ignore(4);
827 [ + - ]: 1 : (void)f.size();
828 : 1 : uint8_t middle{};
829 [ + - ]: 1 : f >> middle;
830 : : // Pos still at 4
831 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(middle, 4);
832 : :
833 : : // Case: Pos at EOF
834 [ + - ]: 1 : f.seek(0, SEEK_END);
835 [ + - ]: 1 : (void)f.size();
836 : 1 : uint8_t end{};
837 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(f >> end, std::ios_base::failure, HasReason{"AutoFile::read: end of file"});
- - - - -
+ + - + -
+ - ]
838 : :
839 [ + - + - : 2 : BOOST_REQUIRE_EQUAL(f.fclose(), 0);
+ - ]
840 [ + - ]: 1 : fs::remove(path);
841 : 2 : }
842 : :
843 : : BOOST_AUTO_TEST_SUITE_END()
|