Skip to content

Commit 3f0512f

Browse files
author
Chandra Pratap
committed
fuzz-tests: Add a wire test for wireaddr functions
Changelog-None: `towire_wireaddr()` and `fromwire_wireaddr()` in `common/wireaddr.h` are responsible for marshalling/unmarshalling BOLT #7 address descriptors. Since these aren't tested by the existing wire fuzz tests, add a roundtrip test for them. This has the added benefit of testing `parse_wireaddr()` as well.
1 parent b97278f commit 3f0512f

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

tests/fuzz/fuzz-wire-wireaddr.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 *wa = tal(tmpctx, struct wireaddr);
27+
const char *err;
28+
29+
err = parse_wireaddr(tmpctx, addr, DEFAULT_PORT, NULL, wa);
30+
31+
if (!err) {
32+
assert(fmt_wireaddr(tmpctx, wa));
33+
34+
u8 *output_buffer = tal_arr(tmpctx, u8, 0);
35+
towire_wireaddr(&output_buffer, wa);
36+
size_t len = tal_bytelen(output_buffer);
37+
38+
struct wireaddr *decoded_wa = tal(tmpctx, struct wireaddr);
39+
assert(fromwire_wireaddr((const u8 **) &output_buffer, &len, decoded_wa));
40+
assert(wireaddr_eq(wa, decoded_wa));
41+
}
42+
43+
clean_tmpctx();
44+
}

0 commit comments

Comments
 (0)