api_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser 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. // The go-ethereum library 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 Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package node
  17. import (
  18. "bytes"
  19. "io"
  20. "net"
  21. "net/http"
  22. "net/url"
  23. "strings"
  24. "testing"
  25. "github.com/ethereum/go-ethereum/rpc"
  26. "github.com/stretchr/testify/assert"
  27. )
  28. // This test uses the admin_startRPC and admin_startWS APIs,
  29. // checking whether the HTTP server is started correctly.
  30. func TestStartRPC(t *testing.T) {
  31. type test struct {
  32. name string
  33. cfg Config
  34. fn func(*testing.T, *Node, *privateAdminAPI)
  35. // Checks. These run after the node is configured and all API calls have been made.
  36. wantReachable bool // whether the HTTP server should be reachable at all
  37. wantHandlers bool // whether RegisterHandler handlers should be accessible
  38. wantRPC bool // whether JSON-RPC/HTTP should be accessible
  39. wantWS bool // whether JSON-RPC/WS should be accessible
  40. }
  41. tests := []test{
  42. {
  43. name: "all off",
  44. cfg: Config{},
  45. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  46. },
  47. wantReachable: false,
  48. wantHandlers: false,
  49. wantRPC: false,
  50. wantWS: false,
  51. },
  52. {
  53. name: "rpc enabled through config",
  54. cfg: Config{HTTPHost: "127.0.0.1"},
  55. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  56. },
  57. wantReachable: true,
  58. wantHandlers: true,
  59. wantRPC: true,
  60. wantWS: false,
  61. },
  62. {
  63. name: "rpc enabled through API",
  64. cfg: Config{},
  65. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  66. _, err := api.StartRPC(sp("127.0.0.1"), ip(0), nil, nil, nil)
  67. assert.NoError(t, err)
  68. },
  69. wantReachable: true,
  70. wantHandlers: true,
  71. wantRPC: true,
  72. wantWS: false,
  73. },
  74. {
  75. name: "rpc start again after failure",
  76. cfg: Config{},
  77. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  78. // Listen on a random port.
  79. listener, err := net.Listen("tcp", "127.0.0.1:0")
  80. if err != nil {
  81. t.Fatal("can't listen:", err)
  82. }
  83. defer listener.Close()
  84. port := listener.Addr().(*net.TCPAddr).Port
  85. // Now try to start RPC on that port. This should fail.
  86. _, err = api.StartRPC(sp("127.0.0.1"), ip(port), nil, nil, nil)
  87. if err == nil {
  88. t.Fatal("StartRPC should have failed on port", port)
  89. }
  90. // Try again after unblocking the port. It should work this time.
  91. listener.Close()
  92. _, err = api.StartRPC(sp("127.0.0.1"), ip(port), nil, nil, nil)
  93. assert.NoError(t, err)
  94. },
  95. wantReachable: true,
  96. wantHandlers: true,
  97. wantRPC: true,
  98. wantWS: false,
  99. },
  100. {
  101. name: "rpc stopped through API",
  102. cfg: Config{HTTPHost: "127.0.0.1"},
  103. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  104. _, err := api.StopRPC()
  105. assert.NoError(t, err)
  106. },
  107. wantReachable: false,
  108. wantHandlers: false,
  109. wantRPC: false,
  110. wantWS: false,
  111. },
  112. {
  113. name: "rpc stopped twice",
  114. cfg: Config{HTTPHost: "127.0.0.1"},
  115. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  116. _, err := api.StopRPC()
  117. assert.NoError(t, err)
  118. _, err = api.StopRPC()
  119. assert.NoError(t, err)
  120. },
  121. wantReachable: false,
  122. wantHandlers: false,
  123. wantRPC: false,
  124. wantWS: false,
  125. },
  126. {
  127. name: "ws enabled through config",
  128. cfg: Config{WSHost: "127.0.0.1"},
  129. wantReachable: true,
  130. wantHandlers: false,
  131. wantRPC: false,
  132. wantWS: true,
  133. },
  134. {
  135. name: "ws enabled through API",
  136. cfg: Config{},
  137. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  138. _, err := api.StartWS(sp("127.0.0.1"), ip(0), nil, nil)
  139. assert.NoError(t, err)
  140. },
  141. wantReachable: true,
  142. wantHandlers: false,
  143. wantRPC: false,
  144. wantWS: true,
  145. },
  146. {
  147. name: "ws stopped through API",
  148. cfg: Config{WSHost: "127.0.0.1"},
  149. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  150. _, err := api.StopWS()
  151. assert.NoError(t, err)
  152. },
  153. wantReachable: false,
  154. wantHandlers: false,
  155. wantRPC: false,
  156. wantWS: false,
  157. },
  158. {
  159. name: "ws stopped twice",
  160. cfg: Config{WSHost: "127.0.0.1"},
  161. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  162. _, err := api.StopWS()
  163. assert.NoError(t, err)
  164. _, err = api.StopWS()
  165. assert.NoError(t, err)
  166. },
  167. wantReachable: false,
  168. wantHandlers: false,
  169. wantRPC: false,
  170. wantWS: false,
  171. },
  172. {
  173. name: "ws enabled after RPC",
  174. cfg: Config{HTTPHost: "127.0.0.1"},
  175. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  176. wsport := n.http.port
  177. _, err := api.StartWS(sp("127.0.0.1"), ip(wsport), nil, nil)
  178. assert.NoError(t, err)
  179. },
  180. wantReachable: true,
  181. wantHandlers: true,
  182. wantRPC: true,
  183. wantWS: true,
  184. },
  185. {
  186. name: "ws enabled after RPC then stopped",
  187. cfg: Config{HTTPHost: "127.0.0.1"},
  188. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  189. wsport := n.http.port
  190. _, err := api.StartWS(sp("127.0.0.1"), ip(wsport), nil, nil)
  191. assert.NoError(t, err)
  192. _, err = api.StopWS()
  193. assert.NoError(t, err)
  194. },
  195. wantReachable: true,
  196. wantHandlers: true,
  197. wantRPC: true,
  198. wantWS: false,
  199. },
  200. {
  201. name: "rpc stopped with ws enabled",
  202. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  203. _, err := api.StartRPC(sp("127.0.0.1"), ip(0), nil, nil, nil)
  204. assert.NoError(t, err)
  205. wsport := n.http.port
  206. _, err = api.StartWS(sp("127.0.0.1"), ip(wsport), nil, nil)
  207. assert.NoError(t, err)
  208. _, err = api.StopRPC()
  209. assert.NoError(t, err)
  210. },
  211. wantReachable: false,
  212. wantHandlers: false,
  213. wantRPC: false,
  214. wantWS: false,
  215. },
  216. {
  217. name: "rpc enabled after ws",
  218. fn: func(t *testing.T, n *Node, api *privateAdminAPI) {
  219. _, err := api.StartWS(sp("127.0.0.1"), ip(0), nil, nil)
  220. assert.NoError(t, err)
  221. wsport := n.http.port
  222. _, err = api.StartRPC(sp("127.0.0.1"), ip(wsport), nil, nil, nil)
  223. assert.NoError(t, err)
  224. },
  225. wantReachable: true,
  226. wantHandlers: true,
  227. wantRPC: true,
  228. wantWS: true,
  229. },
  230. }
  231. for _, test := range tests {
  232. t.Run(test.name, func(t *testing.T) {
  233. // Apply some sane defaults.
  234. config := test.cfg
  235. // config.Logger = testlog.Logger(t, log.LvlDebug)
  236. config.P2P.NoDiscovery = true
  237. // Create Node.
  238. stack, err := New(&config)
  239. if err != nil {
  240. t.Fatal("can't create node:", err)
  241. }
  242. defer stack.Close()
  243. // Register the test handler.
  244. stack.RegisterHandler("test", "/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  245. w.Write([]byte("OK"))
  246. }))
  247. if err := stack.Start(); err != nil {
  248. t.Fatal("can't start node:", err)
  249. }
  250. // Run the API call hook.
  251. if test.fn != nil {
  252. test.fn(t, stack, &privateAdminAPI{stack})
  253. }
  254. // Check if the HTTP endpoints are available.
  255. baseURL := stack.HTTPEndpoint()
  256. reachable := checkReachable(baseURL)
  257. handlersAvailable := checkBodyOK(baseURL + "/test")
  258. rpcAvailable := checkRPC(baseURL)
  259. wsAvailable := checkRPC(strings.Replace(baseURL, "http://", "ws://", 1))
  260. if reachable != test.wantReachable {
  261. t.Errorf("HTTP server is %sreachable, want it %sreachable", not(reachable), not(test.wantReachable))
  262. }
  263. if handlersAvailable != test.wantHandlers {
  264. t.Errorf("RegisterHandler handlers %savailable, want them %savailable", not(handlersAvailable), not(test.wantHandlers))
  265. }
  266. if rpcAvailable != test.wantRPC {
  267. t.Errorf("HTTP RPC %savailable, want it %savailable", not(rpcAvailable), not(test.wantRPC))
  268. }
  269. if wsAvailable != test.wantWS {
  270. t.Errorf("WS RPC %savailable, want it %savailable", not(wsAvailable), not(test.wantWS))
  271. }
  272. })
  273. }
  274. }
  275. // checkReachable checks if the TCP endpoint in rawurl is open.
  276. func checkReachable(rawurl string) bool {
  277. u, err := url.Parse(rawurl)
  278. if err != nil {
  279. panic(err)
  280. }
  281. conn, err := net.Dial("tcp", u.Host)
  282. if err != nil {
  283. return false
  284. }
  285. conn.Close()
  286. return true
  287. }
  288. // checkBodyOK checks whether the given HTTP URL responds with 200 OK and body "OK".
  289. func checkBodyOK(url string) bool {
  290. resp, err := http.Get(url)
  291. if err != nil {
  292. return false
  293. }
  294. defer resp.Body.Close()
  295. if resp.StatusCode != 200 {
  296. return false
  297. }
  298. buf := make([]byte, 2)
  299. if _, err = io.ReadFull(resp.Body, buf); err != nil {
  300. return false
  301. }
  302. return bytes.Equal(buf, []byte("OK"))
  303. }
  304. // checkRPC checks whether JSON-RPC works against the given URL.
  305. func checkRPC(url string) bool {
  306. c, err := rpc.Dial(url)
  307. if err != nil {
  308. return false
  309. }
  310. defer c.Close()
  311. _, err = c.SupportedModules()
  312. return err == nil
  313. }
  314. // string/int pointer helpers.
  315. func sp(s string) *string { return &s }
  316. func ip(i int) *int { return &i }
  317. func not(ok bool) string {
  318. if ok {
  319. return ""
  320. }
  321. return "not "
  322. }