Browse Source

fuzz: add a fuzz target for amounts parsing

Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
travis-experimental
Antoine Poinsot 4 years ago
committed by Christian Decker
parent
commit
2f90c45454
  1. 2
      .gitignore
  2. 10
      tests/fuzz/Makefile
  3. 61
      tests/fuzz/fuzz-amount.c

2
.gitignore

@ -37,6 +37,8 @@ test/test_protocol
test/test_sphinx test/test_sphinx
tests/.pytest.restart tests/.pytest.restart
tests/plugins/test_libplugin tests/plugins/test_libplugin
tests/fuzz/fuzz-*
!tests/fuzz/fuzz-*.c
gossip_store gossip_store
.pytest_cache .pytest_cache
tools/headerversions tools/headerversions

10
tests/fuzz/Makefile

@ -7,12 +7,7 @@ FUZZ_TARGETS_SRC := $(wildcard tests/fuzz/fuzz-*.c)
FUZZ_TARGETS_OBJS := $(FUZZ_TARGETS_SRC:.c=.o) FUZZ_TARGETS_OBJS := $(FUZZ_TARGETS_SRC:.c=.o)
FUZZ_TARGETS_BIN := $(FUZZ_TARGETS_SRC:.c=) FUZZ_TARGETS_BIN := $(FUZZ_TARGETS_SRC:.c=)
FUZZ_COMMON_OBJS := \ FUZZ_COMMON_OBJS := \
common/utils.o
$(FUZZ_TARGETS_OBJS): $(COMMON_HEADERS) $(WIRE_HEADERS) $(COMMON_SRC)
$(FUZZ_TARGETS_BIN): $(LIBFUZZ_OBJS) $(FUZZ_COMMON_OBJS) $(BITCOIN_OBJS)
tests/fuzz/fuzz-addr: \
common/amount.o \ common/amount.o \
common/addr.o \ common/addr.o \
common/base32.o \ common/base32.o \
@ -22,9 +17,12 @@ tests/fuzz/fuzz-addr: \
common/json_stream.o \ common/json_stream.o \
common/wireaddr.o \ common/wireaddr.o \
common/type_to_string.o \ common/type_to_string.o \
common/utils.o \
wire/fromwire.o \ wire/fromwire.o \
wire/onion_wiregen.o \ wire/onion_wiregen.o \
wire/towire.o wire/towire.o
$(FUZZ_TARGETS_OBJS): $(COMMON_HEADERS) $(WIRE_HEADERS) $(COMMON_SRC)
$(FUZZ_TARGETS_BIN): $(LIBFUZZ_OBJS) $(FUZZ_COMMON_OBJS) $(BITCOIN_OBJS)
ALL_C_SOURCES += $(FUZZ_TARGETS_SRC) $(LIBFUZZ_SRC) ALL_C_SOURCES += $(FUZZ_TARGETS_SRC) $(LIBFUZZ_SRC)
ALL_FUZZ_TARGETS += $(FUZZ_TARGETS_BIN) ALL_FUZZ_TARGETS += $(FUZZ_TARGETS_BIN)

61
tests/fuzz/fuzz-amount.c

@ -0,0 +1,61 @@
#include <assert.h>
#include <stdint.h>
#include <tests/fuzz/libfuzz.h>
#include <ccan/tal/tal.h>
#include <common/amount.h>
#include <common/utils.h>
void init(int *argc, char ***argv)
{
}
void run(const uint8_t *data, size_t size)
{
struct amount_msat msat;
struct amount_sat sat;
char *string;
uint8_t *buf;
const char *fmt_msat, *fmt_msatbtc, *fmt_sat, *fmt_satbtc;
/* We should not crash when parsing any string. */
string = tal_arr(NULL, char, size);
for (size_t i = 0; i < size; i++)
string[i] = (char) data[i] % (CHAR_MAX + 1);
parse_amount_msat(&msat, string, tal_count(string));
parse_amount_sat(&sat, string, tal_count(string));
tal_free(string);
/* Same with the wire primitives. */
buf = tal_arr(NULL, uint8_t, 8);
msat = fromwire_amount_msat(&data, &size);
towire_amount_msat(&buf, msat);
sat = fromwire_amount_sat(&data, &size);
towire_amount_sat(&buf, sat);
tal_free(buf);
/* Format should inconditionally produce valid amount strings according to our
* parser */
fmt_msat = fmt_amount_msat(NULL, &msat);
fmt_msatbtc = fmt_amount_msat_btc(NULL, &msat, true);
assert(parse_amount_msat(&msat, fmt_msat, tal_count(fmt_msat)));
assert(parse_amount_msat(&msat, fmt_msatbtc, tal_count(fmt_msatbtc)));
tal_free(fmt_msat);
tal_free(fmt_msatbtc);
fmt_sat = fmt_amount_sat(NULL, &sat);
fmt_satbtc = fmt_amount_sat_btc(NULL, &sat, true);
assert(parse_amount_sat(&sat, fmt_sat, tal_count(fmt_sat)));
assert(parse_amount_sat(&sat, fmt_satbtc, tal_count(fmt_satbtc)));
tal_free(fmt_sat);
tal_free(fmt_satbtc);
}
Loading…
Cancel
Save