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