github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/cmd/u2u/launcher/fake_test.go (about)

     1  package launcher
     2  
     3  import (
     4  	"path/filepath"
     5  	"runtime"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/unicornultrafoundation/go-u2u/crypto"
    11  	"github.com/unicornultrafoundation/go-u2u/params"
    12  
    13  	"github.com/unicornultrafoundation/go-u2u/integration/makefakegenesis"
    14  	"github.com/unicornultrafoundation/go-u2u/native/validatorpk"
    15  )
    16  
    17  func TestFakeNetFlag_NonValidator(t *testing.T) {
    18  	// Start an u2u console, make sure it's cleaned up and terminate the console
    19  	cli := exec(t,
    20  		"--fakenet", "0/3",
    21  		"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", "--cache", "7923", "--datadir.minfreedisk", "1",
    22  		"console")
    23  
    24  	// Gather all the infos the welcome message needs to contain
    25  	cli.SetTemplateFunc("goos", func() string { return runtime.GOOS })
    26  	cli.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
    27  	cli.SetTemplateFunc("gover", runtime.Version)
    28  	cli.SetTemplateFunc("version", func() string { return params.VersionWithCommit("", "") })
    29  	cli.SetTemplateFunc("niltime", genesisStart)
    30  	cli.SetTemplateFunc("apis", func() string { return ipcAPIs })
    31  
    32  	waitForEndpoint(t, filepath.Join(cli.Datadir, "u2u.ipc"), 60*time.Second)
    33  
    34  	// Verify the actual welcome message to the required template
    35  	cli.Expect(`
    36  Welcome to the Hashgraph JavaScript console!
    37  
    38  instance: go-u2u/v{{version}}/{{goos}}-{{goarch}}/{{gover}}
    39  coinbase: {{.Coinbase}}
    40  at block: 1 ({{niltime}})
    41   datadir: {{.Datadir}}
    42   modules: {{apis}}
    43  
    44  To exit, press ctrl-d
    45  > {{.InputLine "exit"}}
    46  `)
    47  	cli.ExpectExit()
    48  
    49  	wantMessages := []string{
    50  		"Unlocked fake validator",
    51  	}
    52  	for _, m := range wantMessages {
    53  		if strings.Contains(cli.StderrText(), m) {
    54  			t.Errorf("stderr text contains %q", m)
    55  		}
    56  	}
    57  }
    58  
    59  func TestFakeNetFlag_Validator(t *testing.T) {
    60  	// Start an u2u console, make sure it's cleaned up and terminate the console
    61  	cli := exec(t,
    62  		"--fakenet", "3/3",
    63  		"--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", "--cache", "7923", "--datadir.minfreedisk", "1",
    64  		"console")
    65  
    66  	// Gather all the infos the welcome message needs to contain
    67  	va := readFakeValidator("3/3")
    68  	cli.Coinbase = "0x0000000000000000000000000000000000000000"
    69  	cli.SetTemplateFunc("goos", func() string { return runtime.GOOS })
    70  	cli.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
    71  	cli.SetTemplateFunc("gover", runtime.Version)
    72  	cli.SetTemplateFunc("version", func() string { return params.VersionWithCommit("", "") })
    73  	cli.SetTemplateFunc("niltime", genesisStart)
    74  	cli.SetTemplateFunc("apis", func() string { return ipcAPIs })
    75  
    76  	waitForEndpoint(t, filepath.Join(cli.Datadir, "u2u.ipc"), 60*time.Second)
    77  
    78  	// Verify the actual welcome message to the required template
    79  	cli.Expect(`
    80  Welcome to the Hashgraph JavaScript console!
    81  
    82  instance: go-u2u/v{{version}}/{{goos}}-{{goarch}}/{{gover}}
    83  coinbase: {{.Coinbase}}
    84  at block: 1 ({{niltime}})
    85   datadir: {{.Datadir}}
    86   modules: {{apis}}
    87  
    88  To exit, press ctrl-d
    89  > {{.InputLine "exit"}}
    90  `)
    91  	cli.ExpectExit()
    92  
    93  	wantMessages := []string{
    94  		"Unlocked validator key",
    95  		"pubkey=" + va.String(),
    96  	}
    97  	for _, m := range wantMessages {
    98  		if !strings.Contains(cli.StderrText(), m) {
    99  			t.Errorf("stderr text does not contain %q", m)
   100  		}
   101  	}
   102  }
   103  
   104  func readFakeValidator(fakenet string) *validatorpk.PubKey {
   105  	n, _, err := parseFakeGen(fakenet)
   106  	if err != nil {
   107  		panic(err)
   108  	}
   109  
   110  	if n < 1 {
   111  		return nil
   112  	}
   113  
   114  	return &validatorpk.PubKey{
   115  		Raw:  crypto.FromECDSAPub(&makefakegenesis.FakeKey(n).PublicKey),
   116  		Type: validatorpk.Types.Secp256k1,
   117  	}
   118  }