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