Skip to content

Commit beb87bf

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 ec05e5d commit beb87bf

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

tests/fuzz/fuzz-wireaddr.c

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

0 commit comments

Comments
 (0)