From 9200abf96eabd82c7e92d49320ffdaf106b0c5ef Mon Sep 17 00:00:00 2001 From: Matt Morehouse Date: Tue, 13 Jun 2023 11:46:18 -0500 Subject: [PATCH] zpay32: allow fuzzer to choose invoice net We add a parameter to select which network will be used for the fuzz tests, rather than hardcoding the network. --- zpay32/fuzz_test.go | 46 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 5 deletions(-) diff --git a/zpay32/fuzz_test.go b/zpay32/fuzz_test.go index 696b09c3a..9855b8c1a 100644 --- a/zpay32/fuzz_test.go +++ b/zpay32/fuzz_test.go @@ -7,9 +7,44 @@ import ( "github.com/btcsuite/btcd/chaincfg" ) +// getPrefixAndChainParams selects network chain parameters based on the fuzzer- +// selected input byte "net". 50% of the time mainnet is selected, while the +// other 50% of the time one of the test networks is selected. For each network +// the appropriate invoice HRP prefix is also returned, with a small chance that +// no prefix is returned, allowing the fuzzer to generate invalid prefixes too. +func getPrefixAndChainParams(net byte) (string, *chaincfg.Params) { + switch { + case net == 0x00: + return "", &chaincfg.RegressionNetParams + case net < 0x20: + return "lnbcrt", &chaincfg.RegressionNetParams + + case net == 0x20: + return "", &chaincfg.TestNet3Params + case net < 0x40: + return "lntb", &chaincfg.TestNet3Params + + case net == 0x40: + return "", &chaincfg.SimNetParams + case net < 0x60: + return "lnsb", &chaincfg.SimNetParams + + case net == 0x60: + return "", &chaincfg.SigNetParams + case net < 0x80: + return "lntbs", &chaincfg.SigNetParams + + case net == 0x80: + return "", &chaincfg.MainNetParams + default: + return "lnbc", &chaincfg.MainNetParams + } +} + func FuzzDecode(f *testing.F) { - f.Fuzz(func(t *testing.T, data string) { - _, _ = Decode(data, &chaincfg.TestNet3Params) + f.Fuzz(func(t *testing.T, net byte, data string) { + _, chainParams := getPrefixAndChainParams(net) + _, _ = Decode(data, chainParams) }) } @@ -44,13 +79,14 @@ func appendChecksum(bech string) string { } func FuzzEncode(f *testing.F) { - f.Fuzz(func(t *testing.T, data string) { + f.Fuzz(func(t *testing.T, net byte, data string) { // Make it easier for the fuzzer to generate valid invoice // encodings by adding the required prefix and valid checksum. - data = "lnbc" + data + hrpPrefix, chainParams := getPrefixAndChainParams(net) + data = hrpPrefix + data data = appendChecksum(data) - inv, err := Decode(data, &chaincfg.MainNetParams) + inv, err := Decode(data, chainParams) if err != nil { return }