consolecmd_test.go 5.4 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. "math/rand"
  19. "os"
  20. "path/filepath"
  21. "runtime"
  22. "sort"
  23. "strconv"
  24. "strings"
  25. "testing"
  26. "time"
  27. "github.com/ethereum/go-ethereum/rpc"
  28. )
  29. // Tests that a node embedded within a console can be started up properly and
  30. // then terminated by closing the input stream.
  31. func TestConsoleWelcome(t *testing.T) {
  32. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  33. // Start a geth console, make sure it's cleaned up and terminate the console
  34. geth := runGeth(t,
  35. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  36. "--etherbase", coinbase, "--shh",
  37. "console")
  38. // Gather all the infos the welcome message needs to contain
  39. geth.setTemplateFunc("goos", func() string { return runtime.GOOS })
  40. geth.setTemplateFunc("gover", runtime.Version)
  41. geth.setTemplateFunc("gethver", func() string { return verString })
  42. geth.setTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
  43. geth.setTemplateFunc("apis", func() []string {
  44. apis := append(strings.Split(rpc.DefaultIPCApis, ","), rpc.MetadataApi)
  45. sort.Strings(apis)
  46. return apis
  47. })
  48. // Verify the actual welcome message to the required template
  49. geth.expect(`
  50. Welcome to the Geth JavaScript console!
  51. instance: Geth/v{{gethver}}/{{goos}}/{{gover}}
  52. coinbase: {{.Etherbase}}
  53. at block: 0 ({{niltime}})
  54. datadir: {{.Datadir}}
  55. modules:{{range apis}} {{.}}:1.0{{end}}
  56. > {{.InputLine "exit"}}
  57. `)
  58. geth.expectExit()
  59. }
  60. // Tests that a console can be attached to a running node via various means.
  61. func TestIPCAttachWelcome(t *testing.T) {
  62. // Configure the instance for IPC attachement
  63. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  64. var ipc string
  65. if runtime.GOOS == "windows" {
  66. ipc = `\\.\pipe\geth` + strconv.Itoa(rand.Int())
  67. } else {
  68. ws := tmpdir(t)
  69. defer os.RemoveAll(ws)
  70. ipc = filepath.Join(ws, "geth.ipc")
  71. }
  72. // Note: we need --shh because testAttachWelcome checks for default
  73. // list of ipc modules and shh is included there.
  74. geth := runGeth(t,
  75. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  76. "--etherbase", coinbase, "--shh", "--ipcpath", ipc)
  77. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  78. testAttachWelcome(t, geth, "ipc:"+ipc)
  79. geth.interrupt()
  80. geth.expectExit()
  81. }
  82. func TestHTTPAttachWelcome(t *testing.T) {
  83. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  84. port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P
  85. geth := runGeth(t,
  86. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  87. "--etherbase", coinbase, "--rpc", "--rpcport", port)
  88. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  89. testAttachWelcome(t, geth, "http://localhost:"+port)
  90. geth.interrupt()
  91. geth.expectExit()
  92. }
  93. func TestWSAttachWelcome(t *testing.T) {
  94. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  95. port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P
  96. geth := runGeth(t,
  97. "--port", "0", "--maxpeers", "0", "--nodiscover", "--nat", "none",
  98. "--etherbase", coinbase, "--ws", "--wsport", port)
  99. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  100. testAttachWelcome(t, geth, "ws://localhost:"+port)
  101. geth.interrupt()
  102. geth.expectExit()
  103. }
  104. func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) {
  105. // Attach to a running geth note and terminate immediately
  106. attach := runGeth(t, "attach", endpoint)
  107. defer attach.expectExit()
  108. attach.stdin.Close()
  109. // Gather all the infos the welcome message needs to contain
  110. attach.setTemplateFunc("goos", func() string { return runtime.GOOS })
  111. attach.setTemplateFunc("gover", runtime.Version)
  112. attach.setTemplateFunc("gethver", func() string { return verString })
  113. attach.setTemplateFunc("etherbase", func() string { return geth.Etherbase })
  114. attach.setTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
  115. attach.setTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })
  116. attach.setTemplateFunc("datadir", func() string { return geth.Datadir })
  117. attach.setTemplateFunc("apis", func() []string {
  118. var apis []string
  119. if strings.HasPrefix(endpoint, "ipc") {
  120. apis = append(strings.Split(rpc.DefaultIPCApis, ","), rpc.MetadataApi)
  121. } else {
  122. apis = append(strings.Split(rpc.DefaultHTTPApis, ","), rpc.MetadataApi)
  123. }
  124. sort.Strings(apis)
  125. return apis
  126. })
  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}}/{{gover}}
  131. coinbase: {{etherbase}}
  132. at block: 0 ({{niltime}}){{if ipc}}
  133. datadir: {{datadir}}{{end}}
  134. modules:{{range apis}} {{.}}:1.0{{end}}
  135. > {{.InputLine "exit" }}
  136. `)
  137. attach.expectExit()
  138. }