From bbe85f3c16624e7634e85b54a043786280734ce4 Mon Sep 17 00:00:00 2001 From: eugene Date: Sat, 26 Sep 2020 00:35:58 -0400 Subject: [PATCH] fuzz/zpay32: add Decode/Encode fuzz tests. These can be expanded on in the future. Encoding is currently done with a static key for test determinism. --- fuzz/zpay32/decode.go | 22 +++++++++++++++++++ fuzz/zpay32/encode.go | 49 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 fuzz/zpay32/decode.go create mode 100644 fuzz/zpay32/encode.go diff --git a/fuzz/zpay32/decode.go b/fuzz/zpay32/decode.go new file mode 100644 index 000000000..0c1d1e5ad --- /dev/null +++ b/fuzz/zpay32/decode.go @@ -0,0 +1,22 @@ +// +build gofuzz + +package zpay32fuzz + +import ( + "github.com/btcsuite/btcd/chaincfg" + "github.com/lightningnetwork/lnd/zpay32" +) + +// Fuzz_decode is used by go-fuzz. +func Fuzz_decode(data []byte) int { + inv, err := zpay32.Decode(string(data), &chaincfg.TestNet3Params) + if err != nil { + return 1 + } + + // Call these functions as a sanity check to make sure the invoice + // is well-formed. + _ = inv.MinFinalCLTVExpiry() + _ = inv.Expiry() + return 1 +} diff --git a/fuzz/zpay32/encode.go b/fuzz/zpay32/encode.go new file mode 100644 index 000000000..4734a5785 --- /dev/null +++ b/fuzz/zpay32/encode.go @@ -0,0 +1,49 @@ +// +build gofuzz + +package zpay32fuzz + +import ( + "encoding/hex" + "fmt" + + "github.com/btcsuite/btcd/btcec" + "github.com/btcsuite/btcd/chaincfg" + "github.com/lightningnetwork/lnd/zpay32" +) + +// Fuzz_encode is used by go-fuzz. +func Fuzz_encode(data []byte) int { + inv, err := zpay32.Decode(string(data), &chaincfg.TestNet3Params) + if err != nil { + return 1 + } + + // Call these functions as a sanity check to make sure the invoice + // is well-formed. + _ = inv.MinFinalCLTVExpiry() + _ = inv.Expiry() + + // Initialize the static key we will be using for this fuzz test. + testPrivKeyBytes, _ := hex.DecodeString("e126f68f7eafcc8b74f54d269fe206be715000f94dac067d1c04a8ca3b2db734") + testPrivKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), testPrivKeyBytes) + + // Then, initialize the testMessageSigner so we can encode out + // invoices with this private key. + testMessageSigner := zpay32.MessageSigner{ + SignCompact: func(hash []byte) ([]byte, error) { + sig, err := btcec.SignCompact(btcec.S256(), + testPrivKey, hash, true) + if err != nil { + return nil, fmt.Errorf("can't sign the "+ + "message: %v", err) + } + return sig, nil + }, + } + _, err = inv.Encode(testMessageSigner) + if err != nil { + return 1 + } + + return 1 +}