From 782f9732986e20a490f79da86ef4bc884c97c4fc Mon Sep 17 00:00:00 2001 From: Conner Date: Fri, 29 Jul 2022 18:32:59 -0400 Subject: [PATCH] zpay32: migrate fuzz tests --- fuzz/zpay32/decode.go | 23 ---------------- fuzz/zpay32/encode.go | 52 ----------------------------------- zpay32/fuzz_test.go | 64 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 75 deletions(-) delete mode 100644 fuzz/zpay32/decode.go delete mode 100644 fuzz/zpay32/encode.go create mode 100644 zpay32/fuzz_test.go diff --git a/fuzz/zpay32/decode.go b/fuzz/zpay32/decode.go deleted file mode 100644 index a5573e208..000000000 --- a/fuzz/zpay32/decode.go +++ /dev/null @@ -1,23 +0,0 @@ -//go:build gofuzz -// +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 deleted file mode 100644 index 9c3a08dbc..000000000 --- a/fuzz/zpay32/encode.go +++ /dev/null @@ -1,52 +0,0 @@ -//go:build gofuzz -// +build gofuzz - -package zpay32fuzz - -import ( - "encoding/hex" - "fmt" - - "github.com/btcsuite/btcd/btcec/v2" - "github.com/btcsuite/btcd/btcec/v2/ecdsa" - "github.com/btcsuite/btcd/chaincfg" - "github.com/btcsuite/btcd/chaincfg/chainhash" - "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(testPrivKeyBytes) - - // Then, initialize the testMessageSigner so we can encode out - // invoices with this private key. - testMessageSigner := zpay32.MessageSigner{ - SignCompact: func(msg []byte) ([]byte, error) { - hash := chainhash.HashB(msg) - sig, err := ecdsa.SignCompact(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 -} diff --git a/zpay32/fuzz_test.go b/zpay32/fuzz_test.go new file mode 100644 index 000000000..f777bcce0 --- /dev/null +++ b/zpay32/fuzz_test.go @@ -0,0 +1,64 @@ +package zpay32 + +import ( + "encoding/hex" + "fmt" + "testing" + + "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcec/v2/ecdsa" + "github.com/btcsuite/btcd/chaincfg" + "github.com/btcsuite/btcd/chaincfg/chainhash" +) + +func Fuzz_decode(f *testing.F) { + f.Fuzz(func(t *testing.T, data string) { + inv, err := Decode(data, &chaincfg.TestNet3Params) + if err != nil { + return + } + + // Call these functions as a sanity check to make sure the + // invoice is well-formed. + _ = inv.MinFinalCLTVExpiry() + _ = inv.Expiry() + }) +} + +func Fuzz_encode(f *testing.F) { + f.Fuzz(func(t *testing.T, data string) { + inv, err := Decode(data, &chaincfg.TestNet3Params) + if err != nil { + return + } + + // 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(testPrivKeyBytes) + + // Then, initialize the testMessageSigner so we can encode out + // invoices with this private key. + testMessageSigner := MessageSigner{ + SignCompact: func(msg []byte) ([]byte, error) { + hash := chainhash.HashB(msg) + sig, err := ecdsa.SignCompact(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 + } + }) +}