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