consolecmd_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // Tests that a node embedded within a console can be started up properly and
  34. // then terminated by closing the input stream.
  35. func TestConsoleWelcome(t *testing.T) {
  36. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  37. // Start a geth console, make sure it's cleaned up and terminate the console
  38. geth := runGeth(t,
  39. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  40. "--etherbase", coinbase,
  41. "console")
  42. // Gather all the infos the welcome message needs to contain
  43. geth.SetTemplateFunc("goos", func() string { return runtime.GOOS })
  44. geth.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
  45. geth.SetTemplateFunc("gover", runtime.Version)
  46. geth.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit("", "") })
  47. geth.SetTemplateFunc("niltime", func() string {
  48. return time.Unix(0, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
  49. })
  50. geth.SetTemplateFunc("apis", func() string { return ipcAPIs })
  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: {{apis}}
  59. To exit, press ctrl-d
  60. > {{.InputLine "exit"}}
  61. `)
  62. geth.ExpectExit()
  63. }
  64. // Tests that a console can be attached to a running node via various means.
  65. func TestIPCAttachWelcome(t *testing.T) {
  66. // Configure the instance for IPC attachment
  67. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  68. var ipc string
  69. if runtime.GOOS == "windows" {
  70. ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999))
  71. } else {
  72. ws := tmpdir(t)
  73. defer os.RemoveAll(ws)
  74. ipc = filepath.Join(ws, "geth.ipc")
  75. }
  76. geth := runGeth(t,
  77. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  78. "--etherbase", coinbase, "--ipcpath", ipc)
  79. defer func() {
  80. geth.Interrupt()
  81. geth.ExpectExit()
  82. }()
  83. waitForEndpoint(t, ipc, 3*time.Second)
  84. testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
  85. }
  86. func TestHTTPAttachWelcome(t *testing.T) {
  87. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  88. port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
  89. geth := runGeth(t,
  90. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  91. "--etherbase", coinbase, "--http", "--http.port", port)
  92. defer func() {
  93. geth.Interrupt()
  94. geth.ExpectExit()
  95. }()
  96. endpoint := "http://127.0.0.1:" + port
  97. waitForEndpoint(t, endpoint, 3*time.Second)
  98. testAttachWelcome(t, geth, endpoint, httpAPIs)
  99. }
  100. func TestWSAttachWelcome(t *testing.T) {
  101. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  102. port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
  103. geth := runGeth(t,
  104. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  105. "--etherbase", coinbase, "--ws", "--ws.port", port)
  106. defer func() {
  107. geth.Interrupt()
  108. geth.ExpectExit()
  109. }()
  110. endpoint := "ws://127.0.0.1:" + port
  111. waitForEndpoint(t, endpoint, 3*time.Second)
  112. testAttachWelcome(t, geth, endpoint, httpAPIs)
  113. }
  114. func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
  115. // Attach to a running geth note and terminate immediately
  116. attach := runGeth(t, "attach", endpoint)
  117. defer attach.ExpectExit()
  118. attach.CloseStdin()
  119. // Gather all the infos the welcome message needs to contain
  120. attach.SetTemplateFunc("goos", func() string { return runtime.GOOS })
  121. attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
  122. attach.SetTemplateFunc("gover", runtime.Version)
  123. attach.SetTemplateFunc("gethver", func() string { return params.VersionWithCommit("", "") })
  124. attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase })
  125. attach.SetTemplateFunc("niltime", func() string {
  126. return time.Unix(0, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
  127. })
  128. attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })
  129. attach.SetTemplateFunc("datadir", func() string { return geth.Datadir })
  130. attach.SetTemplateFunc("apis", func() string { return apis })
  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: {{apis}}
  139. To exit, press ctrl-d
  140. > {{.InputLine "exit" }}
  141. `)
  142. attach.ExpectExit()
  143. }
  144. // trulyRandInt generates a crypto random integer used by the console tests to
  145. // not clash network ports with other tests running cocurrently.
  146. func trulyRandInt(lo, hi int) int {
  147. num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
  148. return int(num.Int64()) + lo
  149. }