github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/cmd/gath/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 "os" 23 "path/filepath" 24 "runtime" 25 "sort" 26 "strconv" 27 "strings" 28 "testing" 29 "time" 30 31 "github.com/atheioschain/go-atheios/params" 32 "github.com/atheioschain/go-atheios/rpc" 33 ) 34 35 // Tests that a node embedded within a console can be started up properly and 36 // then terminated by closing the input stream. 37 func TestConsoleWelcome(t *testing.T) { 38 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 39 40 // Start a gath console, make sure it's cleaned up and terminate the console 41 gath := rungath(t, 42 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 43 "--etherbase", coinbase, "--shh", 44 "console") 45 46 // Gather all the infos the welcome message needs to contain 47 gath.setTemplateFunc("goos", func() string { return runtime.GOOS }) 48 gath.setTemplateFunc("gover", runtime.Version) 49 gath.setTemplateFunc("gathver", func() string { return utils.Version }) 50 gath.setTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) 51 gath.setTemplateFunc("apis", func() []string { 52 apis := append(strings.Split(rpc.DefaultIPCApis, ","), rpc.MetadataApi) 53 sort.Strings(apis) 54 return apis 55 }) 56 57 // Verify the actual welcome message to the required template 58 gath.expect(` 59 Welcome to the gath JavaScript console! 60 61 instance: gath/v{{gathver}}/{{goos}}/{{gover}} 62 coinbase: {{.Etherbase}} 63 at block: 0 ({{niltime}}) 64 datadir: {{.Datadir}} 65 modules:{{range apis}} {{.}}:1.0{{end}} 66 67 > {{.InputLine "exit"}} 68 `) 69 gath.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 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 76 var ipc string 77 if runtime.GOOS == "windows" { 78 ipc = `\\.\pipe\gath` + strconv.Itoa(trulyRandInt(100000, 999999)) 79 } else { 80 ws := tmpdir(t) 81 defer os.RemoveAll(ws) 82 ipc = filepath.Join(ws, "gath.ipc") 83 } 84 // Note: we need --shh because testAttachWelcome checks for default 85 // list of ipc modules and shh is included there. 86 gath := rungath(t, 87 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 88 "--etherbase", coinbase, "--shh", "--ipcpath", ipc) 89 90 time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open 91 testAttachWelcome(t, gath, "ipc:"+ipc) 92 93 gath.interrupt() 94 gath.expectExit() 95 } 96 97 func TestHTTPAttachWelcome(t *testing.T) { 98 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 99 port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P 100 gath := rungath(t, 101 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 102 "--etherbase", coinbase, "--rpc", "--rpcport", port) 103 104 time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open 105 testAttachWelcome(t, gath, "http://localhost:"+port) 106 107 gath.interrupt() 108 gath.expectExit() 109 } 110 111 func TestWSAttachWelcome(t *testing.T) { 112 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 113 port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P 114 115 gath := rungath(t, 116 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 117 "--etherbase", coinbase, "--ws", "--wsport", port) 118 119 time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open 120 testAttachWelcome(t, gath, "ws://localhost:"+port) 121 122 gath.interrupt() 123 gath.expectExit() 124 } 125 126 func testAttachWelcome(t *testing.T, gath *testgath, endpoint string) { 127 // Attach to a running gath note and terminate immediately 128 attach := rungath(t, "attach", endpoint) 129 defer attach.expectExit() 130 attach.stdin.Close() 131 132 // Gather all the infos the welcome message needs to contain 133 attach.setTemplateFunc("goos", func() string { return runtime.GOOS }) 134 attach.setTemplateFunc("gover", runtime.Version) 135 attach.setTemplateFunc("gathver", func() string { return utils.Version }) 136 attach.setTemplateFunc("etherbase", func() string { return gath.Etherbase }) 137 attach.setTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) 138 attach.setTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") }) 139 attach.setTemplateFunc("datadir", func() string { return gath.Datadir }) 140 attach.setTemplateFunc("apis", func() []string { 141 var apis []string 142 if strings.HasPrefix(endpoint, "ipc") { 143 apis = append(strings.Split(rpc.DefaultIPCApis, ","), rpc.MetadataApi) 144 } else { 145 apis = append(strings.Split(rpc.DefaultHTTPApis, ","), rpc.MetadataApi) 146 } 147 sort.Strings(apis) 148 return apis 149 }) 150 151 // Verify the actual welcome message to the required template 152 attach.expect(` 153 Welcome to the gath JavaScript console! 154 155 instance: gath/v{{gathver}}/{{goos}}/{{gover}} 156 coinbase: {{etherbase}} 157 at block: 0 ({{niltime}}){{if ipc}} 158 datadir: {{datadir}}{{end}} 159 modules:{{range apis}} {{.}}:1.0{{end}} 160 161 > {{.InputLine "exit" }} 162 `) 163 attach.expectExit() 164 } 165 166 // trulyRandInt generates a crypto random integer used by the console tests to 167 // not clash network ports with other tests running cocurrently. 168 func trulyRandInt(lo, hi int) int { 169 num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo))) 170 return int(num.Int64()) + lo 171 }