From 85a1230f35c5137281867fbb7164b324c5e9d57e Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Tue, 13 Jun 2023 04:25:24 -0700 Subject: [PATCH] test: types/bech32: fuzz ConvertAndEncode (#16523) --- types/bech32/fuzz_test.go | 50 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 types/bech32/fuzz_test.go diff --git a/types/bech32/fuzz_test.go b/types/bech32/fuzz_test.go new file mode 100644 index 0000000000..5dc004fe83 --- /dev/null +++ b/types/bech32/fuzz_test.go @@ -0,0 +1,50 @@ +package bech32 + +import ( + "crypto/sha256" + "encoding/json" + "testing" +) + +type hrpAndData struct { + HRP string `json:"h"` + Data []byte `json:"d"` +} + +func FuzzConvertAndEncode(f *testing.F) { + // 1. Add seeds + seeds := []*hrpAndData{ + { + "shasum", + func() []byte { + sum := sha256.Sum256([]byte("hello world\n")) + return sum[:] + }(), + }, + { + "shasum", + []byte("49yfqne0parehrupja55kvqcfvxja5wpe54pas8mshffngvj53rs93fk75"), + }, + { + "bech32", + []byte("er8m900ayvv9rg5r6ush4nzvqhj4p9tqnxqkxaaxrs4ueuvhurcs4x3j4j"), + }, + } + + for _, seed := range seeds { + seedJSONBlob, err := json.Marshal(seed) + if err != nil { + continue + } + f.Add(seedJSONBlob) + } + + // 2. Now run the fuzzer. + f.Fuzz(func(t *testing.T, inputJSON []byte) { + had := new(hrpAndData) + if err := json.Unmarshal(inputJSON, had); err != nil { + return + } + _, _ = ConvertAndEncode(had.HRP, had.Data) + }) +}