sys_SUITE.erl 49 KB

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