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