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