consolecmd_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 miner:1.0 net:1.0 personal:1.0 rpc:1.0 shh: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, "--shh",
  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.Version })
  47. geth.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
  48. geth.SetTemplateFunc("apis", func() string { return ipcAPIs })
  49. // Verify the actual welcome message to the required template
  50. geth.Expect(`
  51. Welcome to the Geth JavaScript console!
  52. instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
  53. coinbase: {{.Etherbase}}
  54. at block: 0 ({{niltime}})
  55. datadir: {{.Datadir}}
  56. modules: {{apis}}
  57. > {{.InputLine "exit"}}
  58. `)
  59. geth.ExpectExit()
  60. }
  61. // Tests that a console can be attached to a running node via various means.
  62. func TestIPCAttachWelcome(t *testing.T) {
  63. // Configure the instance for IPC attachement
  64. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  65. var ipc string
  66. if runtime.GOOS == "windows" {
  67. ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999))
  68. } else {
  69. ws := tmpdir(t)
  70. defer os.RemoveAll(ws)
  71. ipc = filepath.Join(ws, "geth.ipc")
  72. }
  73. // Note: we need --shh because testAttachWelcome checks for default
  74. // list of ipc modules and shh is included there.
  75. geth := runGeth(t,
  76. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  77. "--etherbase", coinbase, "--shh", "--ipcpath", ipc)
  78. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  79. testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
  80. geth.Interrupt()
  81. geth.ExpectExit()
  82. }
  83. func TestHTTPAttachWelcome(t *testing.T) {
  84. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  85. port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
  86. geth := runGeth(t,
  87. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  88. "--etherbase", coinbase, "--rpc", "--rpcport", port)
  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. geth.Interrupt()
  92. geth.ExpectExit()
  93. }
  94. func TestWSAttachWelcome(t *testing.T) {
  95. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  96. port := strconv.Itoa(trulyRandInt(1024, 65536)) // Yeah, sometimes this will fail, sorry :P
  97. geth := runGeth(t,
  98. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  99. "--etherbase", coinbase, "--ws", "--wsport", port)
  100. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  101. testAttachWelcome(t, geth, "ws://localhost:"+port, httpAPIs)
  102. geth.Interrupt()
  103. geth.ExpectExit()
  104. }
  105. func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
  106. // Attach to a running geth note and terminate immediately
  107. attach := runGeth(t, "attach", endpoint)
  108. defer attach.ExpectExit()
  109. attach.CloseStdin()
  110. // Gather all the infos the welcome message needs to contain
  111. attach.SetTemplateFunc("goos", func() string { return runtime.GOOS })
  112. attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
  113. attach.SetTemplateFunc("gover", runtime.Version)
  114. attach.SetTemplateFunc("gethver", func() string { return params.Version })
  115. attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase })
  116. attach.SetTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
  117. attach.SetTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })
  118. attach.SetTemplateFunc("datadir", func() string { return geth.Datadir })
  119. attach.SetTemplateFunc("apis", func() string { return apis })
  120. // Verify the actual welcome message to the required template
  121. attach.Expect(`
  122. Welcome to the Geth JavaScript console!
  123. instance: Geth/v{{gethver}}/{{goos}}-{{goarch}}/{{gover}}
  124. coinbase: {{etherbase}}
  125. at block: 0 ({{niltime}}){{if ipc}}
  126. datadir: {{datadir}}{{end}}
  127. modules: {{apis}}
  128. > {{.InputLine "exit" }}
  129. `)
  130. attach.ExpectExit()
  131. }
  132. // trulyRandInt generates a crypto random integer used by the console tests to
  133. // not clash network ports with other tests running cocurrently.
  134. func trulyRandInt(lo, hi int) int {
  135. num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
  136. return int(num.Int64()) + lo
  137. }