Skip to content

Commit f3aa659

Browse files
tests: Add fuzzing harness for CAutoFile (streams.h)
1 parent e507c07 commit f3aa659

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ FUZZ_TARGETS = \
1010
test/fuzz/addrman_deserialize \
1111
test/fuzz/asmap \
1212
test/fuzz/asmap_direct \
13+
test/fuzz/autofile \
1314
test/fuzz/banentry_deserialize \
1415
test/fuzz/banman \
1516
test/fuzz/base_encode_decode \
@@ -350,6 +351,12 @@ test_fuzz_asmap_direct_LDADD = $(FUZZ_SUITE_LD_COMMON)
350351
test_fuzz_asmap_direct_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
351352
test_fuzz_asmap_direct_SOURCES = test/fuzz/asmap_direct.cpp
352353

354+
test_fuzz_autofile_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
355+
test_fuzz_autofile_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
356+
test_fuzz_autofile_LDADD = $(FUZZ_SUITE_LD_COMMON)
357+
test_fuzz_autofile_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
358+
test_fuzz_autofile_SOURCES = test/fuzz/autofile.cpp
359+
353360
test_fuzz_banentry_deserialize_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) -DBANENTRY_DESERIALIZE=1
354361
test_fuzz_banentry_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
355362
test_fuzz_banentry_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)

src/test/fuzz/autofile.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2020 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 <optional.h>
6+
#include <streams.h>
7+
#include <test/fuzz/FuzzedDataProvider.h>
8+
#include <test/fuzz/fuzz.h>
9+
#include <test/fuzz/util.h>
10+
11+
#include <array>
12+
#include <cstdint>
13+
#include <iostream>
14+
#include <optional>
15+
#include <string>
16+
#include <vector>
17+
18+
void test_one_input(const std::vector<uint8_t>& buffer)
19+
{
20+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
21+
FuzzedAutoFileProvider fuzzed_auto_file_provider = ConsumeAutoFile(fuzzed_data_provider);
22+
CAutoFile auto_file = fuzzed_auto_file_provider.open();
23+
while (fuzzed_data_provider.ConsumeBool()) {
24+
switch (fuzzed_data_provider.ConsumeIntegralInRange<int>(0, 5)) {
25+
case 0: {
26+
std::array<uint8_t, 4096> arr{};
27+
try {
28+
auto_file.read((char*)arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
29+
} catch (const std::ios_base::failure&) {
30+
}
31+
break;
32+
}
33+
case 1: {
34+
const std::array<uint8_t, 4096> arr{};
35+
try {
36+
auto_file.write((const char*)arr.data(), fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
37+
} catch (const std::ios_base::failure&) {
38+
}
39+
break;
40+
}
41+
case 2: {
42+
try {
43+
auto_file.ignore(fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096));
44+
} catch (const std::ios_base::failure&) {
45+
}
46+
break;
47+
}
48+
case 3: {
49+
auto_file.fclose();
50+
break;
51+
}
52+
case 4: {
53+
ReadFromStream(fuzzed_data_provider, auto_file);
54+
break;
55+
}
56+
case 5: {
57+
WriteToStream(fuzzed_data_provider, auto_file);
58+
break;
59+
}
60+
}
61+
}
62+
(void)auto_file.Get();
63+
(void)auto_file.GetType();
64+
(void)auto_file.GetVersion();
65+
(void)auto_file.IsNull();
66+
if (fuzzed_data_provider.ConsumeBool()) {
67+
FILE* f = auto_file.release();
68+
if (f != nullptr) {
69+
fclose(f);
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)