github.com/DFWallet/tendermint-cosmos@v0.0.2/test/fuzz/p2p/pex/init-corpus/main.go (about)

     1  // nolint: gosec
     2  package main
     3  
     4  import (
     5  	"flag"
     6  	"fmt"
     7  	"io/ioutil"
     8  	"log"
     9  	"math/rand"
    10  	"os"
    11  	"path/filepath"
    12  
    13  	"github.com/DFWallet/tendermint-cosmos/crypto/ed25519"
    14  	"github.com/DFWallet/tendermint-cosmos/p2p"
    15  	tmp2p "github.com/DFWallet/tendermint-cosmos/proto/tendermint/p2p"
    16  )
    17  
    18  func main() {
    19  	baseDir := flag.String("base", ".", `where the "corpus" directory will live`)
    20  	flag.Parse()
    21  
    22  	initCorpus(*baseDir)
    23  }
    24  
    25  func initCorpus(rootDir string) {
    26  	log.SetFlags(0)
    27  
    28  	corpusDir := filepath.Join(rootDir, "corpus")
    29  	if err := os.MkdirAll(corpusDir, 0755); err != nil {
    30  		log.Fatalf("Creating %q err: %v", corpusDir, err)
    31  	}
    32  	sizes := []int{0, 1, 2, 17, 5, 31}
    33  
    34  	// Make the PRNG predictable
    35  	rand.Seed(10)
    36  
    37  	for _, n := range sizes {
    38  		var addrs []*p2p.NetAddress
    39  
    40  		// IPv4 addresses
    41  		for i := 0; i < n; i++ {
    42  			privKey := ed25519.GenPrivKey()
    43  			addr := fmt.Sprintf(
    44  				"%s@%v.%v.%v.%v:26656",
    45  				p2p.PubKeyToID(privKey.PubKey()),
    46  				rand.Int()%256,
    47  				rand.Int()%256,
    48  				rand.Int()%256,
    49  				rand.Int()%256,
    50  			)
    51  			netAddr, _ := p2p.NewNetAddressString(addr)
    52  			addrs = append(addrs, netAddr)
    53  		}
    54  
    55  		// IPv6 addresses
    56  		privKey := ed25519.GenPrivKey()
    57  		ipv6a, err := p2p.NewNetAddressString(
    58  			fmt.Sprintf("%s@[ff02::1:114]:26656", p2p.PubKeyToID(privKey.PubKey())))
    59  		if err != nil {
    60  			log.Fatalf("can't create a new netaddress: %v", err)
    61  		}
    62  		addrs = append(addrs, ipv6a)
    63  
    64  		msg := tmp2p.Message{
    65  			Sum: &tmp2p.Message_PexAddrs{
    66  				PexAddrs: &tmp2p.PexAddrs{Addrs: p2p.NetAddressesToProto(addrs)},
    67  			},
    68  		}
    69  		bz, err := msg.Marshal()
    70  		if err != nil {
    71  			log.Fatalf("unable to marshal: %v", err)
    72  		}
    73  
    74  		filename := filepath.Join(rootDir, "corpus", fmt.Sprintf("%d", n))
    75  
    76  		if err := ioutil.WriteFile(filename, bz, 0644); err != nil {
    77  			log.Fatalf("can't write %X to %q: %v", bz, filename, err)
    78  		}
    79  
    80  		log.Printf("wrote %q", filename)
    81  	}
    82  }