consolecmd_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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/console"
  28. "github.com/ethereum/go-ethereum/rpc"
  29. )
  30. // Tests that a node embedded within a console can be started up properly and
  31. // then terminated by closing the input stream.
  32. func TestConsoleWelcome(t *testing.T) {
  33. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  34. // Start a geth console, make sure it's cleaned up and terminate the console
  35. geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "-shh", "console")
  36. defer geth.expectExit()
  37. geth.stdin.Close()
  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. geth.setTemplateFunc("prompt", func() string { return console.DefaultPrompt })
  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}}/{{gover}}
  53. coinbase: {{.Etherbase}}
  54. at block: 0 ({{niltime}})
  55. datadir: {{.Datadir}}
  56. modules:{{range apis}} {{.}}:1.0{{end}}
  57. {{prompt}}
  58. `)
  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. // Run the parent geth and attach with a child console
  73. geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "-shh", "--ipcpath", ipc)
  74. defer geth.interrupt()
  75. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  76. testAttachWelcome(t, geth, "ipc:"+ipc)
  77. }
  78. func TestHTTPAttachWelcome(t *testing.T) {
  79. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  80. port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P
  81. geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "--rpc", "--rpcport", port)
  82. defer geth.interrupt()
  83. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  84. testAttachWelcome(t, geth, "http://localhost:"+port)
  85. }
  86. func TestWSAttachWelcome(t *testing.T) {
  87. coinbase := "0x8605cdbbdb6d264aa742e77020dcbc58fcdce182"
  88. port := strconv.Itoa(rand.Intn(65535-1024) + 1024) // Yeah, sometimes this will fail, sorry :P
  89. geth := runGeth(t, "--nat", "none", "--nodiscover", "--etherbase", coinbase, "--ws", "--wsport", port)
  90. defer geth.interrupt()
  91. time.Sleep(2 * time.Second) // Simple way to wait for the RPC endpoint to open
  92. testAttachWelcome(t, geth, "ws://localhost:"+port)
  93. }
  94. func testAttachWelcome(t *testing.T, geth *testgeth, endpoint string) {
  95. // Attach to a running geth note and terminate immediately
  96. attach := runGeth(t, "attach", endpoint)
  97. defer attach.expectExit()
  98. attach.stdin.Close()
  99. // Gather all the infos the welcome message needs to contain
  100. attach.setTemplateFunc("goos", func() string { return runtime.GOOS })
  101. attach.setTemplateFunc("gover", runtime.Version)
  102. attach.setTemplateFunc("gethver", func() string { return verString })
  103. attach.setTemplateFunc("etherbase", func() string { return geth.Etherbase })
  104. attach.setTemplateFunc("niltime", func() string { return time.Unix(0, 0).Format(time.RFC1123) })
  105. attach.setTemplateFunc("ipc", func() bool { return strings.HasPrefix(endpoint, "ipc") })
  106. attach.setTemplateFunc("datadir", func() string { return geth.Datadir })
  107. attach.setTemplateFunc("apis", func() []string {
  108. var apis []string
  109. if strings.HasPrefix(endpoint, "ipc") {
  110. apis = append(strings.Split(rpc.DefaultIPCApis, ","), rpc.MetadataApi)
  111. } else {
  112. apis = append(strings.Split(rpc.DefaultHTTPApis, ","), rpc.MetadataApi)
  113. }
  114. sort.Strings(apis)
  115. return apis
  116. })
  117. attach.setTemplateFunc("prompt", func() string { return console.DefaultPrompt })
  118. // Verify the actual welcome message to the required template
  119. attach.expect(`
  120. Welcome to the Geth JavaScript console!
  121. instance: Geth/v{{gethver}}/{{goos}}/{{gover}}
  122. coinbase: {{etherbase}}
  123. at block: 0 ({{niltime}}){{if ipc}}
  124. datadir: {{datadir}}{{end}}
  125. modules:{{range apis}} {{.}}:1.0{{end}}
  126. {{prompt}}
  127. `)
  128. }