fuzz_test.go raw

   1  //go:build gofuzz || go1.18
   2  // +build gofuzz go1.18
   3  
   4  // Copyright (c) 2013-2017 The btcsuite developers
   5  // Copyright (c) 2015-2022 The Decred developers
   6  // Use of this source code is governed by an ISC
   7  // license that can be found in the LICENSE file.
   8  
   9  package btcec
  10  
  11  import (
  12  	"testing"
  13  
  14  	"next.orly.dev/pkg/nostr/encoders/hex"
  15  )
  16  
  17  func FuzzParsePubKey(f *testing.F) {
  18  	// 1. Seeds from pubkey tests.
  19  	for _, test := range pubKeyTests {
  20  		if test.isValid {
  21  			f.Add(test.key)
  22  		}
  23  	}
  24  	// 2. Seeds from recovery tests.
  25  	var recoveryTestPubKeys = []string{
  26  		"04E32DF42865E97135ACFB65F3BAE71BDC86F4D49150AD6A440B6F15878109880A0A2B2667F7E725CEEA70C673093BF67663E0312623C8E091B13CF2C0F11EF652",
  27  		"04A7640409AA2083FDAD38B2D8DE1263B2251799591D840653FB02DBBA503D7745FCB83D80E08A1E02896BE691EA6AFFB8A35939A646F1FC79052A744B1C82EDC3",
  28  	}
  29  	for _, pubKey := range recoveryTestPubKeys {
  30  		seed, err := hex.Dec(pubKey)
  31  		if err != nil {
  32  			f.Fatal(err)
  33  		}
  34  		f.Add(seed)
  35  	}
  36  	// Now run the fuzzer.
  37  	f.Fuzz(
  38  		func(t *testing.T, input []byte) {
  39  			key, err := ParsePubKey(input)
  40  			if key == nil && err == nil {
  41  				panic("key==nil && err==nil")
  42  			}
  43  			if key != nil && err != nil {
  44  				panic("key!=nil yet err!=nil")
  45  			}
  46  		},
  47  	)
  48  }
  49