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