Skip to content

Commit a59d21d

Browse files
Chandra Prataprustyrussell
authored andcommitted
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 103cd1c commit a59d21d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

tests/fuzz/fuzz-wireaddr.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 <assert.h>
3+
#include <bitcoin/chainparams.h>
4+
#include <ccan/ccan/tal/str/str.h>
5+
#include <common/setup.h>
6+
#include <common/utils.h>
7+
#include <common/wireaddr.h>
8+
#include <tests/fuzz/libfuzz.h>
9+
10+
#define DEFAULT_PORT 9735
11+
12+
void init(int *argc, char ***argv)
13+
{
14+
/* Don't call this if we're in unit-test mode, as libfuzz.c does it */
15+
if (!tmpctx)
16+
common_setup("fuzzer");
17+
chainparams = chainparams_for_network("bitcoin");
18+
}
19+
20+
void run(const uint8_t *data, size_t size)
21+
{
22+
char *addr = to_string(tmpctx, data, size);
23+
24+
struct wireaddr wa, decoded_wa;
25+
const char *err;
26+
27+
err = parse_wireaddr(tmpctx, addr, DEFAULT_PORT, NULL, &wa);
28+
29+
if (!err) {
30+
assert(fmt_wireaddr(tmpctx, &wa));
31+
32+
u8 *output_buffer = tal_arr(tmpctx, u8, 0);
33+
towire_wireaddr(&output_buffer, &wa);
34+
size_t len = tal_bytelen(output_buffer);
35+
36+
assert(fromwire_wireaddr((const u8 **)&output_buffer, &len,
37+
&decoded_wa));
38+
assert(wireaddr_eq(&wa, &decoded_wa));
39+
}
40+
41+
clean_tmpctx();
42+
}

0 commit comments

Comments
 (0)