sys_SUITE.erl 37 KB

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