acceptor_SUITE.erl 45 KB

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