fuzz-tests: Enhance b64_encode() validation with roundtrip decoding
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.
This commit is contained in:
committed by
Rusty Russell
parent
b4d0da5ff4
commit
e721b0a89a
@@ -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;
|
||||
}
|
||||
|
||||
@@ -5,5 +5,6 @@
|
||||
#include <ccan/tal/tal.h>
|
||||
|
||||
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 */
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user