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