Skip to content

Commit 0949cde

Browse files
author
Chandra Pratap
committed
fuzz-tests: add a test for handle_peer_error_or_warning()
Changelog-None: `handle_peer_error_or_warning()` in `common/read_peer_message.{c, h}` is responsible for parsing any incoming `error` or `warning` messages as defined in BOLT #1. Add a test for it.
1 parent 396d0a9 commit 0949cde

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

tests/fuzz/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,11 @@ FUZZ_COMMON_OBJS := \
7272
$(FUZZ_TARGETS_OBJS): $(COMMON_HEADERS) $(WIRE_HEADERS) $(COMMON_SRC)
7373
$(FUZZ_TARGETS_BIN): $(LIBFUZZ_OBJS) $(FUZZ_COMMON_OBJS) $(BITCOIN_OBJS)
7474

75+
tests/fuzz/fuzz-error-warning: common/peer_billboard.o \
76+
common/wire_error.o \
77+
common/peer_io.o \
78+
common/read_peer_msg.o \
79+
common/peer_status_wiregen.o
80+
7581
ALL_C_SOURCES += $(FUZZ_TARGETS_SRC) $(LIBFUZZ_SRC)
7682
ALL_FUZZ_TARGETS += $(FUZZ_TARGETS_BIN)

tests/fuzz/fuzz-error-warning.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "config.h"
2+
#include <stdio.h>
3+
#include <fcntl.h>
4+
#include <setjmp.h>
5+
#include <common/per_peer_state.h>
6+
#include <common/peer_failed.h>
7+
#include <common/read_peer_msg.h>
8+
#include <common/status.h>
9+
#include <common/utils.h>
10+
#include <tests/fuzz/libfuzz.h>
11+
12+
static jmp_buf exit_jmp;
13+
14+
/* MOCKS START */
15+
/* Stub for peer_failed_connection_lost */
16+
void peer_failed_connection_lost(void)
17+
{ fprintf(stderr, "peer_failed_connection_lost called!\n"); abort(); }
18+
/* Stub for peer_failed_received_errmsg */
19+
void peer_failed_received_errmsg(struct per_peer_state *pps UNNEEDED,
20+
bool disconnect UNNEEDED,
21+
const char *desc)
22+
23+
{ longjmp(exit_jmp, 1); }
24+
/* MOCKS END */
25+
26+
void init(int *argc, char ***argv)
27+
{
28+
int devnull = open("/dev/null", O_WRONLY);
29+
status_setup_sync(devnull);
30+
}
31+
32+
void run(const u8 *data, size_t size)
33+
{
34+
if (setjmp(exit_jmp) != 0)
35+
return;
36+
37+
u8 *msg = tal_dup_arr(tmpctx, u8, data, size, 0);
38+
struct per_peer_state pps = { .peer_fd = -1 };
39+
handle_peer_error_or_warning(&pps, msg);
40+
41+
clean_tmpctx();
42+
}

0 commit comments

Comments
 (0)