github.com/theQRL/go-zond@v0.2.1/cmd/gzond/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 "path/filepath" 23 "runtime" 24 "strconv" 25 "strings" 26 "testing" 27 "time" 28 29 "github.com/theQRL/go-zond/params" 30 ) 31 32 const ( 33 ipcAPIs = "admin:1.0 debug:1.0 engine:1.0 miner:1.0 net:1.0 rpc:1.0 txpool:1.0 web3:1.0 zond:1.0" 34 httpAPIs = "net:1.0 rpc:1.0 web3:1.0 zond:1.0" 35 ) 36 37 // spawns gzond with the given command line args, using a set of flags to minimise 38 // memory and disk IO. If the args don't set --datadir, the 39 // child g gets a temporary data directory. 40 func runMinimalGzond(t *testing.T, args ...string) *testgzond { 41 // --networkid=1337 to avoid cache bump 42 // --syncmode=full to avoid allocating fast sync bloom 43 allArgs := []string{"--networkid", "1337", "--authrpc.port", "0", "--syncmode=full", "--port", "0", 44 "--nat", "none", "--nodiscover", "--maxpeers", "0", "--cache", "64", 45 "--datadir.minfreedisk", "0"} 46 return runGzond(t, append(allArgs, args...)...) 47 } 48 49 // Tests that a node embedded within a console can be started up properly and 50 // then terminated by closing the input stream. 51 func TestConsoleWelcome(t *testing.T) { 52 // Start a gzond console, make sure it's cleaned up and terminate the console 53 gzond := runMinimalGzond(t, "console") 54 55 // Gather all the infos the welcome message needs to contain 56 gzond.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 57 gzond.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 58 gzond.SetTemplateFunc("gover", runtime.Version) 59 gzond.SetTemplateFunc("gzondver", func() string { return params.VersionWithCommit("", "") }) 60 gzond.SetTemplateFunc("niltime", func() string { 61 // TODO(now.youtrack.cloud/issue/TGZ-16): we need to change the time based on the chain config selected 62 // return time.Unix(1548854791, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)") 63 return time.Unix(0, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)") 64 }) 65 gzond.SetTemplateFunc("apis", func() string { return ipcAPIs }) 66 67 // Verify the actual welcome message to the required template 68 gzond.Expect(` 69 Welcome to the Gzond JavaScript console! 70 71 instance: Gzond/v{{gzondver}}/{{goos}}-{{goarch}}/{{gover}} 72 at block: 0 ({{niltime}}) 73 datadir: {{.Datadir}} 74 modules: {{apis}} 75 76 To exit, press ctrl-d or type exit 77 > {{.InputLine "exit"}} 78 `) 79 gzond.ExpectExit() 80 } 81 82 // Tests that a console can be attached to a running node via various means. 83 func TestAttachWelcome(t *testing.T) { 84 var ( 85 ipc string 86 httpPort string 87 wsPort string 88 ) 89 // Configure the instance for IPC attachment 90 if runtime.GOOS == "windows" { 91 ipc = `\\.\pipe\gzond` + strconv.Itoa(trulyRandInt(100000, 999999)) 92 } else { 93 ipc = filepath.Join(t.TempDir(), "gzond.ipc") 94 } 95 // And HTTP + WS attachment 96 p := trulyRandInt(1024, 65533) // Yeah, sometimes this will fail, sorry :P 97 httpPort = strconv.Itoa(p) 98 wsPort = strconv.Itoa(p + 1) 99 gzond := runMinimalGzond(t, 100 "--ipcpath", ipc, 101 "--http", "--http.port", httpPort, 102 "--ws", "--ws.port", wsPort) 103 t.Run("ipc", func(t *testing.T) { 104 waitForEndpoint(t, ipc, 3*time.Second) 105 testAttachWelcome(t, gzond, "ipc:"+ipc, ipcAPIs) 106 }) 107 t.Run("http", func(t *testing.T) { 108 endpoint := "http://127.0.0.1:" + httpPort 109 waitForEndpoint(t, endpoint, 3*time.Second) 110 testAttachWelcome(t, gzond, endpoint, httpAPIs) 111 }) 112 t.Run("ws", func(t *testing.T) { 113 endpoint := "ws://127.0.0.1:" + wsPort 114 waitForEndpoint(t, endpoint, 3*time.Second) 115 testAttachWelcome(t, gzond, endpoint, httpAPIs) 116 }) 117 gzond.Kill() 118 } 119 120 func testAttachWelcome(t *testing.T, gzond *testgzond, endpoint, apis string) { 121 // Attach to a running gzond node and terminate immediately 122 attach := runGzond(t, "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 attach.SetTemplateFunc("gzondver", func() string { return params.VersionWithCommit("", "") }) 131 attach.SetTemplateFunc("niltime", func() string { 132 // TODO(now.youtrack.cloud/issue/TGZ-16): we need to change the time based on the chain config selected 133 // return time.Unix(1548854791, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)") 134 return time.Unix(0, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)") 135 }) 136 attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") }) 137 attach.SetTemplateFunc("datadir", func() string { return gzond.Datadir }) 138 attach.SetTemplateFunc("apis", func() string { return apis }) 139 140 // Verify the actual welcome message to the required template 141 attach.Expect(` 142 Welcome to the Gzond JavaScript console! 143 144 instance: Gzond/v{{gzondver}}/{{goos}}-{{goarch}}/{{gover}} 145 at block: 0 ({{niltime}}){{if ipc}} 146 datadir: {{datadir}}{{end}} 147 modules: {{apis}} 148 149 To exit, press ctrl-d or type exit 150 > {{.InputLine "exit" }} 151 `) 152 attach.ExpectExit() 153 } 154 155 // trulyRandInt generates a crypto random integer used by the console tests to 156 // not clash network ports with other tests running concurrently. 157 func trulyRandInt(lo, hi int) int { 158 num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo))) 159 return int(num.Int64()) + lo 160 }