github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/cmd/geth/consolecmd_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser 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 // The go-aigar library 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 Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package main 19 20 import ( 21 "crypto/rand" 22 "math/big" 23 "os" 24 "path/filepath" 25 "runtime" 26 "strconv" 27 "strings" 28 "testing" 29 "time" 30 31 "github.com/AigarNetwork/aigar/params" 32 ) 33 34 const ( 35 ipcAPIs = "admin:1.0 debug:1.0 eth:1.0 ethash: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 := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 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.VersionWithCommit("", "") }) 55 geth.SetTemplateFunc("niltime", func() string { return time.Unix(0, 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 Geth JavaScript console! 61 62 instance: Geth/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 := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 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, "geth.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 waitForEndpoint(t, ipc, 3*time.Second) 92 testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs) 93 94 geth.Interrupt() 95 geth.ExpectExit() 96 } 97 98 func TestHTTPAttachWelcome(t *testing.T) { 99 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 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 endpoint := "http://127.0.0.1:" + port 106 waitForEndpoint(t, endpoint, 3*time.Second) 107 testAttachWelcome(t, geth, endpoint, httpAPIs) 108 109 geth.Interrupt() 110 geth.ExpectExit() 111 } 112 113 func TestWSAttachWelcome(t *testing.T) { 114 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 115 port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P 116 117 geth := runGeth(t, 118 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 119 "--etherbase", coinbase, "--ws", "--wsport", port) 120 121 endpoint := "ws://127.0.0.1:" + port 122 waitForEndpoint(t, endpoint, 3*time.Second) 123 testAttachWelcome(t, geth, endpoint, httpAPIs) 124 125 geth.Interrupt() 126 geth.ExpectExit() 127 } 128 129 func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) { 130 // Attach to a running geth note and terminate immediately 131 attach := runGeth(t, "attach", endpoint) 132 defer attach.ExpectExit() 133 attach.CloseStdin() 134 135 // Gather all the infos the welcome message needs to contain 136 attach.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 137 attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 138 attach.SetTemplateFunc("gover", runtime.Version) 139 attach.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit("", "") }) 140 attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase }) 141 attach.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) 142 attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") }) 143 attach.SetTemplateFunc("datadir", func() string { return geth.Datadir }) 144 attach.SetTemplateFunc("apis", func() string { return apis }) 145 146 // Verify the actual welcome message to the required template 147 attach.Expect(` 148 Welcome to the Geth JavaScript console! 149 150 instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}} 151 coinbase: {{etherbase}} 152 at block: 0 ({{niltime}}){{if ipc}} 153 datadir: {{datadir}}{{end}} 154 modules: {{apis}} 155 156 > {{.InputLine "exit" }} 157 `) 158 attach.ExpectExit() 159 } 160 161 // trulyRandInt generates a crypto random integer used by the console tests to 162 // not clash network ports with other tests running cocurrently. 163 func trulyRandInt(lo, hi int) int { 164 num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo))) 165 return int(num.Int64()) + lo 166 }