Skip to content

Commit 8c2ec45

Browse files
author
Chandra Pratap
committed
fuzz-tests: Add a wire test for wireaddr_internal
Changelog-None: Wire functions in `common/wireaddr.{c, h}` consume untrusted input. Since no tests exist for these functions, add one for the wire operations of `wireaddr_internal`.
1 parent b97278f commit 8c2ec45

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "config.h"
2+
#include <assert.h>
3+
#include <ccan/ccan/tal/str/str.h>
4+
#include <common/utils.h>
5+
#include <common/setup.h>
6+
#include <common/wireaddr.h>
7+
#include <bitcoin/chainparams.h>
8+
#include <tests/fuzz/libfuzz.h>
9+
10+
#define DEFAULT_PORT 9735
11+
12+
void init(int *argc, char ***argv)
13+
{
14+
common_setup("fuzzer");
15+
chainparams = chainparams_for_network("bitcoin");
16+
}
17+
18+
void run(const uint8_t *data, size_t size)
19+
{
20+
char *addr = tal_arr(tmpctx, char, size + 1);
21+
if (!addr)
22+
return;
23+
memcpy(addr, data, size);
24+
addr[size] = '\0';
25+
26+
struct wireaddr_internal *wa = tal(tmpctx, struct wireaddr_internal);
27+
const char *err;
28+
29+
err = parse_wireaddr_internal(tmpctx, addr, DEFAULT_PORT, NULL, wa);
30+
31+
if (!err) {
32+
u8 *output_buffer = tal_arr(tmpctx, u8, 0);
33+
towire_wireaddr_internal(&output_buffer, wa);
34+
size_t len = tal_bytelen(output_buffer);
35+
36+
struct wireaddr_internal *decoded_wa = tal(tmpctx, struct wireaddr_internal);
37+
assert(fromwire_wireaddr_internal((const u8 **) &output_buffer, &len, decoded_wa));
38+
assert(wireaddr_internal_eq(wa, decoded_wa));
39+
}
40+
41+
clean_tmpctx();
42+
}

0 commit comments

Comments
 (0)