consolecmd_test.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. package main
  17. import (
  18. "crypto/rand"
  19. "math/big"
  20. "os"
  21. "path/filepath"
  22. "runtime"
  23. "sort"
  24. "strconv"
  25. "strings"
  26. "testing"
  27. "time"
  28. "github.com/ethereum/go-ethereum/params"
  29. "github.com/ethereum/go-ethereum/rpc"
  30. )
  31. // Tests that a node embedded within a console can be started up properly and
  32. // then terminated by closing the input stream.
  33. func TestConsoleWelcome(t *testing.T) {
  34. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  35. // Start a geth console, make sure it's cleaned up and terminate the console
  36. geth := runGeth(t,
  37. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  38. "--etherbase", coinbase, "--shh",
  39. "console")
  40. // Gather all the infos the welcome message needs to contain
  41. geth.setTemplateFunc("goos", func() string { return runtime.GOOS })
  42. geth.setTemplateFunc("goarch", func() string { return runtime.GOARCH })
  43. geth.setTemplateFunc("gover", runtime.Version)
  44. geth.setTemplateFunc("gethver", func() string { return params.Version })
  45. geth.setTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
  46. geth.setTemplateFunc("apis", func() []string {
  47. apis := append(strings.Split(rpc.DefaultIPCApis, ","), rpc.MetadataApi)
  48. sort.Strings(apis)
  49. return apis
  50. })
  51. // Verify the actual welcome message to the required template
  52. geth.expect(`
  53. Welcome to the Geth JavaScript console!
  54. instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
  55. coinbase: {{.Etherbase}}
  56. at block: 0 ({{niltime}})
  57. datadir: {{.Datadir}}
  58. modules:{{range apis}} {{.}}:1.0{{end}}
  59. > {{.InputLine "exit"}}
  60. `)
  61. geth.expectExit()
  62. }
  63. // Tests that a console can be attached to a running node via various means.
  64. func TestIPCAttachWelcome(t *testing.T) {
  65. // Configure the instance for IPC attachement
  66. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  67. var ipc string
  68. if runtime.GOOS == "windows" {
  69. ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999))
  70. } else {
  71. ws := tmpdir(t)
  72. defer os.RemoveAll(ws)
  73. ipc = filepath.Join(ws, "geth.ipc")
  74. }
  75. // Note: we need --shh because testAttachWelcome checks for default
  76. // list of ipc modules and shh is included there.
  77. geth := runGeth(t,
  78. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  79. "--etherbase", coinbase, "--shh", "--ipcpath", ipc)
  80. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  81. testAttachWelcome(t, geth, "ipc:"+ipc)
  82. geth.interrupt()
  83. geth.expectExit()
  84. }
  85. func TestHTTPAttachWelcome(t *testing.T) {
  86. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  87. port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
  88. geth := runGeth(t,
  89. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  90. "--etherbase", coinbase, "--rpc", "--rpcport", port)
  91. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  92. testAttachWelcome(t, geth, "http://localhost:"+port)
  93. geth.interrupt()
  94. geth.expectExit()
  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. geth := runGeth(t,
  100. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  101. "--etherbase", coinbase, "--ws", "--wsport", port)
  102. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  103. testAttachWelcome(t, geth, "ws://localhost:"+port)
  104. geth.interrupt()
  105. geth.expectExit()
  106. }
  107. func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) {
  108. // Attach to a running geth note and terminate immediately
  109. attach := runGeth(t, "attach", endpoint)
  110. defer attach.expectExit()
  111. attach.stdin.Close()
  112. // Gather all the infos the welcome message needs to contain
  113. attach.setTemplateFunc("goos", func() string { return runtime.GOOS })
  114. attach.setTemplateFunc("goarch", func() string { return runtime.GOARCH })
  115. attach.setTemplateFunc("gover", runtime.Version)
  116. attach.setTemplateFunc("gethver", func() string { return params.Version })
  117. attach.setTemplateFunc("etherbase", func() string { return geth.Etherbase })
  118. attach.setTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
  119. attach.setTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })
  120. attach.setTemplateFunc("datadir", func() string { return geth.Datadir })
  121. attach.setTemplateFunc("apis", func() []string {
  122. var apis []string
  123. if strings.HasPrefix(endpoint, "ipc") {
  124. apis = append(strings.Split(rpc.DefaultIPCApis, ","), rpc.MetadataApi)
  125. } else {
  126. apis = append(strings.Split(rpc.DefaultHTTPApis, ","), rpc.MetadataApi)
  127. }
  128. sort.Strings(apis)
  129. return apis
  130. })
  131. // Verify the actual welcome message to the required template
  132. attach.expect(`
  133. Welcome to the Geth JavaScript console!
  134. instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
  135. coinbase: {{etherbase}}
  136. at block: 0 ({{niltime}}){{if ipc}}
  137. datadir: {{datadir}}{{end}}
  138. modules:{{range apis}} {{.}}:1.0{{end}}
  139. > {{.InputLine "exit" }}
  140. `)
  141. attach.expectExit()
  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. }