github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/cmd/geth/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 "math/big" 22 "path/filepath" 23 "runtime" 24 "strconv" 25 "strings" 26 "testing" 27 "time" 28 29 "github.com/tacshi/go-ethereum/internal/version" 30 "github.com/tacshi/go-ethereum/params" 31 ) 32 33 const ( 34 ipcAPIs = "admin:1.0 clique:1.0 debug:1.0 engine:1.0 eth:1.0 miner:1.0 net:1.0 rpc:1.0 txpool:1.0 web3:1.0" 35 httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0" 36 ) 37 38 // spawns geth with the given command line args, using a set of flags to minimise 39 // memory and disk IO. If the args don't set --datadir, the 40 // child g gets a temporary data directory. 41 func runMinimalGeth(t *testing.T, args ...string) *testgeth { 42 // --goerli to make the 'writing genesis to disk' faster (no accounts) 43 // --networkid=1337 to avoid cache bump 44 // --syncmode=full to avoid allocating fast sync bloom 45 allArgs := []string{"--goerli", "--networkid", "1337", "--authrpc.port", "0", "--syncmode=full", "--port", "0", 46 "--nat", "none", "--nodiscover", "--maxpeers", "0", "--cache", "64", 47 "--datadir.minfreedisk", "0"} 48 return runGeth(t, append(allArgs, args...)...) 49 } 50 51 // Tests that a node embedded within a console can be started up properly and 52 // then terminated by closing the input stream. 53 func TestConsoleWelcome(t *testing.T) { 54 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 55 56 // Start a geth console, make sure it's cleaned up and terminate the console 57 geth := runMinimalGeth(t, "--miner.etherbase", coinbase, "console") 58 59 // Gather all the infos the welcome message needs to contain 60 geth.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 61 geth.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 62 geth.SetTemplateFunc("gover", runtime.Version) 63 git, _ := version.VCS() 64 geth.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit(git.Commit, "") }) 65 geth.SetTemplateFunc("niltime", func() string { 66 return time.Unix(1548854791, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)") 67 }) 68 geth.SetTemplateFunc("apis", func() string { return ipcAPIs }) 69 70 // Verify the actual welcome message to the required template 71 geth.Expect(` 72 Welcome to the Geth JavaScript console! 73 74 instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}} 75 coinbase: {{.Etherbase}} 76 at block: 0 ({{niltime}}) 77 datadir: {{.Datadir}} 78 modules: {{apis}} 79 80 To exit, press ctrl-d or type exit 81 > {{.InputLine "exit"}} 82 `) 83 geth.ExpectExit() 84 } 85 86 // Tests that a console can be attached to a running node via various means. 87 func TestAttachWelcome(t *testing.T) { 88 var ( 89 ipc string 90 httpPort string 91 wsPort string 92 ) 93 // Configure the instance for IPC attachment 94 if runtime.GOOS == "windows" { 95 ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999)) 96 } else { 97 ipc = filepath.Join(t.TempDir(), "geth.ipc") 98 } 99 // And HTTP + WS attachment 100 p := trulyRandInt(1024, 65533) // Yeah, sometimes this will fail, sorry :P 101 httpPort = strconv.Itoa(p) 102 wsPort = strconv.Itoa(p + 1) 103 geth := runMinimalGeth(t, "--miner.etherbase", "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182", 104 "--ipcpath", ipc, 105 "--http", "--http.port", httpPort, 106 "--ws", "--ws.port", wsPort) 107 t.Run("ipc", func(t *testing.T) { 108 waitForEndpoint(t, ipc, 3*time.Second) 109 testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs) 110 }) 111 t.Run("http", func(t *testing.T) { 112 endpoint := "http://127.0.0.1:" + httpPort 113 waitForEndpoint(t, endpoint, 3*time.Second) 114 testAttachWelcome(t, geth, endpoint, httpAPIs) 115 }) 116 t.Run("ws", func(t *testing.T) { 117 endpoint := "ws://127.0.0.1:" + wsPort 118 waitForEndpoint(t, endpoint, 3*time.Second) 119 testAttachWelcome(t, geth, endpoint, httpAPIs) 120 }) 121 geth.ExpectExit() 122 } 123 124 func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) { 125 // Attach to a running geth node and terminate immediately 126 attach := runGeth(t, "attach", endpoint) 127 defer attach.ExpectExit() 128 attach.CloseStdin() 129 130 // Gather all the infos the welcome message needs to contain 131 attach.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 132 attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 133 attach.SetTemplateFunc("gover", runtime.Version) 134 git, _ := version.VCS() 135 attach.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit(git.Commit, "") }) 136 attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase }) 137 attach.SetTemplateFunc("niltime", func() string { 138 return time.Unix(1548854791, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)") 139 }) 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 Geth JavaScript console! 147 148 instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}} 149 coinbase: {{etherbase}} 150 at block: 0 ({{niltime}}){{if ipc}} 151 datadir: {{datadir}}{{end}} 152 modules: {{apis}} 153 154 To exit, press ctrl-d or type exit 155 > {{.InputLine "exit" }} 156 `) 157 attach.ExpectExit() 158 } 159 160 // trulyRandInt generates a crypto random integer used by the console tests to 161 // not clash network ports with other tests running concurrently. 162 func trulyRandInt(lo, hi int) int { 163 num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo))) 164 return int(num.Int64()) + lo 165 }