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