|
| 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 | + "math/rand" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + "runtime" |
| 24 | + "sort" |
| 25 | + "strconv" |
| 26 | + "strings" |
| 27 | + "testing" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/ethereum/go-ethereum/console" |
| 31 | + "github.com/ethereum/go-ethereum/rpc" |
| 32 | +) |
| 33 | + |
| 34 | +// Tests that a node embedded within a console can be started up properly and |
| 35 | +// then terminated by closing the input stream. |
| 36 | +func TestConsoleWelcome(t *testing.T) { |
| 37 | + coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" |
| 38 | + |
| 39 | + // Start a geth console, make sure it's cleaned up and terminate the console |
| 40 | + geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "-shh", "console") |
| 41 | + defer geth.expectExit() |
| 42 | + geth.stdin.Close() |
| 43 | + |
| 44 | + // Gather all the infos the welcome message needs to contain |
| 45 | + geth.setTemplateFunc("goos", func() string { return runtime.GOOS }) |
| 46 | + geth.setTemplateFunc("gover", runtime.Version) |
| 47 | + geth.setTemplateFunc("gethver", func() string { return verString }) |
| 48 | + geth.setTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) |
| 49 | + geth.setTemplateFunc("apis", func() []string { |
| 50 | + apis := append(strings.Split(rpc.DefaultIPCApis, ","), rpc.MetadataApi) |
| 51 | + sort.Strings(apis) |
| 52 | + return apis |
| 53 | + }) |
| 54 | + geth.setTemplateFunc("prompt", func() string { return console.DefaultPrompt }) |
| 55 | + |
| 56 | + // Verify the actual welcome message to the required template |
| 57 | + geth.expect(` |
| 58 | +Welcome to the Geth JavaScript console! |
| 59 | +
|
| 60 | +instance: Geth/v{{gethver}}/{{goos}}/{{gover}} |
| 61 | +coinbase: {{.Etherbase}} |
| 62 | +at block: 0 ({{niltime}}) |
| 63 | + datadir: {{.Datadir}} |
| 64 | + modules:{{range apis}} {{.}}:1.0{{end}} |
| 65 | +
|
| 66 | +{{prompt}} |
| 67 | +`) |
| 68 | +} |
| 69 | + |
| 70 | +// Tests that a console can be attached to a running node via various means. |
| 71 | +func TestIPCAttachWelcome(t *testing.T) { |
| 72 | + // Configure the instance for IPC attachement |
| 73 | + coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" |
| 74 | + |
| 75 | + var ipc string |
| 76 | + if runtime.GOOS == "windows" { |
| 77 | + ipc = `\\.\pipe\geth` + strconv.Itoa(rand.Int()) |
| 78 | + } else { |
| 79 | + ws := tmpdir(t) |
| 80 | + defer os.RemoveAll(ws) |
| 81 | + |
| 82 | + ipc = filepath.Join(ws, "geth.ipc") |
| 83 | + } |
| 84 | + // Run the parent geth and attach with a child console |
| 85 | + geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "-shh", "--ipcpath", ipc) |
| 86 | + defer geth.interrupt() |
| 87 | + |
| 88 | + time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open |
| 89 | + testAttachWelcome(t, geth, "ipc:"+ipc) |
| 90 | +} |
| 91 | + |
| 92 | +func TestHTTPAttachWelcome(t *testing.T) { |
| 93 | + coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" |
| 94 | + port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P |
| 95 | + |
| 96 | + geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "--rpc", "--rpcport", port) |
| 97 | + defer geth.interrupt() |
| 98 | + |
| 99 | + time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open |
| 100 | + testAttachWelcome(t, geth, "http://localhost:"+port) |
| 101 | +} |
| 102 | + |
| 103 | +func TestWSAttachWelcome(t *testing.T) { |
| 104 | + coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" |
| 105 | + port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P |
| 106 | + |
| 107 | + geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "--ws", "--wsport", port) |
| 108 | + defer geth.interrupt() |
| 109 | + |
| 110 | + time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open |
| 111 | + testAttachWelcome(t, geth, "ws://localhost:"+port) |
| 112 | +} |
| 113 | + |
| 114 | +func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) { |
| 115 | + // Attach to a running geth note and terminate immediately |
| 116 | + attach := runGeth(t, "attach", endpoint) |
| 117 | + defer attach.expectExit() |
| 118 | + attach.stdin.Close() |
| 119 | + |
| 120 | + // Gather all the infos the welcome message needs to contain |
| 121 | + attach.setTemplateFunc("goos", func() string { return runtime.GOOS }) |
| 122 | + attach.setTemplateFunc("gover", runtime.Version) |
| 123 | + attach.setTemplateFunc("gethver", func() string { return verString }) |
| 124 | + attach.setTemplateFunc("etherbase", func() string { return geth.Etherbase }) |
| 125 | + attach.setTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) |
| 126 | + attach.setTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") }) |
| 127 | + attach.setTemplateFunc("datadir", func() string { return geth.Datadir }) |
| 128 | + attach.setTemplateFunc("apis", func() []string { |
| 129 | + var apis []string |
| 130 | + if strings.HasPrefix(endpoint, "ipc") { |
| 131 | + apis = append(strings.Split(rpc.DefaultIPCApis, ","), rpc.MetadataApi) |
| 132 | + } else { |
| 133 | + apis = append(strings.Split(rpc.DefaultHTTPApis, ","), rpc.MetadataApi) |
| 134 | + } |
| 135 | + sort.Strings(apis) |
| 136 | + return apis |
| 137 | + }) |
| 138 | + attach.setTemplateFunc("prompt", func() string { return console.DefaultPrompt }) |
| 139 | + |
| 140 | + // Verify the actual welcome message to the required template |
| 141 | + attach.expect(` |
| 142 | +Welcome to the Geth JavaScript console! |
| 143 | +
|
| 144 | +instance: Geth/v{{gethver}}/{{goos}}/{{gover}} |
| 145 | +coinbase: {{etherbase}} |
| 146 | +at block: 0 ({{niltime}}){{if ipc}} |
| 147 | + datadir: {{datadir}}{{end}} |
| 148 | + modules:{{range apis}} {{.}}:1.0{{end}} |
| 149 | +
|
| 150 | +{{prompt}} |
| 151 | +`) |
| 152 | +} |
0 commit comments