sys_SUITE.erl 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  1. %% Copyright (c) 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(sys_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. all() ->
  21. [{group, sys}].
  22. groups() ->
  23. [{sys, [parallel], ct_helper:all(?MODULE)}].
  24. init_per_suite(Config) ->
  25. ct:print("This test suite will produce error reports about "
  26. "EXIT signals for unknown processes."),
  27. ProtoOpts = #{
  28. env => #{dispatch => init_dispatch(Config)}
  29. },
  30. %% Clear listener.
  31. {ok, _} = cowboy:start_clear(clear, [{port, 0}], ProtoOpts),
  32. ClearPort = ranch:get_port(clear),
  33. %% TLS listener.
  34. TLSOpts = ct_helper:get_certs_from_ets(),
  35. {ok, _} = cowboy:start_tls(tls, TLSOpts ++ [{port, 0}], ProtoOpts),
  36. TLSPort = ranch:get_port(tls),
  37. [
  38. {clear_port, ClearPort},
  39. %% @todo Add the h2 stuff to the opts.
  40. {tls_opts, TLSOpts},
  41. {tls_port, TLSPort}
  42. |Config].
  43. end_per_suite(_) ->
  44. ok = cowboy:stop_listener(clear),
  45. ok = cowboy:stop_listener(tls).
  46. init_dispatch(_) ->
  47. cowboy_router:compile([{"[...]", [
  48. {"/", hello_h, []},
  49. {"/loop", long_polling_sys_h, []},
  50. {"/ws", ws_echo, []}
  51. ]}]).
  52. do_get_remote_pid_tcp(Socket) when is_port(Socket) ->
  53. do_get_remote_pid_tcp(inet:sockname(Socket));
  54. do_get_remote_pid_tcp(SockName) ->
  55. AllPorts = [{P, erlang:port_info(P)} || P <- erlang:ports()],
  56. [Pid] = [
  57. proplists:get_value(connected, I)
  58. || {P, I} <- AllPorts,
  59. I =/= undefined,
  60. proplists:get_value(name, I) =:= "tcp_inet",
  61. inet:peername(P) =:= SockName],
  62. Pid.
  63. -include_lib("ssl/src/ssl_connection.hrl").
  64. do_get_remote_pid_tls(Socket) ->
  65. %% This gives us the pid of the sslsocket process.
  66. %% We must introspect this process in order to retrieve the connection pid.
  67. TLSPid = do_get_remote_pid_tcp(ssl:sockname(Socket)),
  68. {_, #state{user_application={_, UserPid}}} = sys:get_state(TLSPid),
  69. UserPid.
  70. do_get_parent_pid(Pid) ->
  71. {_, ProcDict} = process_info(Pid, dictionary),
  72. {_, [Parent|_]} = lists:keyfind('$ancestors', 1, ProcDict),
  73. Parent.
  74. %% proc_lib.
  75. proc_lib_initial_call_clear(Config) ->
  76. doc("Confirm that clear connection processes are started using proc_lib."),
  77. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
  78. timer:sleep(100),
  79. Pid = do_get_remote_pid_tcp(Socket),
  80. {cowboy_clear, _, _} = proc_lib:initial_call(Pid),
  81. ok.
  82. proc_lib_initial_call_tls(Config) ->
  83. doc("Confirm that TLS connection processes are started using proc_lib."),
  84. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config), config(tls_opts, Config)),
  85. timer:sleep(100),
  86. Pid = do_get_remote_pid_tls(Socket),
  87. {cowboy_tls, _, _} = proc_lib:initial_call(Pid),
  88. ok.
  89. %% System messages.
  90. %%
  91. %% Plain system messages are received as {system, From, Msg}.
  92. %% The content and meaning of this message are not interpreted by
  93. %% the receiving process module. When a system message is received,
  94. %% function handle_system_msg/6 is called to handle the request.
  95. bad_system_from_h1(Config) ->
  96. doc("h1: Sending a system message with a bad From value results in a process crash."),
  97. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  98. timer:sleep(100),
  99. Pid = do_get_remote_pid_tcp(Socket),
  100. ct_helper_error_h:ignore(Pid, gen, reply, 2),
  101. Pid ! {system, bad, get_state},
  102. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  103. false = is_process_alive(Pid),
  104. ok.
  105. bad_system_from_h2(Config) ->
  106. doc("h2: Sending a system message with a bad From value results in a process crash."),
  107. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  108. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  109. %% Skip the SETTINGS frame.
  110. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  111. timer:sleep(100),
  112. Pid = do_get_remote_pid_tls(Socket),
  113. ct_helper_error_h:ignore(Pid, gen, reply, 2),
  114. Pid ! {system, bad, get_state},
  115. {error, closed} = ssl:recv(Socket, 0, 1000),
  116. false = is_process_alive(Pid),
  117. ok.
  118. bad_system_from_ws(Config) ->
  119. doc("ws: Sending a system message with a bad From value results in a process crash."),
  120. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  121. [binary, {active, false}]),
  122. ok = gen_tcp:send(Socket,
  123. "GET /ws HTTP/1.1\r\n"
  124. "Host: localhost\r\n"
  125. "Connection: Upgrade\r\n"
  126. "Origin: http://localhost\r\n"
  127. "Sec-WebSocket-Version: 13\r\n"
  128. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  129. "Upgrade: websocket\r\n"
  130. "\r\n"),
  131. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  132. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  133. timer:sleep(100),
  134. Pid = do_get_remote_pid_tcp(Socket),
  135. ct_helper_error_h:ignore(Pid, gen, reply, 2),
  136. Pid ! {system, bad, get_state},
  137. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  138. false = is_process_alive(Pid),
  139. ok.
  140. bad_system_from_loop(Config) ->
  141. doc("loop: Sending a system message with a bad From value results in a process crash."),
  142. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  143. ok = gen_tcp:send(Socket,
  144. "GET /loop HTTP/1.1\r\n"
  145. "Host: localhost\r\n"
  146. "\r\n"),
  147. timer:sleep(100),
  148. SupPid = do_get_remote_pid_tcp(Socket),
  149. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  150. ct_helper_error_h:ignore(Pid, gen, reply, 2),
  151. Pid ! {system, bad, get_state},
  152. {ok, "HTTP/1.1 500 "} = gen_tcp:recv(Socket, 13, 1000),
  153. false = is_process_alive(Pid),
  154. ok.
  155. bad_system_message_h1(Config) ->
  156. doc("h1: Sending a system message with a bad Request value results in an error."),
  157. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
  158. timer:sleep(100),
  159. Pid = do_get_remote_pid_tcp(Socket),
  160. Ref = make_ref(),
  161. Pid ! {system, {self(), Ref}, hello},
  162. receive
  163. {Ref, {error, {unknown_system_msg, hello}}} ->
  164. ok
  165. after 1000 ->
  166. error(timeout)
  167. end.
  168. bad_system_message_h2(Config) ->
  169. doc("h2: Sending a system message with a bad Request value results in an error."),
  170. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  171. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  172. %% Skip the SETTINGS frame.
  173. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  174. timer:sleep(100),
  175. Pid = do_get_remote_pid_tls(Socket),
  176. Ref = make_ref(),
  177. Pid ! {system, {self(), Ref}, hello},
  178. receive
  179. {Ref, {error, {unknown_system_msg, hello}}} ->
  180. ok
  181. after 1000 ->
  182. error(timeout)
  183. end.
  184. bad_system_message_ws(Config) ->
  185. doc("ws: Sending a system message with a bad Request value results in an error."),
  186. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  187. [binary, {active, false}]),
  188. ok = gen_tcp:send(Socket,
  189. "GET /ws HTTP/1.1\r\n"
  190. "Host: localhost\r\n"
  191. "Connection: Upgrade\r\n"
  192. "Origin: http://localhost\r\n"
  193. "Sec-WebSocket-Version: 13\r\n"
  194. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  195. "Upgrade: websocket\r\n"
  196. "\r\n"),
  197. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  198. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  199. timer:sleep(100),
  200. Pid = do_get_remote_pid_tcp(Socket),
  201. Ref = make_ref(),
  202. Pid ! {system, {self(), Ref}, hello},
  203. receive
  204. {Ref, {error, {unknown_system_msg, hello}}} ->
  205. ok
  206. after 1000 ->
  207. error(timeout)
  208. end.
  209. bad_system_message_loop(Config) ->
  210. doc("loop: Sending a system message with a bad Request value results in an error."),
  211. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  212. ok = gen_tcp:send(Socket,
  213. "GET /loop HTTP/1.1\r\n"
  214. "Host: localhost\r\n"
  215. "\r\n"),
  216. timer:sleep(100),
  217. SupPid = do_get_remote_pid_tcp(Socket),
  218. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  219. Ref = make_ref(),
  220. Pid ! {system, {self(), Ref}, hello},
  221. receive
  222. {Ref, {error, {unknown_system_msg, hello}}} ->
  223. ok
  224. after 1000 ->
  225. error(timeout)
  226. end.
  227. good_system_message_h1(Config) ->
  228. doc("h1: System messages are handled properly."),
  229. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
  230. timer:sleep(100),
  231. Pid = do_get_remote_pid_tcp(Socket),
  232. Ref = make_ref(),
  233. Pid ! {system, {self(), Ref}, get_state},
  234. receive
  235. {Ref, Result} when element(1, Result) =/= error ->
  236. ok
  237. after 1000 ->
  238. error(timeout)
  239. end.
  240. good_system_message_h2(Config) ->
  241. doc("h2: System messages are handled properly."),
  242. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  243. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  244. %% Skip the SETTINGS frame.
  245. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  246. timer:sleep(100),
  247. Pid = do_get_remote_pid_tls(Socket),
  248. Ref = make_ref(),
  249. Pid ! {system, {self(), Ref}, get_state},
  250. receive
  251. {Ref, Result} when element(1, Result) =/= error ->
  252. ok
  253. after 1000 ->
  254. error(timeout)
  255. end.
  256. good_system_message_ws(Config) ->
  257. doc("ws: Sending a system message with a bad Request value results in an error."),
  258. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  259. [binary, {active, false}]),
  260. ok = gen_tcp:send(Socket,
  261. "GET /ws HTTP/1.1\r\n"
  262. "Host: localhost\r\n"
  263. "Connection: Upgrade\r\n"
  264. "Origin: http://localhost\r\n"
  265. "Sec-WebSocket-Version: 13\r\n"
  266. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  267. "Upgrade: websocket\r\n"
  268. "\r\n"),
  269. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  270. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  271. timer:sleep(100),
  272. Pid = do_get_remote_pid_tcp(Socket),
  273. Ref = make_ref(),
  274. Pid ! {system, {self(), Ref}, get_state},
  275. receive
  276. {Ref, Result} when element(1, Result) =/= error ->
  277. ok
  278. after 1000 ->
  279. error(timeout)
  280. end.
  281. good_system_message_loop(Config) ->
  282. doc("loop: Sending a system message with a bad Request value results in an error."),
  283. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  284. ok = gen_tcp:send(Socket,
  285. "GET /loop HTTP/1.1\r\n"
  286. "Host: localhost\r\n"
  287. "\r\n"),
  288. timer:sleep(100),
  289. SupPid = do_get_remote_pid_tcp(Socket),
  290. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  291. Ref = make_ref(),
  292. Pid ! {system, {self(), Ref}, get_state},
  293. receive
  294. {Ref, Result} when element(1, Result) =/= error ->
  295. ok
  296. after 1000 ->
  297. error(timeout)
  298. end.
  299. %% 'EXIT'.
  300. %%
  301. %% Shutdown messages. If the process traps exits, it must be able
  302. %% to handle a shutdown request from its parent, the supervisor.
  303. %% The message {'EXIT', Parent, Reason} from the parent is an order
  304. %% to terminate. The process must terminate when this message is
  305. %% received, normally with the same Reason as Parent.
  306. trap_exit_parent_exit_h1(Config) ->
  307. doc("h1: A process trapping exits must stop when receiving "
  308. "an 'EXIT' message from its parent."),
  309. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  310. [{active, false}]),
  311. timer:sleep(100),
  312. Pid = do_get_remote_pid_tcp(Socket),
  313. Parent = do_get_parent_pid(Pid),
  314. Pid ! {'EXIT', Parent, shutdown},
  315. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  316. false = is_process_alive(Pid),
  317. ok.
  318. trap_exit_parent_exit_h2(Config) ->
  319. doc("h2: A process trapping exits must stop when receiving "
  320. "an 'EXIT' message from its parent."),
  321. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  322. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  323. %% Skip the SETTINGS frame.
  324. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  325. timer:sleep(100),
  326. Pid = do_get_remote_pid_tls(Socket),
  327. Parent = do_get_parent_pid(Pid),
  328. Pid ! {'EXIT', Parent, shutdown},
  329. {error, closed} = ssl:recv(Socket, 0, 1000),
  330. false = is_process_alive(Pid),
  331. ok.
  332. trap_exit_parent_exit_ws(Config) ->
  333. doc("ws: A process trapping exits must stop when receiving "
  334. "an 'EXIT' message from its parent."),
  335. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  336. [binary, {active, false}]),
  337. ok = gen_tcp:send(Socket,
  338. "GET /ws HTTP/1.1\r\n"
  339. "Host: localhost\r\n"
  340. "Connection: Upgrade\r\n"
  341. "Origin: http://localhost\r\n"
  342. "Sec-WebSocket-Version: 13\r\n"
  343. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  344. "Upgrade: websocket\r\n"
  345. "\r\n"),
  346. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  347. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  348. timer:sleep(100),
  349. Pid = do_get_remote_pid_tcp(Socket),
  350. Parent = do_get_parent_pid(Pid),
  351. Pid ! {'EXIT', Parent, shutdown},
  352. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  353. false = is_process_alive(Pid),
  354. ok.
  355. trap_exit_parent_exit_loop(Config) ->
  356. doc("loop: A process trapping exits must stop when receiving "
  357. "an 'EXIT' message from its parent."),
  358. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  359. ok = gen_tcp:send(Socket,
  360. "GET /loop HTTP/1.1\r\n"
  361. "Host: localhost\r\n"
  362. "\r\n"),
  363. timer:sleep(100),
  364. Parent = do_get_remote_pid_tcp(Socket),
  365. [{_, Pid, _, _}] = supervisor:which_children(Parent),
  366. Pid ! {'EXIT', Parent, shutdown},
  367. %% We exit normally but didn't send a response.
  368. {ok, "HTTP/1.1 204 "} = gen_tcp:recv(Socket, 13, 1000),
  369. false = is_process_alive(Pid),
  370. ok.
  371. trap_exit_other_exit_h1(Config) ->
  372. doc("h1: A process trapping exits must ignore "
  373. "'EXIT' messages from unknown processes."),
  374. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  375. [{active, false}]),
  376. timer:sleep(100),
  377. Pid = do_get_remote_pid_tcp(Socket),
  378. Pid ! {'EXIT', self(), shutdown},
  379. ok = gen_tcp:send(Socket,
  380. "GET / HTTP/1.1\r\n"
  381. "Host: localhost\r\n"
  382. "\r\n"),
  383. {ok, "HTTP/1.1 200 "} = gen_tcp:recv(Socket, 13, 1000),
  384. true = is_process_alive(Pid),
  385. ok.
  386. trap_exit_other_exit_h2(Config) ->
  387. doc("h2: A process trapping exits must ignore "
  388. "'EXIT' messages from unknown processes."),
  389. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  390. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  391. %% Do the handshake.
  392. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  393. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  394. ok = ssl:send(Socket, cow_http2:settings_ack()),
  395. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  396. timer:sleep(100),
  397. Pid = do_get_remote_pid_tls(Socket),
  398. Pid ! {'EXIT', self(), shutdown},
  399. %% Send a HEADERS frame as a request.
  400. {HeadersBlock, _} = cow_hpack:encode([
  401. {<<":method">>, <<"GET">>},
  402. {<<":scheme">>, <<"https">>},
  403. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  404. {<<":path">>, <<"/">>}
  405. ]),
  406. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  407. %% Receive a HEADERS frame as a response.
  408. {ok, << _:24, 1:8, _:40 >>} = ssl:recv(Socket, 9, 6000),
  409. true = is_process_alive(Pid),
  410. ok.
  411. trap_exit_other_exit_ws(Config) ->
  412. doc("ws: A process trapping exits must ignore "
  413. "'EXIT' messages from unknown processes."),
  414. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  415. [binary, {active, false}]),
  416. ok = gen_tcp:send(Socket,
  417. "GET /ws HTTP/1.1\r\n"
  418. "Host: localhost\r\n"
  419. "Connection: Upgrade\r\n"
  420. "Origin: http://localhost\r\n"
  421. "Sec-WebSocket-Version: 13\r\n"
  422. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  423. "Upgrade: websocket\r\n"
  424. "\r\n"),
  425. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  426. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  427. timer:sleep(100),
  428. Pid = do_get_remote_pid_tcp(Socket),
  429. Pid ! {'EXIT', self(), shutdown},
  430. %% The process stays alive.
  431. {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
  432. true = is_process_alive(Pid),
  433. ok.
  434. trap_exit_other_exit_loop(Config) ->
  435. doc("loop: A process trapping exits must ignore "
  436. "'EXIT' messages from unknown processes."),
  437. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  438. ok = gen_tcp:send(Socket,
  439. "GET /loop HTTP/1.1\r\n"
  440. "Host: localhost\r\n"
  441. "\r\n"),
  442. timer:sleep(100),
  443. Parent = do_get_remote_pid_tcp(Socket),
  444. [{_, Pid, _, _}] = supervisor:which_children(Parent),
  445. Pid ! {'EXIT', self(), shutdown},
  446. %% The process stays alive.
  447. {ok, "HTTP/1.1 299 "} = gen_tcp:recv(Socket, 13, 1000),
  448. true = is_process_alive(Pid),
  449. ok.
  450. %% get_modules.
  451. %%
  452. %% If the modules used to implement the process change dynamically
  453. %% during runtime, the process must understand one more message.
  454. %% An example is the gen_event processes. The message is
  455. %% {_Label, {From, Ref}, get_modules}. The reply to this message is
  456. %% From ! {Ref, Modules}, where Modules is a list of the currently
  457. %% active modules in the process.
  458. %%
  459. %% For example:
  460. %%
  461. %% 1> application:start(sasl).
  462. %% ok
  463. %% 2> gen:call(alarm_handler, self(), get_modules).
  464. %% {ok,[alarm_handler]}
  465. %% 3> whereis(alarm_handler) ! {'$gen', {self(), make_ref()}, get_modules}.
  466. %% {'$gen',{<0.61.0>,#Ref<0.2900144977.374865921.142102>},
  467. %% get_modules}
  468. %% 4> flush().
  469. %% Shell got {#Ref<0.2900144977.374865921.142102>,[alarm_handler]}
  470. %%
  471. %% Cowboy's connection processes change dynamically: it starts with
  472. %% cowboy_clear or cowboy_tls, then becomes cowboy_http or cowboy_http2
  473. %% and may then become or involve cowboy_websocket. On top of that
  474. %% it has various callback modules in the form of stream handlers.
  475. %% @todo
  476. %get_modules_h1(Config) ->
  477. %get_modules_h2(Config) ->
  478. %get_modules_ws(Config) ->
  479. %get_modules_loop(Config) ->
  480. %% @todo On top of this we will want to make the supervisor calls
  481. %% in ranch_conns_sup return dynamic instead of a list of modules.
  482. %% sys.
  483. %% @todo sys:change_code/4,5 and Module:system_code_change/4
  484. %sys_change_code_h1(Config) ->
  485. %sys_change_code_h2(Config) ->
  486. %sys_change_code_ws(Config) ->
  487. %sys_change_code_loop(Config) ->
  488. %% @todo sys:get_state/1,2 and Module:system_get_state/1
  489. %sys_get_state_h1(Config) ->
  490. %sys_get_state_h2(Config) ->
  491. %sys_get_state_ws(Config) ->
  492. %sys_get_state_loop(Config) ->
  493. %% @todo sys:get_status/1,2
  494. %sys_get_status_h1(Config) ->
  495. %sys_get_status_h2(Config) ->
  496. %sys_get_status_ws(Config) ->
  497. %sys_get_status_loop(Config) ->
  498. %% @todo sys:replace_state/2,3 and Module:replace_state/2
  499. %sys_replace_state_h1(Config) ->
  500. %sys_replace_state_h2(Config) ->
  501. %sys_replace_state_ws(Config) ->
  502. %sys_replace_state_loop(Config) ->
  503. %% @todo sys:resume/1,2 and sys:suspend/1,2 and Module:system_continue/3
  504. %sys_suspend_and_resume_h1(Config) ->
  505. %sys_suspend_and_resume_h2(Config) ->
  506. %sys_suspend_and_resume_ws(Config) ->
  507. %sys_suspend_and_resume_loop(Config) ->
  508. %% @todo sys:terminate/2,3 and Module:system_terminate/4
  509. %sys_terminate_h1(Config) ->
  510. %sys_terminate_h2(Config) ->
  511. %sys_terminate_ws(Config) ->
  512. %sys_terminate_loop(Config) ->
  513. %% @todo Debugging functionality from sys.
  514. %%
  515. %% The functions make references to a debug structure.
  516. %% The debug structure is a list of dbg_opt(), which is
  517. %% an internal data type used by function handle_system_msg/6.
  518. %% No debugging is performed if it is an empty list.
  519. %%
  520. %% Cowboy currently does not implement sys debugging.
  521. %%
  522. %% The following functions are concerned:
  523. %%
  524. %% * sys:install/2,3
  525. %% * sys:log/2,3
  526. %% * sys:log_to_file/2,3
  527. %% * sys:no_debug/1,2
  528. %% * sys:remove/2,3
  529. %% * sys:statistics/2,3
  530. %% * sys:trace/2,3
  531. %% * call debug_options/1
  532. %% * call get_debug/3
  533. %% * call handle_debug/4
  534. %% * call print_log/1
  535. %% supervisor.
  536. %%
  537. %% The connection processes act as supervisors by default
  538. %% so they must handle the supervisor messages.
  539. %% supervisor:count_children/1.
  540. supervisor_count_children_h1(Config) ->
  541. doc("h1: The function supervisor:count_children/1 must work."),
  542. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  543. [{active, false}]),
  544. timer:sleep(100),
  545. Pid = do_get_remote_pid_tcp(Socket),
  546. %% No request was sent so there's no children.
  547. Counts1 = supervisor:count_children(Pid),
  548. 1 = proplists:get_value(specs, Counts1),
  549. 0 = proplists:get_value(active, Counts1),
  550. 0 = proplists:get_value(supervisors, Counts1),
  551. 0 = proplists:get_value(workers, Counts1),
  552. %% Send a request, observe that a children exists.
  553. ok = gen_tcp:send(Socket,
  554. "GET /loop HTTP/1.1\r\n"
  555. "Host: localhost\r\n"
  556. "\r\n"),
  557. timer:sleep(100),
  558. Counts2 = supervisor:count_children(Pid),
  559. 1 = proplists:get_value(specs, Counts2),
  560. 1 = proplists:get_value(active, Counts2),
  561. 0 = proplists:get_value(supervisors, Counts2),
  562. 1 = proplists:get_value(workers, Counts2),
  563. ok.
  564. supervisor_count_children_h2(Config) ->
  565. doc("h2: The function supervisor:count_children/1 must work."),
  566. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  567. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  568. %% Do the handshake.
  569. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  570. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  571. ok = ssl:send(Socket, cow_http2:settings_ack()),
  572. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  573. timer:sleep(100),
  574. Pid = do_get_remote_pid_tls(Socket),
  575. %% No request was sent so there's no children.
  576. Counts1 = supervisor:count_children(Pid),
  577. 1 = proplists:get_value(specs, Counts1),
  578. 0 = proplists:get_value(active, Counts1),
  579. 0 = proplists:get_value(supervisors, Counts1),
  580. 0 = proplists:get_value(workers, Counts1),
  581. %% Send a request, observe that a children exists.
  582. {HeadersBlock, _} = cow_hpack:encode([
  583. {<<":method">>, <<"GET">>},
  584. {<<":scheme">>, <<"https">>},
  585. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  586. {<<":path">>, <<"/loop">>}
  587. ]),
  588. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  589. timer:sleep(100),
  590. Counts2 = supervisor:count_children(Pid),
  591. 1 = proplists:get_value(specs, Counts2),
  592. 1 = proplists:get_value(active, Counts2),
  593. 0 = proplists:get_value(supervisors, Counts2),
  594. 1 = proplists:get_value(workers, Counts2),
  595. ok.
  596. supervisor_count_children_ws(Config) ->
  597. doc("ws: The function supervisor:count_children/1 must work. "
  598. "Websocket connections never have children."),
  599. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  600. [binary, {active, false}]),
  601. ok = gen_tcp:send(Socket,
  602. "GET /ws HTTP/1.1\r\n"
  603. "Host: localhost\r\n"
  604. "Connection: Upgrade\r\n"
  605. "Origin: http://localhost\r\n"
  606. "Sec-WebSocket-Version: 13\r\n"
  607. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  608. "Upgrade: websocket\r\n"
  609. "\r\n"),
  610. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  611. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  612. timer:sleep(100),
  613. Pid = do_get_remote_pid_tcp(Socket),
  614. Counts = supervisor:count_children(Pid),
  615. 1 = proplists:get_value(specs, Counts),
  616. 0 = proplists:get_value(active, Counts),
  617. 0 = proplists:get_value(supervisors, Counts),
  618. 0 = proplists:get_value(workers, Counts),
  619. ok.
  620. %% supervisor:delete_child/2.
  621. supervisor_delete_child_not_found_h1(Config) ->
  622. doc("h1: The function supervisor:delete_child/2 must return {error, not_found}."),
  623. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  624. [{active, false}]),
  625. timer:sleep(100),
  626. Pid = do_get_remote_pid_tcp(Socket),
  627. %% When no children exist.
  628. {error, not_found} = supervisor:delete_child(Pid, cowboy_http),
  629. %% When a child exists.
  630. ok = gen_tcp:send(Socket,
  631. "GET /loop HTTP/1.1\r\n"
  632. "Host: localhost\r\n"
  633. "\r\n"),
  634. timer:sleep(100),
  635. {error, not_found} = supervisor:delete_child(Pid, cowboy_http),
  636. ok.
  637. supervisor_delete_child_not_found_h2(Config) ->
  638. doc("h2: The function supervisor:delete_child/2 must return {error, not_found}."),
  639. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  640. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  641. %% Do the handshake.
  642. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  643. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  644. ok = ssl:send(Socket, cow_http2:settings_ack()),
  645. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  646. timer:sleep(100),
  647. Pid = do_get_remote_pid_tls(Socket),
  648. %% When no children exist.
  649. {error, not_found} = supervisor:delete_child(Pid, cowboy_http2),
  650. %% When a child exists.
  651. {HeadersBlock, _} = cow_hpack:encode([
  652. {<<":method">>, <<"GET">>},
  653. {<<":scheme">>, <<"https">>},
  654. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  655. {<<":path">>, <<"/loop">>}
  656. ]),
  657. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  658. timer:sleep(100),
  659. {error, not_found} = supervisor:delete_child(Pid, cowboy_http2),
  660. ok.
  661. supervisor_delete_child_not_found_ws(Config) ->
  662. doc("ws: The function supervisor:delete_child/2 must return {error, not_found}."),
  663. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  664. [binary, {active, false}]),
  665. ok = gen_tcp:send(Socket,
  666. "GET /ws HTTP/1.1\r\n"
  667. "Host: localhost\r\n"
  668. "Connection: Upgrade\r\n"
  669. "Origin: http://localhost\r\n"
  670. "Sec-WebSocket-Version: 13\r\n"
  671. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  672. "Upgrade: websocket\r\n"
  673. "\r\n"),
  674. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  675. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  676. timer:sleep(100),
  677. Pid = do_get_remote_pid_tcp(Socket),
  678. {error, not_found} = supervisor:delete_child(Pid, cowboy_websocket),
  679. ok.
  680. %% supervisor:get_childspec/2.
  681. supervisor_get_childspec_not_found_h1(Config) ->
  682. doc("h1: The function supervisor:get_childspec/2 must return {error, not_found}."),
  683. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  684. [{active, false}]),
  685. timer:sleep(100),
  686. Pid = do_get_remote_pid_tcp(Socket),
  687. %% When no children exist.
  688. {error, not_found} = supervisor:get_childspec(Pid, cowboy_http),
  689. %% When a child exists.
  690. ok = gen_tcp:send(Socket,
  691. "GET /loop HTTP/1.1\r\n"
  692. "Host: localhost\r\n"
  693. "\r\n"),
  694. timer:sleep(100),
  695. {error, not_found} = supervisor:get_childspec(Pid, cowboy_http),
  696. ok.
  697. supervisor_get_childspec_not_found_h2(Config) ->
  698. doc("h2: The function supervisor:get_childspec/2 must return {error, not_found}."),
  699. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  700. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  701. %% Do the handshake.
  702. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  703. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  704. ok = ssl:send(Socket, cow_http2:settings_ack()),
  705. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  706. timer:sleep(100),
  707. Pid = do_get_remote_pid_tls(Socket),
  708. %% When no children exist.
  709. {error, not_found} = supervisor:get_childspec(Pid, cowboy_http2),
  710. %% When a child exists.
  711. {HeadersBlock, _} = cow_hpack:encode([
  712. {<<":method">>, <<"GET">>},
  713. {<<":scheme">>, <<"https">>},
  714. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  715. {<<":path">>, <<"/loop">>}
  716. ]),
  717. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  718. timer:sleep(100),
  719. {error, not_found} = supervisor:get_childspec(Pid, cowboy_http2),
  720. ok.
  721. supervisor_get_childspec_not_found_ws(Config) ->
  722. doc("ws: The function supervisor:get_childspec/2 must return {error, not_found}."),
  723. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  724. [binary, {active, false}]),
  725. ok = gen_tcp:send(Socket,
  726. "GET /ws HTTP/1.1\r\n"
  727. "Host: localhost\r\n"
  728. "Connection: Upgrade\r\n"
  729. "Origin: http://localhost\r\n"
  730. "Sec-WebSocket-Version: 13\r\n"
  731. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  732. "Upgrade: websocket\r\n"
  733. "\r\n"),
  734. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  735. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  736. timer:sleep(100),
  737. Pid = do_get_remote_pid_tcp(Socket),
  738. {error, not_found} = supervisor:get_childspec(Pid, cowboy_websocket),
  739. ok.
  740. %% supervisor:restart_child/2.
  741. supervisor_restart_child_not_found_h1(Config) ->
  742. doc("h1: The function supervisor:restart_child/2 must return {error, not_found}."),
  743. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  744. [{active, false}]),
  745. timer:sleep(100),
  746. Pid = do_get_remote_pid_tcp(Socket),
  747. %% When no children exist.
  748. {error, not_found} = supervisor:restart_child(Pid, cowboy_http),
  749. %% When a child exists.
  750. ok = gen_tcp:send(Socket,
  751. "GET /loop HTTP/1.1\r\n"
  752. "Host: localhost\r\n"
  753. "\r\n"),
  754. timer:sleep(100),
  755. {error, not_found} = supervisor:restart_child(Pid, cowboy_http),
  756. ok.
  757. supervisor_restart_child_not_found_h2(Config) ->
  758. doc("h2: The function supervisor:restart_child/2 must return {error, not_found}."),
  759. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  760. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  761. %% Do the handshake.
  762. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  763. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  764. ok = ssl:send(Socket, cow_http2:settings_ack()),
  765. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  766. timer:sleep(100),
  767. Pid = do_get_remote_pid_tls(Socket),
  768. %% When no children exist.
  769. {error, not_found} = supervisor:restart_child(Pid, cowboy_http2),
  770. %% When a child exists.
  771. {HeadersBlock, _} = cow_hpack:encode([
  772. {<<":method">>, <<"GET">>},
  773. {<<":scheme">>, <<"https">>},
  774. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  775. {<<":path">>, <<"/loop">>}
  776. ]),
  777. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  778. timer:sleep(100),
  779. {error, not_found} = supervisor:restart_child(Pid, cowboy_http2),
  780. ok.
  781. supervisor_restart_child_not_found_ws(Config) ->
  782. doc("ws: The function supervisor:restart_child/2 must return {error, not_found}."),
  783. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  784. [binary, {active, false}]),
  785. ok = gen_tcp:send(Socket,
  786. "GET /ws HTTP/1.1\r\n"
  787. "Host: localhost\r\n"
  788. "Connection: Upgrade\r\n"
  789. "Origin: http://localhost\r\n"
  790. "Sec-WebSocket-Version: 13\r\n"
  791. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  792. "Upgrade: websocket\r\n"
  793. "\r\n"),
  794. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  795. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  796. timer:sleep(100),
  797. Pid = do_get_remote_pid_tcp(Socket),
  798. {error, not_found} = supervisor:restart_child(Pid, cowboy_websocket),
  799. ok.
  800. %% supervisor:start_child/2 must return {error, start_child_disabled}
  801. supervisor_start_child_not_found_h1(Config) ->
  802. doc("h1: The function supervisor:start_child/2 must return {error, start_child_disabled}."),
  803. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  804. [{active, false}]),
  805. timer:sleep(100),
  806. Pid = do_get_remote_pid_tcp(Socket),
  807. {error, start_child_disabled} = supervisor:start_child(Pid, #{
  808. id => error,
  809. start => {error, error, []}
  810. }),
  811. ok.
  812. supervisor_start_child_not_found_h2(Config) ->
  813. doc("h2: The function supervisor:start_child/2 must return {error, start_child_disabled}."),
  814. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  815. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  816. %% Do the handshake.
  817. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  818. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  819. ok = ssl:send(Socket, cow_http2:settings_ack()),
  820. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  821. timer:sleep(100),
  822. Pid = do_get_remote_pid_tls(Socket),
  823. {error, start_child_disabled} = supervisor:start_child(Pid, #{
  824. id => error,
  825. start => {error, error, []}
  826. }),
  827. ok.
  828. supervisor_start_child_not_found_ws(Config) ->
  829. doc("ws: The function supervisor:start_child/2 must return {error, start_child_disabled}."),
  830. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  831. [binary, {active, false}]),
  832. ok = gen_tcp:send(Socket,
  833. "GET /ws HTTP/1.1\r\n"
  834. "Host: localhost\r\n"
  835. "Connection: Upgrade\r\n"
  836. "Origin: http://localhost\r\n"
  837. "Sec-WebSocket-Version: 13\r\n"
  838. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  839. "Upgrade: websocket\r\n"
  840. "\r\n"),
  841. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  842. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  843. timer:sleep(100),
  844. Pid = do_get_remote_pid_tcp(Socket),
  845. {error, start_child_disabled} = supervisor:start_child(Pid, #{
  846. id => error,
  847. start => {error, error, []}
  848. }),
  849. ok.
  850. %% supervisor:terminate_child/2.
  851. supervisor_terminate_child_not_found_h1(Config) ->
  852. doc("h1: The function supervisor:terminate_child/2 must return {error, not_found}."),
  853. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  854. [{active, false}]),
  855. timer:sleep(100),
  856. Pid = do_get_remote_pid_tcp(Socket),
  857. %% When no children exist.
  858. {error, not_found} = supervisor:terminate_child(Pid, cowboy_http),
  859. %% When a child exists.
  860. ok = gen_tcp:send(Socket,
  861. "GET /loop HTTP/1.1\r\n"
  862. "Host: localhost\r\n"
  863. "\r\n"),
  864. timer:sleep(100),
  865. {error, not_found} = supervisor:terminate_child(Pid, cowboy_http),
  866. ok.
  867. supervisor_terminate_child_not_found_h2(Config) ->
  868. doc("h2: The function supervisor:terminate_child/2 must return {error, not_found}."),
  869. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  870. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  871. %% Do the handshake.
  872. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  873. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  874. ok = ssl:send(Socket, cow_http2:settings_ack()),
  875. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  876. timer:sleep(100),
  877. Pid = do_get_remote_pid_tls(Socket),
  878. %% When no children exist.
  879. {error, not_found} = supervisor:terminate_child(Pid, cowboy_http2),
  880. %% When a child exists.
  881. {HeadersBlock, _} = cow_hpack:encode([
  882. {<<":method">>, <<"GET">>},
  883. {<<":scheme">>, <<"https">>},
  884. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  885. {<<":path">>, <<"/loop">>}
  886. ]),
  887. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  888. timer:sleep(100),
  889. {error, not_found} = supervisor:terminate_child(Pid, cowboy_http2),
  890. ok.
  891. supervisor_terminate_child_not_found_ws(Config) ->
  892. doc("ws: The function supervisor:terminate_child/2 must return {error, not_found}."),
  893. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  894. [binary, {active, false}]),
  895. ok = gen_tcp:send(Socket,
  896. "GET /ws HTTP/1.1\r\n"
  897. "Host: localhost\r\n"
  898. "Connection: Upgrade\r\n"
  899. "Origin: http://localhost\r\n"
  900. "Sec-WebSocket-Version: 13\r\n"
  901. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  902. "Upgrade: websocket\r\n"
  903. "\r\n"),
  904. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  905. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  906. timer:sleep(100),
  907. Pid = do_get_remote_pid_tcp(Socket),
  908. {error, not_found} = supervisor:terminate_child(Pid, cowboy_websocket),
  909. ok.
  910. %% supervisor:which_children/1.
  911. %%
  912. %% @todo The list of modules returned is probably wrong. This will
  913. %% need to be corrected when get_modules gets implemented.
  914. supervisor_which_children_h1(Config) ->
  915. doc("h1: The function supervisor:which_children/1 must work."),
  916. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  917. [{active, false}]),
  918. timer:sleep(100),
  919. Pid = do_get_remote_pid_tcp(Socket),
  920. %% No request was sent so there's no children.
  921. [] = supervisor:which_children(Pid),
  922. %% Send a request, observe that a children exists.
  923. ok = gen_tcp:send(Socket,
  924. "GET /loop HTTP/1.1\r\n"
  925. "Host: localhost\r\n"
  926. "\r\n"),
  927. timer:sleep(100),
  928. [{cowboy_http, Child, worker, [cowboy_http]}] = supervisor:which_children(Pid),
  929. true = is_pid(Child),
  930. ok.
  931. supervisor_which_children_h2(Config) ->
  932. doc("h2: The function supervisor:which_children/1 must work."),
  933. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  934. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  935. %% Do the handshake.
  936. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  937. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  938. ok = ssl:send(Socket, cow_http2:settings_ack()),
  939. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  940. timer:sleep(100),
  941. Pid = do_get_remote_pid_tls(Socket),
  942. %% No request was sent so there's no children.
  943. [] = supervisor:which_children(Pid),
  944. %% Send a request, observe that a children exists.
  945. {HeadersBlock, _} = cow_hpack:encode([
  946. {<<":method">>, <<"GET">>},
  947. {<<":scheme">>, <<"https">>},
  948. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  949. {<<":path">>, <<"/loop">>}
  950. ]),
  951. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  952. timer:sleep(100),
  953. [{cowboy_http2, Child, worker, [cowboy_http2]}] = supervisor:which_children(Pid),
  954. true = is_pid(Child),
  955. ok.
  956. supervisor_which_children_ws(Config) ->
  957. doc("ws: The function supervisor:which_children/1 must work. "
  958. "Websocket connections never have children."),
  959. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  960. [binary, {active, false}]),
  961. ok = gen_tcp:send(Socket,
  962. "GET /ws HTTP/1.1\r\n"
  963. "Host: localhost\r\n"
  964. "Connection: Upgrade\r\n"
  965. "Origin: http://localhost\r\n"
  966. "Sec-WebSocket-Version: 13\r\n"
  967. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  968. "Upgrade: websocket\r\n"
  969. "\r\n"),
  970. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  971. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  972. timer:sleep(100),
  973. Pid = do_get_remote_pid_tcp(Socket),
  974. [] = supervisor:which_children(Pid),
  975. ok.