github.com/vipernet-xyz/tm@v0.34.24/test/fuzz/p2p/addrbook/init-corpus/main.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/json"
     5  	"flag"
     6  	"fmt"
     7  	"log"
     8  	"net"
     9  	"os"
    10  	"path/filepath"
    11  
    12  	"github.com/vipernet-xyz/tm/crypto/ed25519"
    13  	"github.com/vipernet-xyz/tm/p2p"
    14  )
    15  
    16  func main() {
    17  	baseDir := flag.String("base", ".", `where the "corpus" directory will live`)
    18  	flag.Parse()
    19  
    20  	initCorpus(*baseDir)
    21  }
    22  
    23  func initCorpus(baseDir string) {
    24  	log.SetFlags(0)
    25  
    26  	// create "corpus" directory
    27  	corpusDir := filepath.Join(baseDir, "corpus")
    28  	if err := os.MkdirAll(corpusDir, 0o755); err != nil {
    29  		log.Fatalf("Creating %q err: %v", corpusDir, err)
    30  	}
    31  
    32  	// create corpus
    33  	privKey := ed25519.GenPrivKey()
    34  	addrs := []*p2p.NetAddress{
    35  		{ID: p2p.PubKeyToID(privKey.PubKey()), IP: net.IPv4(0, 0, 0, 0), Port: 0},
    36  		{ID: p2p.PubKeyToID(privKey.PubKey()), IP: net.IPv4(127, 0, 0, 0), Port: 80},
    37  		{ID: p2p.PubKeyToID(privKey.PubKey()), IP: net.IPv4(213, 87, 10, 200), Port: 8808},
    38  		{ID: p2p.PubKeyToID(privKey.PubKey()), IP: net.IPv4(111, 111, 111, 111), Port: 26656},
    39  		{ID: p2p.PubKeyToID(privKey.PubKey()), IP: net.ParseIP("2001:db8::68"), Port: 26656},
    40  	}
    41  
    42  	for i, addr := range addrs {
    43  		filename := filepath.Join(corpusDir, fmt.Sprintf("%d.json", i))
    44  
    45  		bz, err := json.Marshal(addr)
    46  		if err != nil {
    47  			log.Fatalf("can't marshal %v: %v", addr, err)
    48  		}
    49  
    50  		//nolint:gosec // G306: Expect WriteFile permissions to be 0600 or less
    51  		if err := os.WriteFile(filename, bz, 0o644); err != nil {
    52  			log.Fatalf("can't write %v to %q: %v", addr, filename, err)
    53  		}
    54  
    55  		log.Printf("wrote %q", filename)
    56  	}
    57  }