acceptor_SUITE.erl 46 KB

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