2022-07-30 00:32:59 +02:00
|
|
|
package zpay32
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
2022-11-07 21:58:46 +01:00
|
|
|
func FuzzDecode(f *testing.F) {
|
2022-07-30 00:32:59 +02:00
|
|
|
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()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-11-07 21:58:46 +01:00
|
|
|
func FuzzEncode(f *testing.F) {
|
2022-07-30 00:32:59 +02:00
|
|
|
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.
|
|
|
|
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)
|
2022-11-07 23:38:16 +01:00
|
|
|
sig, err := ecdsa.SignCompact(testPrivKey, hash,
|
|
|
|
true)
|
2022-07-30 00:32:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil,
|
|
|
|
fmt.Errorf("can't sign the "+
|
|
|
|
"message: %v", err)
|
|
|
|
}
|
2022-11-07 23:38:16 +01:00
|
|
|
|
2022-07-30 00:32:59 +02:00
|
|
|
return sig, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, err = inv.Encode(testMessageSigner)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|