sys_SUITE.erl 52 KB

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