consolecmd_test.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. "crypto/tls"
  20. "flag"
  21. "io/ioutil"
  22. "math/big"
  23. "os"
  24. "path/filepath"
  25. "runtime"
  26. "strconv"
  27. "strings"
  28. "testing"
  29. "time"
  30. "github.com/ethereum/go-ethereum/cmd/utils"
  31. "github.com/ethereum/go-ethereum/params"
  32. testifyassert "github.com/stretchr/testify/assert"
  33. "gopkg.in/urfave/cli.v1"
  34. )
  35. const (
  36. ipcAPIs = "admin:1.0 debug:1.0 eth:1.0 istanbul:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0"
  37. httpAPIs = "admin:1.0 eth:1.0 net:1.0 rpc:1.0 web3:1.0"
  38. nodeKey = "b68c0338aa4b266bf38ebe84c6199ae9fac8b29f32998b3ed2fbeafebe8d65c9"
  39. )
  40. var genesis = `{
  41. "config": {
  42. "chainId": 2017,
  43. "homesteadBlock": 1,
  44. "eip150Block": 2,
  45. "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
  46. "eip155Block": 3,
  47. "eip158Block": 3,
  48. "istanbul": {
  49. "epoch": 30000,
  50. "policy": 0
  51. }
  52. },
  53. "nonce": "0x0",
  54. "timestamp": "0x0",
  55. "gasLimit": "0x47b760",
  56. "difficulty": "0x1",
  57. "mixHash": "0x63746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365",
  58. "coinbase": "0x0000000000000000000000000000000000000000",
  59. "alloc": {
  60. "491937757d1b26e29c507b8d4c0b233c2747e68d": {
  61. "balance": "0x446c3b15f9926687d2c40534fdb564000000000000"
  62. }
  63. },
  64. "number": "0x0",
  65. "gasUsed": "0x0",
  66. "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000"
  67. }
  68. `
  69. // spawns geth with the given command line args, using a set of flags to minimise
  70. // memory and disk IO. If the args don't set --datadir, the
  71. // child g gets a temporary data directory.
  72. func runMinimalGeth(t *testing.T, args ...string) *testgeth {
  73. // --ropsten to make the 'writing genesis to disk' faster (no accounts): it is disabled for Quorum compatibility purpose
  74. // --networkid=1337 to avoid cache bump
  75. // --syncmode=full to avoid allocating fast sync bloom
  76. allArgs := []string{ /*"--ropsten",*/ "--networkid", "1337", "--syncmode=full", "--port", "0",
  77. "--nat", "none", "--nodiscover", "--maxpeers", "0", "--cache", "64"}
  78. return runGeth(t, append(allArgs, args...)...)
  79. }
  80. // Tests that a node embedded within a console can be started up properly and
  81. // then terminated by closing the input stream.
  82. func TestConsoleWelcome(t *testing.T) {
  83. defer SetResetPrivateConfig("ignore")()
  84. coinbase := "0x491937757d1b26e29c507b8d4c0b233c2747e68d"
  85. datadir := setupIstanbul(t)
  86. defer os.RemoveAll(datadir)
  87. // Start a geth console, make sure it's cleaned up and terminate the console
  88. geth := runMinimalGeth(t, "--datadir", datadir, "--miner.etherbase", coinbase, "console")
  89. // Gather all the infos the welcome message needs to contain
  90. geth.SetTemplateFunc("goos", func() string { return runtime.GOOS })
  91. geth.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
  92. geth.SetTemplateFunc("gover", runtime.Version)
  93. geth.SetTemplateFunc("gethver", func() string { return params.VersionWithMeta })
  94. geth.SetTemplateFunc("quorumver", func() string { return params.QuorumVersion })
  95. geth.SetTemplateFunc("niltime", func() string {
  96. return time.Unix(0, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
  97. })
  98. geth.SetTemplateFunc("apis", func() string { return ipcAPIs })
  99. // Verify the actual welcome message to the required template
  100. geth.Expect(`
  101. Welcome to the Geth JavaScript console!
  102. instance: Geth/v{{gethver}}(quorum-v{{quorumver}})/{{goos}}-{{goarch}}/{{gover}}
  103. coinbase: {{.Etherbase}}
  104. at block: 0 ({{niltime}})
  105. datadir: {{.Datadir}}
  106. modules: {{apis}}
  107. To exit, press ctrl-d
  108. > {{.InputLine "exit"}}
  109. `)
  110. geth.ExpectExit()
  111. }
  112. // Tests that a console can be attached to a running node via various means.
  113. func TestAttachWelcome(t *testing.T) {
  114. var (
  115. ipc string
  116. httpPort string
  117. wsPort string
  118. )
  119. defer SetResetPrivateConfig("ignore")()
  120. // Configure the instance for IPC attachment
  121. coinbase := "0x491937757d1b26e29c507b8d4c0b233c2747e68d"
  122. datadir := setupIstanbul(t)
  123. defer os.RemoveAll(datadir)
  124. if runtime.GOOS == "windows" {
  125. ipc = `\\.\pipe\geth` + strconv.Itoa(trulyRandInt(100000, 999999))
  126. } else {
  127. ipc = filepath.Join(datadir, "geth.ipc")
  128. }
  129. // And HTTP + WS attachment
  130. p := trulyRandInt(1024, 65533) // Yeah, sometimes this will fail, sorry :P
  131. httpPort = strconv.Itoa(p)
  132. wsPort = strconv.Itoa(p + 1)
  133. geth := runMinimalGeth(t, "--datadir", datadir, "--miner.etherbase", coinbase,
  134. "--ipcpath", ipc,
  135. "--http", "--http.port", httpPort, "--http.api", "admin,eth,net,web3",
  136. "--ws", "--ws.port", wsPort, "--ws.api", "admin,eth,net,web3")
  137. t.Run("ipc", func(t *testing.T) {
  138. waitForEndpoint(t, ipc, 3*time.Second)
  139. testAttachWelcome(t, geth, "ipc:"+ipc, ipcAPIs)
  140. })
  141. t.Run("http", func(t *testing.T) {
  142. endpoint := "http://127.0.0.1:" + httpPort
  143. waitForEndpoint(t, endpoint, 3*time.Second)
  144. testAttachWelcome(t, geth, endpoint, httpAPIs)
  145. })
  146. t.Run("ws", func(t *testing.T) {
  147. endpoint := "ws://127.0.0.1:" + wsPort
  148. waitForEndpoint(t, endpoint, 3*time.Second)
  149. testAttachWelcome(t, geth, endpoint, httpAPIs)
  150. })
  151. }
  152. func testAttachWelcome(t *testing.T, geth *testgeth, endpoint, apis string) {
  153. // Attach to a running geth note and terminate immediately
  154. attach := runGeth(t, "attach", endpoint)
  155. defer attach.ExpectExit()
  156. attach.CloseStdin()
  157. // Gather all the infos the welcome message needs to contain
  158. attach.SetTemplateFunc("goos", func() string { return runtime.GOOS })
  159. attach.SetTemplateFunc("goarch", func() string { return runtime.GOARCH })
  160. attach.SetTemplateFunc("gover", runtime.Version)
  161. attach.SetTemplateFunc("gethver", func() string { return params.VersionWithMeta })
  162. attach.SetTemplateFunc("quorumver", func() string { return params.QuorumVersion })
  163. attach.SetTemplateFunc("etherbase", func() string { return geth.Etherbase })
  164. attach.SetTemplateFunc("niltime", func() string {
  165. return time.Unix(0, 0).Format("Mon Jan 02 2006 15:04:05 GMT-0700 (MST)")
  166. })
  167. attach.SetTemplateFunc("ipc", func() bool {
  168. return strings.HasPrefix(endpoint, "ipc") || strings.Contains(apis, "admin")
  169. })
  170. attach.SetTemplateFunc("datadir", func() string { return geth.Datadir })
  171. attach.SetTemplateFunc("apis", func() string { return apis })
  172. // Verify the actual welcome message to the required template
  173. attach.Expect(`
  174. Welcome to the Geth JavaScript console!
  175. instance: Geth/v{{gethver}}(quorum-v{{quorumver}})/{{goos}}-{{goarch}}/{{gover}}
  176. coinbase: {{etherbase}}
  177. at block: 0 ({{niltime}}){{if ipc}}
  178. datadir: {{datadir}}{{end}}
  179. modules: {{apis}}
  180. To exit, press ctrl-d
  181. > {{.InputLine "exit" }}
  182. `)
  183. attach.ExpectExit()
  184. }
  185. // trulyRandInt generates a crypto random integer used by the console tests to
  186. // not clash network ports with other tests running cocurrently.
  187. func trulyRandInt(lo, hi int) int {
  188. num, _ := rand.Int(rand.Reader, big.NewInt(int64(hi-lo)))
  189. return int(num.Int64()) + lo
  190. }
  191. // setupIstanbul creates a temporary directory and copies nodekey and genesis.json.
  192. // It initializes istanbul by calling geth init
  193. func setupIstanbul(t *testing.T) string {
  194. datadir := tmpdir(t)
  195. gethPath := filepath.Join(datadir, "geth")
  196. os.Mkdir(gethPath, 0700)
  197. // Initialize the data directory with the custom genesis block
  198. json := filepath.Join(datadir, "genesis.json")
  199. if err := ioutil.WriteFile(json, []byte(genesis), 0600); err != nil {
  200. t.Fatalf("failed to write genesis file: %v", err)
  201. }
  202. nodeKeyFile := filepath.Join(gethPath, "nodekey")
  203. if err := ioutil.WriteFile(nodeKeyFile, []byte(nodeKey), 0600); err != nil {
  204. t.Fatalf("failed to write nodekey file: %v", err)
  205. }
  206. runGeth(t, "--datadir", datadir, "init", json).WaitExit()
  207. return datadir
  208. }
  209. func TestReadTLSClientConfig_whenCustomizeTLSCipherSuites(t *testing.T) {
  210. assert := testifyassert.New(t)
  211. flagSet := new(flag.FlagSet)
  212. flagSet.Bool(utils.RPCClientTLSInsecureSkipVerify.Name, true, "")
  213. flagSet.String(utils.RPCClientTLSCipherSuites.Name, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", "")
  214. ctx := cli.NewContext(nil, flagSet, nil)
  215. tlsConf, ok, err := readTLSClientConfig("https://arbitraryendpoint", ctx)
  216. assert.NoError(err)
  217. assert.True(ok, "has custom TLS client configuration")
  218. assert.True(tlsConf.InsecureSkipVerify)
  219. assert.Len(tlsConf.CipherSuites, 2)
  220. assert.Contains(tlsConf.CipherSuites, tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384)
  221. assert.Contains(tlsConf.CipherSuites, tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384)
  222. }
  223. func TestReadTLSClientConfig_whenTypicalTLS(t *testing.T) {
  224. assert := testifyassert.New(t)
  225. flagSet := new(flag.FlagSet)
  226. ctx := cli.NewContext(nil, flagSet, nil)
  227. tlsConf, ok, err := readTLSClientConfig("https://arbitraryendpoint", ctx)
  228. assert.NoError(err)
  229. assert.False(ok, "no custom TLS client configuration")
  230. assert.Nil(tlsConf, "no custom TLS config is set")
  231. }
  232. func TestReadTLSClientConfig_whenTLSInsecureFlagSet(t *testing.T) {
  233. assert := testifyassert.New(t)
  234. flagSet := new(flag.FlagSet)
  235. flagSet.Bool(utils.RPCClientTLSInsecureSkipVerify.Name, true, "")
  236. ctx := cli.NewContext(nil, flagSet, nil)
  237. tlsConf, ok, err := readTLSClientConfig("https://arbitraryendpoint", ctx)
  238. assert.NoError(err)
  239. assert.True(ok, "has custom TLS client configuration")
  240. assert.True(tlsConf.InsecureSkipVerify)
  241. assert.Len(tlsConf.CipherSuites, 0)
  242. }
  243. func SetResetPrivateConfig(value string) func() {
  244. existingValue := os.Getenv("PRIVATE_CONFIG")
  245. os.Setenv("PRIVATE_CONFIG", value)
  246. return func() {
  247. os.Setenv("PRIVATE_CONFIG", existingValue)
  248. }
  249. }