github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/cmd/quickchain/consolecmd_test.go (about) 1 2 package main 3 4 import ( 5 "crypto/rand" 6 "math/big" 7 "os" 8 "path/filepath" 9 "runtime" 10 "strconv" 11 "strings" 12 "testing" 13 "time" 14 15 "github.com/quickchainproject/quickchain/params" 16 ) 17 18 const ( 19 ipcAPIs = "admin:1.0 debug:1.0 dpos:1.0 let:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0" 20 httpAPIs = "let:1.0 net:1.0 rpc:1.0 web3:1.0" 21 ) 22 23 // Tests that a node embedded within a console can be started up properly and 24 // then terminated by closing the input stream. 25 func TestConsoleWelcome(t *testing.T) { 26 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 27 28 // Start a geth console, make sure it's cleaned up and terminate the console 29 geth := runGeth(t, 30 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 31 "--etherbase", coinbase, 32 "console") 33 34 // Gather all the infos the welcome message needs to contain 35 geth.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 36 geth.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 37 geth.SetTemplateFunc("gover", runtime.Version) 38 geth.SetTemplateFunc("gethver", func() string { return params.Version }) 39 geth.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) 40 geth.SetTemplateFunc("apis", func() string { return ipcAPIs }) 41 42 // Verify the actual welcome message to the required template 43 geth.Expect(` 44 Welcome to the Quickchain JavaScript console! 45 46 instance: Quickchain/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}} 47 coinbase: {{.Etherbase}} 48 at block: 0 ({{niltime}}) 49 datadir: {{.Datadir}} 50 modules: {{apis}} 51 52 > {{.InputLine "exit"}} 53 `) 54 geth.ExpectExit() 55 } 56 57 // Tests that a console can be attached to a running node via various means. 58 func TestIPCAttachWelcome(t *testing.T) { 59 // Configure the instance for IPC attachement 60 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 61 var ipc string 62 if runtime.GOOS == "windows" { 63 ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999)) 64 } else { 65 ws := tmpdir(t) 66 defer os.RemoveAll(ws) 67 ipc = filepath.Join(ws, "qct.ipc") 68 } 69 // Note: we need --shh because testAttachWelcome checks for default 70 // list of ipc modules and shh is included there. 71 geth := runGeth(t, 72 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 73 "--etherbase", coinbase, "--ipcpath", ipc) 74 75 time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open 76 testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs) 77 78 geth.Interrupt() 79 geth.ExpectExit() 80 } 81 82 func TestHTTPAttachWelcome(t *testing.T) { 83 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 84 port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P 85 geth := runGeth(t, 86 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 87 "--etherbase", coinbase, "--rpc", "--rpcport", port) 88 89 time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open 90 testAttachWelcome(t, geth, "http://localhost:"+port, httpAPIs) 91 92 geth.Interrupt() 93 geth.ExpectExit() 94 } 95 96 func TestWSAttachWelcome(t *testing.T) { 97 coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182" 98 port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P 99 100 geth := runGeth(t, 101 "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none", 102 "--etherbase", coinbase, "--ws", "--wsport", port) 103 104 time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open 105 testAttachWelcome(t, geth, "ws://localhost:"+port, httpAPIs) 106 107 geth.Interrupt() 108 geth.ExpectExit() 109 } 110 111 func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) { 112 // Attach to a running geth note and terminate immediately 113 attach := runGeth(t, "attach", endpoint) 114 defer attach.ExpectExit() 115 attach.CloseStdin() 116 117 // Gather all the infos the welcome message needs to contain 118 attach.SetTemplateFunc("goos", func() string { return runtime.GOOS }) 119 attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH }) 120 attach.SetTemplateFunc("gover", runtime.Version) 121 attach.SetTemplateFunc("gethver", func() string { return params.Version }) 122 attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase }) 123 attach.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) }) 124 attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") }) 125 attach.SetTemplateFunc("datadir", func() string { return geth.Datadir }) 126 attach.SetTemplateFunc("apis", func() string { return apis }) 127 128 // Verify the actual welcome message to the required template 129 attach.Expect(` 130 Welcome to the Quickchain JavaScript console! 131 132 instance: Quickchain/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}} 133 coinbase: {{etherbase}} 134 at block: 0 ({{niltime}}){{if ipc}} 135 datadir: {{datadir}}{{end}} 136 modules: {{apis}} 137 138 > {{.InputLine "exit" }} 139 `) 140 attach.ExpectExit() 141 } 142 143 // trulyRandInt generates a crypto random integer used by the console tests to 144 // not clash network ports with other tests running cocurrently. 145 func trulyRandInt(lo, hi int) int { 146 num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo))) 147 return int(num.Int64()) + lo 148 }