From e721b0a89a59129190196a64726e72ff346a7bcb Mon Sep 17 00:00:00 2001 From: Chandra Pratap Date: Fri, 28 Mar 2025 16:46:34 +0000 Subject: [PATCH] fuzz-tests: Enhance b64_encode() validation with roundtrip decoding MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changelog-None: Currently, fuzz testing for b64_encode() merely encodes input and frees the result, providing no real verification of its behavior. Introduce a new b64_decode() function (modeled after b32_decode()) and update the fuzz test to perform a roundtrip—encoding followed by decoding—to ensure that b64_encode() correctly preserves the original data. --- common/base64.c | 9 +++++++++ common/base64.h | 1 + tests/fuzz/fuzz-base32-64.c | 3 +++ 3 files changed, 13 insertions(+) diff --git a/common/base64.c b/common/base64.c index 75db1a469..687e68471 100644 --- a/common/base64.c +++ b/common/base64.c @@ -14,3 +14,12 @@ char *b64_encode(const tal_t *ctx, const void *data, size_t len) str[enclen] = '\0'; return str; } + +u8 *b64_decode(const tal_t *ctx, const char *str, size_t len) +{ + size_t dlen = base64_decoded_length(len); + u8 *ret = tal_arr(ctx, u8, dlen); + if (base64_decode((char *)ret, dlen, str, len) < 0) + return tal_free(ret); + return ret; +} diff --git a/common/base64.h b/common/base64.h index f963541db..2c2b4d4a3 100644 --- a/common/base64.h +++ b/common/base64.h @@ -5,5 +5,6 @@ #include char *b64_encode(const tal_t *ctx, const void *data, size_t len); +u8 *b64_decode(const tal_t *ctx, const char *str, size_t len); #endif /* LIGHTNING_COMMON_BASE64_H */ diff --git a/tests/fuzz/fuzz-base32-64.c b/tests/fuzz/fuzz-base32-64.c index 9dbec2e8f..de30f96da 100644 --- a/tests/fuzz/fuzz-base32-64.c +++ b/tests/fuzz/fuzz-base32-64.c @@ -21,5 +21,8 @@ void run(const uint8_t *data, size_t size) tal_free(decoded); encoded = b64_encode(NULL, data, size); + decoded = b64_decode(NULL, encoded, strlen(encoded)); + assert(memcmp(decoded, data, size) == 0); tal_free(encoded); + tal_free(decoded); }