sys_SUITE.erl 37 KB

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