consolecmd_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "strconv"
  24. "strings"
  25. "testing"
  26. "time"
  27. "github.com/ethereum/go-ethereum/params"
  28. )
  29. const (
  30. ipcAPIs = "admin:1.0 debug: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"
  31. httpAPIs = "eth:1.0 net:1.0 rpc:1.0 web3:1.0"
  32. )
  33. // spawns geth with the given command line args, using a set of flags to minimise
  34. // memory and disk IO. If the args don't set --datadir, the
  35. // child g gets a temporary data directory.
  36. func runMinimalGeth(t *testing.T, args ...string) *testgeth {
  37. // --ropsten to make the 'writing genesis to disk' faster (no accounts)
  38. // --networkid=1337 to avoid cache bump
  39. // --syncmode=full to avoid allocating fast sync bloom
  40. allArgs := []string{"--ropsten", "--networkid", "1337", "--syncmode=full", "--port", "0",
  41. "--nat", "none", "--nodiscover", "--maxpeers", "0", "--cache", "64"}
  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
  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. ws := tmpdir(t)
  84. defer os.RemoveAll(ws)
  85. ipc = filepath.Join(ws, "geth.ipc")
  86. }
  87. // And HTTP + WS attachment
  88. p := trulyRandInt(1024, 65533) // Yeah, sometimes this will fail, sorry :P
  89. httpPort = strconv.Itoa(p)
  90. wsPort = strconv.Itoa(p + 1)
  91. geth := runMinimalGeth(t, "--miner.etherbase", "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182",
  92. "--ipcpath", ipc,
  93. "--http", "--http.port", httpPort,
  94. "--ws", "--ws.port", wsPort)
  95. t.Run("ipc", func(t *testing.T) {
  96. waitForEndpoint(t, ipc, 3*time.Second)
  97. testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
  98. })
  99. t.Run("http", func(t *testing.T) {
  100. endpoint := "http://127.0.0.1:" + httpPort
  101. waitForEndpoint(t, endpoint, 3*time.Second)
  102. testAttachWelcome(t, geth, endpoint, httpAPIs)
  103. })
  104. t.Run("ws", func(t *testing.T) {
  105. endpoint := "ws://127.0.0.1:" + wsPort
  106. waitForEndpoint(t, endpoint, 3*time.Second)
  107. testAttachWelcome(t, geth, endpoint, httpAPIs)
  108. })
  109. }
  110. func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
  111. // Attach to a running geth note and terminate immediately
  112. attach := runGeth(t, "attach", endpoint)
  113. defer attach.ExpectExit()
  114. attach.CloseStdin()
  115. // Gather all the infos the welcome message needs to contain
  116. attach.SetTemplateFunc("goos", func() string { return runtime.GOOS })
  117. attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
  118. attach.SetTemplateFunc("gover", runtime.Version)
  119. attach.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit("", "") })
  120. attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase })
  121. attach.SetTemplateFunc("niltime", func() string {
  122. return time.Unix(0, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
  123. })
  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. // Verify the actual welcome message to the required template
  128. attach.Expect(`
  129. Welcome to the Geth JavaScript console!
  130. instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
  131. coinbase: {{etherbase}}
  132. at block: 0 ({{niltime}}){{if ipc}}
  133. datadir: {{datadir}}{{end}}
  134. modules: {{apis}}
  135. To exit, press ctrl-d
  136. > {{.InputLine "exit" }}
  137. `)
  138. attach.ExpectExit()
  139. }
  140. // trulyRandInt generates a crypto random integer used by the console tests to
  141. // not clash network ports with other tests running cocurrently.
  142. func trulyRandInt(lo, hi int) int {
  143. num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
  144. return int(num.Int64()) + lo
  145. }