1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393 |
- -module(sys_SUITE).
- -compile(export_all).
- -compile(nowarn_export_all).
- -import(ct_helper, [config/2]).
- -import(ct_helper, [doc/1]).
- -import(ct_helper, [get_parent_pid/1]).
- -import(ct_helper, [get_remote_pid_tcp/1]).
- -import(ct_helper, [get_remote_pid_tls/1]).
- -import(ct_helper, [is_process_down/1]).
- -import(cowboy_test, [gun_open/1]).
- all() ->
- [{group, sys}].
- groups() ->
- [{sys, [parallel], ct_helper:all(?MODULE)}].
- init_per_suite(Config) ->
- ProtoOpts = #{
- env => #{dispatch => init_dispatch(Config)},
- logger => ?MODULE
- },
-
- {ok, _} = cowboy:start_clear(clear, [{port, 0}], ProtoOpts),
- ClearPort = ranch:get_port(clear),
-
- TLSOpts = ct_helper:get_certs_from_ets(),
- {ok, _} = cowboy:start_tls(tls, TLSOpts ++ [{port, 0}], ProtoOpts),
- TLSPort = ranch:get_port(tls),
- [
- {clear_port, ClearPort},
-
- {tls_opts, TLSOpts},
- {tls_port, TLSPort}
- |Config].
- end_per_suite(_) ->
- ok = cowboy:stop_listener(clear),
- ok = cowboy:stop_listener(tls).
- init_dispatch(_) ->
- cowboy_router:compile([{"[...]", [
- {"/", hello_h, []},
- {"/loop", long_polling_sys_h, []},
- {"/ws", ws_echo, []}
- ]}]).
- error(Format, Args) ->
- error_logger:error_msg(Format, Args).
- warning("Received EXIT signal " ++ _, [{'EXIT', _, {shutdown, ?MODULE}}|_]) ->
- ok;
- warning(Format, Args) ->
- error_logger:warning_msg(Format, Args).
- proc_lib_initial_call_clear(Config) ->
- doc("Confirm that clear connection processes are started using proc_lib."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {cowboy_clear, _, _} = proc_lib:initial_call(Pid),
- ok.
- proc_lib_initial_call_tls(Config) ->
- doc("Confirm that TLS connection processes are started using proc_lib."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config), config(tls_opts, Config)),
- timer:sleep(100),
- Pid = get_remote_pid_tls(Socket),
- {cowboy_tls, _, _} = proc_lib:initial_call(Pid),
- ok.
- bad_system_from_h1(Config) ->
- doc("h1: Sending a system message with a bad From value results in a process crash."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- ct_helper_error_h:ignore(Pid, gen, reply, 2),
- Pid ! {system, bad, get_state},
- {error, closed} = gen_tcp:recv(Socket, 0, 1000),
- false = is_process_alive(Pid),
- ok.
- bad_system_from_h2(Config) ->
- doc("h2: Sending a system message with a bad From value results in a process crash."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-
- {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
- timer:sleep(100),
- Pid = get_remote_pid_tls(Socket),
- ct_helper_error_h:ignore(Pid, gen, reply, 2),
- Pid ! {system, bad, get_state},
- {error, closed} = ssl:recv(Socket, 0, 1000),
- false = is_process_alive(Pid),
- ok.
- bad_system_from_ws(Config) ->
- doc("ws: Sending a system message with a bad From value results in a process crash."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- ct_helper_error_h:ignore(Pid, gen, reply, 2),
- Pid ! {system, bad, get_state},
- {error, closed} = gen_tcp:recv(Socket, 0, 1000),
- false = is_process_alive(Pid),
- ok.
- bad_system_from_loop(Config) ->
- doc("loop: Sending a system message with a bad From value results in a process crash."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- SupPid = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(SupPid),
- ct_helper_error_h:ignore(Pid, gen, reply, 2),
- Pid ! {system, bad, get_state},
- {ok, "HTTP/1.1 500 "} = gen_tcp:recv(Socket, 13, 1000),
- false = is_process_alive(Pid),
- ok.
- bad_system_message_h1(Config) ->
- doc("h1: Sending a system message with a bad Request value results in an error."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- Ref = make_ref(),
- Pid ! {system, {self(), Ref}, hello},
- receive
- {Ref, {error, {unknown_system_msg, hello}}} ->
- ok
- after 1000 ->
- error(timeout)
- end.
- bad_system_message_h2(Config) ->
- doc("h2: Sending a system message with a bad Request value results in an error."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-
- {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
- timer:sleep(100),
- Pid = get_remote_pid_tls(Socket),
- Ref = make_ref(),
- Pid ! {system, {self(), Ref}, hello},
- receive
- {Ref, {error, {unknown_system_msg, hello}}} ->
- ok
- after 1000 ->
- error(timeout)
- end.
- bad_system_message_ws(Config) ->
- doc("ws: Sending a system message with a bad Request value results in an error."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- Ref = make_ref(),
- Pid ! {system, {self(), Ref}, hello},
- receive
- {Ref, {error, {unknown_system_msg, hello}}} ->
- ok
- after 1000 ->
- error(timeout)
- end.
- bad_system_message_loop(Config) ->
- doc("loop: Sending a system message with a bad Request value results in an error."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- SupPid = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(SupPid),
- Ref = make_ref(),
- Pid ! {system, {self(), Ref}, hello},
- receive
- {Ref, {error, {unknown_system_msg, hello}}} ->
- ok
- after 1000 ->
- error(timeout)
- end.
- good_system_message_h1(Config) ->
- doc("h1: System messages are handled properly."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- Ref = make_ref(),
- Pid ! {system, {self(), Ref}, get_state},
- receive
- {Ref, Result} when element(1, Result) =/= error ->
- ok
- after 1000 ->
- error(timeout)
- end.
- good_system_message_h2(Config) ->
- doc("h2: System messages are handled properly."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-
- {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
- timer:sleep(100),
- Pid = get_remote_pid_tls(Socket),
- Ref = make_ref(),
- Pid ! {system, {self(), Ref}, get_state},
- receive
- {Ref, Result} when element(1, Result) =/= error ->
- ok
- after 1000 ->
- error(timeout)
- end.
- good_system_message_ws(Config) ->
- doc("ws: System messages are handled properly."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- Ref = make_ref(),
- Pid ! {system, {self(), Ref}, get_state},
- receive
- {Ref, Result} when element(1, Result) =/= error ->
- ok
- after 1000 ->
- error(timeout)
- end.
- good_system_message_loop(Config) ->
- doc("loop: System messages are handled properly."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- SupPid = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(SupPid),
- Ref = make_ref(),
- Pid ! {system, {self(), Ref}, get_state},
- receive
- {Ref, Result} when element(1, Result) =/= error ->
- ok
- after 1000 ->
- error(timeout)
- end.
- trap_exit_parent_exit_h1(Config) ->
- doc("h1: A process trapping exits must stop when receiving "
- "an 'EXIT' message from its parent."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- Parent = get_parent_pid(Pid),
- Pid ! {'EXIT', Parent, {shutdown, ?MODULE}},
- {error, closed} = gen_tcp:recv(Socket, 0, 1000),
- true = is_process_down(Pid),
- ok.
- trap_exit_parent_exit_h2(Config) ->
- doc("h2: A process trapping exits must stop when receiving "
- "an 'EXIT' message from its parent."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-
- {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
- timer:sleep(100),
- Pid = get_remote_pid_tls(Socket),
- Parent = get_parent_pid(Pid),
- Pid ! {'EXIT', Parent, {shutdown, ?MODULE}},
- {error, closed} = ssl:recv(Socket, 0, 1000),
- true = is_process_down(Pid),
- ok.
- trap_exit_parent_exit_ws(Config) ->
- doc("ws: A process trapping exits must stop when receiving "
- "an 'EXIT' message from its parent."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- Parent = get_parent_pid(Pid),
- Pid ! {'EXIT', Parent, {shutdown, ?MODULE}},
- {error, closed} = gen_tcp:recv(Socket, 0, 1000),
- true = is_process_down(Pid),
- ok.
- trap_exit_parent_exit_loop(Config) ->
- doc("loop: A process trapping exits must stop when receiving "
- "an 'EXIT' message from its parent."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- Parent = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(Parent),
- Pid ! {'EXIT', Parent, {shutdown, ?MODULE}},
-
- {ok, "HTTP/1.1 204 "} = gen_tcp:recv(Socket, 13, 1000),
- true = is_process_down(Pid),
- ok.
- trap_exit_other_exit_h1(Config) ->
- doc("h1: A process trapping exits must ignore "
- "'EXIT' messages from unknown processes."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- Pid ! {'EXIT', self(), {shutdown, ?MODULE}},
- ok = gen_tcp:send(Socket,
- "GET / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {ok, "HTTP/1.1 200 "} = gen_tcp:recv(Socket, 13, 1000),
- true = is_process_alive(Pid),
- ok.
- trap_exit_other_exit_h2(Config) ->
- doc("h2: A process trapping exits must ignore "
- "'EXIT' messages from unknown processes."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
- do_http2_handshake(Socket),
- Pid = get_remote_pid_tls(Socket),
- Pid ! {'EXIT', self(), {shutdown, ?MODULE}},
-
- {HeadersBlock, _} = cow_hpack:encode([
- {<<":method">>, <<"GET">>},
- {<<":scheme">>, <<"https">>},
- {<<":authority">>, <<"localhost">>},
- {<<":path">>, <<"/">>}
- ]),
- ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
-
- {ok, << _:24, 1:8, _:40 >>} = ssl:recv(Socket, 9, 6000),
- true = is_process_alive(Pid),
- ok.
- trap_exit_other_exit_ws(Config) ->
- doc("ws: A process trapping exits must ignore "
- "'EXIT' messages from unknown processes."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- Pid ! {'EXIT', self(), {shutdown, ?MODULE}},
-
- {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
- true = is_process_alive(Pid),
- ok.
- trap_exit_other_exit_loop(Config) ->
- doc("loop: A process trapping exits must ignore "
- "'EXIT' messages from unknown processes."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- Parent = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(Parent),
- Pid ! {'EXIT', self(), {shutdown, ?MODULE}},
-
- {ok, "HTTP/1.1 299 "} = gen_tcp:recv(Socket, 13, 1000),
- true = is_process_alive(Pid),
- ok.
- sys_change_code_h1(Config) ->
- doc("h1: The sys:change_code/4 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- ok = sys:suspend(Pid),
- ok = gen_tcp:send(Socket,
- "GET / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, timeout} = gen_tcp:recv(Socket, 13, 500),
- ok = sys:change_code(Pid, cowboy_http, undefined, undefined),
- ok = sys:resume(Pid),
- {ok, "HTTP/1.1 200 "} = gen_tcp:recv(Socket, 13, 500),
- ok.
- sys_change_code_h2(Config) ->
- doc("h2: The sys:change_code/4 function works as expected."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
- do_http2_handshake(Socket),
- Pid = get_remote_pid_tls(Socket),
-
-
- ok = sys:suspend(Pid),
- {HeadersBlock, _} = cow_hpack:encode([
- {<<":method">>, <<"GET">>},
- {<<":scheme">>, <<"http">>},
- {<<":authority">>, <<"localhost">>},
- {<<":path">>, <<"/">>}
- ]),
- ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
-
- {error, timeout} = ssl:recv(Socket, 9, 500),
- ok = sys:change_code(Pid, cowboy_http2, undefined, undefined),
- ok = sys:resume(Pid),
- {ok, << _:24, 1:8, _:40 >>} = ssl:recv(Socket, 9, 6000),
- ok.
- sys_change_code_ws(Config) ->
- doc("ws: The sys:change_code/4 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- ok = sys:suspend(Pid),
- Mask = 16#37fa213d,
- MaskedHello = ws_SUITE:do_mask(<<"Hello">>, Mask, <<>>),
- ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
- {error, timeout} = gen_tcp:recv(Socket, 0, 500),
- ok = sys:change_code(Pid, cowboy_websocket, undefined, undefined),
- ok = sys:resume(Pid),
- {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
- ok.
- sys_change_code_loop(Config) ->
- doc("loop: The sys:change_code/4 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- SupPid = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(SupPid),
-
-
- ok = sys:suspend(Pid),
- {error, timeout} = gen_tcp:recv(Socket, 13, 1000),
- ok = sys:change_code(Pid, cowboy_loop, undefined, undefined),
- ok = sys:resume(Pid),
- {ok, "HTTP/1.1 299 "} = gen_tcp:recv(Socket, 13, 500),
- ok.
- sys_get_state_h1(Config) ->
- doc("h1: The sys:get_state/1 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- State = sys:get_state(Pid),
- state = element(1, State),
- ok.
- sys_get_state_h2(Config) ->
- doc("h2: The sys:get_state/1 function works as expected."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-
- {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
- timer:sleep(100),
- Pid = get_remote_pid_tls(Socket),
- {State, Buffer} = sys:get_state(Pid),
- state = element(1, State),
- true = is_binary(Buffer),
- ok.
- sys_get_state_ws(Config) ->
- doc("ws: The sys:get_state/1 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {State, undefined, ParseState} = sys:get_state(Pid),
- state = element(1, State),
- case element(1, ParseState) of
- ps_header -> ok;
- ps_payload -> ok
- end.
- sys_get_state_loop(Config) ->
- doc("loop: The sys:get_state/1 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- SupPid = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(SupPid),
- {Req, Env, long_polling_sys_h, undefined} = sys:get_state(Pid),
- #{pid := _, streamid := _} = Req,
- #{dispatch := _} = Env,
- ok.
- sys_get_status_h1(Config) ->
- doc("h1: The sys:get_status/1 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {status, Pid, {module, cowboy_http}, _} = sys:get_status(Pid),
- ok.
- sys_get_status_h2(Config) ->
- doc("h2: The sys:get_status/1 function works as expected."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-
- {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
- timer:sleep(100),
- Pid = get_remote_pid_tls(Socket),
- {status, Pid, {module, cowboy_http2}, _} = sys:get_status(Pid),
- ok.
- sys_get_status_ws(Config) ->
- doc("ws: The sys:get_status/1 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {status, Pid, {module, cowboy_websocket}, _} = sys:get_status(Pid),
- ok.
- sys_get_status_loop(Config) ->
- doc("loop: The sys:get_status/1 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- SupPid = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(SupPid),
- {status, Pid, {module, cowboy_loop}, _} = sys:get_status(Pid),
- ok.
- sys_replace_state_h1(Config) ->
- doc("h1: The sys:replace_state/2 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- State = sys:replace_state(Pid, fun(S) -> S end),
- state = element(1, State),
- ok.
- sys_replace_state_h2(Config) ->
- doc("h2: The sys:replace_state/2 function works as expected."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-
- {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
- timer:sleep(100),
- Pid = get_remote_pid_tls(Socket),
- {State, Buffer} = sys:replace_state(Pid, fun(S) -> S end),
- state = element(1, State),
- true = is_binary(Buffer),
- ok.
- sys_replace_state_ws(Config) ->
- doc("ws: The sys:replace_state/2 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {State, undefined, ParseState} = sys:replace_state(Pid, fun(S) -> S end),
- state = element(1, State),
- case element(1, ParseState) of
- ps_header -> ok;
- ps_payload -> ok
- end.
- sys_replace_state_loop(Config) ->
- doc("loop: The sys:replace_state/2 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- SupPid = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(SupPid),
- {Req, Env, long_polling_sys_h, undefined} = sys:replace_state(Pid, fun(S) -> S end),
- #{pid := _, streamid := _} = Req,
- #{dispatch := _} = Env,
- ok.
- sys_suspend_and_resume_h1(Config) ->
- doc("h1: The sys:suspend/1 and sys:resume/1 functions work as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- ok = sys:suspend(Pid),
- ok = gen_tcp:send(Socket,
- "GET / HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- {error, timeout} = gen_tcp:recv(Socket, 13, 500),
- ok = sys:resume(Pid),
- {ok, "HTTP/1.1 200 "} = gen_tcp:recv(Socket, 13, 500),
- ok.
- sys_suspend_and_resume_h2(Config) ->
- doc("h2: The sys:suspend/1 and sys:resume/1 functions work as expected."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
- do_http2_handshake(Socket),
- Pid = get_remote_pid_tls(Socket),
-
-
- ok = sys:suspend(Pid),
- {HeadersBlock, _} = cow_hpack:encode([
- {<<":method">>, <<"GET">>},
- {<<":scheme">>, <<"http">>},
- {<<":authority">>, <<"localhost">>},
- {<<":path">>, <<"/">>}
- ]),
- ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
-
- {error, timeout} = ssl:recv(Socket, 9, 500),
- ok = sys:resume(Pid),
- {ok, << _:24, 1:8, _:40 >>} = ssl:recv(Socket, 9, 6000),
- ok.
- sys_suspend_and_resume_ws(Config) ->
- doc("ws: The sys:suspend/1 and sys:resume/1 functions work as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- ok = sys:suspend(Pid),
- Mask = 16#37fa213d,
- MaskedHello = ws_SUITE:do_mask(<<"Hello">>, Mask, <<>>),
- ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
- {error, timeout} = gen_tcp:recv(Socket, 0, 500),
- ok = sys:resume(Pid),
- {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
- ok.
- sys_suspend_and_resume_loop(Config) ->
- doc("loop: The sys:suspend/1 and sys:resume/1 functions work as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- SupPid = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(SupPid),
-
-
- ok = sys:suspend(Pid),
- {error, timeout} = gen_tcp:recv(Socket, 13, 1000),
- ok = sys:resume(Pid),
- {ok, "HTTP/1.1 299 "} = gen_tcp:recv(Socket, 13, 500),
- ok.
- sys_terminate_h1(Config) ->
- doc("h1: The sys:terminate/2,3 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- ok = sys:terminate(Pid, {shutdown, ?MODULE}),
- {error, closed} = gen_tcp:recv(Socket, 0, 500),
- ok.
- sys_terminate_h2(Config) ->
- doc("h2: The sys:terminate/2,3 function works as expected."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
-
- {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
- timer:sleep(100),
- Pid = get_remote_pid_tls(Socket),
- ok = sys:terminate(Pid, {shutdown, ?MODULE}),
- {error, closed} = ssl:recv(Socket, 0, 500),
- ok.
- sys_terminate_ws(Config) ->
- doc("ws: The sys:terminate/2,3 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- ok = sys:terminate(Pid, {shutdown, ?MODULE}),
- {error, closed} = gen_tcp:recv(Socket, 0, 500),
- ok.
- sys_terminate_loop(Config) ->
- doc("loop: The sys:terminate/2,3 function works as expected."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config), [{active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- SupPid = get_remote_pid_tcp(Socket),
- [{_, Pid, _, _}] = supervisor:which_children(SupPid),
-
- ok = sys:terminate(Pid, {shutdown, ?MODULE}),
- {ok, "HTTP/1.1 204 "} = gen_tcp:recv(Socket, 13, 500),
- ok.
- supervisor_count_children_h1(Config) ->
- doc("h1: The function supervisor:count_children/1 must work."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
-
- Counts1 = supervisor:count_children(Pid),
- 1 = proplists:get_value(specs, Counts1),
- 0 = proplists:get_value(active, Counts1),
- 0 = proplists:get_value(supervisors, Counts1),
- 0 = proplists:get_value(workers, Counts1),
-
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- Counts2 = supervisor:count_children(Pid),
- 1 = proplists:get_value(specs, Counts2),
- 1 = proplists:get_value(active, Counts2),
- 0 = proplists:get_value(supervisors, Counts2),
- 1 = proplists:get_value(workers, Counts2),
- ok.
- supervisor_count_children_h2(Config) ->
- doc("h2: The function supervisor:count_children/1 must work."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
- do_http2_handshake(Socket),
- Pid = get_remote_pid_tls(Socket),
-
- Counts1 = supervisor:count_children(Pid),
- 1 = proplists:get_value(specs, Counts1),
- 0 = proplists:get_value(active, Counts1),
- 0 = proplists:get_value(supervisors, Counts1),
- 0 = proplists:get_value(workers, Counts1),
-
- {HeadersBlock, _} = cow_hpack:encode([
- {<<":method">>, <<"GET">>},
- {<<":scheme">>, <<"https">>},
- {<<":authority">>, <<"localhost">>},
- {<<":path">>, <<"/loop">>}
- ]),
- ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
- timer:sleep(100),
- Counts2 = supervisor:count_children(Pid),
- 1 = proplists:get_value(specs, Counts2),
- 1 = proplists:get_value(active, Counts2),
- 0 = proplists:get_value(supervisors, Counts2),
- 1 = proplists:get_value(workers, Counts2),
- ok.
- supervisor_count_children_ws(Config) ->
- doc("ws: The function supervisor:count_children/1 must work. "
- "Websocket connections never have children."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- Counts = supervisor:count_children(Pid),
- 1 = proplists:get_value(specs, Counts),
- 0 = proplists:get_value(active, Counts),
- 0 = proplists:get_value(supervisors, Counts),
- 0 = proplists:get_value(workers, Counts),
- ok.
- supervisor_delete_child_not_found_h1(Config) ->
- doc("h1: The function supervisor:delete_child/2 must return {error, not_found}."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
-
- {error, not_found} = supervisor:delete_child(Pid, cowboy_http),
-
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- {error, not_found} = supervisor:delete_child(Pid, cowboy_http),
- ok.
- supervisor_delete_child_not_found_h2(Config) ->
- doc("h2: The function supervisor:delete_child/2 must return {error, not_found}."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
- do_http2_handshake(Socket),
- Pid = get_remote_pid_tls(Socket),
-
- {error, not_found} = supervisor:delete_child(Pid, cowboy_http2),
-
- {HeadersBlock, _} = cow_hpack:encode([
- {<<":method">>, <<"GET">>},
- {<<":scheme">>, <<"https">>},
- {<<":authority">>, <<"localhost">>},
- {<<":path">>, <<"/loop">>}
- ]),
- ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
- timer:sleep(100),
- {error, not_found} = supervisor:delete_child(Pid, cowboy_http2),
- ok.
- supervisor_delete_child_not_found_ws(Config) ->
- doc("ws: The function supervisor:delete_child/2 must return {error, not_found}."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {error, not_found} = supervisor:delete_child(Pid, cowboy_websocket),
- ok.
- supervisor_get_childspec_not_found_h1(Config) ->
- doc("h1: The function supervisor:get_childspec/2 must return {error, not_found}."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
-
- {error, not_found} = supervisor:get_childspec(Pid, cowboy_http),
-
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- {error, not_found} = supervisor:get_childspec(Pid, cowboy_http),
- ok.
- supervisor_get_childspec_not_found_h2(Config) ->
- doc("h2: The function supervisor:get_childspec/2 must return {error, not_found}."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
- do_http2_handshake(Socket),
- Pid = get_remote_pid_tls(Socket),
-
- {error, not_found} = supervisor:get_childspec(Pid, cowboy_http2),
-
- {HeadersBlock, _} = cow_hpack:encode([
- {<<":method">>, <<"GET">>},
- {<<":scheme">>, <<"https">>},
- {<<":authority">>, <<"localhost">>},
- {<<":path">>, <<"/loop">>}
- ]),
- ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
- timer:sleep(100),
- {error, not_found} = supervisor:get_childspec(Pid, cowboy_http2),
- ok.
- supervisor_get_childspec_not_found_ws(Config) ->
- doc("ws: The function supervisor:get_childspec/2 must return {error, not_found}."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {error, not_found} = supervisor:get_childspec(Pid, cowboy_websocket),
- ok.
- supervisor_restart_child_not_found_h1(Config) ->
- doc("h1: The function supervisor:restart_child/2 must return {error, not_found}."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
-
- {error, not_found} = supervisor:restart_child(Pid, cowboy_http),
-
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- {error, not_found} = supervisor:restart_child(Pid, cowboy_http),
- ok.
- supervisor_restart_child_not_found_h2(Config) ->
- doc("h2: The function supervisor:restart_child/2 must return {error, not_found}."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
- do_http2_handshake(Socket),
- Pid = get_remote_pid_tls(Socket),
-
- {error, not_found} = supervisor:restart_child(Pid, cowboy_http2),
-
- {HeadersBlock, _} = cow_hpack:encode([
- {<<":method">>, <<"GET">>},
- {<<":scheme">>, <<"https">>},
- {<<":authority">>, <<"localhost">>},
- {<<":path">>, <<"/loop">>}
- ]),
- ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
- timer:sleep(100),
- {error, not_found} = supervisor:restart_child(Pid, cowboy_http2),
- ok.
- supervisor_restart_child_not_found_ws(Config) ->
- doc("ws: The function supervisor:restart_child/2 must return {error, not_found}."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {error, not_found} = supervisor:restart_child(Pid, cowboy_websocket),
- ok.
- supervisor_start_child_not_found_h1(Config) ->
- doc("h1: The function supervisor:start_child/2 must return {error, start_child_disabled}."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {error, start_child_disabled} = supervisor:start_child(Pid, #{
- id => error,
- start => {error, error, []}
- }),
- ok.
- supervisor_start_child_not_found_h2(Config) ->
- doc("h2: The function supervisor:start_child/2 must return {error, start_child_disabled}."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
- do_http2_handshake(Socket),
- Pid = get_remote_pid_tls(Socket),
- {error, start_child_disabled} = supervisor:start_child(Pid, #{
- id => error,
- start => {error, error, []}
- }),
- ok.
- supervisor_start_child_not_found_ws(Config) ->
- doc("ws: The function supervisor:start_child/2 must return {error, start_child_disabled}."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {error, start_child_disabled} = supervisor:start_child(Pid, #{
- id => error,
- start => {error, error, []}
- }),
- ok.
- supervisor_terminate_child_not_found_h1(Config) ->
- doc("h1: The function supervisor:terminate_child/2 must return {error, not_found}."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
-
- {error, not_found} = supervisor:terminate_child(Pid, cowboy_http),
-
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- {error, not_found} = supervisor:terminate_child(Pid, cowboy_http),
- ok.
- supervisor_terminate_child_not_found_h2(Config) ->
- doc("h2: The function supervisor:terminate_child/2 must return {error, not_found}."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
- do_http2_handshake(Socket),
- Pid = get_remote_pid_tls(Socket),
-
- {error, not_found} = supervisor:terminate_child(Pid, cowboy_http2),
-
- {HeadersBlock, _} = cow_hpack:encode([
- {<<":method">>, <<"GET">>},
- {<<":scheme">>, <<"https">>},
- {<<":authority">>, <<"localhost">>},
- {<<":path">>, <<"/loop">>}
- ]),
- ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
- timer:sleep(100),
- {error, not_found} = supervisor:terminate_child(Pid, cowboy_http2),
- ok.
- supervisor_terminate_child_not_found_ws(Config) ->
- doc("ws: The function supervisor:terminate_child/2 must return {error, not_found}."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- {error, not_found} = supervisor:terminate_child(Pid, cowboy_websocket),
- ok.
- supervisor_which_children_h1(Config) ->
- doc("h1: The function supervisor:which_children/1 must work."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [{active, false}]),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
-
- [] = supervisor:which_children(Pid),
-
- ok = gen_tcp:send(Socket,
- "GET /loop HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "\r\n"),
- timer:sleep(100),
- [{cowboy_http, Child, worker, [cowboy_http]}] = supervisor:which_children(Pid),
- true = is_pid(Child),
- ok.
- supervisor_which_children_h2(Config) ->
- doc("h2: The function supervisor:which_children/1 must work."),
- {ok, Socket} = ssl:connect("localhost", config(tls_port, Config),
- [{active, false}, binary, {alpn_advertised_protocols, [<<"h2">>]}]),
- do_http2_handshake(Socket),
- Pid = get_remote_pid_tls(Socket),
-
- [] = supervisor:which_children(Pid),
-
- {HeadersBlock, _} = cow_hpack:encode([
- {<<":method">>, <<"GET">>},
- {<<":scheme">>, <<"https">>},
- {<<":authority">>, <<"localhost">>},
- {<<":path">>, <<"/loop">>}
- ]),
- ok = ssl:send(Socket, cow_http2:headers(1, fin, HeadersBlock)),
- timer:sleep(100),
- [{cowboy_http2, Child, worker, [cowboy_http2]}] = supervisor:which_children(Pid),
- true = is_pid(Child),
- ok.
- supervisor_which_children_ws(Config) ->
- doc("ws: The function supervisor:which_children/1 must work. "
- "Websocket connections never have children."),
- {ok, Socket} = gen_tcp:connect("localhost", config(clear_port, Config),
- [binary, {active, false}]),
- ok = gen_tcp:send(Socket,
- "GET /ws HTTP/1.1\r\n"
- "Host: localhost\r\n"
- "Connection: Upgrade\r\n"
- "Origin: http://localhost\r\n"
- "Sec-WebSocket-Version: 13\r\n"
- "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
- "Upgrade: websocket\r\n"
- "\r\n"),
- {ok, Handshake} = gen_tcp:recv(Socket, 0, 5000),
- {ok, {http_response, {1, 1}, 101, _}, _} = erlang:decode_packet(http, Handshake, []),
- timer:sleep(100),
- Pid = get_remote_pid_tcp(Socket),
- [] = supervisor:which_children(Pid),
- ok.
- do_http2_handshake(Socket) ->
- ok = ssl:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"),
- {ok, <<_,_,_,4,_/bits>>} = ssl:recv(Socket, 0, 1000),
- ok = ssl:send(Socket, [cow_http2:settings(#{}), cow_http2:settings_ack()]),
- {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
- ok.
|