acceptor_SUITE.erl 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. %% Copyright (c) 2011-2018, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. -module(acceptor_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -dialyzer({nowarn_function, misc_wait_for_connections/1}).
  18. -import(ct_helper, [doc/1]).
  19. -import(ct_helper, [name/0]).
  20. %% ct.
  21. all() ->
  22. [{group, tcp}, {group, ssl}, {group, misc}, {group, supervisor}].
  23. groups() ->
  24. [{tcp, [
  25. tcp_accept_socket,
  26. tcp_active_echo,
  27. tcp_echo,
  28. tcp_graceful,
  29. tcp_inherit_options,
  30. tcp_max_connections,
  31. tcp_max_connections_and_beyond,
  32. tcp_max_connections_infinity,
  33. tcp_remove_connections,
  34. tcp_set_max_connections,
  35. tcp_set_max_connections_clean,
  36. tcp_getopts_capability,
  37. tcp_getstat_capability,
  38. tcp_upgrade,
  39. tcp_error_eaddrinuse,
  40. tcp_error_eacces
  41. ]}, {ssl, [
  42. ssl_accept_error,
  43. ssl_accept_socket,
  44. ssl_active_echo,
  45. ssl_echo,
  46. ssl_graceful,
  47. ssl_sni_echo,
  48. ssl_sni_fail,
  49. ssl_getopts_capability,
  50. ssl_getstat_capability,
  51. ssl_error_eaddrinuse,
  52. ssl_error_no_cert,
  53. ssl_error_eacces
  54. ]}, {misc, [
  55. misc_bad_transport,
  56. misc_bad_transport_options,
  57. misc_info,
  58. misc_info_embedded,
  59. misc_wait_for_connections
  60. ]}, {supervisor, [
  61. connection_type_supervisor,
  62. connection_type_supervisor_separate_from_connection,
  63. supervisor_changed_options_restart,
  64. supervisor_clean_child_restart,
  65. supervisor_clean_conns_sup_restart,
  66. supervisor_clean_restart,
  67. supervisor_conns_alive,
  68. supervisor_protocol_start_link_crash,
  69. supervisor_server_recover_state,
  70. supervisor_unexpected_message
  71. ]}].
  72. %% misc.
  73. misc_bad_transport(_) ->
  74. doc("Reject invalid transport modules."),
  75. {error, badarg} = ranch:start_listener(misc_bad_transport,
  76. bad_transport, [], echo_protocol, []),
  77. ok.
  78. misc_bad_transport_options(_) ->
  79. doc("Ignore invalid transport options."),
  80. {ok, _} = ranch:start_listener(misc_bad_transport,
  81. ranch_tcp, [binary, {packet, 4}, <<"garbage">>, raw, backlog], echo_protocol, []),
  82. ok.
  83. misc_info(_) ->
  84. doc("Information about listeners."),
  85. %% Open a listener with a few connections.
  86. {ok, Pid1} = ranch:start_listener({misc_info, tcp},
  87. ranch_tcp, [{num_acceptors, 1}],
  88. remove_conn_and_wait_protocol, [{remove, true, 2500}]),
  89. Port1 = ranch:get_port({misc_info, tcp}),
  90. %% Open a few more listeners with different arguments.
  91. {ok, Pid2} = ranch:start_listener({misc_info, act},
  92. ranch_tcp, [{num_acceptors, 2}], active_echo_protocol, {}),
  93. Port2 = ranch:get_port({misc_info, act}),
  94. ranch:set_max_connections({misc_info, act}, infinity),
  95. Opts = ct_helper:get_certs_from_ets(),
  96. {ok, Pid3} = ranch:start_listener({misc_info, ssl},
  97. ranch_ssl, [{num_acceptors, 3}|Opts], echo_protocol, [{}]),
  98. Port3 = ranch:get_port({misc_info, ssl}),
  99. %% Open 5 connections, 3 removed from the count.
  100. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  101. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  102. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  103. receive after 250 -> ok end,
  104. ranch:set_protocol_options({misc_info, tcp}, [{remove, false, 2500}]),
  105. receive after 250 -> ok end,
  106. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  107. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  108. receive after 250 -> ok end,
  109. %% Confirm the info returned by Ranch is correct.
  110. [
  111. {{misc_info, act}, [
  112. {pid, Pid2},
  113. {status, _},
  114. {ip, _},
  115. {port, Port2},
  116. {num_acceptors, 2},
  117. {max_connections, infinity}, %% Option was modified.
  118. {active_connections, 0},
  119. {all_connections, 0},
  120. {transport, ranch_tcp},
  121. {transport_options, [{num_acceptors, 2}]},
  122. {protocol, active_echo_protocol},
  123. {protocol_options, {}}
  124. ]},
  125. {{misc_info, ssl}, [
  126. {pid, Pid3},
  127. {status, _},
  128. {ip, _},
  129. {port, Port3},
  130. {num_acceptors, 3},
  131. {max_connections, 1024},
  132. {active_connections, 0},
  133. {all_connections, 0},
  134. {transport, ranch_ssl},
  135. {transport_options, [{num_acceptors, 3}|Opts]},
  136. {protocol, echo_protocol},
  137. {protocol_options, [{}]}
  138. ]},
  139. {{misc_info, tcp}, [
  140. {pid, Pid1},
  141. {status, _},
  142. {ip, _},
  143. {port, Port1},
  144. {num_acceptors, 1},
  145. {max_connections, 1024},
  146. {active_connections, 2},
  147. {all_connections, 5},
  148. {transport, ranch_tcp},
  149. {transport_options, [{num_acceptors, 1}]},
  150. {protocol, remove_conn_and_wait_protocol},
  151. {protocol_options, [{remove, false, 2500}]} %% Option was modified.
  152. ]}
  153. ] = do_get_listener_info(misc_info),
  154. %% Get acceptors.
  155. [_] = ranch:procs({misc_info, tcp}, acceptors),
  156. [_, _] = ranch:procs({misc_info, act}, acceptors),
  157. [_, _, _] = ranch:procs({misc_info, ssl}, acceptors),
  158. %% Get connections.
  159. [_, _, _, _, _] = ranch:procs({misc_info, tcp}, connections),
  160. [] = ranch:procs({misc_info, act}, connections),
  161. [] = ranch:procs({misc_info, ssl}, connections),
  162. ok.
  163. misc_info_embedded(_) ->
  164. doc("Information about listeners in embedded mode."),
  165. {ok, SupPid} = embedded_sup:start_link(),
  166. %% Open a listener with a few connections.
  167. {ok, Pid1} = embedded_sup:start_listener(SupPid, {misc_info_embedded, tcp}, ranch_tcp, [{num_acceptors, 1}], remove_conn_and_wait_protocol, [{remove, true, 2500}]),
  168. Port1 = ranch:get_port({misc_info_embedded, tcp}),
  169. %% Open a few more listeners with different arguments.
  170. {ok, Pid2} = embedded_sup:start_listener(SupPid, {misc_info_embedded, act}, ranch_tcp, [{num_acceptors, 2}], active_echo_protocol, {}),
  171. Port2 = ranch:get_port({misc_info_embedded, act}),
  172. ranch:set_max_connections({misc_info_embedded, act}, infinity),
  173. Opts = ct_helper:get_certs_from_ets(),
  174. {ok, Pid3} = embedded_sup:start_listener(SupPid, {misc_info_embedded, ssl},
  175. ranch_ssl, [{num_acceptors, 3}|Opts], echo_protocol, [{}]),
  176. Port3 = ranch:get_port({misc_info_embedded, ssl}),
  177. %% Open 5 connections, 3 removed from the count.
  178. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  179. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  180. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  181. receive after 250 -> ok end,
  182. ranch:set_protocol_options({misc_info_embedded, tcp}, [{remove, false, 2500}]),
  183. receive after 250 -> ok end,
  184. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  185. {ok, _} = gen_tcp:connect("localhost", Port1, [binary, {active, false}, {packet, raw}]),
  186. receive after 250 -> ok end,
  187. %% Confirm the info returned by Ranch is correct.
  188. [
  189. {{misc_info_embedded, act}, [
  190. {pid, Pid2},
  191. {status, _},
  192. {ip, _},
  193. {port, Port2},
  194. {num_acceptors, 2},
  195. {max_connections, infinity}, %% Option was modified.
  196. {active_connections, 0},
  197. {all_connections, 0},
  198. {transport, ranch_tcp},
  199. {transport_options, [{num_acceptors, 2}]},
  200. {protocol, active_echo_protocol},
  201. {protocol_options, {}}
  202. ]},
  203. {{misc_info_embedded, ssl}, [
  204. {pid, Pid3},
  205. {status, _},
  206. {ip, _},
  207. {port, Port3},
  208. {num_acceptors, 3},
  209. {max_connections, 1024},
  210. {active_connections, 0},
  211. {all_connections, 0},
  212. {transport, ranch_ssl},
  213. {transport_options, [{num_acceptors, 3}|Opts]},
  214. {protocol, echo_protocol},
  215. {protocol_options, [{}]}
  216. ]},
  217. {{misc_info_embedded, tcp}, [
  218. {pid, Pid1},
  219. {status, _},
  220. {ip, _},
  221. {port, Port1},
  222. {num_acceptors, 1},
  223. {max_connections, 1024},
  224. {active_connections, 2},
  225. {all_connections, 5},
  226. {transport, ranch_tcp},
  227. {transport_options, [{num_acceptors, 1}]},
  228. {protocol, remove_conn_and_wait_protocol},
  229. {protocol_options, [{remove, false, 2500}]} %% Option was modified.
  230. ]}
  231. ] = do_get_listener_info(misc_info_embedded),
  232. %% Get acceptors.
  233. [_] = ranch:procs({misc_info_embedded, tcp}, acceptors),
  234. [_, _] = ranch:procs({misc_info_embedded, act}, acceptors),
  235. [_, _, _] = ranch:procs({misc_info_embedded, ssl}, acceptors),
  236. %% Get connections.
  237. [_, _, _, _, _] = ranch:procs({misc_info_embedded, tcp}, connections),
  238. [] = ranch:procs({misc_info_embedded, act}, connections),
  239. [] = ranch:procs({misc_info_embedded, ssl}, connections),
  240. %% Stop embedded tcp listener and ensure it is gone.
  241. ok = embedded_sup:stop_listener(SupPid, {misc_info_embedded, tcp}),
  242. timer:sleep(500),
  243. [{{misc_info_embedded, act}, _}, {{misc_info_embedded, ssl}, _}] =
  244. do_get_listener_info(misc_info_embedded),
  245. %% Stop embedded act listener and ensure it is gone.
  246. ok = embedded_sup:stop_listener(SupPid, {misc_info_embedded, act}),
  247. timer:sleep(500),
  248. [{{misc_info_embedded, ssl}, _}] =
  249. do_get_listener_info(misc_info_embedded),
  250. %% Stop embedded ssl listener and ensure it is gone.
  251. ok = embedded_sup:stop_listener(SupPid, {misc_info_embedded, ssl}),
  252. timer:sleep(500),
  253. [] = do_get_listener_info(misc_info_embedded),
  254. %% Stop embedded supervisor.
  255. embedded_sup:stop(SupPid),
  256. ok.
  257. do_get_listener_info(ListenerGroup) ->
  258. lists:sort([L || L={{G, _}, _} <- ranch:info(), G=:=ListenerGroup]).
  259. misc_wait_for_connections(_) ->
  260. doc("Ensure wait for connections works."),
  261. Name = name(),
  262. Self = self(),
  263. %% Ensure invalid arguments are rejected.
  264. {'EXIT', {badarg, _}} = begin catch ranch:wait_for_connections(Name, 'foo', 0) end,
  265. {'EXIT', {badarg, _}} = begin catch ranch:wait_for_connections(Name, '==', -1) end,
  266. {'EXIT', {badarg, _}} = begin catch ranch:wait_for_connections(Name, '==', 0, -1) end,
  267. {'EXIT', {badarg, _}} = begin catch ranch:wait_for_connections(Name, '<', 0) end,
  268. %% Create waiters for increasing number of connections.
  269. Pid1GT = do_create_waiter(Self, Name, '>', 0),
  270. Pid1GE = do_create_waiter(Self, Name, '>=', 1),
  271. Pid1EQ = do_create_waiter(Self, Name, '==', 1),
  272. Pid2GT = do_create_waiter(Self, Name, '>', 1),
  273. Pid2GE = do_create_waiter(Self, Name, '>=', 2),
  274. Pid2EQ = do_create_waiter(Self, Name, '==', 2),
  275. {ok, _} = ranch:start_listener(Name,
  276. ranch_tcp, [{num_acceptors, 1}],
  277. echo_protocol, []),
  278. Port = ranch:get_port(Name),
  279. %% Create some connections, ensure that waiters respond.
  280. {ok, Sock1} = gen_tcp:connect("localhost", Port, []),
  281. ok = do_expect_waiter(Pid1GT),
  282. ok = do_expect_waiter(Pid1GE),
  283. ok = do_expect_waiter(Pid1EQ),
  284. ok = do_expect_waiter(undefined),
  285. {ok, Sock2} = gen_tcp:connect("localhost", Port, []),
  286. ok = do_expect_waiter(Pid2GT),
  287. ok = do_expect_waiter(Pid2GE),
  288. ok = do_expect_waiter(Pid2EQ),
  289. ok = do_expect_waiter(undefined),
  290. %% Create waiters for decreasing number of connections.
  291. Pid3LT = do_create_waiter(Self, Name, '<', 2),
  292. Pid3LE = do_create_waiter(Self, Name, '=<', 1),
  293. Pid3EQ = do_create_waiter(Self, Name, '==', 1),
  294. Pid4LT = do_create_waiter(Self, Name, '<', 1),
  295. Pid4LE = do_create_waiter(Self, Name, '=<', 0),
  296. Pid4EQ = do_create_waiter(Self, Name, '==', 0),
  297. %% Close connections, ensure that waiters respond.
  298. ok = gen_tcp:close(Sock1),
  299. ok = do_expect_waiter(Pid3LT),
  300. ok = do_expect_waiter(Pid3LE),
  301. ok = do_expect_waiter(Pid3EQ),
  302. ok = do_expect_waiter(undefined),
  303. ok = gen_tcp:close(Sock2),
  304. ok = do_expect_waiter(Pid4LT),
  305. ok = do_expect_waiter(Pid4LE),
  306. ok = do_expect_waiter(Pid4EQ),
  307. ok = do_expect_waiter(undefined),
  308. ok = ranch:stop_listener(Name),
  309. %% Make sure the listener stopped.
  310. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  311. ok.
  312. do_create_waiter(ReplyTo, Ref, Op, NumConns) ->
  313. spawn(fun () -> ok = ranch:wait_for_connections(Ref, Op, NumConns, 100),
  314. ReplyTo ! {wait_connections, self()} end).
  315. do_expect_waiter(WaiterPid) ->
  316. receive
  317. {wait_connections, _} when WaiterPid=:=undefined ->
  318. error;
  319. {wait_connections, Pid} when Pid=:=WaiterPid ->
  320. ok
  321. after 1000 ->
  322. case WaiterPid of
  323. undefined ->
  324. ok;
  325. _ ->
  326. timeout
  327. end
  328. end.
  329. %% ssl.
  330. ssl_accept_error(_) ->
  331. doc("Acceptor must not crash if client disconnects in the middle of SSL handshake."),
  332. Name = name(),
  333. Opts = ct_helper:get_certs_from_ets(),
  334. {ok, ListenerSup} = ranch:start_listener(Name,
  335. ranch_ssl, [{num_acceptors, 1}|Opts], echo_protocol, []),
  336. Port = ranch:get_port(Name),
  337. ListenerSupChildren = supervisor:which_children(ListenerSup),
  338. {_, AcceptorsSup, _, _} = lists:keyfind(ranch_acceptors_sup, 1, ListenerSupChildren),
  339. [{{acceptor, _, _}, AcceptorPid, _, _}] = supervisor:which_children(AcceptorsSup),
  340. true = is_process_alive(AcceptorPid),
  341. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  342. ok = gen_tcp:close(Socket),
  343. receive after 500 -> ok end,
  344. true = is_process_alive(AcceptorPid),
  345. ok = ranch:stop_listener(Name).
  346. ssl_accept_socket(_) ->
  347. doc("Ensure that listener can use an externally opened SSL listen socket."),
  348. Name = name(),
  349. Opts = ct_helper:get_certs_from_ets(),
  350. {ok, S} = ssl:listen(0, [binary, {active, false}, {packet, raw}, {reuseaddr, true}|Opts]),
  351. {ok, _} = ranch:start_listener(Name, ranch_ssl, [{socket, S}], echo_protocol, []),
  352. Port = ranch:get_port(Name),
  353. {ok, Socket} = ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  354. ok = ssl:send(Socket, <<"TCP Ranch is working!">>),
  355. {ok, <<"TCP Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  356. ok = ranch:stop_listener(Name),
  357. {error, closed} = ssl:recv(Socket, 0, 1000),
  358. %% Make sure the listener stopped.
  359. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  360. ok.
  361. ssl_active_echo(_) ->
  362. doc("Ensure that active mode works with SSL transport."),
  363. Name = name(),
  364. Opts = ct_helper:get_certs_from_ets(),
  365. {ok, _} = ranch:start_listener(Name, ranch_ssl, Opts, active_echo_protocol, []),
  366. Port = ranch:get_port(Name),
  367. {ok, Socket} = ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  368. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  369. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  370. ok = ranch:stop_listener(Name),
  371. {error, closed} = ssl:recv(Socket, 0, 1000),
  372. %% Make sure the listener stopped.
  373. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  374. ok.
  375. ssl_echo(_) ->
  376. doc("Ensure that passive mode works with SSL transport."),
  377. Name = name(),
  378. Opts = ct_helper:get_certs_from_ets(),
  379. {ok, _} = ranch:start_listener(Name, ranch_ssl, Opts, echo_protocol, []),
  380. Port = ranch:get_port(Name),
  381. {ok, Socket} = ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  382. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  383. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  384. ok = ranch:stop_listener(Name),
  385. {error, closed} = ssl:recv(Socket, 0, 1000),
  386. %% Make sure the listener stopped.
  387. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  388. ok.
  389. ssl_sni_echo(_) ->
  390. case application:get_key(ssl, vsn) of
  391. {ok, Vsn} when Vsn >= "7.0" ->
  392. do_ssl_sni_echo();
  393. _ ->
  394. {skip, "No SNI support."}
  395. end.
  396. do_ssl_sni_echo() ->
  397. doc("Ensure that SNI works with SSL transport."),
  398. Name = name(),
  399. Opts = ct_helper:get_certs_from_ets(),
  400. {ok, _} = ranch:start_listener(Name, ranch_ssl, [{sni_hosts, [{"localhost", Opts}]}], echo_protocol, []),
  401. Port = ranch:get_port(Name),
  402. {ok, Socket} = ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  403. ok = ssl:send(Socket, <<"SSL Ranch is working!">>),
  404. {ok, <<"SSL Ranch is working!">>} = ssl:recv(Socket, 21, 1000),
  405. ok = ranch:stop_listener(Name),
  406. {error, closed} = ssl:recv(Socket, 0, 1000),
  407. %% Make sure the listener stopped.
  408. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  409. ok.
  410. ssl_sni_fail(_) ->
  411. case application:get_key(ssl, vsn) of
  412. {ok, Vsn} when Vsn >= "7.0" ->
  413. do_ssl_sni_fail();
  414. _ ->
  415. {skip, "No SNI support."}
  416. end.
  417. do_ssl_sni_fail() ->
  418. doc("Ensure that connection fails when host is not in SNI list."),
  419. Name = name(),
  420. Opts = ct_helper:get_certs_from_ets(),
  421. {ok, _} = ranch:start_listener(Name, ranch_ssl, [{sni_hosts, [{"pouet", Opts}]}], echo_protocol, []),
  422. Port = ranch:get_port(Name),
  423. {error, _} = ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  424. ok = ranch:stop_listener(Name),
  425. %% Make sure the listener stopped.
  426. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  427. ok.
  428. ssl_graceful(_) ->
  429. doc("Ensure suspending and resuming of listeners does not kill active connections."),
  430. Name = name(),
  431. Opts = ct_helper:get_certs_from_ets(),
  432. {ok, _} = ranch:start_listener(Name, ranch_ssl, Opts, echo_protocol, []),
  433. Port = ranch:get_port(Name),
  434. %% Make sure connections with a fresh listener work.
  435. running = ranch:get_status(Name),
  436. {ok, Socket1} = ssl:connect("localhost", Port,
  437. [binary, {active, false}, {packet, raw}]),
  438. ok = ssl:send(Socket1, <<"SSL with fresh listener">>),
  439. {ok, <<"SSL with fresh listener">>} = ssl:recv(Socket1, 23, 1000),
  440. %% Make sure transport options cannot be changed on a running listener.
  441. {error, running} = ranch:set_transport_options(Name, [{port, Port}|Opts]),
  442. %% Suspend listener, make sure established connections keep running.
  443. ok = ranch:suspend_listener(Name),
  444. suspended = ranch:get_status(Name),
  445. ok = ssl:send(Socket1, <<"SSL with suspended listener">>),
  446. {ok, <<"SSL with suspended listener">>} = ssl:recv(Socket1, 27, 1000),
  447. %% Make sure new connections are refused on the suspended listener.
  448. {error, econnrefused} = ssl:connect("localhost", Port,
  449. [binary, {active, false}, {packet, raw}]),
  450. %% Make sure transport options can be changed when listener is suspended.
  451. ok = ranch:set_transport_options(Name, [{port, Port}|Opts]),
  452. %% Resume listener, make sure connections can be established again.
  453. ok = ranch:resume_listener(Name),
  454. running = ranch:get_status(Name),
  455. {ok, Socket2} = ssl:connect("localhost", Port,
  456. [binary, {active, false}, {packet, raw}]),
  457. ok = ssl:send(Socket2, <<"SSL with resumed listener">>),
  458. {ok, <<"SSL with resumed listener">>} = ssl:recv(Socket2, 25, 1000),
  459. %% Make sure transport options cannot be changed on resumed listener.
  460. {error, running} = ranch:set_transport_options(Name, [{port, Port}|Opts]),
  461. ok = ranch:stop_listener(Name),
  462. {error, closed} = ssl:recv(Socket1, 0, 1000),
  463. {error, closed} = ssl:recv(Socket2, 0, 1000),
  464. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  465. ok.
  466. ssl_getopts_capability(_) ->
  467. doc("Ensure getopts/2 capability."),
  468. Name=name(),
  469. Opts=ct_helper:get_certs_from_ets(),
  470. {ok, _}=ranch:start_listener(Name, ranch_ssl, Opts, transport_capabilities_protocol, []),
  471. Port=ranch:get_port(Name),
  472. {ok, Socket}=ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  473. ok=ssl:send(Socket, <<"getopts/2">>),
  474. {ok, <<"OK">>}=ssl:recv(Socket, 0, 1000),
  475. ok=ranch:stop_listener(Name),
  476. {error, closed}=ssl:recv(Socket, 0, 1000),
  477. {'EXIT', _}=begin catch ranch:get_port(Name) end,
  478. ok.
  479. ssl_getstat_capability(_) ->
  480. case application:get_key(ssl, vsn) of
  481. {ok, Vsn} when Vsn>="8.0" ->
  482. do_ssl_getstat_capability();
  483. _ ->
  484. {skip, "No getstat/{1,2} support."}
  485. end.
  486. do_ssl_getstat_capability() ->
  487. doc("Ensure getstat/{1,2} capability."),
  488. Name=name(),
  489. Opts=ct_helper:get_certs_from_ets(),
  490. {ok, _}=ranch:start_listener(Name, ranch_ssl, Opts, transport_capabilities_protocol, []),
  491. Port=ranch:get_port(Name),
  492. {ok, Socket}=ssl:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  493. ok=ssl:send(Socket, <<"getstat/1">>),
  494. {ok, <<"OK">>}=ssl:recv(Socket, 0, 1000),
  495. ok=ssl:send(Socket, <<"getstat/2">>),
  496. {ok, <<"OK">>}=ssl:recv(Socket, 0, 1000),
  497. ok=ranch:stop_listener(Name),
  498. {error, closed}=ssl:recv(Socket, 0, 1000),
  499. {'EXIT', _}=begin catch ranch:get_port(Name) end,
  500. ok.
  501. ssl_error_eaddrinuse(_) ->
  502. doc("Ensure that failure due to an eaddrinuse returns a compact readable error."),
  503. Name = name(),
  504. Opts = ct_helper:get_certs_from_ets(),
  505. {ok, _} = ranch:start_listener(Name, ranch_ssl, Opts, active_echo_protocol, []),
  506. Port = ranch:get_port(Name),
  507. {error, eaddrinuse} = ranch:start_listener({Name, fails},
  508. ranch_ssl, [{port, Port}|Opts], active_echo_protocol, []),
  509. ok = ranch:stop_listener(Name),
  510. %% Make sure the listener stopped.
  511. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  512. ok.
  513. ssl_error_no_cert(_) ->
  514. doc("Ensure that failure due to missing certificate returns a compact readable error."),
  515. {error, no_cert} = ranch:start_listener(name(), ranch_ssl, [], active_echo_protocol, []),
  516. ok.
  517. ssl_error_eacces(_) ->
  518. case os:type() of
  519. {win32, nt} ->
  520. doc("There are no privileged ports on Windows.");
  521. _ ->
  522. doc("Ensure that failure due to an eacces returns a compact readable error."),
  523. Name = name(),
  524. Opts = ct_helper:get_certs_from_ets(),
  525. {error, eacces} = ranch:start_listener(Name,
  526. ranch_ssl, [{port, 283}|Opts], active_echo_protocol, []),
  527. ok
  528. end.
  529. %% tcp.
  530. tcp_accept_socket(_) ->
  531. doc("Ensure that listener can use an externally opened TCP listen socket."),
  532. Name = name(),
  533. {ok, S} = gen_tcp:listen(0, [binary, {active, false}, {packet, raw}, {reuseaddr, true}]),
  534. {ok, _} = ranch:start_listener(Name, ranch_tcp, [{socket, S}], echo_protocol, []),
  535. Port = ranch:get_port(Name),
  536. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  537. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  538. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  539. ok = ranch:stop_listener(Name),
  540. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  541. %% Make sure the listener stopped.
  542. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  543. ok.
  544. tcp_active_echo(_) ->
  545. doc("Ensure that active mode works with TCP transport."),
  546. Name = name(),
  547. {ok, _} = ranch:start_listener(Name, ranch_tcp, [], active_echo_protocol, []),
  548. Port = ranch:get_port(Name),
  549. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  550. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  551. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  552. ok = ranch:stop_listener(Name),
  553. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  554. %% Make sure the listener stopped.
  555. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  556. ok.
  557. tcp_echo(_) ->
  558. doc("Ensure that passive mode works with TCP transport."),
  559. Name = name(),
  560. {ok, _} = ranch:start_listener(Name, ranch_tcp, [], echo_protocol, []),
  561. Port = ranch:get_port(Name),
  562. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  563. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  564. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  565. ok = ranch:stop_listener(Name),
  566. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  567. %% Make sure the listener stopped.
  568. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  569. ok.
  570. tcp_graceful(_) ->
  571. doc("Ensure suspending and resuming of listeners does not kill active connections."),
  572. Name = name(),
  573. {ok, _} = ranch:start_listener(Name, ranch_tcp, [], echo_protocol, []),
  574. Port = ranch:get_port(Name),
  575. %% Make sure connections with a fresh listener work.
  576. running = ranch:get_status(Name),
  577. {ok, Socket1} = gen_tcp:connect("localhost", Port,
  578. [binary, {active, false}, {packet, raw}]),
  579. ok = gen_tcp:send(Socket1, <<"TCP with fresh listener">>),
  580. {ok, <<"TCP with fresh listener">>} = gen_tcp:recv(Socket1, 23, 1000),
  581. %% Make sure transport options cannot be changed on a running listener.
  582. {error, running} = ranch:set_transport_options(Name, [{port, Port}]),
  583. %% Suspend listener, make sure established connections keep running.
  584. ok = ranch:suspend_listener(Name),
  585. suspended = ranch:get_status(Name),
  586. ok = gen_tcp:send(Socket1, <<"TCP with suspended listener">>),
  587. {ok, <<"TCP with suspended listener">>} = gen_tcp:recv(Socket1, 27, 1000),
  588. %% Make sure new connections are refused on the suspended listener.
  589. {error, econnrefused} = gen_tcp:connect("localhost", Port,
  590. [binary, {active, false}, {packet, raw}]),
  591. %% Make sure transport options can be changed when listener is suspended.
  592. ok = ranch:set_transport_options(Name, [{port, Port}]),
  593. %% Resume listener, make sure connections can be established again.
  594. ok = ranch:resume_listener(Name),
  595. running = ranch:get_status(Name),
  596. {ok, Socket2} = gen_tcp:connect("localhost", Port,
  597. [binary, {active, false}, {packet, raw}]),
  598. ok = gen_tcp:send(Socket2, <<"TCP with resumed listener">>),
  599. {ok, <<"TCP with resumed listener">>} = gen_tcp:recv(Socket2, 25, 1000),
  600. %% Make sure transport options cannot be changed on resumed listener.
  601. {error, running} = ranch:set_transport_options(Name, [{port, Port}]),
  602. ok = ranch:stop_listener(Name),
  603. {error, closed} = gen_tcp:recv(Socket1, 0, 1000),
  604. {error, closed} = gen_tcp:recv(Socket2, 0, 1000),
  605. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  606. ok.
  607. tcp_inherit_options(_) ->
  608. doc("Ensure TCP options are inherited in the protocol."),
  609. Name = name(),
  610. Opts = [{nodelay, false}, {send_timeout_close, false}],
  611. {ok, _} = ranch:start_listener(Name, ranch_tcp, Opts, check_tcp_options, [{pid, self()} | Opts]),
  612. Port = ranch:get_port(Name),
  613. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, true}, {packet, raw}]),
  614. receive checked -> ok after 1000 -> error(timeout) end,
  615. ok = gen_tcp:close(Socket),
  616. ok = ranch:stop_listener(Name).
  617. tcp_max_connections(_) ->
  618. doc("Ensure the max_connections option actually limits connections."),
  619. Name = name(),
  620. {ok, _} = ranch:start_listener(Name,
  621. ranch_tcp, [{max_connections, 10}, {num_acceptors, 1}],
  622. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  623. Port = ranch:get_port(Name),
  624. ok = connect_loop(Port, 11, 150),
  625. 10 = ranch_server:count_connections(Name),
  626. 10 = receive_loop(connected, 400),
  627. 1 = receive_loop(connected, 1000),
  628. ok = ranch:stop_listener(Name).
  629. tcp_max_connections_and_beyond(_) ->
  630. doc("Ensure the max_connections option works when connections are removed from the count."),
  631. Name = name(),
  632. {ok, _} = ranch:start_listener(Name,
  633. ranch_tcp, [{max_connections, 10}, {num_acceptors, 1}],
  634. remove_conn_and_wait_protocol, [{remove, true, 2500}]),
  635. Port = ranch:get_port(Name),
  636. ok = connect_loop(Port, 10, 0),
  637. receive after 250 -> ok end,
  638. 0 = ranch_server:count_connections(Name),
  639. 10 = length(supervisor:which_children(ranch_server:get_connections_sup(Name))),
  640. Counts = supervisor:count_children(ranch_server:get_connections_sup(Name)),
  641. {_, 1} = lists:keyfind(specs, 1, Counts),
  642. {_, 0} = lists:keyfind(supervisors, 1, Counts),
  643. {_, 10} = lists:keyfind(active, 1, Counts),
  644. {_, 10} = lists:keyfind(workers, 1, Counts),
  645. ranch:set_protocol_options(Name, [{remove, false, 2500}]),
  646. receive after 250 -> ok end,
  647. ok = connect_loop(Port, 10, 0),
  648. receive after 250 -> ok end,
  649. 10 = ranch_server:count_connections(Name),
  650. 20 = length(supervisor:which_children(ranch_server:get_connections_sup(Name))),
  651. Counts2 = supervisor:count_children(ranch_server:get_connections_sup(Name)),
  652. {_, 20} = lists:keyfind(active, 1, Counts2),
  653. {_, 20} = lists:keyfind(workers, 1, Counts2),
  654. ok = ranch:stop_listener(Name).
  655. tcp_max_connections_infinity(_) ->
  656. doc("Set the max_connections option from 10 to infinity and back to 10."),
  657. Name = name(),
  658. {ok, _} = ranch:start_listener(Name,
  659. ranch_tcp, [{max_connections, 10}, {num_acceptors, 1}],
  660. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  661. Port = ranch:get_port(Name),
  662. ok = connect_loop(Port, 20, 0),
  663. 10 = ranch_server:count_connections(Name),
  664. 10 = receive_loop(connected, 1000),
  665. 10 = ranch_server:count_connections(Name),
  666. 10 = ranch:get_max_connections(Name),
  667. ranch:set_max_connections(Name, infinity),
  668. receive after 250 -> ok end,
  669. 20 = ranch_server:count_connections(Name),
  670. infinity = ranch:get_max_connections(Name),
  671. ranch:set_max_connections(Name, 10),
  672. 20 = ranch_server:count_connections(Name),
  673. 10 = receive_loop(connected, 1000),
  674. ok = ranch:stop_listener(Name).
  675. tcp_remove_connections(_) ->
  676. doc("Ensure that removed connections are only removed once."),
  677. Name = name(),
  678. {ok, _} = ranch:start_listener(Name,
  679. ranch_tcp, [],
  680. remove_conn_and_wait_protocol, [{remove, true, 0}]),
  681. Port = ranch:get_port(Name),
  682. ok = connect_loop(Port, 10, 0),
  683. receive after 250 -> ok end,
  684. 0 = ranch_server:count_connections(Name),
  685. ok = ranch:stop_listener(Name).
  686. tcp_set_max_connections(_) ->
  687. doc("Ensure that changing the max_connections option to a larger value allows for more connections."),
  688. Name = name(),
  689. {ok, _} = ranch:start_listener(Name,
  690. ranch_tcp, [{max_connections, 10}, {num_acceptors, 1}],
  691. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  692. Port = ranch:get_port(Name),
  693. ok = connect_loop(Port, 20, 0),
  694. 10 = ranch_server:count_connections(Name),
  695. 10 = receive_loop(connected, 1000),
  696. 10 = ranch:get_max_connections(Name),
  697. ranch:set_max_connections(Name, 20),
  698. 10 = receive_loop(connected, 1000),
  699. 20 = ranch:get_max_connections(Name),
  700. ok = ranch:stop_listener(Name).
  701. tcp_set_max_connections_clean(Config) ->
  702. case code:is_module_native(?MODULE) of
  703. true -> doc("This test uses tracing and is not compatible with native code.");
  704. false -> do_tcp_set_max_connections_clean(Config)
  705. end.
  706. do_tcp_set_max_connections_clean(_) ->
  707. doc("Ensure that setting max_connections does not crash any process."),
  708. Name = name(),
  709. {ok, ListSupPid} = ranch:start_listener(Name, ranch_tcp,
  710. [{max_connections, 4}],
  711. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  712. Children = supervisor:which_children(ListSupPid),
  713. {_, AccSupPid, _, _} = lists:keyfind(ranch_acceptors_sup, 1, Children),
  714. 1 = erlang:trace(ListSupPid, true, [procs]),
  715. 1 = erlang:trace(AccSupPid, true, [procs]),
  716. Port = ranch:get_port(Name),
  717. N = 20,
  718. ok = connect_loop(Port, N*5, 0),
  719. %% Randomly set max_connections.
  720. [spawn(ranch, set_max_connections, [Name, Max]) ||
  721. Max <- lists:flatten(lists:duplicate(N, [6, 4, 8, infinity]))],
  722. receive
  723. {trace, _, spawn, _, _} ->
  724. error(dirty_set_max_connections)
  725. after
  726. 2000 -> ok
  727. end,
  728. _ = erlang:trace(all, false, [all]),
  729. ok = clean_traces(),
  730. ok = ranch:stop_listener(Name).
  731. tcp_getopts_capability(_) ->
  732. doc("Ensure getopts/2 capability."),
  733. Name=name(),
  734. {ok, _}=ranch:start_listener(Name, ranch_tcp, [], transport_capabilities_protocol, []),
  735. Port=ranch:get_port(Name),
  736. {ok, Socket}=gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  737. ok=gen_tcp:send(Socket, <<"getopts/2">>),
  738. {ok, <<"OK">>}=gen_tcp:recv(Socket, 0, 1000),
  739. ok=ranch:stop_listener(Name),
  740. {error, closed}=gen_tcp:recv(Socket, 0, 1000),
  741. {'EXIT', _}=begin catch ranch:get_port(Name) end,
  742. ok.
  743. tcp_getstat_capability(_) ->
  744. doc("Ensure getstat/{1,2} capability."),
  745. Name=name(),
  746. {ok, _}=ranch:start_listener(Name, ranch_tcp, [], transport_capabilities_protocol, []),
  747. Port=ranch:get_port(Name),
  748. {ok, Socket}=gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  749. ok=gen_tcp:send(Socket, <<"getstat/1">>),
  750. {ok, <<"OK">>}=gen_tcp:recv(Socket, 0, 1000),
  751. ok=gen_tcp:send(Socket, <<"getstat/2">>),
  752. {ok, <<"OK">>}=gen_tcp:recv(Socket, 0, 1000),
  753. ok=ranch:stop_listener(Name),
  754. {error, closed}=gen_tcp:recv(Socket, 0, 1000),
  755. {'EXIT', _}=begin catch ranch:get_port(Name) end,
  756. ok.
  757. tcp_upgrade(_) ->
  758. doc("Ensure that protocol options can be updated."),
  759. Name = name(),
  760. {ok, _} = ranch:start_listener(Name,
  761. ranch_tcp, [],
  762. notify_and_wait_protocol, [{msg, connected}, {pid, self()}]),
  763. Port = ranch:get_port(Name),
  764. ok = connect_loop(Port, 1, 0),
  765. receive connected -> ok after 1000 -> error(timeout) end,
  766. ranch:set_protocol_options(Name, [{msg, upgraded}, {pid, self()}]),
  767. ok = connect_loop(Port, 1, 0),
  768. receive upgraded -> ok after 1000 -> error(timeout) end,
  769. ok = ranch:stop_listener(Name).
  770. tcp_error_eaddrinuse(_) ->
  771. doc("Ensure that failure due to an eaddrinuse returns a compact readable error."),
  772. Name = name(),
  773. {ok, _} = ranch:start_listener(Name, ranch_tcp, [], active_echo_protocol, []),
  774. Port = ranch:get_port(Name),
  775. {error, eaddrinuse} = ranch:start_listener({Name, fails},
  776. ranch_tcp, [{port, Port}], active_echo_protocol, []),
  777. ok = ranch:stop_listener(Name),
  778. %% Make sure the listener stopped.
  779. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  780. ok.
  781. tcp_error_eacces(_) ->
  782. case os:type() of
  783. {win32, nt} ->
  784. doc("There are no privileged ports on Windows.");
  785. _ ->
  786. doc("Ensure that failure due to an eacces returns a compact readable error."),
  787. Name = name(),
  788. {error, eacces} = ranch:start_listener(Name,
  789. ranch_tcp, [{port, 283}], active_echo_protocol, []),
  790. ok
  791. end.
  792. %% Supervisor tests
  793. connection_type_supervisor(_) ->
  794. doc("The supervisor connection type must be reflected in the specifications."),
  795. Name = name(),
  796. {ok, _} = ranch:start_listener(Name,
  797. ranch_tcp, [{connection_type, supervisor}],
  798. echo_protocol, []),
  799. Port = ranch:get_port(Name),
  800. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  801. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  802. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  803. ConnsSup = ranch_server:get_connections_sup(Name),
  804. [{echo_protocol, _, supervisor, [echo_protocol]}] = supervisor:which_children(ConnsSup),
  805. ok = ranch:stop_listener(Name),
  806. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  807. %% Make sure the listener stopped.
  808. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  809. ok.
  810. connection_type_supervisor_separate_from_connection(_) ->
  811. doc("The supervisor connection type allows separate supervised and connection processes."),
  812. Name = name(),
  813. {ok, _} = ranch:start_listener(Name,
  814. ranch_tcp, [{connection_type, supervisor}],
  815. supervisor_separate, []),
  816. Port = ranch:get_port(Name),
  817. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  818. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  819. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  820. ConnsSup = ranch_server:get_connections_sup(Name),
  821. [{supervisor_separate, _, supervisor, [supervisor_separate]}] = supervisor:which_children(ConnsSup),
  822. ok = ranch:stop_listener(Name),
  823. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  824. %% Make sure the listener stopped.
  825. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  826. ok.
  827. supervisor_changed_options_restart(_) ->
  828. doc("Ensure that a listener is restarted with changed transport options."),
  829. Name = name(),
  830. %% Start a listener using send_timeout as option change marker.
  831. {ok, ListenerSupPid1} = ranch:start_listener(Name,
  832. ranch_tcp, [{send_timeout, 300000}],
  833. echo_protocol, []),
  834. %% Ensure send_timeout is really set to initial value.
  835. {ok, [{send_timeout, 300000}]}
  836. = inet:getopts(do_get_listener_socket(ListenerSupPid1), [send_timeout]),
  837. %% Change send_timeout option.
  838. ok = ranch:suspend_listener(Name),
  839. ok = ranch:set_transport_options(Name, [{send_timeout, 300001}]),
  840. ok = ranch:resume_listener(Name),
  841. %% Ensure send_timeout is really set to the changed value.
  842. {ok, [{send_timeout, 300001}]}
  843. = inet:getopts(do_get_listener_socket(ListenerSupPid1), [send_timeout]),
  844. %% Crash the listener_sup process, allow a short time for restart to succeed.
  845. exit(ListenerSupPid1, kill),
  846. timer:sleep(1000),
  847. %% Obtain pid of restarted listener_sup process.
  848. [ListenerSupPid2] = [Pid || {{ranch_listener_sup, Ref}, Pid, supervisor, _}
  849. <- supervisor:which_children(ranch_sup), Ref =:= Name],
  850. %% Ensure send_timeout is still set to the changed value.
  851. {ok, [{send_timeout, 300001}]}
  852. = inet:getopts(do_get_listener_socket(ListenerSupPid2), [send_timeout]),
  853. ok = ranch:stop_listener(Name),
  854. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  855. ok.
  856. supervisor_clean_child_restart(Config) ->
  857. case code:is_module_native(?MODULE) of
  858. true -> doc("This test uses tracing and is not compatible with native code.");
  859. false -> do_supervisor_clean_child_restart(Config)
  860. end.
  861. do_supervisor_clean_child_restart(_) ->
  862. doc("Verify that only the relevant parts of the supervision tree restarted "
  863. "when the listening socket is closed."),
  864. Name = name(),
  865. %% Trace socket allocations.
  866. _ = erlang:trace(new, true, [call]),
  867. 1 = erlang:trace_pattern({ranch_tcp, listen, 1},
  868. [{'_', [], [{return_trace}]}], [global]),
  869. {ok, Pid} = ranch:start_listener(Name,
  870. ranch_tcp, [{num_acceptors, 1}], echo_protocol, []),
  871. %% Trace supervisor spawns.
  872. 1 = erlang:trace(Pid, true, [procs, set_on_spawn]),
  873. ConnsSup = ranch_server:get_connections_sup(Name),
  874. %% Manually shut the listening socket down.
  875. LSocket = receive
  876. {trace, _, return_from, {ranch_tcp, listen, 1}, {ok, Socket}} ->
  877. Socket
  878. after 0 ->
  879. error(lsocket_unknown)
  880. end,
  881. ok = gen_tcp:close(LSocket),
  882. receive after 1000 -> ok end,
  883. %% Verify that supervisor and its first two children are alive.
  884. true = is_process_alive(Pid),
  885. true = is_process_alive(ConnsSup),
  886. %% Check that acceptors_sup is restarted properly.
  887. AccSupPid = receive {trace, Pid, spawn, Pid1, _} -> Pid1 end,
  888. receive {trace, AccSupPid, spawn, _, _} -> ok end,
  889. %% No more traces then.
  890. receive
  891. {trace, _, spawn, _, _} -> error(invalid_restart)
  892. after 1000 -> ok end,
  893. %% Verify that children still registered right.
  894. ConnsSup = ranch_server:get_connections_sup(Name),
  895. _ = erlang:trace_pattern({ranch_tcp, listen, 1}, false, []),
  896. _ = erlang:trace(all, false, [all]),
  897. ok = clean_traces(),
  898. ok = ranch:stop_listener(Name).
  899. supervisor_clean_conns_sup_restart(_) ->
  900. doc("Verify that a conns_sup can not register with the same name as an already "
  901. "registered ranch_conns_sup that is still alive. Make sure this does not crash "
  902. "the ranch_server process."),
  903. Name = name(),
  904. {ok, _} = ranch:start_listener(Name,
  905. ranch_tcp, [], echo_protocol, []),
  906. Server = erlang:whereis(ranch_server),
  907. ServerMonRef = erlang:monitor(process, Server),
  908. %% Exit because Name already registered and is alive.
  909. {'EXIT', _} = (catch ranch_server:set_connections_sup(Name, self())),
  910. receive
  911. {'DOWN', ServerMonRef, process, Server, _} ->
  912. error(ranch_server_down)
  913. after
  914. 1000 ->
  915. ok
  916. end,
  917. ok = ranch:stop_listener(Name).
  918. supervisor_clean_restart(Config) ->
  919. case code:is_module_native(?MODULE) of
  920. true -> doc("This test uses tracing and is not compatible with native code.");
  921. false -> do_supervisor_clean_restart(Config)
  922. end.
  923. do_supervisor_clean_restart(_) ->
  924. doc("Verify that killing ranch_conns_sup does not crash everything "
  925. "and that it restarts properly."),
  926. Name = name(),
  927. NumAcc = 4,
  928. {ok, Pid} = ranch:start_listener(Name,
  929. ranch_tcp, [{num_acceptors, NumAcc}], echo_protocol, []),
  930. %% Trace supervisor spawns.
  931. 1 = erlang:trace(Pid, true, [procs, set_on_spawn]),
  932. ConnsSup0 = ranch_server:get_connections_sup(Name),
  933. erlang:exit(ConnsSup0, kill),
  934. receive after 1000 -> ok end,
  935. %% Verify that supervisor is alive
  936. true = is_process_alive(Pid),
  937. %% ...but children are dead.
  938. false = is_process_alive(ConnsSup0),
  939. %% Receive traces from newly started children
  940. ConnsSup = receive {trace, Pid, spawn, Pid2, _} -> Pid2 end,
  941. AccSupPid = receive {trace, Pid, spawn, Pid3, _} -> Pid3 end,
  942. %% ...and its acceptors.
  943. [receive {trace, AccSupPid, spawn, _Pid, _} -> ok end ||
  944. _ <- lists:seq(1, NumAcc)],
  945. %% No more traces then.
  946. receive
  947. {trace, EPid, spawn, _, _} when EPid == Pid; EPid == AccSupPid ->
  948. error(invalid_restart)
  949. after 1000 -> ok end,
  950. %% Verify that new children registered themselves properly.
  951. ConnsSup = ranch_server:get_connections_sup(Name),
  952. _ = erlang:trace(all, false, [all]),
  953. ok = clean_traces(),
  954. ok = ranch:stop_listener(Name).
  955. supervisor_conns_alive(Config) ->
  956. case code:is_module_native(?MODULE) of
  957. true -> doc("This test uses tracing and is not compatible with native code.");
  958. false -> do_supervisor_conns_alive(Config)
  959. end.
  960. do_supervisor_conns_alive(_) ->
  961. doc("Ensure that active connections stay open when the listening socket gets closed."),
  962. Name = name(),
  963. _ = erlang:trace(new, true, [call]),
  964. 1 = erlang:trace_pattern({ranch_tcp, listen, 1},
  965. [{'_', [], [{return_trace}]}], [global]),
  966. {ok, _} = ranch:start_listener(Name,
  967. ranch_tcp, [],
  968. remove_conn_and_wait_protocol, [{remove, false, 2500}]),
  969. %% Get the listener socket
  970. LSocket = receive
  971. {trace, _, return_from, {ranch_tcp, listen, 1}, {ok, S}} ->
  972. S
  973. after 500 ->
  974. error(lsocket_unknown)
  975. end,
  976. TcpPort = ranch:get_port(Name),
  977. {ok, Socket} = gen_tcp:connect("localhost", TcpPort,
  978. [binary, {active, true}, {packet, raw}]),
  979. receive after 500 -> ok end,
  980. %% Shut the socket down
  981. ok = gen_tcp:close(LSocket),
  982. %% Assert that client is still viable.
  983. receive {tcp_closed, _} -> error(closed) after 1500 -> ok end,
  984. ok = gen_tcp:send(Socket, <<"poke">>),
  985. receive {tcp_closed, _} -> ok end,
  986. _ = erlang:trace(all, false, [all]),
  987. ok = clean_traces(),
  988. ok = ranch:stop_listener(Name).
  989. supervisor_protocol_start_link_crash(_) ->
  990. doc("Ensure a protocol start crash does not kill all connections."),
  991. Name = name(),
  992. {ok, _} = ranch:start_listener(Name, ranch_tcp, [], crash_protocol, []),
  993. ConnsSup = ranch_server:get_connections_sup(Name),
  994. Port = ranch:get_port(Name),
  995. {ok, _} = gen_tcp:connect("localhost", Port, [binary, {active, true}, {packet, raw}]),
  996. receive after 500 -> ok end,
  997. ConnsSup = ranch_server:get_connections_sup(Name),
  998. ok = ranch:stop_listener(Name).
  999. supervisor_server_recover_state(Config) ->
  1000. case code:is_module_native(?MODULE) of
  1001. true -> doc("This test uses tracing and is not compatible with native code.");
  1002. false -> do_supervisor_server_recover_state(Config)
  1003. end.
  1004. do_supervisor_server_recover_state(_) ->
  1005. doc("Ensure that when ranch_server crashes and restarts, it recovers "
  1006. "its state and continues monitoring the same processes."),
  1007. Name = name(),
  1008. _ = erlang:trace(new, true, [call]),
  1009. 1 = erlang:trace_pattern({ranch_server, init, 1},
  1010. [{'_', [], [{return_trace}]}], [global]),
  1011. {ok, _} = ranch:start_listener(Name, ranch_tcp, [], echo_protocol, []),
  1012. ConnsSup = ranch_server:get_connections_sup(Name),
  1013. ServerPid = erlang:whereis(ranch_server),
  1014. {monitors, Monitors} = erlang:process_info(ServerPid, monitors),
  1015. erlang:exit(ServerPid, kill),
  1016. receive
  1017. {trace, ServerPid2, return_from, {ranch_server, init, 1}, _Result} ->
  1018. {monitors, Monitors2} = erlang:process_info(ServerPid2, monitors),
  1019. %% Check that ranch_server is monitoring the same processes.
  1020. true = (lists:usort(Monitors) == lists:usort(Monitors2))
  1021. after
  1022. 1000 ->
  1023. error(timeout)
  1024. end,
  1025. ConnsSup = ranch_server:get_connections_sup(Name),
  1026. ok = ranch:stop_listener(Name),
  1027. %% Check ranch_server has removed the ranch_conns_sup.
  1028. {'EXIT', {badarg, _}} = (catch ranch_server:get_connections_sup(Name)),
  1029. _ = erlang:trace(all, false, [all]),
  1030. ok = clean_traces().
  1031. supervisor_unexpected_message(_) ->
  1032. doc("Ensure the connections supervisor stays alive when it receives "
  1033. "an unexpected message."),
  1034. Name = name(),
  1035. {ok, ListenerPid} = ranch:start_listener(Name, ranch_tcp, [], echo_protocol, []),
  1036. Port = ranch:get_port(Name),
  1037. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}, {packet, raw}]),
  1038. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  1039. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  1040. %% Send the unexpected message to ranch_conns_sup.
  1041. Procs = supervisor:which_children(ListenerPid),
  1042. {_, ConnsSup, _, _} = lists:keyfind(ranch_conns_sup, 1, Procs),
  1043. ConnsSup ! hello,
  1044. %% Connection is still up.
  1045. ok = gen_tcp:send(Socket, <<"TCP Ranch is working!">>),
  1046. {ok, <<"TCP Ranch is working!">>} = gen_tcp:recv(Socket, 21, 1000),
  1047. ok = ranch:stop_listener(Name),
  1048. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  1049. %% Make sure the listener stopped.
  1050. {'EXIT', _} = begin catch ranch:get_port(Name) end,
  1051. ok.
  1052. %% Utility functions.
  1053. connect_loop(_, 0, _) ->
  1054. ok;
  1055. connect_loop(Port, N, Sleep) ->
  1056. {ok, _} = gen_tcp:connect("localhost", Port,
  1057. [binary, {active, false}, {packet, raw}]),
  1058. receive after Sleep -> ok end,
  1059. connect_loop(Port, N - 1, Sleep).
  1060. receive_loop(Message, Timeout) ->
  1061. receive_loop(Message, Timeout, 0).
  1062. receive_loop(Message, Timeout, N) ->
  1063. receive Message ->
  1064. receive_loop(Message, Timeout, N + 1)
  1065. after Timeout ->
  1066. N
  1067. end.
  1068. clean_traces() ->
  1069. receive
  1070. {trace, _, _, _} ->
  1071. clean_traces();
  1072. {trace, _, _, _, _} ->
  1073. clean_traces()
  1074. after 0 ->
  1075. ok
  1076. end.
  1077. do_get_listener_socket(ListenerSupPid) ->
  1078. [AcceptorsSupPid] = [Pid || {ranch_acceptors_sup, Pid, supervisor, _}
  1079. <- supervisor:which_children(ListenerSupPid)],
  1080. {links, Links} = erlang:process_info(AcceptorsSupPid, links),
  1081. [LSocket] = [P || P <- Links, is_port(P)],
  1082. LSocket.