github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/chain/neatio/consolecmd_test.go (about) 1 package main 2 3 import ( 4 "crypto/rand" 5 "math/big" 6 "os" 7 "path/filepath" 8 "runtime" 9 "strconv" 10 "strings" 11 "testing" 12 "time" 13 14 "github.com/neatlab/neatio/params" 15 ) 16 17 const ( 18 ipcAPIs = "admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 shh:1.0 txpool:1.0 web3:1.0" 19 httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0" 20 ) 21 22 func TestConsoleWelcome(t *testing.T) { 23 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 24 25 neatio := runneatchain(t, 26 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 27 "--etherbase", coinbase, "--shh", 28 "console") 29 30 neatio.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 31 neatio.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 32 neatio.SetTemplateFunc("gover", runtime.Version) 33 neatio.SetTemplateFunc("neatiover", func() string { return params.Version }) 34 neatio.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) 35 neatio.SetTemplateFunc("apis", func() string { return ipcAPIs }) 36 37 neatio.Expect(` 38 Welcome to the neatio JavaScript console! 39 40 instance: neatio/v{{neatiover}}/{{goos}}-{{goarch}}/{{gover}} 41 coinbase: {{.Etherbase}} 42 at block: 0 ({{niltime}}) 43 datadir: {{.Datadir}} 44 modules: {{apis}} 45 46 > {{.InputLine "exit"}} 47 `) 48 neatio.ExpectExit() 49 } 50 51 func TestIPCAttachWelcome(t *testing.T) { 52 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 53 var ipc string 54 if runtime.GOOS == "windows" { 55 ipc = `\\.\pipe\neatio` + strconv.Itoa(trulyRandInt(100000, 999999)) 56 } else { 57 ws := tmpdir(t) 58 defer os.RemoveAll(ws) 59 ipc = filepath.Join(ws, "neatio.ipc") 60 } 61 neatio := runneatchain(t, 62 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 63 "--etherbase", coinbase, "--shh", "--ipcpath", ipc) 64 65 time.Sleep(2 * time.Second) 66 testAttachWelcome(t, neatio, "ipc:"+ipc, ipcAPIs) 67 68 neatio.Interrupt() 69 neatio.ExpectExit() 70 } 71 72 func TestHTTPAttachWelcome(t *testing.T) { 73 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 74 port := strconv.Itoa(trulyRandInt(1024, 65536)) 75 neatio := runneatchain(t, 76 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 77 "--etherbase", coinbase, "--rpc", "--rpcport", port) 78 79 time.Sleep(2 * time.Second) 80 testAttachWelcome(t, neatio, "http://localhost:"+port, httpAPIs) 81 82 neatio.Interrupt() 83 neatio.ExpectExit() 84 } 85 86 func TestWSAttachWelcome(t *testing.T) { 87 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 88 port := strconv.Itoa(trulyRandInt(1024, 65536)) 89 90 neatio := runneatchain(t, 91 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 92 "--etherbase", coinbase, "--ws", "--wsport", port) 93 94 time.Sleep(2 * time.Second) 95 testAttachWelcome(t, neatio, "ws://localhost:"+port, httpAPIs) 96 97 neatio.Interrupt() 98 neatio.ExpectExit() 99 } 100 101 func testAttachWelcome(t *testing.T, neatio *testneatchain, endpoint, apis string) { 102 attach := runneatchain(t, "attach", endpoint) 103 defer attach.ExpectExit() 104 attach.CloseStdin() 105 106 attach.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 107 attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 108 attach.SetTemplateFunc("gover", runtime.Version) 109 attach.SetTemplateFunc("neatiover", func() string { return params.Version }) 110 attach.SetTemplateFunc("etherbase", func() string { return neatio.Etherbase }) 111 attach.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) 112 attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") }) 113 attach.SetTemplateFunc("datadir", func() string { return neatio.Datadir }) 114 attach.SetTemplateFunc("apis", func() string { return apis }) 115 116 attach.Expect(` 117 Welcome to the neatio JavaScript console! 118 119 instance: neatio/v{{neatiover}}/{{goos}}-{{goarch}}/{{gover}} 120 coinbase: {{etherbase}} 121 at block: 0 ({{niltime}}){{if ipc}} 122 datadir: {{datadir}}{{end}} 123 modules: {{apis}} 124 125 > {{.InputLine "exit" }} 126 `) 127 attach.ExpectExit() 128 } 129 130 func trulyRandInt(lo, hi int) int { 131 num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo))) 132 return int(num.Int64()) + lo 133 }