consolecmd_test.go 5.7 KB

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