api_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.StartHTTP(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.StartHTTP(sp("127.0.0.1"), ip(port), nil, nil, nil)
  87. if err == nil {
  88. t.Fatal("StartHTTP 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.StartHTTP(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.StopHTTP()
  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.StopHTTP()
  117. assert.NoError(t, err)
  118. _, err = api.StopHTTP()
  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.StartHTTP(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.StopHTTP()
  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.StartHTTP(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. test := test
  233. t.Run(test.name, func(t *testing.T) {
  234. t.Parallel()
  235. // Apply some sane defaults.
  236. config := test.cfg
  237. // config.Logger = testlog.Logger(t, log.LvlDebug)
  238. config.P2P.NoDiscovery = true
  239. // Create Node.
  240. stack, err := New(&config)
  241. if err != nil {
  242. t.Fatal("can't create node:", err)
  243. }
  244. defer stack.Close()
  245. // Register the test handler.
  246. stack.RegisterHandler("test", "/test", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  247. w.Write([]byte("OK"))
  248. }))
  249. if err := stack.Start(); err != nil {
  250. t.Fatal("can't start node:", err)
  251. }
  252. // Run the API call hook.
  253. if test.fn != nil {
  254. test.fn(t, stack, &privateAdminAPI{stack})
  255. }
  256. // Check if the HTTP endpoints are available.
  257. baseURL := stack.HTTPEndpoint()
  258. reachable := checkReachable(baseURL)
  259. handlersAvailable := checkBodyOK(baseURL + "/test")
  260. rpcAvailable := checkRPC(baseURL)
  261. wsAvailable := checkRPC(strings.Replace(baseURL, "http://", "ws://", 1))
  262. if reachable != test.wantReachable {
  263. t.Errorf("HTTP server is %sreachable, want it %sreachable", not(reachable), not(test.wantReachable))
  264. }
  265. if handlersAvailable != test.wantHandlers {
  266. t.Errorf("RegisterHandler handlers %savailable, want them %savailable", not(handlersAvailable), not(test.wantHandlers))
  267. }
  268. if rpcAvailable != test.wantRPC {
  269. t.Errorf("HTTP RPC %savailable, want it %savailable", not(rpcAvailable), not(test.wantRPC))
  270. }
  271. if wsAvailable != test.wantWS {
  272. t.Errorf("WS RPC %savailable, want it %savailable", not(wsAvailable), not(test.wantWS))
  273. }
  274. })
  275. }
  276. }
  277. // checkReachable checks if the TCP endpoint in rawurl is open.
  278. func checkReachable(rawurl string) bool {
  279. u, err := url.Parse(rawurl)
  280. if err != nil {
  281. panic(err)
  282. }
  283. conn, err := net.Dial("tcp", u.Host)
  284. if err != nil {
  285. return false
  286. }
  287. conn.Close()
  288. return true
  289. }
  290. // checkBodyOK checks whether the given HTTP URL responds with 200 OK and body "OK".
  291. func checkBodyOK(url string) bool {
  292. resp, err := http.Get(url)
  293. if err != nil {
  294. return false
  295. }
  296. defer resp.Body.Close()
  297. if resp.StatusCode != 200 {
  298. return false
  299. }
  300. buf := make([]byte, 2)
  301. if _, err = io.ReadFull(resp.Body, buf); err != nil {
  302. return false
  303. }
  304. return bytes.Equal(buf, []byte("OK"))
  305. }
  306. // checkRPC checks whether JSON-RPC works against the given URL.
  307. func checkRPC(url string) bool {
  308. c, err := rpc.Dial(url)
  309. if err != nil {
  310. return false
  311. }
  312. defer c.Close()
  313. _, err = c.SupportedModules()
  314. return err == nil
  315. }
  316. // string/int pointer helpers.
  317. func sp(s string) *string { return &s }
  318. func ip(i int) *int { return &i }
  319. func not(ok bool) string {
  320. if ok {
  321. return ""
  322. }
  323. return "not "
  324. }