github.com/nnlgsakib/mind-dpos@v0.0.0-20230606105614-f3c8ca06f808/cmd/gttc/consolecmd_test.go (about) 1 // Copyright 2016 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "crypto/rand" 21 "github.com/TTCECO/gttc/common/hexutil" 22 "math/big" 23 "os" 24 "path/filepath" 25 "runtime" 26 "strconv" 27 "strings" 28 "testing" 29 "time" 30 31 "github.com/TTCECO/gttc/params" 32 ) 33 34 const ( 35 ipcAPIs = "admin:1.0 alien: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" 36 httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0" 37 ) 38 39 // Tests that a node embedded within a console can be started up properly and 40 // then terminated by closing the input stream. 41 func TestConsoleWelcome(t *testing.T) { 42 coinbase := hexutil.CustomHexPrefix + "8605cdbbdb6d264aa742e77020dcbc58fcdce182" 43 44 // Start a geth console, make sure it's cleaned up and terminate the console 45 geth := runGeth(t, 46 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 47 "--etherbase", coinbase, "--shh", 48 "console") 49 50 // Gather all the infos the welcome message needs to contain 51 geth.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 52 geth.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 53 geth.SetTemplateFunc("gover", runtime.Version) 54 geth.SetTemplateFunc("gethver", func() string { return params.Version }) 55 geth.SetTemplateFunc("niltime", func() string { return time.Unix(1554004800, 0).Format(time.RFC1123) }) 56 geth.SetTemplateFunc("apis", func() string { return ipcAPIs }) 57 58 // Verify the actual welcome message to the required template 59 geth.Expect(` 60 Welcome to the Gttc JavaScript console! 61 62 instance: gttc/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}} 63 coinbase: {{.Etherbase}} 64 at block: 0 ({{niltime}}) 65 datadir: {{.Datadir}} 66 modules: {{apis}} 67 68 > {{.InputLine "exit"}} 69 `) 70 geth.ExpectExit() 71 } 72 73 // Tests that a console can be attached to a running node via various means. 74 func TestIPCAttachWelcome(t *testing.T) { 75 // Configure the instance for IPC attachement 76 coinbase := hexutil.CustomHexPrefix + "8605cdbbdb6d264aa742e77020dcbc58fcdce182" 77 var ipc string 78 if runtime.GOOS == "windows" { 79 ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999)) 80 } else { 81 ws := tmpdir(t) 82 defer os.RemoveAll(ws) 83 ipc = filepath.Join(ws, "gttc.ipc") 84 } 85 // Note: we need --shh because testAttachWelcome checks for default 86 // list of ipc modules and shh is included there. 87 geth := runGeth(t, 88 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 89 "--etherbase", coinbase, "--shh", "--ipcpath", ipc) 90 91 time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open 92 testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs) 93 94 geth.Interrupt() 95 geth.ExpectExit() 96 } 97 98 func TestHTTPAttachWelcome(t *testing.T) { 99 coinbase := hexutil.CustomHexPrefix + "8605cdbbdb6d264aa742e77020dcbc58fcdce182" 100 port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P 101 geth := runGeth(t, 102 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 103 "--etherbase", coinbase, "--rpc", "--rpcport", port) 104 105 time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open 106 testAttachWelcome(t, geth, "http://localhost:"+port, httpAPIs) 107 108 geth.Interrupt() 109 geth.ExpectExit() 110 } 111 112 func TestWSAttachWelcome(t *testing.T) { 113 coinbase := hexutil.CustomHexPrefix + "8605cdbbdb6d264aa742e77020dcbc58fcdce182" 114 port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P 115 116 geth := runGeth(t, 117 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 118 "--etherbase", coinbase, "--ws", "--wsport", port) 119 120 time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open 121 testAttachWelcome(t, geth, "ws://localhost:"+port, httpAPIs) 122 123 geth.Interrupt() 124 geth.ExpectExit() 125 } 126 127 func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) { 128 // Attach to a running geth note and terminate immediately 129 attach := runGeth(t, "attach", endpoint) 130 defer attach.ExpectExit() 131 attach.CloseStdin() 132 133 // Gather all the infos the welcome message needs to contain 134 attach.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 135 attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 136 attach.SetTemplateFunc("gover", runtime.Version) 137 attach.SetTemplateFunc("gethver", func() string { return params.Version }) 138 attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase }) 139 attach.SetTemplateFunc("niltime", func() string { return time.Unix(1554004800, 0).Format(time.RFC1123) }) 140 attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") }) 141 attach.SetTemplateFunc("datadir", func() string { return geth.Datadir }) 142 attach.SetTemplateFunc("apis", func() string { return apis }) 143 144 // Verify the actual welcome message to the required template 145 attach.Expect(` 146 Welcome to the Gttc JavaScript console! 147 148 instance: gttc/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}} 149 coinbase: {{etherbase}} 150 at block: 0 ({{niltime}}){{if ipc}} 151 datadir: {{datadir}}{{end}} 152 modules: {{apis}} 153 154 > {{.InputLine "exit" }} 155 `) 156 attach.ExpectExit() 157 } 158 159 // trulyRandInt generates a crypto random integer used by the console tests to 160 // not clash network ports with other tests running cocurrently. 161 func trulyRandInt(lo, hi int) int { 162 num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo))) 163 return int(num.Int64()) + lo 164 }