mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-01 17:47:30 +01:00
fuzz: add a target for common/bech32
Signed-off-by: Antoine Poinsot <darosior@protonmail.com>
This commit is contained in:
parent
ae4dc231c1
commit
63b1ae54a1
1 changed files with 50 additions and 0 deletions
50
tests/fuzz/fuzz-bech32.c
Normal file
50
tests/fuzz/fuzz-bech32.c
Normal file
|
@ -0,0 +1,50 @@
|
|||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <tests/fuzz/libfuzz.h>
|
||||
|
||||
#include <common/bech32.h>
|
||||
|
||||
void init(int *argc, char ***argv)
|
||||
{
|
||||
}
|
||||
|
||||
void run(const uint8_t *data, size_t size)
|
||||
{
|
||||
const char hrp_inv[5] = "lnbc\0", hrp_addr[3] = "bc\0";
|
||||
char *bech32_str, *hrp_out, *addr;
|
||||
uint8_t *data_out;
|
||||
size_t data_out_len;
|
||||
int wit_version;
|
||||
|
||||
/* Buffer size is defined in each function's doc comment. */
|
||||
bech32_str = malloc(size + strlen(hrp_inv) + 8);
|
||||
/* FIXME: needs a dictionary / a startup seed corpus to pass this more
|
||||
* frequently. */
|
||||
if (bech32_encode(bech32_str, hrp_inv, data, size, size) == 1) {
|
||||
hrp_out = malloc(strlen(bech32_str) - 6);
|
||||
data_out = malloc(strlen(bech32_str) - 8);
|
||||
bech32_decode(hrp_out, data_out, &data_out_len, bech32_str, size);
|
||||
free(hrp_out);
|
||||
free(data_out);
|
||||
}
|
||||
free(bech32_str);
|
||||
|
||||
data_out = malloc(size);
|
||||
|
||||
/* This is also used as part of sign and check message. */
|
||||
data_out_len = 0;
|
||||
bech32_convert_bits(data_out, &data_out_len, 8, data, size, 5, 1);
|
||||
data_out_len = 0;
|
||||
bech32_convert_bits(data_out, &data_out_len, 8, data, size, 5, 0);
|
||||
|
||||
addr = malloc(73 + strlen(hrp_addr));
|
||||
wit_version = 0;
|
||||
if (segwit_addr_encode(addr, hrp_addr, wit_version, data, size) == 1)
|
||||
segwit_addr_decode(&wit_version, data_out, &data_out_len, hrp_addr, addr);
|
||||
wit_version = 1;
|
||||
if (segwit_addr_encode(addr, hrp_addr, wit_version, data, size) == 1)
|
||||
segwit_addr_decode(&wit_version, data_out, &data_out_len, hrp_addr, addr);
|
||||
free(addr);
|
||||
|
||||
free(data_out);
|
||||
}
|
Loading…
Add table
Reference in a new issue