acceptor_SUITE.erl 46 KB

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