sys_SUITE.erl 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434
  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(ct_helper, [get_parent_pid/1]).
  20. -import(ct_helper, [get_remote_pid_tcp/1]).
  21. -import(ct_helper, [get_remote_pid_tls/1]).
  22. -import(cowboy_test, [gun_open/1]).
  23. all() ->
  24. [{group, sys}].
  25. groups() ->
  26. [{sys, [parallel], ct_helper:all(?MODULE)}].
  27. init_per_suite(Config) ->
  28. ct:print("This test suite will produce error reports about "
  29. "EXIT signals for unknown processes."),
  30. ProtoOpts = #{
  31. env => #{dispatch => init_dispatch(Config)}
  32. },
  33. %% Clear listener.
  34. {ok, _} = cowboy:start_clear(clear, [{port, 0}], ProtoOpts),
  35. ClearPort = ranch:get_port(clear),
  36. %% TLS listener.
  37. TLSOpts = ct_helper:get_certs_from_ets(),
  38. {ok, _} = cowboy:start_tls(tls, TLSOpts ++ [{port, 0}], ProtoOpts),
  39. TLSPort = ranch:get_port(tls),
  40. [
  41. {clear_port, ClearPort},
  42. %% @todo Add the h2 stuff to the opts.
  43. {tls_opts, TLSOpts},
  44. {tls_port, TLSPort}
  45. |Config].
  46. end_per_suite(_) ->
  47. ok = cowboy:stop_listener(clear),
  48. ok = cowboy:stop_listener(tls).
  49. init_dispatch(_) ->
  50. cowboy_router:compile([{"[...]", [
  51. {"/", hello_h, []},
  52. {"/loop", long_polling_sys_h, []},
  53. {"/ws", ws_echo, []}
  54. ]}]).
  55. %% proc_lib.
  56. proc_lib_initial_call_clear(Config) ->
  57. doc("Confirm that clear connection processes are started using proc_lib."),
  58. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
  59. timer:sleep(100),
  60. Pid = get_remote_pid_tcp(Socket),
  61. {cowboy_clear, _, _} = proc_lib:initial_call(Pid),
  62. ok.
  63. proc_lib_initial_call_tls(Config) ->
  64. doc("Confirm that TLS connection processes are started using proc_lib."),
  65. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config), config(tls_opts, Config)),
  66. timer:sleep(100),
  67. Pid = get_remote_pid_tls(Socket),
  68. {cowboy_tls, _, _} = proc_lib:initial_call(Pid),
  69. ok.
  70. %% System messages.
  71. %%
  72. %% Plain system messages are received as {system, From, Msg}.
  73. %% The content and meaning of this message are not interpreted by
  74. %% the receiving process module. When a system message is received,
  75. %% function handle_system_msg/6 is called to handle the request.
  76. bad_system_from_h1(Config) ->
  77. doc("h1: Sending a system message with a bad From value results in a process crash."),
  78. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  79. timer:sleep(100),
  80. Pid = get_remote_pid_tcp(Socket),
  81. ct_helper_error_h:ignore(Pid, gen, reply, 2),
  82. Pid ! {system, bad, get_state},
  83. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  84. false = is_process_alive(Pid),
  85. ok.
  86. bad_system_from_h2(Config) ->
  87. doc("h2: Sending a system message with a bad From value results in a process crash."),
  88. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  89. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  90. %% Skip the SETTINGS frame.
  91. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  92. timer:sleep(100),
  93. Pid = get_remote_pid_tls(Socket),
  94. ct_helper_error_h:ignore(Pid, gen, reply, 2),
  95. Pid ! {system, bad, get_state},
  96. {error, closed} = ssl:recv(Socket, 0, 1000),
  97. false = is_process_alive(Pid),
  98. ok.
  99. bad_system_from_ws(Config) ->
  100. doc("ws: Sending a system message with a bad From value results in a process crash."),
  101. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  102. [binary, {active, false}]),
  103. ok = gen_tcp:send(Socket,
  104. "GET /ws HTTP/1.1\r\n"
  105. "Host: localhost\r\n"
  106. "Connection: Upgrade\r\n"
  107. "Origin: http://localhost\r\n"
  108. "Sec-WebSocket-Version: 13\r\n"
  109. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  110. "Upgrade: websocket\r\n"
  111. "\r\n"),
  112. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  113. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  114. timer:sleep(100),
  115. Pid = get_remote_pid_tcp(Socket),
  116. ct_helper_error_h:ignore(Pid, gen, reply, 2),
  117. Pid ! {system, bad, get_state},
  118. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  119. false = is_process_alive(Pid),
  120. ok.
  121. bad_system_from_loop(Config) ->
  122. doc("loop: 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), [{active, false}]),
  124. ok = gen_tcp:send(Socket,
  125. "GET /loop HTTP/1.1\r\n"
  126. "Host: localhost\r\n"
  127. "\r\n"),
  128. timer:sleep(100),
  129. SupPid = get_remote_pid_tcp(Socket),
  130. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  131. ct_helper_error_h:ignore(Pid, gen, reply, 2),
  132. Pid ! {system, bad, get_state},
  133. {ok, "HTTP/1.1 500 "} = gen_tcp:recv(Socket, 13, 1000),
  134. false = is_process_alive(Pid),
  135. ok.
  136. bad_system_message_h1(Config) ->
  137. doc("h1: Sending a system message with a bad Request value results in an error."),
  138. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
  139. timer:sleep(100),
  140. Pid = get_remote_pid_tcp(Socket),
  141. Ref = make_ref(),
  142. Pid ! {system, {self(), Ref}, hello},
  143. receive
  144. {Ref, {error, {unknown_system_msg, hello}}} ->
  145. ok
  146. after 1000 ->
  147. error(timeout)
  148. end.
  149. bad_system_message_h2(Config) ->
  150. doc("h2: Sending a system message with a bad Request value results in an error."),
  151. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  152. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  153. %% Skip the SETTINGS frame.
  154. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  155. timer:sleep(100),
  156. Pid = get_remote_pid_tls(Socket),
  157. Ref = make_ref(),
  158. Pid ! {system, {self(), Ref}, hello},
  159. receive
  160. {Ref, {error, {unknown_system_msg, hello}}} ->
  161. ok
  162. after 1000 ->
  163. error(timeout)
  164. end.
  165. bad_system_message_ws(Config) ->
  166. doc("ws: Sending a system message with a bad Request value results in an error."),
  167. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  168. [binary, {active, false}]),
  169. ok = gen_tcp:send(Socket,
  170. "GET /ws HTTP/1.1\r\n"
  171. "Host: localhost\r\n"
  172. "Connection: Upgrade\r\n"
  173. "Origin: http://localhost\r\n"
  174. "Sec-WebSocket-Version: 13\r\n"
  175. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  176. "Upgrade: websocket\r\n"
  177. "\r\n"),
  178. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  179. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  180. timer:sleep(100),
  181. Pid = get_remote_pid_tcp(Socket),
  182. Ref = make_ref(),
  183. Pid ! {system, {self(), Ref}, hello},
  184. receive
  185. {Ref, {error, {unknown_system_msg, hello}}} ->
  186. ok
  187. after 1000 ->
  188. error(timeout)
  189. end.
  190. bad_system_message_loop(Config) ->
  191. doc("loop: Sending a system message with a bad Request value results in an error."),
  192. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  193. ok = gen_tcp:send(Socket,
  194. "GET /loop HTTP/1.1\r\n"
  195. "Host: localhost\r\n"
  196. "\r\n"),
  197. timer:sleep(100),
  198. SupPid = get_remote_pid_tcp(Socket),
  199. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  200. Ref = make_ref(),
  201. Pid ! {system, {self(), Ref}, hello},
  202. receive
  203. {Ref, {error, {unknown_system_msg, hello}}} ->
  204. ok
  205. after 1000 ->
  206. error(timeout)
  207. end.
  208. good_system_message_h1(Config) ->
  209. doc("h1: System messages are handled properly."),
  210. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
  211. timer:sleep(100),
  212. Pid = get_remote_pid_tcp(Socket),
  213. Ref = make_ref(),
  214. Pid ! {system, {self(), Ref}, get_state},
  215. receive
  216. {Ref, Result} when element(1, Result) =/= error ->
  217. ok
  218. after 1000 ->
  219. error(timeout)
  220. end.
  221. good_system_message_h2(Config) ->
  222. doc("h2: System messages are handled properly."),
  223. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  224. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  225. %% Skip the SETTINGS frame.
  226. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  227. timer:sleep(100),
  228. Pid = get_remote_pid_tls(Socket),
  229. Ref = make_ref(),
  230. Pid ! {system, {self(), Ref}, get_state},
  231. receive
  232. {Ref, Result} when element(1, Result) =/= error ->
  233. ok
  234. after 1000 ->
  235. error(timeout)
  236. end.
  237. good_system_message_ws(Config) ->
  238. doc("ws: System messages are handled properly."),
  239. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  240. [binary, {active, false}]),
  241. ok = gen_tcp:send(Socket,
  242. "GET /ws HTTP/1.1\r\n"
  243. "Host: localhost\r\n"
  244. "Connection: Upgrade\r\n"
  245. "Origin: http://localhost\r\n"
  246. "Sec-WebSocket-Version: 13\r\n"
  247. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  248. "Upgrade: websocket\r\n"
  249. "\r\n"),
  250. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  251. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  252. timer:sleep(100),
  253. Pid = get_remote_pid_tcp(Socket),
  254. Ref = make_ref(),
  255. Pid ! {system, {self(), Ref}, get_state},
  256. receive
  257. {Ref, Result} when element(1, Result) =/= error ->
  258. ok
  259. after 1000 ->
  260. error(timeout)
  261. end.
  262. good_system_message_loop(Config) ->
  263. doc("loop: System messages are handled properly."),
  264. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  265. ok = gen_tcp:send(Socket,
  266. "GET /loop HTTP/1.1\r\n"
  267. "Host: localhost\r\n"
  268. "\r\n"),
  269. timer:sleep(100),
  270. SupPid = get_remote_pid_tcp(Socket),
  271. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  272. Ref = make_ref(),
  273. Pid ! {system, {self(), Ref}, get_state},
  274. receive
  275. {Ref, Result} when element(1, Result) =/= error ->
  276. ok
  277. after 1000 ->
  278. error(timeout)
  279. end.
  280. %% 'EXIT'.
  281. %%
  282. %% Shutdown messages. If the process traps exits, it must be able
  283. %% to handle a shutdown request from its parent, the supervisor.
  284. %% The message {'EXIT', Parent, Reason} from the parent is an order
  285. %% to terminate. The process must terminate when this message is
  286. %% received, normally with the same Reason as Parent.
  287. trap_exit_parent_exit_h1(Config) ->
  288. doc("h1: A process trapping exits must stop when receiving "
  289. "an 'EXIT' message from its parent."),
  290. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  291. [{active, false}]),
  292. timer:sleep(100),
  293. Pid = get_remote_pid_tcp(Socket),
  294. Parent = get_parent_pid(Pid),
  295. Pid ! {'EXIT', Parent, shutdown},
  296. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  297. false = is_process_alive(Pid),
  298. ok.
  299. trap_exit_parent_exit_h2(Config) ->
  300. doc("h2: A process trapping exits must stop when receiving "
  301. "an 'EXIT' message from its parent."),
  302. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  303. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  304. %% Skip the SETTINGS frame.
  305. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  306. timer:sleep(100),
  307. Pid = get_remote_pid_tls(Socket),
  308. Parent = get_parent_pid(Pid),
  309. Pid ! {'EXIT', Parent, shutdown},
  310. {error, closed} = ssl:recv(Socket, 0, 1000),
  311. false = is_process_alive(Pid),
  312. ok.
  313. trap_exit_parent_exit_ws(Config) ->
  314. doc("ws: A process trapping exits must stop when receiving "
  315. "an 'EXIT' message from its parent."),
  316. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  317. [binary, {active, false}]),
  318. ok = gen_tcp:send(Socket,
  319. "GET /ws HTTP/1.1\r\n"
  320. "Host: localhost\r\n"
  321. "Connection: Upgrade\r\n"
  322. "Origin: http://localhost\r\n"
  323. "Sec-WebSocket-Version: 13\r\n"
  324. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  325. "Upgrade: websocket\r\n"
  326. "\r\n"),
  327. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  328. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  329. timer:sleep(100),
  330. Pid = get_remote_pid_tcp(Socket),
  331. Parent = get_parent_pid(Pid),
  332. Pid ! {'EXIT', Parent, shutdown},
  333. {error, closed} = gen_tcp:recv(Socket, 0, 1000),
  334. false = is_process_alive(Pid),
  335. ok.
  336. trap_exit_parent_exit_loop(Config) ->
  337. doc("loop: A process trapping exits must stop when receiving "
  338. "an 'EXIT' message from its parent."),
  339. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  340. ok = gen_tcp:send(Socket,
  341. "GET /loop HTTP/1.1\r\n"
  342. "Host: localhost\r\n"
  343. "\r\n"),
  344. timer:sleep(100),
  345. Parent = get_remote_pid_tcp(Socket),
  346. [{_, Pid, _, _}] = supervisor:which_children(Parent),
  347. Pid ! {'EXIT', Parent, shutdown},
  348. %% We exit normally but didn't send a response.
  349. {ok, "HTTP/1.1 204 "} = gen_tcp:recv(Socket, 13, 1000),
  350. false = is_process_alive(Pid),
  351. ok.
  352. trap_exit_other_exit_h1(Config) ->
  353. doc("h1: A process trapping exits must ignore "
  354. "'EXIT' messages from unknown processes."),
  355. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  356. [{active, false}]),
  357. timer:sleep(100),
  358. Pid = get_remote_pid_tcp(Socket),
  359. Pid ! {'EXIT', self(), shutdown},
  360. ok = gen_tcp:send(Socket,
  361. "GET / HTTP/1.1\r\n"
  362. "Host: localhost\r\n"
  363. "\r\n"),
  364. {ok, "HTTP/1.1 200 "} = gen_tcp:recv(Socket, 13, 1000),
  365. true = is_process_alive(Pid),
  366. ok.
  367. trap_exit_other_exit_h2(Config) ->
  368. doc("h2: A process trapping exits must ignore "
  369. "'EXIT' messages from unknown processes."),
  370. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  371. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  372. %% Do the handshake.
  373. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  374. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  375. ok = ssl:send(Socket, cow_http2:settings_ack()),
  376. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  377. timer:sleep(100),
  378. Pid = get_remote_pid_tls(Socket),
  379. Pid ! {'EXIT', self(), shutdown},
  380. %% Send a HEADERS frame as a request.
  381. {HeadersBlock, _} = cow_hpack:encode([
  382. {<<":method">>, <<"GET">>},
  383. {<<":scheme">>, <<"https">>},
  384. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  385. {<<":path">>, <<"/">>}
  386. ]),
  387. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  388. %% Receive a HEADERS frame as a response.
  389. {ok, << _:24, 1:8, _:40 >>} = ssl:recv(Socket, 9, 6000),
  390. true = is_process_alive(Pid),
  391. ok.
  392. trap_exit_other_exit_ws(Config) ->
  393. doc("ws: A process trapping exits must ignore "
  394. "'EXIT' messages from unknown processes."),
  395. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  396. [binary, {active, false}]),
  397. ok = gen_tcp:send(Socket,
  398. "GET /ws HTTP/1.1\r\n"
  399. "Host: localhost\r\n"
  400. "Connection: Upgrade\r\n"
  401. "Origin: http://localhost\r\n"
  402. "Sec-WebSocket-Version: 13\r\n"
  403. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  404. "Upgrade: websocket\r\n"
  405. "\r\n"),
  406. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  407. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  408. timer:sleep(100),
  409. Pid = get_remote_pid_tcp(Socket),
  410. Pid ! {'EXIT', self(), shutdown},
  411. %% The process stays alive.
  412. {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
  413. true = is_process_alive(Pid),
  414. ok.
  415. trap_exit_other_exit_loop(Config) ->
  416. doc("loop: A process trapping exits must ignore "
  417. "'EXIT' messages from unknown processes."),
  418. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  419. ok = gen_tcp:send(Socket,
  420. "GET /loop HTTP/1.1\r\n"
  421. "Host: localhost\r\n"
  422. "\r\n"),
  423. timer:sleep(100),
  424. Parent = get_remote_pid_tcp(Socket),
  425. [{_, Pid, _, _}] = supervisor:which_children(Parent),
  426. Pid ! {'EXIT', self(), shutdown},
  427. %% The process stays alive.
  428. {ok, "HTTP/1.1 299 "} = gen_tcp:recv(Socket, 13, 1000),
  429. true = is_process_alive(Pid),
  430. ok.
  431. %% get_modules.
  432. %%
  433. %% If the modules used to implement the process change dynamically
  434. %% during runtime, the process must understand one more message.
  435. %% An example is the gen_event processes. The message is
  436. %% {_Label, {From, Ref}, get_modules}. The reply to this message is
  437. %% From ! {Ref, Modules}, where Modules is a list of the currently
  438. %% active modules in the process.
  439. %%
  440. %% For example:
  441. %%
  442. %% 1> application:start(sasl).
  443. %% ok
  444. %% 2> gen:call(alarm_handler, self(), get_modules).
  445. %% {ok,[alarm_handler]}
  446. %% 3> whereis(alarm_handler) ! {'$gen', {self(), make_ref()}, get_modules}.
  447. %% {'$gen',{<0.61.0>,#Ref<0.2900144977.374865921.142102>},
  448. %% get_modules}
  449. %% 4> flush().
  450. %% Shell got {#Ref<0.2900144977.374865921.142102>,[alarm_handler]}
  451. %%
  452. %% Cowboy's connection processes change dynamically: it starts with
  453. %% cowboy_clear or cowboy_tls, then becomes cowboy_http or cowboy_http2
  454. %% and may then become or involve cowboy_websocket. On top of that
  455. %% it has various callback modules in the form of stream handlers.
  456. %% @todo
  457. %get_modules_h1(Config) ->
  458. %get_modules_h2(Config) ->
  459. %get_modules_ws(Config) ->
  460. %get_modules_loop(Config) ->
  461. %% @todo On top of this we will want to make the supervisor calls
  462. %% in ranch_conns_sup return dynamic instead of a list of modules.
  463. %% sys:change_code/4,5.
  464. %%
  465. %% We do not actually change the module code, we just ensure that
  466. %% calling this function does not crash the process. The function
  467. %% Module:system_code_change/4 will be called within the process.
  468. sys_change_code_h1(Config) ->
  469. doc("h1: The sys:change_code/4 function works as expected."),
  470. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  471. timer:sleep(100),
  472. Pid = get_remote_pid_tcp(Socket),
  473. ok = sys:suspend(Pid),
  474. ok = gen_tcp:send(Socket,
  475. "GET / HTTP/1.1\r\n"
  476. "Host: localhost\r\n"
  477. "\r\n"),
  478. {error, timeout} = gen_tcp:recv(Socket, 13, 500),
  479. ok = sys:change_code(Pid, cowboy_http, undefined, undefined),
  480. ok = sys:resume(Pid),
  481. {ok, "HTTP/1.1 200 "} = gen_tcp:recv(Socket, 13, 500),
  482. ok.
  483. sys_change_code_h2(Config) ->
  484. doc("h2: The sys:change_code/4 function works as expected."),
  485. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  486. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  487. timer:sleep(100),
  488. Pid = get_remote_pid_tls(Socket),
  489. %% Send a valid preface.
  490. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  491. %% Receive the server preface.
  492. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  493. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  494. %% Send the SETTINGS ack.
  495. ok = ssl:send(Socket, cow_http2:settings_ack()),
  496. %% Receive the SETTINGS ack.
  497. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  498. %% Suspend the process and try to get a request in. The
  499. %% response will not come back until we resume the process.
  500. ok = sys:suspend(Pid),
  501. {HeadersBlock, _} = cow_hpack:encode([
  502. {<<":method">>, <<"GET">>},
  503. {<<":scheme">>, <<"http">>},
  504. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  505. {<<":path">>, <<"/">>}
  506. ]),
  507. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  508. %% Receive a HEADERS frame as a response.
  509. {error, timeout} = ssl:recv(Socket, 9, 500),
  510. ok = sys:change_code(Pid, cowboy_http2, undefined, undefined),
  511. ok = sys:resume(Pid),
  512. {ok, << _:24, 1:8, _:40 >>} = ssl:recv(Socket, 9, 6000),
  513. ok.
  514. sys_change_code_ws(Config) ->
  515. doc("ws: The sys:change_code/4 function works as expected."),
  516. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  517. [binary, {active, false}]),
  518. ok = gen_tcp:send(Socket,
  519. "GET /ws HTTP/1.1\r\n"
  520. "Host: localhost\r\n"
  521. "Connection: Upgrade\r\n"
  522. "Origin: http://localhost\r\n"
  523. "Sec-WebSocket-Version: 13\r\n"
  524. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  525. "Upgrade: websocket\r\n"
  526. "\r\n"),
  527. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  528. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  529. timer:sleep(100),
  530. Pid = get_remote_pid_tcp(Socket),
  531. ok = sys:suspend(Pid),
  532. Mask = 16#37fa213d,
  533. MaskedHello = ws_SUITE:do_mask(<<"Hello">>, Mask, <<>>),
  534. ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  535. {error, timeout} = gen_tcp:recv(Socket, 0, 500),
  536. ok = sys:change_code(Pid, cowboy_websocket, undefined, undefined),
  537. ok = sys:resume(Pid),
  538. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  539. ok.
  540. sys_change_code_loop(Config) ->
  541. doc("loop: The sys:change_code/4 function works as expected."),
  542. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  543. ok = gen_tcp:send(Socket,
  544. "GET /loop HTTP/1.1\r\n"
  545. "Host: localhost\r\n"
  546. "\r\n"),
  547. timer:sleep(100),
  548. SupPid = get_remote_pid_tcp(Socket),
  549. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  550. %% The process sends a response 500ms after initializing.
  551. %% We expect to not receive it until we resume it.
  552. ok = sys:suspend(Pid),
  553. {error, timeout} = gen_tcp:recv(Socket, 13, 1000),
  554. ok = sys:change_code(Pid, cowboy_loop, undefined, undefined),
  555. ok = sys:resume(Pid),
  556. {ok, "HTTP/1.1 299 "} = gen_tcp:recv(Socket, 13, 500),
  557. ok.
  558. %% sys:get_state/1,2.
  559. %%
  560. %% None of the modules implement Module:system_get_state/1
  561. %% at this time so sys:get_state/1,2 returns the Misc value.
  562. sys_get_state_h1(Config) ->
  563. doc("h1: The sys:get_state/1 function works as expected."),
  564. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
  565. timer:sleep(100),
  566. Pid = get_remote_pid_tcp(Socket),
  567. {State, Buffer} = sys:get_state(Pid),
  568. state = element(1, State),
  569. true = is_binary(Buffer),
  570. ok.
  571. sys_get_state_h2(Config) ->
  572. doc("h2: The sys:get_state/1 function works as expected."),
  573. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  574. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  575. %% Skip the SETTINGS frame.
  576. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  577. timer:sleep(100),
  578. Pid = get_remote_pid_tls(Socket),
  579. {State, Buffer} = sys:get_state(Pid),
  580. state = element(1, State),
  581. true = is_binary(Buffer),
  582. ok.
  583. sys_get_state_ws(Config) ->
  584. doc("ws: The sys:get_state/1 function works as expected."),
  585. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  586. [binary, {active, false}]),
  587. ok = gen_tcp:send(Socket,
  588. "GET /ws HTTP/1.1\r\n"
  589. "Host: localhost\r\n"
  590. "Connection: Upgrade\r\n"
  591. "Origin: http://localhost\r\n"
  592. "Sec-WebSocket-Version: 13\r\n"
  593. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  594. "Upgrade: websocket\r\n"
  595. "\r\n"),
  596. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  597. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  598. timer:sleep(100),
  599. Pid = get_remote_pid_tcp(Socket),
  600. {State, undefined, ParseState} = sys:get_state(Pid),
  601. state = element(1, State),
  602. case element(1, ParseState) of
  603. ps_header -> ok;
  604. ps_payload -> ok
  605. end.
  606. sys_get_state_loop(Config) ->
  607. doc("loop: The sys:get_state/1 function works as expected."),
  608. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  609. ok = gen_tcp:send(Socket,
  610. "GET /loop HTTP/1.1\r\n"
  611. "Host: localhost\r\n"
  612. "\r\n"),
  613. timer:sleep(100),
  614. SupPid = get_remote_pid_tcp(Socket),
  615. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  616. {Req, Env, long_polling_sys_h, undefined} = sys:get_state(Pid),
  617. #{pid := _, streamid := _} = Req,
  618. #{dispatch := _} = Env,
  619. ok.
  620. %% sys:get_status/1,2.
  621. sys_get_status_h1(Config) ->
  622. doc("h1: The sys:get_status/1 function works as expected."),
  623. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
  624. timer:sleep(100),
  625. Pid = get_remote_pid_tcp(Socket),
  626. {status, Pid, {module, cowboy_http}, _} = sys:get_status(Pid),
  627. ok.
  628. sys_get_status_h2(Config) ->
  629. doc("h2: The sys:get_status/1 function works as expected."),
  630. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  631. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  632. %% Skip the SETTINGS frame.
  633. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  634. timer:sleep(100),
  635. Pid = get_remote_pid_tls(Socket),
  636. {status, Pid, {module, cowboy_http2}, _} = sys:get_status(Pid),
  637. ok.
  638. sys_get_status_ws(Config) ->
  639. doc("ws: The sys:get_status/1 function works as expected."),
  640. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  641. [binary, {active, false}]),
  642. ok = gen_tcp:send(Socket,
  643. "GET /ws HTTP/1.1\r\n"
  644. "Host: localhost\r\n"
  645. "Connection: Upgrade\r\n"
  646. "Origin: http://localhost\r\n"
  647. "Sec-WebSocket-Version: 13\r\n"
  648. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  649. "Upgrade: websocket\r\n"
  650. "\r\n"),
  651. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  652. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  653. timer:sleep(100),
  654. Pid = get_remote_pid_tcp(Socket),
  655. {status, Pid, {module, cowboy_websocket}, _} = sys:get_status(Pid),
  656. ok.
  657. sys_get_status_loop(Config) ->
  658. doc("loop: The sys:get_status/1 function works as expected."),
  659. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  660. ok = gen_tcp:send(Socket,
  661. "GET /loop HTTP/1.1\r\n"
  662. "Host: localhost\r\n"
  663. "\r\n"),
  664. timer:sleep(100),
  665. SupPid = get_remote_pid_tcp(Socket),
  666. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  667. {status, Pid, {module, cowboy_loop}, _} = sys:get_status(Pid),
  668. ok.
  669. %% sys:replace_state/2,3.
  670. %%
  671. %% None of the modules implement Module:system_replace_state/2
  672. %% at this time so sys:replace_state/2,3 handles the Misc value.
  673. %%
  674. %% We don't actually replace the state, we only care about
  675. %% whether the call executes as expected.
  676. sys_replace_state_h1(Config) ->
  677. doc("h1: The sys:replace_state/2 function works as expected."),
  678. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
  679. timer:sleep(100),
  680. Pid = get_remote_pid_tcp(Socket),
  681. {State, Buffer} = sys:replace_state(Pid, fun(S) -> S end),
  682. state = element(1, State),
  683. true = is_binary(Buffer),
  684. ok.
  685. sys_replace_state_h2(Config) ->
  686. doc("h2: The sys:replace_state/2 function works as expected."),
  687. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  688. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  689. %% Skip the SETTINGS frame.
  690. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  691. timer:sleep(100),
  692. Pid = get_remote_pid_tls(Socket),
  693. {State, Buffer} = sys:replace_state(Pid, fun(S) -> S end),
  694. state = element(1, State),
  695. true = is_binary(Buffer),
  696. ok.
  697. sys_replace_state_ws(Config) ->
  698. doc("ws: The sys:replace_state/2 function works as expected."),
  699. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  700. [binary, {active, false}]),
  701. ok = gen_tcp:send(Socket,
  702. "GET /ws HTTP/1.1\r\n"
  703. "Host: localhost\r\n"
  704. "Connection: Upgrade\r\n"
  705. "Origin: http://localhost\r\n"
  706. "Sec-WebSocket-Version: 13\r\n"
  707. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  708. "Upgrade: websocket\r\n"
  709. "\r\n"),
  710. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  711. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  712. timer:sleep(100),
  713. Pid = get_remote_pid_tcp(Socket),
  714. {State, undefined, ParseState} = sys:replace_state(Pid, fun(S) -> S end),
  715. state = element(1, State),
  716. case element(1, ParseState) of
  717. ps_header -> ok;
  718. ps_payload -> ok
  719. end.
  720. sys_replace_state_loop(Config) ->
  721. doc("loop: The sys:replace_state/2 function works as expected."),
  722. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  723. ok = gen_tcp:send(Socket,
  724. "GET /loop HTTP/1.1\r\n"
  725. "Host: localhost\r\n"
  726. "\r\n"),
  727. timer:sleep(100),
  728. SupPid = get_remote_pid_tcp(Socket),
  729. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  730. {Req, Env, long_polling_sys_h, undefined} = sys:replace_state(Pid, fun(S) -> S end),
  731. #{pid := _, streamid := _} = Req,
  732. #{dispatch := _} = Env,
  733. ok.
  734. %% sys:suspend/1 and sys:resume/1.
  735. sys_suspend_and_resume_h1(Config) ->
  736. doc("h1: The sys:suspend/1 and sys:resume/1 functions work as expected."),
  737. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  738. timer:sleep(100),
  739. Pid = get_remote_pid_tcp(Socket),
  740. ok = sys:suspend(Pid),
  741. ok = gen_tcp:send(Socket,
  742. "GET / HTTP/1.1\r\n"
  743. "Host: localhost\r\n"
  744. "\r\n"),
  745. {error, timeout} = gen_tcp:recv(Socket, 13, 500),
  746. ok = sys:resume(Pid),
  747. {ok, "HTTP/1.1 200 "} = gen_tcp:recv(Socket, 13, 500),
  748. ok.
  749. sys_suspend_and_resume_h2(Config) ->
  750. doc("h2: The sys:suspend/1 and sys:resume/1 functions work as expected."),
  751. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  752. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  753. timer:sleep(100),
  754. Pid = get_remote_pid_tls(Socket),
  755. %% Send a valid preface.
  756. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  757. %% Receive the server preface.
  758. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  759. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  760. %% Send the SETTINGS ack.
  761. ok = ssl:send(Socket, cow_http2:settings_ack()),
  762. %% Receive the SETTINGS ack.
  763. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  764. %% Suspend the process and try to get a request in. The
  765. %% response will not come back until we resume the process.
  766. ok = sys:suspend(Pid),
  767. {HeadersBlock, _} = cow_hpack:encode([
  768. {<<":method">>, <<"GET">>},
  769. {<<":scheme">>, <<"http">>},
  770. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  771. {<<":path">>, <<"/">>}
  772. ]),
  773. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  774. %% Receive a HEADERS frame as a response.
  775. {error, timeout} = ssl:recv(Socket, 9, 500),
  776. ok = sys:resume(Pid),
  777. {ok, << _:24, 1:8, _:40 >>} = ssl:recv(Socket, 9, 6000),
  778. ok.
  779. sys_suspend_and_resume_ws(Config) ->
  780. doc("ws: The sys:suspend/1 and sys:resume/1 functions work as expected."),
  781. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  782. [binary, {active, false}]),
  783. ok = gen_tcp:send(Socket,
  784. "GET /ws HTTP/1.1\r\n"
  785. "Host: localhost\r\n"
  786. "Connection: Upgrade\r\n"
  787. "Origin: http://localhost\r\n"
  788. "Sec-WebSocket-Version: 13\r\n"
  789. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  790. "Upgrade: websocket\r\n"
  791. "\r\n"),
  792. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  793. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  794. timer:sleep(100),
  795. Pid = get_remote_pid_tcp(Socket),
  796. ok = sys:suspend(Pid),
  797. Mask = 16#37fa213d,
  798. MaskedHello = ws_SUITE:do_mask(<<"Hello">>, Mask, <<>>),
  799. ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  800. {error, timeout} = gen_tcp:recv(Socket, 0, 500),
  801. ok = sys:resume(Pid),
  802. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  803. ok.
  804. sys_suspend_and_resume_loop(Config) ->
  805. doc("loop: The sys:suspend/1 and sys:resume/1 functions work as expected."),
  806. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  807. ok = gen_tcp:send(Socket,
  808. "GET /loop HTTP/1.1\r\n"
  809. "Host: localhost\r\n"
  810. "\r\n"),
  811. timer:sleep(100),
  812. SupPid = get_remote_pid_tcp(Socket),
  813. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  814. %% The process sends a response 500ms after initializing.
  815. %% We expect to not receive it until we resume it.
  816. ok = sys:suspend(Pid),
  817. {error, timeout} = gen_tcp:recv(Socket, 13, 1000),
  818. ok = sys:resume(Pid),
  819. {ok, "HTTP/1.1 299 "} = gen_tcp:recv(Socket, 13, 500),
  820. ok.
  821. %% sys:terminate/2,3.
  822. %%
  823. %% The callback Module:system_terminate/4 is used in all cases.
  824. sys_terminate_h1(Config) ->
  825. doc("h1: The sys:terminate/1 function works as expected."),
  826. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  827. timer:sleep(100),
  828. Pid = get_remote_pid_tcp(Socket),
  829. ok = sys:terminate(Pid, {shutdown, test}),
  830. {error, closed} = gen_tcp:recv(Socket, 0, 500),
  831. ok.
  832. sys_terminate_h2(Config) ->
  833. doc("h2: The sys:terminate/1 function works as expected."),
  834. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  835. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  836. %% Skip the SETTINGS frame.
  837. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  838. timer:sleep(100),
  839. Pid = get_remote_pid_tls(Socket),
  840. ok = sys:terminate(Pid, {shutdown, test}),
  841. {error, closed} = ssl:recv(Socket, 0, 500),
  842. ok.
  843. sys_terminate_ws(Config) ->
  844. doc("ws: The sys:terminate/1 function works as expected."),
  845. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  846. [binary, {active, false}]),
  847. ok = gen_tcp:send(Socket,
  848. "GET /ws HTTP/1.1\r\n"
  849. "Host: localhost\r\n"
  850. "Connection: Upgrade\r\n"
  851. "Origin: http://localhost\r\n"
  852. "Sec-WebSocket-Version: 13\r\n"
  853. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  854. "Upgrade: websocket\r\n"
  855. "\r\n"),
  856. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  857. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  858. timer:sleep(100),
  859. Pid = get_remote_pid_tcp(Socket),
  860. ok = sys:terminate(Pid, {shutdown, test}),
  861. {error, closed} = gen_tcp:recv(Socket, 0, 500),
  862. ok.
  863. sys_terminate_loop(Config) ->
  864. doc("loop: The sys:terminate/1 function works as expected."),
  865. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
  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. SupPid = get_remote_pid_tcp(Socket),
  872. [{_, Pid, _, _}] = supervisor:which_children(SupPid),
  873. %% We stop the process normally and therefore get a 204.
  874. ok = sys:terminate(Pid, {shutdown, test}),
  875. {ok, "HTTP/1.1 204 "} = gen_tcp:recv(Socket, 13, 500),
  876. ok.
  877. %% @todo Debugging functionality from sys.
  878. %%
  879. %% The functions make references to a debug structure.
  880. %% The debug structure is a list of dbg_opt(), which is
  881. %% an internal data type used by the function handle_system_msg/6.
  882. %% No debugging is performed if it is an empty list.
  883. %%
  884. %% Cowboy currently does not implement sys debugging.
  885. %%
  886. %% The following functions are concerned:
  887. %%
  888. %% * sys:install/2,3
  889. %% * sys:log/2,3
  890. %% * sys:log_to_file/2,3
  891. %% * sys:no_debug/1,2
  892. %% * sys:remove/2,3
  893. %% * sys:statistics/2,3
  894. %% * sys:trace/2,3
  895. %% * call debug_options/1
  896. %% * call get_debug/3
  897. %% * call handle_debug/4
  898. %% * call print_log/1
  899. %% supervisor.
  900. %%
  901. %% The connection processes act as supervisors by default
  902. %% so they must handle the supervisor messages.
  903. %% supervisor:count_children/1.
  904. supervisor_count_children_h1(Config) ->
  905. doc("h1: The function supervisor:count_children/1 must work."),
  906. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  907. [{active, false}]),
  908. timer:sleep(100),
  909. Pid = get_remote_pid_tcp(Socket),
  910. %% No request was sent so there's no children.
  911. Counts1 = supervisor:count_children(Pid),
  912. 1 = proplists:get_value(specs, Counts1),
  913. 0 = proplists:get_value(active, Counts1),
  914. 0 = proplists:get_value(supervisors, Counts1),
  915. 0 = proplists:get_value(workers, Counts1),
  916. %% Send a request, observe that a children exists.
  917. ok = gen_tcp:send(Socket,
  918. "GET /loop HTTP/1.1\r\n"
  919. "Host: localhost\r\n"
  920. "\r\n"),
  921. timer:sleep(100),
  922. Counts2 = supervisor:count_children(Pid),
  923. 1 = proplists:get_value(specs, Counts2),
  924. 1 = proplists:get_value(active, Counts2),
  925. 0 = proplists:get_value(supervisors, Counts2),
  926. 1 = proplists:get_value(workers, Counts2),
  927. ok.
  928. supervisor_count_children_h2(Config) ->
  929. doc("h2: The function supervisor:count_children/1 must work."),
  930. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  931. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  932. %% Do the handshake.
  933. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  934. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  935. ok = ssl:send(Socket, cow_http2:settings_ack()),
  936. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  937. timer:sleep(100),
  938. Pid = get_remote_pid_tls(Socket),
  939. %% No request was sent so there's no children.
  940. Counts1 = supervisor:count_children(Pid),
  941. 1 = proplists:get_value(specs, Counts1),
  942. 0 = proplists:get_value(active, Counts1),
  943. 0 = proplists:get_value(supervisors, Counts1),
  944. 0 = proplists:get_value(workers, Counts1),
  945. %% Send a request, observe that a children exists.
  946. {HeadersBlock, _} = cow_hpack:encode([
  947. {<<":method">>, <<"GET">>},
  948. {<<":scheme">>, <<"https">>},
  949. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  950. {<<":path">>, <<"/loop">>}
  951. ]),
  952. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  953. timer:sleep(100),
  954. Counts2 = supervisor:count_children(Pid),
  955. 1 = proplists:get_value(specs, Counts2),
  956. 1 = proplists:get_value(active, Counts2),
  957. 0 = proplists:get_value(supervisors, Counts2),
  958. 1 = proplists:get_value(workers, Counts2),
  959. ok.
  960. supervisor_count_children_ws(Config) ->
  961. doc("ws: The function supervisor:count_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 = get_remote_pid_tcp(Socket),
  978. Counts = supervisor:count_children(Pid),
  979. 1 = proplists:get_value(specs, Counts),
  980. 0 = proplists:get_value(active, Counts),
  981. 0 = proplists:get_value(supervisors, Counts),
  982. 0 = proplists:get_value(workers, Counts),
  983. ok.
  984. %% supervisor:delete_child/2.
  985. supervisor_delete_child_not_found_h1(Config) ->
  986. doc("h1: The function supervisor:delete_child/2 must return {error, not_found}."),
  987. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  988. [{active, false}]),
  989. timer:sleep(100),
  990. Pid = get_remote_pid_tcp(Socket),
  991. %% When no children exist.
  992. {error, not_found} = supervisor:delete_child(Pid, cowboy_http),
  993. %% When a child exists.
  994. ok = gen_tcp:send(Socket,
  995. "GET /loop HTTP/1.1\r\n"
  996. "Host: localhost\r\n"
  997. "\r\n"),
  998. timer:sleep(100),
  999. {error, not_found} = supervisor:delete_child(Pid, cowboy_http),
  1000. ok.
  1001. supervisor_delete_child_not_found_h2(Config) ->
  1002. doc("h2: The function supervisor:delete_child/2 must return {error, not_found}."),
  1003. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  1004. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  1005. %% Do the handshake.
  1006. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  1007. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  1008. ok = ssl:send(Socket, cow_http2:settings_ack()),
  1009. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  1010. timer:sleep(100),
  1011. Pid = get_remote_pid_tls(Socket),
  1012. %% When no children exist.
  1013. {error, not_found} = supervisor:delete_child(Pid, cowboy_http2),
  1014. %% When a child exists.
  1015. {HeadersBlock, _} = cow_hpack:encode([
  1016. {<<":method">>, <<"GET">>},
  1017. {<<":scheme">>, <<"https">>},
  1018. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1019. {<<":path">>, <<"/loop">>}
  1020. ]),
  1021. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1022. timer:sleep(100),
  1023. {error, not_found} = supervisor:delete_child(Pid, cowboy_http2),
  1024. ok.
  1025. supervisor_delete_child_not_found_ws(Config) ->
  1026. doc("ws: The function supervisor:delete_child/2 must return {error, not_found}."),
  1027. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1028. [binary, {active, false}]),
  1029. ok = gen_tcp:send(Socket,
  1030. "GET /ws HTTP/1.1\r\n"
  1031. "Host: localhost\r\n"
  1032. "Connection: Upgrade\r\n"
  1033. "Origin: http://localhost\r\n"
  1034. "Sec-WebSocket-Version: 13\r\n"
  1035. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  1036. "Upgrade: websocket\r\n"
  1037. "\r\n"),
  1038. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  1039. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  1040. timer:sleep(100),
  1041. Pid = get_remote_pid_tcp(Socket),
  1042. {error, not_found} = supervisor:delete_child(Pid, cowboy_websocket),
  1043. ok.
  1044. %% supervisor:get_childspec/2.
  1045. supervisor_get_childspec_not_found_h1(Config) ->
  1046. doc("h1: The function supervisor:get_childspec/2 must return {error, not_found}."),
  1047. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1048. [{active, false}]),
  1049. timer:sleep(100),
  1050. Pid = get_remote_pid_tcp(Socket),
  1051. %% When no children exist.
  1052. {error, not_found} = supervisor:get_childspec(Pid, cowboy_http),
  1053. %% When a child exists.
  1054. ok = gen_tcp:send(Socket,
  1055. "GET /loop HTTP/1.1\r\n"
  1056. "Host: localhost\r\n"
  1057. "\r\n"),
  1058. timer:sleep(100),
  1059. {error, not_found} = supervisor:get_childspec(Pid, cowboy_http),
  1060. ok.
  1061. supervisor_get_childspec_not_found_h2(Config) ->
  1062. doc("h2: The function supervisor:get_childspec/2 must return {error, not_found}."),
  1063. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  1064. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  1065. %% Do the handshake.
  1066. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  1067. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  1068. ok = ssl:send(Socket, cow_http2:settings_ack()),
  1069. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  1070. timer:sleep(100),
  1071. Pid = get_remote_pid_tls(Socket),
  1072. %% When no children exist.
  1073. {error, not_found} = supervisor:get_childspec(Pid, cowboy_http2),
  1074. %% When a child exists.
  1075. {HeadersBlock, _} = cow_hpack:encode([
  1076. {<<":method">>, <<"GET">>},
  1077. {<<":scheme">>, <<"https">>},
  1078. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1079. {<<":path">>, <<"/loop">>}
  1080. ]),
  1081. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1082. timer:sleep(100),
  1083. {error, not_found} = supervisor:get_childspec(Pid, cowboy_http2),
  1084. ok.
  1085. supervisor_get_childspec_not_found_ws(Config) ->
  1086. doc("ws: The function supervisor:get_childspec/2 must return {error, not_found}."),
  1087. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1088. [binary, {active, false}]),
  1089. ok = gen_tcp:send(Socket,
  1090. "GET /ws HTTP/1.1\r\n"
  1091. "Host: localhost\r\n"
  1092. "Connection: Upgrade\r\n"
  1093. "Origin: http://localhost\r\n"
  1094. "Sec-WebSocket-Version: 13\r\n"
  1095. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  1096. "Upgrade: websocket\r\n"
  1097. "\r\n"),
  1098. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  1099. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  1100. timer:sleep(100),
  1101. Pid = get_remote_pid_tcp(Socket),
  1102. {error, not_found} = supervisor:get_childspec(Pid, cowboy_websocket),
  1103. ok.
  1104. %% supervisor:restart_child/2.
  1105. supervisor_restart_child_not_found_h1(Config) ->
  1106. doc("h1: The function supervisor:restart_child/2 must return {error, not_found}."),
  1107. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1108. [{active, false}]),
  1109. timer:sleep(100),
  1110. Pid = get_remote_pid_tcp(Socket),
  1111. %% When no children exist.
  1112. {error, not_found} = supervisor:restart_child(Pid, cowboy_http),
  1113. %% When a child exists.
  1114. ok = gen_tcp:send(Socket,
  1115. "GET /loop HTTP/1.1\r\n"
  1116. "Host: localhost\r\n"
  1117. "\r\n"),
  1118. timer:sleep(100),
  1119. {error, not_found} = supervisor:restart_child(Pid, cowboy_http),
  1120. ok.
  1121. supervisor_restart_child_not_found_h2(Config) ->
  1122. doc("h2: The function supervisor:restart_child/2 must return {error, not_found}."),
  1123. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  1124. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  1125. %% Do the handshake.
  1126. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  1127. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  1128. ok = ssl:send(Socket, cow_http2:settings_ack()),
  1129. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  1130. timer:sleep(100),
  1131. Pid = get_remote_pid_tls(Socket),
  1132. %% When no children exist.
  1133. {error, not_found} = supervisor:restart_child(Pid, cowboy_http2),
  1134. %% When a child exists.
  1135. {HeadersBlock, _} = cow_hpack:encode([
  1136. {<<":method">>, <<"GET">>},
  1137. {<<":scheme">>, <<"https">>},
  1138. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1139. {<<":path">>, <<"/loop">>}
  1140. ]),
  1141. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1142. timer:sleep(100),
  1143. {error, not_found} = supervisor:restart_child(Pid, cowboy_http2),
  1144. ok.
  1145. supervisor_restart_child_not_found_ws(Config) ->
  1146. doc("ws: The function supervisor:restart_child/2 must return {error, not_found}."),
  1147. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1148. [binary, {active, false}]),
  1149. ok = gen_tcp:send(Socket,
  1150. "GET /ws HTTP/1.1\r\n"
  1151. "Host: localhost\r\n"
  1152. "Connection: Upgrade\r\n"
  1153. "Origin: http://localhost\r\n"
  1154. "Sec-WebSocket-Version: 13\r\n"
  1155. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  1156. "Upgrade: websocket\r\n"
  1157. "\r\n"),
  1158. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  1159. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  1160. timer:sleep(100),
  1161. Pid = get_remote_pid_tcp(Socket),
  1162. {error, not_found} = supervisor:restart_child(Pid, cowboy_websocket),
  1163. ok.
  1164. %% supervisor:start_child/2 must return {error, start_child_disabled}
  1165. supervisor_start_child_not_found_h1(Config) ->
  1166. doc("h1: The function supervisor:start_child/2 must return {error, start_child_disabled}."),
  1167. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1168. [{active, false}]),
  1169. timer:sleep(100),
  1170. Pid = get_remote_pid_tcp(Socket),
  1171. {error, start_child_disabled} = supervisor:start_child(Pid, #{
  1172. id => error,
  1173. start => {error, error, []}
  1174. }),
  1175. ok.
  1176. supervisor_start_child_not_found_h2(Config) ->
  1177. doc("h2: The function supervisor:start_child/2 must return {error, start_child_disabled}."),
  1178. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  1179. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  1180. %% Do the handshake.
  1181. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  1182. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  1183. ok = ssl:send(Socket, cow_http2:settings_ack()),
  1184. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  1185. timer:sleep(100),
  1186. Pid = get_remote_pid_tls(Socket),
  1187. {error, start_child_disabled} = supervisor:start_child(Pid, #{
  1188. id => error,
  1189. start => {error, error, []}
  1190. }),
  1191. ok.
  1192. supervisor_start_child_not_found_ws(Config) ->
  1193. doc("ws: The function supervisor:start_child/2 must return {error, start_child_disabled}."),
  1194. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1195. [binary, {active, false}]),
  1196. ok = gen_tcp:send(Socket,
  1197. "GET /ws HTTP/1.1\r\n"
  1198. "Host: localhost\r\n"
  1199. "Connection: Upgrade\r\n"
  1200. "Origin: http://localhost\r\n"
  1201. "Sec-WebSocket-Version: 13\r\n"
  1202. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  1203. "Upgrade: websocket\r\n"
  1204. "\r\n"),
  1205. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  1206. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  1207. timer:sleep(100),
  1208. Pid = get_remote_pid_tcp(Socket),
  1209. {error, start_child_disabled} = supervisor:start_child(Pid, #{
  1210. id => error,
  1211. start => {error, error, []}
  1212. }),
  1213. ok.
  1214. %% supervisor:terminate_child/2.
  1215. supervisor_terminate_child_not_found_h1(Config) ->
  1216. doc("h1: The function supervisor:terminate_child/2 must return {error, not_found}."),
  1217. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1218. [{active, false}]),
  1219. timer:sleep(100),
  1220. Pid = get_remote_pid_tcp(Socket),
  1221. %% When no children exist.
  1222. {error, not_found} = supervisor:terminate_child(Pid, cowboy_http),
  1223. %% When a child exists.
  1224. ok = gen_tcp:send(Socket,
  1225. "GET /loop HTTP/1.1\r\n"
  1226. "Host: localhost\r\n"
  1227. "\r\n"),
  1228. timer:sleep(100),
  1229. {error, not_found} = supervisor:terminate_child(Pid, cowboy_http),
  1230. ok.
  1231. supervisor_terminate_child_not_found_h2(Config) ->
  1232. doc("h2: The function supervisor:terminate_child/2 must return {error, not_found}."),
  1233. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  1234. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  1235. %% Do the handshake.
  1236. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  1237. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  1238. ok = ssl:send(Socket, cow_http2:settings_ack()),
  1239. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  1240. timer:sleep(100),
  1241. Pid = get_remote_pid_tls(Socket),
  1242. %% When no children exist.
  1243. {error, not_found} = supervisor:terminate_child(Pid, cowboy_http2),
  1244. %% When a child exists.
  1245. {HeadersBlock, _} = cow_hpack:encode([
  1246. {<<":method">>, <<"GET">>},
  1247. {<<":scheme">>, <<"https">>},
  1248. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1249. {<<":path">>, <<"/loop">>}
  1250. ]),
  1251. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1252. timer:sleep(100),
  1253. {error, not_found} = supervisor:terminate_child(Pid, cowboy_http2),
  1254. ok.
  1255. supervisor_terminate_child_not_found_ws(Config) ->
  1256. doc("ws: The function supervisor:terminate_child/2 must return {error, not_found}."),
  1257. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1258. [binary, {active, false}]),
  1259. ok = gen_tcp:send(Socket,
  1260. "GET /ws HTTP/1.1\r\n"
  1261. "Host: localhost\r\n"
  1262. "Connection: Upgrade\r\n"
  1263. "Origin: http://localhost\r\n"
  1264. "Sec-WebSocket-Version: 13\r\n"
  1265. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  1266. "Upgrade: websocket\r\n"
  1267. "\r\n"),
  1268. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  1269. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  1270. timer:sleep(100),
  1271. Pid = get_remote_pid_tcp(Socket),
  1272. {error, not_found} = supervisor:terminate_child(Pid, cowboy_websocket),
  1273. ok.
  1274. %% supervisor:which_children/1.
  1275. %%
  1276. %% @todo The list of modules returned is probably wrong. This will
  1277. %% need to be corrected when get_modules gets implemented.
  1278. supervisor_which_children_h1(Config) ->
  1279. doc("h1: The function supervisor:which_children/1 must work."),
  1280. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1281. [{active, false}]),
  1282. timer:sleep(100),
  1283. Pid = get_remote_pid_tcp(Socket),
  1284. %% No request was sent so there's no children.
  1285. [] = supervisor:which_children(Pid),
  1286. %% Send a request, observe that a children exists.
  1287. ok = gen_tcp:send(Socket,
  1288. "GET /loop HTTP/1.1\r\n"
  1289. "Host: localhost\r\n"
  1290. "\r\n"),
  1291. timer:sleep(100),
  1292. [{cowboy_http, Child, worker, [cowboy_http]}] = supervisor:which_children(Pid),
  1293. true = is_pid(Child),
  1294. ok.
  1295. supervisor_which_children_h2(Config) ->
  1296. doc("h2: The function supervisor:which_children/1 must work."),
  1297. {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
  1298. [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
  1299. %% Do the handshake.
  1300. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  1301. {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
  1302. ok = ssl:send(Socket, cow_http2:settings_ack()),
  1303. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  1304. timer:sleep(100),
  1305. Pid = get_remote_pid_tls(Socket),
  1306. %% No request was sent so there's no children.
  1307. [] = supervisor:which_children(Pid),
  1308. %% Send a request, observe that a children exists.
  1309. {HeadersBlock, _} = cow_hpack:encode([
  1310. {<<":method">>, <<"GET">>},
  1311. {<<":scheme">>, <<"https">>},
  1312. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1313. {<<":path">>, <<"/loop">>}
  1314. ]),
  1315. ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
  1316. timer:sleep(100),
  1317. [{cowboy_http2, Child, worker, [cowboy_http2]}] = supervisor:which_children(Pid),
  1318. true = is_pid(Child),
  1319. ok.
  1320. supervisor_which_children_ws(Config) ->
  1321. doc("ws: The function supervisor:which_children/1 must work. "
  1322. "Websocket connections never have children."),
  1323. {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
  1324. [binary, {active, false}]),
  1325. ok = gen_tcp:send(Socket,
  1326. "GET /ws HTTP/1.1\r\n"
  1327. "Host: localhost\r\n"
  1328. "Connection: Upgrade\r\n"
  1329. "Origin: http://localhost\r\n"
  1330. "Sec-WebSocket-Version: 13\r\n"
  1331. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  1332. "Upgrade: websocket\r\n"
  1333. "\r\n"),
  1334. {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
  1335. {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
  1336. timer:sleep(100),
  1337. Pid = get_remote_pid_tcp(Socket),
  1338. [] = supervisor:which_children(Pid),
  1339. ok.