github.com/core-coin/go-core/v2@v2.1.9/cmd/gocore/consolecmd_test.go (about) 1 // Copyright 2023 by the Authors 2 // This file is part of go-core. 3 // 4 // go-core 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-core 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-core. 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 "strconv" 26 "strings" 27 "testing" 28 "time" 29 30 "github.com/core-coin/go-core/v2/params" 31 ) 32 33 const ( 34 ipcAPIs = "admin:1.0 cryptore:1.0 debug:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0 xcb:1.0" 35 httpAPIs = "net:1.0 rpc:1.0 web3:1.0 xcb:1.0" 36 ) 37 38 // spawns gocore with the given command line args, using a set of flags to minimise 39 // memory and disk IO. If the args don't set --datadir, the 40 // child g gets a temporary data directory. 41 func runMinimalGocore(t *testing.T, args ...string) *testgocore { 42 // --syncmode=full to avoid allocating fast sync bloom 43 allArgs := []string{"--syncmode=full", "--port", "0", 44 "--nat", "none", "--nodiscover", "--maxpeers", "0", "--cache", "64"} 45 return runGocore(t, append(allArgs, args...)...) 46 } 47 48 // Tests that a node embedded within a console can be started up properly and 49 // then terminated by closing the input stream. 50 func TestConsoleWelcome(t *testing.T) { 51 coinbase := "cb348605cdbbdb6d264aa742e77020dcbc58fcdce182" 52 53 // Start a gocore console, make sure it's cleaned up and terminate the console 54 gocore := runMinimalGocore(t, "--miner.corebase", coinbase, "console") 55 56 // Gather all the infos the welcome message needs to contain 57 gocore.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 58 gocore.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 59 gocore.SetTemplateFunc("gover", runtime.Version) 60 gocore.SetTemplateFunc("gocorever", func() string { return params.VersionWithTag("", "", "") }) 61 gocore.SetTemplateFunc("niltime", func() string { 62 return time.Unix(1651833293, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)") 63 }) 64 gocore.SetTemplateFunc("apis", func() string { return ipcAPIs }) 65 66 // Verify the actual welcome message to the required template 67 gocore.Expect(` 68 Welcome to the Gocore JavaScript console! 69 70 instance: Gocore/{{goos}}-{{goarch}}/{{gover}} 71 coinbase: {{.Corebase}} 72 at block: 0 ({{niltime}}) 73 datadir: {{.Datadir}} 74 modules: {{apis}} 75 76 To exit, press ctrl-d 77 > {{.InputLine "exit"}} 78 `) 79 gocore.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\gocore` + strconv.Itoa(trulyRandInt(100000, 999999)) 92 } else { 93 ws := tmpdir(t) 94 defer os.RemoveAll(ws) 95 ipc = filepath.Join(ws, "gocore.ipc") 96 } 97 // And HTTP + WS attachment 98 p := trulyRandInt(1024, 65533) // Yeah, sometimes this will fail, sorry :P 99 httpPort = strconv.Itoa(p) 100 wsPort = strconv.Itoa(p + 1) 101 gocore := runMinimalGocore(t, "--miner.corebase", "cb348605cdbbdb6d264aa742e77020dcbc58fcdce182", 102 "--ipcpath", ipc, 103 "--http", "--http.port", httpPort, 104 "--ws", "--ws.port", wsPort) 105 t.Run("ipc", func(t *testing.T) { 106 waitForEndpoint(t, ipc, 12*time.Second) 107 testAttachWelcome(t, gocore, "ipc:"+ipc, ipcAPIs) 108 }) 109 t.Run("http", func(t *testing.T) { 110 endpoint := "http://127.0.0.1:" + httpPort 111 waitForEndpoint(t, endpoint, 12*time.Second) 112 testAttachWelcome(t, gocore, endpoint, httpAPIs) 113 }) 114 t.Run("ws", func(t *testing.T) { 115 endpoint := "ws://127.0.0.1:" + wsPort 116 waitForEndpoint(t, endpoint, 12*time.Second) 117 testAttachWelcome(t, gocore, endpoint, httpAPIs) 118 }) 119 } 120 121 func testAttachWelcome(t *testing.T, gocore *testgocore, endpoint, apis string) { 122 // Attach to a running gocore note and terminate immediately 123 attach := runGocore(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("gocorever", func() string { return params.VersionWithTag("", "", "") }) 132 attach.SetTemplateFunc("corebase", func() string { return gocore.Corebase }) 133 attach.SetTemplateFunc("niltime", func() string { 134 return time.Unix(1651833293, 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 gocore.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 Gocore JavaScript console! 143 144 instance: Gocore/{{goos}}-{{goarch}}/{{gover}} 145 coinbase: {{corebase}} 146 at block: 0 ({{niltime}}){{if ipc}} 147 datadir: {{datadir}}{{end}} 148 modules: {{apis}} 149 150 To exit, press ctrl-d 151 > {{.InputLine "exit" }} 152 `) 153 attach.ExpectExit() 154 } 155 156 // trulyRandInt generates a crypto random integer used by the console tests to 157 // not clash network ports with other tests running cocurrently. 158 func trulyRandInt(lo, hi int) int { 159 num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo))) 160 return int(num.Int64()) + lo 161 }