Skip to content

Commit d5dbb48

Browse files
tests: Add fuzzing harness for ISO-8601 related functions
1 parent cfec3e0 commit d5dbb48

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/Makefile.test.include

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ FUZZ_TARGETS = \
2222
test/fuzz/inv_deserialize \
2323
test/fuzz/messageheader_deserialize \
2424
test/fuzz/netaddr_deserialize \
25+
test/fuzz/parse_iso8601 \
2526
test/fuzz/script \
2627
test/fuzz/script_flags \
2728
test/fuzz/service_deserialize \
@@ -269,6 +270,12 @@ test_fuzz_netaddr_deserialize_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
269270
test_fuzz_netaddr_deserialize_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
270271
test_fuzz_netaddr_deserialize_LDADD = $(FUZZ_SUITE_LD_COMMON)
271272

273+
test_fuzz_parse_iso8601_SOURCES = $(FUZZ_SUITE) test/fuzz/parse_iso8601.cpp
274+
test_fuzz_parse_iso8601_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
275+
test_fuzz_parse_iso8601_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
276+
test_fuzz_parse_iso8601_LDFLAGS = $(RELDFLAGS) $(AM_LDFLAGS) $(LIBTOOL_APP_LDFLAGS)
277+
test_fuzz_parse_iso8601_LDADD = $(FUZZ_SUITE_LD_COMMON)
278+
272279
test_fuzz_script_SOURCES = $(FUZZ_SUITE) test/fuzz/script.cpp
273280
test_fuzz_script_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
274281
test_fuzz_script_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)

src/test/fuzz/parse_iso8601.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) 2019 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 <test/fuzz/FuzzedDataProvider.h>
6+
#include <test/fuzz/fuzz.h>
7+
#include <util/time.h>
8+
9+
#include <cassert>
10+
#include <cstdint>
11+
#include <string>
12+
#include <vector>
13+
14+
void test_one_input(const std::vector<uint8_t>& buffer)
15+
{
16+
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
17+
18+
const int64_t random_time = fuzzed_data_provider.ConsumeIntegral<int64_t>();
19+
const std::string random_string = fuzzed_data_provider.ConsumeRemainingBytesAsString();
20+
21+
const std::string iso8601_datetime = FormatISO8601DateTime(random_time);
22+
const int64_t parsed_time_1 = ParseISO8601DateTime(iso8601_datetime);
23+
if (random_time >= 0) {
24+
assert(parsed_time_1 >= 0);
25+
if (iso8601_datetime.length() == 20) {
26+
assert(parsed_time_1 == random_time);
27+
}
28+
}
29+
30+
const int64_t parsed_time_2 = ParseISO8601DateTime(random_string);
31+
assert(parsed_time_2 >= 0);
32+
}

0 commit comments

Comments
 (0)