consolecmd_test.go 5.8 KB

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