github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/cmd/sipe/consolecmd_test.go (about) 1 // Copyright 2016 The go-simplechain Authors 2 // This file is part of go-simplechain. 3 // 4 // go-simplechain 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-simplechain 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-simplechain. 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/bigzoro/my_simplechain/params" 31 ) 32 33 const ( 34 //add scrypt:1.0 and remove ethash:1.0 35 ipcAPIs = "admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 scrypt: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(1546531200, 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 Sipe JavaScript console! 61 62 instance: Sipe/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}} 63 coinbase: {{.GetSigner}} 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\sipe` + strconv.Itoa(trulyRandInt(100000, 999999)) 80 } else { 81 ws := tmpdir(t) 82 defer os.RemoveAll(ws) 83 ipc = filepath.Join(ws, "sipe.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(1546531200, 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 Sipe JavaScript console! 149 150 instance: Sipe/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 }