diff options
26 files changed, 806 insertions, 693 deletions
@@ -7,7 +7,8 @@ PROJECT = cowboy ERLC_OPTS ?= -Werror +debug_info +warn_export_all +warn_export_vars \ +warn_shadow_vars +warn_obsolete_guard +warn_missing_spec COMPILE_FIRST = cowboy_middleware cowboy_sub_protocol -CT_SUITES = eunit http spdy ws +CT_SUITES = eunit http loop_handler spdy ws +CT_OPTS += -pa test -ct_hooks cowboy_ct_hook [] PLT_APPS = crypto public_key ssl # Dependencies. @@ -23,11 +24,3 @@ dep_gun = pkg://gun master # Standard targets. include erlang.mk - -# Extra targets. - -.PHONY: autobahn - -autobahn: clean clean-deps deps app build-tests - @mkdir -p logs/ - @$(CT_RUN) -suite autobahn_SUITE @@ -1,7 +1,7 @@ #!/bin/sh KERL_INSTALL_PATH=~/erlang -KERL_RELEASES="r15b01 r15b02 r15b03 r16b r16b01 r16b02 r16b03-1 17.0 17.0_native maint master" +KERL_RELEASES="r15b01 r15b02 r15b03 r16b r16b01 r16b02 r16b03-1 17.0 17.0_native" make build-tests diff --git a/guide/req.md b/guide/req.md index 2b59152..f93623a 100644 --- a/guide/req.md +++ b/guide/req.md @@ -62,8 +62,10 @@ or `{error, atom()}`. This includes the following functions: also includes the `chunk/2` function which always returns `ok`. -The final group modifies the Req object, so it always return -a new `Req`. It includes the following functions: `compact/1`, +The final group modifies the Req object state without +performing any immediate operations. As these functions +can't fail, they always return a new `Req` directly. +This includes the following functions: `compact/1`, `delete_resp_header/2`, `set_meta/3`, `set_resp_body/2`, `set_resp_body_fun/{2,3}`, `set_resp_cookie/4`, `set_resp_header/3`. diff --git a/src/cowboy_http.erl b/src/cowboy_http.erl index 754e74a..1cf73bf 100644 --- a/src/cowboy_http.erl +++ b/src/cowboy_http.erl @@ -241,7 +241,7 @@ language_tag(Data, Fun) -> -spec language_subtag(binary(), fun(), binary(), [binary()]) -> any(). language_subtag(Data, Fun, Tag, Acc) -> - alpha(Data, + alphanumeric(Data, fun (_Rest, SubTag) when byte_size(SubTag) =:= 0; byte_size(SubTag) > 8 -> {error, badarg}; (<< $-, Rest/binary >>, SubTag) -> @@ -615,6 +615,23 @@ alpha(<< C, Rest/binary >>, Fun, Acc) alpha(Data, Fun, Acc) -> Fun(Data, Acc). +-spec alphanumeric(binary(), fun()) -> any(). +alphanumeric(Data, Fun) -> + alphanumeric(Data, Fun, <<>>). + +-spec alphanumeric(binary(), fun(), binary()) -> any(). +alphanumeric(<<>>, Fun, Acc) -> + Fun(<<>>, Acc); +alphanumeric(<< C, Rest/binary >>, Fun, Acc) + when C >= $a andalso C =< $z; + C >= $A andalso C =< $Z; + C >= $0 andalso C =< $9 -> + C2 = cowboy_bstr:char_to_lower(C), + alphanumeric(Rest, Fun, << Acc/binary, C2 >>); +alphanumeric(Data, Fun, Acc) -> + Fun(Data, Acc). + +%% @doc Parse either a token or a quoted string. -spec word(binary(), fun()) -> any(). word(Data = << $", _/binary >>, Fun) -> quoted_string(Data, Fun); @@ -865,12 +882,13 @@ nonempty_language_range_list_test_() -> {<<"en-gb">>, 800}, {<<"en">>, 700} ]}, - {<<"en, en-US, en-cockney, i-cherokee, x-pig-latin">>, [ + {<<"en, en-US, en-cockney, i-cherokee, x-pig-latin, es-419">>, [ {<<"en">>, 1000}, {<<"en-us">>, 1000}, {<<"en-cockney">>, 1000}, {<<"i-cherokee">>, 1000}, - {<<"x-pig-latin">>, 1000} + {<<"x-pig-latin">>, 1000}, + {<<"es-419">>, 1000} ]} ], [{V, fun() -> R = nonempty_list(V, fun language_range/2) end} diff --git a/src/cowboy_spdy.erl b/src/cowboy_spdy.erl index e5aeb21..ce75419 100644 --- a/src/cowboy_spdy.erl +++ b/src/cowboy_spdy.erl @@ -33,9 +33,11 @@ %% Internal transport functions. -export([name/0]). +-export([messages/0]). -export([recv/3]). -export([send/2]). -export([sendfile/2]). +-export([setopts/2]). -type streamid() :: non_neg_integer(). -type socket() :: {pid(), streamid()}. @@ -45,8 +47,8 @@ pid :: pid(), input = nofin :: fin | nofin, in_buffer = <<>> :: binary(), - is_recv = false :: {true, {non_neg_integer(), pid()}, - pid(), non_neg_integer(), reference()} | false, + is_recv = false :: false | {active, socket(), pid()} + | {passive, socket(), pid(), non_neg_integer(), reference()}, output = nofin :: fin | nofin }). @@ -138,15 +140,15 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, TRef = erlang:send_after(Timeout, self(), {recv_timeout, FromSocket}), loop(replace_child(Child#child{ - is_recv={true, FromSocket, FromPid, Length, TRef}}, + is_recv={passive, FromSocket, FromPid, Length, TRef}}, State)) end; {recv_timeout, {Pid, StreamID}} when Pid =:= self() -> - Child = #child{is_recv={true, FromSocket, FromPid, _, _}} + Child = #child{is_recv={passive, FromSocket, FromPid, _, _}} = get_child(StreamID, State), FromPid ! {recv, FromSocket, {error, timeout}}, - loop(replace_child(Child#child{is_recv=false}, State)); + loop(replace_child(Child#child{is_recv=passive}, State)); {reply, {Pid, StreamID}, Status, Headers} when Pid =:= self() -> Child = #child{output=nofin} = get_child(StreamID, State), @@ -178,6 +180,22 @@ loop(State=#state{parent=Parent, socket=Socket, transport=Transport, Child = #child{output=nofin} = get_child(StreamID, State), data_from_file(State, StreamID, Filepath), loop(replace_child(Child#child{output=fin}, State)); + {active, FromSocket = {Pid, StreamID}, FromPid} when Pid =:= self() -> + Child = #child{in_buffer=InBuffer, is_recv=false} + = get_child(StreamID, State), + case InBuffer of + <<>> -> + loop(replace_child(Child#child{ + is_recv={active, FromSocket, FromPid}}, State)); + _ -> + FromPid ! {spdy, FromSocket, InBuffer}, + loop(replace_child(Child#child{in_buffer= <<>>}, State)) + end; + {passive, FromSocket = {Pid, StreamID}, FromPid} when Pid =:= self() -> + Child = #child{is_recv=IsRecv} = get_child(StreamID, State), + %% Make sure we aren't in the middle of a recv call. + case IsRecv of false -> ok; {active, FromSocket, FromPid} -> ok end, + loop(replace_child(Child#child{is_recv=false}, State)); {'EXIT', Parent, Reason} -> exit(Reason); {'EXIT', Pid, _} -> @@ -262,11 +280,14 @@ handle_frame(State, {data, StreamID, IsFin, Data}) -> Data2 = << Buffer/binary, Data/binary >>, IsFin2 = if IsFin -> fin; true -> nofin end, Child2 = case IsRecv of - {true, FromSocket, FromPid, 0, TRef} -> + {active, FromSocket, FromPid} -> + FromPid ! {spdy, FromSocket, Data}, + Child#child{input=IsFin2, is_recv=false}; + {passive, FromSocket, FromPid, 0, TRef} -> FromPid ! {recv, FromSocket, {ok, Data2}}, cancel_recv_timeout(StreamID, TRef), Child#child{input=IsFin2, in_buffer= <<>>, is_recv=false}; - {true, FromSocket, FromPid, Length, TRef} + {passive, FromSocket, FromPid, Length, TRef} when byte_size(Data2) >= Length -> << Data3:Length/binary, Rest/binary >> = Data2, FromPid ! {recv, FromSocket, {ok, Data3}}, @@ -443,6 +464,10 @@ stream_close(Socket = {Pid, _}) -> name() -> spdy. +-spec messages() -> {spdy, spdy_closed, spdy_error}. +messages() -> + {spdy, spdy_closed, spdy_error}. + -spec recv(socket(), non_neg_integer(), timeout()) -> {ok, binary()} | {error, timeout}. recv(Socket = {Pid, _}, Length, Timeout) -> @@ -463,3 +488,11 @@ send(Socket, Data) -> sendfile(Socket = {Pid, _}, Filepath) -> _ = Pid ! {sendfile, Socket, Filepath}, {ok, undefined}. + +-spec setopts(inet:socket(), list()) -> ok. +setopts(Socket = {Pid, _}, [{active, once}]) -> + _ = Pid ! {active, Socket, self()}, + ok; +setopts(Socket = {Pid, _}, [{active, false}]) -> + _ = Pid ! {passive, Socket, self()}, + ok. diff --git a/test/autobahn_SUITE.erl b/test/autobahn_SUITE.erl deleted file mode 100644 index da56667..0000000 --- a/test/autobahn_SUITE.erl +++ /dev/null @@ -1,100 +0,0 @@ -%% Copyright (c) 2011, Magnus Klaar <[email protected]> -%% -%% Permission to use, copy, modify, and/or distribute this software for any -%% purpose with or without fee is hereby granted, provided that the above -%% copyright notice and this permission notice appear in all copies. -%% -%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES -%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF -%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR -%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES -%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN -%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF -%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - --module(autobahn_SUITE). - -%% This CT suite reuses the websocket server test suite from the Autobahn -%% project. The Autobahn project is a websocket implementation for Python. -%% Given that we don't expect to find the packages and tools to properly -%% set up and run such a test on a system used primarily for Erlang devlopment -%% this test suite is not included in the default 'ct' target in the makefile. - --include_lib("common_test/include/ct.hrl"). - --export([all/0, groups/0, init_per_suite/1, end_per_suite/1, - init_per_group/2, end_per_group/2]). %% ct. --export([run_tests/1]). %% autobahn. - -%% ct. - -all() -> - [{group, autobahn}]. - -groups() -> - BaseTests = [run_tests], - [{autobahn, [], BaseTests}]. - -init_per_suite(Config) -> - application:start(crypto), - application:start(cowlib), - application:start(ranch), - application:start(cowboy), - %% /tmp must be used as the parent directory for the virtualenv because - %% the directory names used in CT are so long that the interpreter path - %% in the scripts generated by virtualenv get so long that the system - %% refuses to execute them. - EnvPath = "/tmp/cowboy_autobahn_env", - os:putenv("AB_TESTS_ENV", EnvPath), - os:putenv("AB_TESTS_PRIV", ?config(priv_dir, Config)), - BinPath = filename:join(?config(data_dir, Config), "test.py"), - Stdout = os:cmd(BinPath ++ " setup"), - ct:log("~s~n", [Stdout]), - case string:str(Stdout, "AB-TESTS-SETUP-OK") of - 0 -> erlang:error(failed); - _ -> [{env_path, EnvPath},{bin_path,BinPath}|Config] - end. - -end_per_suite(_Config) -> - os:cmd("deactivate"), - application:stop(cowboy), - application:stop(ranch), - application:stop(cowlib), - application:stop(crypto), - ok. - -init_per_group(autobahn, Config) -> - Port = 33080, - cowboy:start_http(autobahn, 100, [{port, Port}], [ - {env, [{dispatch, init_dispatch()}]} - ]), - [{port, Port}|Config]. - -end_per_group(Listener, _Config) -> - cowboy:stop_listener(Listener), - ok. - -%% Dispatch configuration. - -init_dispatch() -> - cowboy_router:compile([{"localhost", [ - {"/echo", autobahn_echo, []}]}]). - -%% autobahn cases - -run_tests(Config) -> - PrivDir = ?config(priv_dir, Config), - IndexFile = filename:join([PrivDir, "reports", "servers", "index.html"]), - ct:log("<h2><a href=\"~s\">Full Test Results Report</a></h2>~n", [IndexFile]), - BinPath = ?config(bin_path, Config), - Stdout = os:cmd(BinPath ++ " test"), - ct:log("~s~n", [Stdout]), - case string:str(Stdout, "AB-TESTS-TEST-OK") of - 0 -> erlang:error(failed); - _ -> ok - end, - {ok, IndexHTML} = file:read_file(IndexFile), - case length(binary:matches(IndexHTML, <<"case_failed">>)) > 2 of - true -> erlang:error(failed); - false -> ok - end. diff --git a/test/autobahn_SUITE_data/autobahn_echo.erl b/test/autobahn_SUITE_data/autobahn_echo.erl deleted file mode 100644 index 07f7fd9..0000000 --- a/test/autobahn_SUITE_data/autobahn_echo.erl +++ /dev/null @@ -1,26 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(autobahn_echo). --behaviour(cowboy_websocket_handler). --export([init/3]). --export([websocket_init/3, websocket_handle/3, - websocket_info/3, websocket_terminate/3]). - -init(_Any, _Req, _Opts) -> - {upgrade, protocol, cowboy_websocket}. - -websocket_init(_TransportName, Req, _Opts) -> - {ok, Req, undefined}. - -websocket_handle({text, Data}, Req, State) -> - {reply, {text, Data}, Req, State}; -websocket_handle({binary, Data}, Req, State) -> - {reply, {binary, Data}, Req, State}; -websocket_handle(_Frame, Req, State) -> - {ok, Req, State}. - -websocket_info(_Info, Req, State) -> - {ok, Req, State}. - -websocket_terminate(_Reason, _Req, _State) -> - ok. diff --git a/test/autobahn_SUITE_data/test.py b/test/autobahn_SUITE_data/test.py deleted file mode 100755 index f142683..0000000 --- a/test/autobahn_SUITE_data/test.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python -import os -import os.path -import sys -import subprocess - - -AB_TESTS_ENV = os.getenv("AB_TESTS_ENV") -AB_TESTS_PRIV = os.getenv("AB_TESTS_PRIV") - -VIRTUALENV_URL = 'https://raw.github.com/pypa/virtualenv/master/virtualenv.py' -VIRTUALENV_BIN = os.path.join(AB_TESTS_ENV, "virtualenv.py") -INSTALL_BIN = os.path.join(AB_TESTS_ENV, "bin", "easy_install") - - -def activate_env(env): - """ - See 'Using Virtualenv without bin/python' at http://www.virtualenv.org - """ - activate_this = os.path.join(env, 'bin', 'activate_this.py') - exec(compile(open(activate_this).read(), activate_this, 'exec'), - dict(__file__=activate_this)) - -def install_env(env): - """ - Install a new virtualenv at a path and also install the Autobahn package. - """ - os.makedirs(env) if not os.path.isdir(env) else None - subprocess.check_call(["curl", "-sS", VIRTUALENV_URL, "-o", VIRTUALENV_BIN]) - subprocess.check_call(["python", VIRTUALENV_BIN, env]) - activate_env(env) - subprocess.check_call([INSTALL_BIN, "http://pypi.python.org/packages/2.7/a/autobahntestsuite/autobahntestsuite-0.5.3-py2.7.egg"]) - -def client_config(): - """ - See comment on SUPPORTED_SPEC_VERSIONS in Autobahn/.../websocket.py - """ - base = { - 'options': {'failByDrop': False}, - 'enable-ssl': False, - 'servers': [{ - 'agent': 'Cowboy', - 'url': 'ws://localhost:33080/echo', - 'options': {'version': 18} # RFC6455 - }], - 'cases': ['*'], - 'exclude-cases': [] } - return base - -def run_test(env, config): - activate_env(env) - from twisted.python import log - from twisted.internet import reactor - from autobahntestsuite.fuzzing import FuzzingClientFactory - os.chdir(AB_TESTS_PRIV) - log.startLogging(sys.stdout) - fuzzer = FuzzingClientFactory(config) - return reactor.run() - - -def main(): - cmd = sys.argv[1] - if cmd == 'setup': - install_env(AB_TESTS_ENV) - print('AB-TESTS-SETUP-OK') - elif cmd == 'test': - run_test(AB_TESTS_ENV, client_config()) - print('AB-TESTS-TEST-OK') - else: - return 1 - -if __name__ == '__main__': - main() diff --git a/test/cowboy_ct_hook.erl b/test/cowboy_ct_hook.erl new file mode 100644 index 0000000..1586412 --- /dev/null +++ b/test/cowboy_ct_hook.erl @@ -0,0 +1,23 @@ +%% Copyright (c) 2014, Loïc Hoguin <[email protected]> +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(cowboy_ct_hook). + +-export([init/2]). + +init(_, _) -> + cowboy_test:start([cowboy, gun]), + cowboy_test:make_certs(), + error_logger:add_report_handler(cowboy_error_h), + {ok, undefined}. diff --git a/test/cowboy_error_h.erl b/test/cowboy_error_h.erl new file mode 100644 index 0000000..b4ae78f --- /dev/null +++ b/test/cowboy_error_h.erl @@ -0,0 +1,145 @@ +%% Copyright (c) 2014, Loïc Hoguin <[email protected]> +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(cowboy_error_h). +-behaviour(gen_event). + +%% Public interface. +-export([ignore/3]). + +%% gen_event. +-export([init/1]). +-export([handle_event/2]). +-export([handle_call/2]). +-export([handle_info/2]). +-export([terminate/2]). +-export([code_change/3]). + +%% Public interface. + +%% Ignore crashes from Pid occuring in M:F/A. +ignore(M, F, A) -> + gen_event:call(error_logger, ?MODULE, {expect, {self(), M, F, A}}). + +%% gen_event. + +init(_) -> + spawn(fun() -> error_logger:tty(false) end), + {ok, []}. + +%% Ignore supervisor and progress reports. +handle_event({info_report, _, {_, progress, _}}, State) -> + {ok, State}; +handle_event({info_report, _, {_, std_info, _}}, State) -> + {ok, State}; +handle_event({error_report, _, {_, supervisor_report, _}}, State) -> + {ok, State}; +%% Ignore gun retry failures. +handle_event({error_report, _, {_, crash_report, + [[{initial_call, {gun, init, _}}, _, _, + {error_info, {error, gone, _}}|_]|_]}}, + State) -> + {ok, State}; +%% Ignore emulator reports that are a duplicate of what Ranch gives us. +%% +%% The emulator always sends strings for errors, which makes it very +%% difficult to extract the information we need, hence the regexps. +handle_event(Event = {error, GL, {emulator, _, Msg}}, State) + when node(GL) =:= node() -> + Result = re:run(Msg, + "Error in process ([^\s]+).+? with exit value: " + ".+?{stacktrace,\\[{([^,]+),([^,]+),(.+)", + [{capture, all_but_first, list}]), + case Result of + nomatch -> + write_event(Event), + {ok, State}; + {match, [PidStr, MStr, FStr, Rest]} -> + A = case Rest of + "[]" ++ _ -> + 0; + "[" ++ Rest2 -> + count_args(Rest2, 1, 0); + _ -> + {match, [AStr]} = re:run(Rest, "([^,]+).+", + [{capture, all_but_first, list}]), + list_to_integer(AStr) + end, + Crash = {list_to_pid(PidStr), list_to_existing_atom(MStr), + list_to_existing_atom(FStr), A}, + case lists:member(Crash, State) of + true -> + {ok, lists:delete(Crash, State)}; + false -> + write_event(Event), + {ok, State} + end + end; +handle_event(Event = {error, GL, + {_, "Ranch listener" ++ _, [_, _, Pid, {[_, _, + {stacktrace, [{M, F, A, _}|_]}|_], _}]}}, + State) when node(GL) =:= node() -> + A2 = if is_list(A) -> length(A); true -> A end, + Crash = {Pid, M, F, A2}, + case lists:member(Crash, State) of + true -> + {ok, lists:delete(Crash, State)}; + false -> + write_event(Event), + {ok, State} + end; +handle_event(Event = {_, GL, _}, State) when node(GL) =:= node() -> + write_event(Event), + {ok, State}; +handle_event(_, State) -> + {ok, State}. + +handle_call({expect, Crash}, State) -> + {ok, ok, [Crash, Crash|State]}; +handle_call(_, State) -> + {ok, {error, bad_query}, State}. + +handle_info(_, State) -> + {ok, State}. + +terminate(_, _) -> + spawn(fun() -> error_logger:tty(true) end), + ok. + +code_change(_, State, _) -> + {ok, State}. + +%% Internal. + +write_event(Event) -> + error_logger_tty_h:write_event( + {erlang:universaltime(), Event}, + io). + +count_args("]" ++ _, N, 0) -> + N; +count_args("]" ++ Tail, N, Levels) -> + count_args(Tail, N, Levels - 1); +count_args("[" ++ Tail, N, Levels) -> + count_args(Tail, N, Levels + 1); +count_args("}" ++ Tail, N, Levels) -> + count_args(Tail, N, Levels - 1); +count_args("{" ++ Tail, N, Levels) -> + count_args(Tail, N, Levels + 1); +count_args("," ++ Tail, N, Levels = 0) -> + count_args(Tail, N + 1, Levels); +count_args("," ++ Tail, N, Levels) -> + count_args(Tail, N, Levels); +count_args([_|Tail], N, Levels) -> + count_args(Tail, N, Levels). diff --git a/test/cowboy_test.erl b/test/cowboy_test.erl new file mode 100644 index 0000000..f4a5706 --- /dev/null +++ b/test/cowboy_test.erl @@ -0,0 +1,200 @@ +%% Copyright (c) 2014, Loïc Hoguin <[email protected]> +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(cowboy_test). +-compile(export_all). + +%% Start and stop applications and their dependencies. + +start(Apps) -> + _ = [do_start(App) || App <- Apps], + ok. + +do_start(App) -> + case application:start(App) of + ok -> + ok; + {error, {not_started, Dep}} -> + do_start(Dep), + do_start(App) + end. + +%% SSL certificate creation and safekeeping. + +make_certs() -> + {_, Cert, Key} = ct_helper:make_certs(), + CertOpts = [{cert, Cert}, {key, Key}], + Pid = spawn(fun() -> receive after infinity -> ok end end), + ?MODULE = ets:new(?MODULE, [ordered_set, public, named_table, + {heir, Pid, undefined}]), + ets:insert(?MODULE, {cert_opts, CertOpts}), + ok. + +get_certs() -> + ets:lookup_element(?MODULE, cert_opts, 2). + +%% Quick configuration value retrieval. + +config(Key, Config) -> + {_, Value} = lists:keyfind(Key, 1, Config), + Value. + +%% Test case description. + +doc(String) -> + ct:comment(String), + ct:log(String). + +%% List of all test cases in the suite. + +all(Suite) -> + lists:usort([F || {F, 1} <- Suite:module_info(exports), + F =/= module_info, + F =/= test, %% This is leftover from the eunit parse_transform... + F =/= all, + F =/= groups, + string:substr(atom_to_list(F), 1, 5) =/= "init_", + string:substr(atom_to_list(F), 1, 4) =/= "end_", + string:substr(atom_to_list(F), 1, 3) =/= "do_" + ]). + +%% Listeners initialization. + +init_http(Ref, ProtoOpts, Config) -> + {ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [ + {max_keepalive, 50}, + {timeout, 500} + |ProtoOpts]), + Port = ranch:get_port(Ref), + [{type, tcp}, {port, Port}, {opts, []}|Config]. + +init_https(Ref, ProtoOpts, Config) -> + Opts = get_certs(), + {ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [ + {max_keepalive, 50}, + {timeout, 500} + |ProtoOpts]), + Port = ranch:get_port(Ref), + [{type, ssl}, {port, Port}, {opts, Opts}|Config]. + +init_spdy(Ref, ProtoOpts, Config) -> + Opts = get_certs(), + {ok, _} = cowboy:start_spdy(Ref, 100, Opts ++ [{port, 0}], + ProtoOpts), + Port = ranch:get_port(Ref), + [{type, ssl}, {port, Port}, {opts, Opts}|Config]. + +%% Common group of listeners used by most suites. + +common_all() -> + [ + {group, http}, + {group, https}, + {group, spdy}, + {group, http_compress}, + {group, https_compress}, + {group, spdy_compress} + ]. + +common_groups(Tests) -> + [ + {http, [parallel], Tests}, + {https, [parallel], Tests}, + {spdy, [parallel], Tests}, + {http_compress, [parallel], Tests}, + {https_compress, [parallel], Tests}, + {spdy_compress, [parallel], Tests} + ]. + +init_common_groups(Name = http, Config, Mod) -> + init_http(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]} + ], Config); +init_common_groups(Name = https, Config, Mod) -> + init_https(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]} + ], Config); +init_common_groups(Name = spdy, Config, Mod) -> + init_spdy(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]} + ], Config); +init_common_groups(Name = http_compress, Config, Mod) -> + init_http(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]}, + {compress, true} + ], Config); +init_common_groups(Name = https_compress, Config, Mod) -> + init_https(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]}, + {compress, true} + ], Config); +init_common_groups(Name = spdy_compress, Config, Mod) -> + init_spdy(Name, [ + {env, [{dispatch, Mod:init_dispatch(Config)}]}, + {compress, true} + ], Config). + +%% Support functions for testing using Gun. + +gun_open(Config) -> + gun_open(Config, []). + +gun_open(Config, Opts) -> + {ok, ConnPid} = gun:open("localhost", config(port, Config), + [{retry, 0}, {type, config(type, Config)}|Opts]), + ConnPid. + +gun_monitor_open(Config) -> + gun_monitor_open(Config, []). + +gun_monitor_open(Config, Opts) -> + ConnPid = gun_open(Config, Opts), + {ConnPid, monitor(process, ConnPid)}. + +gun_is_gone(ConnPid, MRef) -> + receive {'DOWN', MRef, process, ConnPid, gone} -> ok + after 500 -> error(timeout) end. + +%% Support functions for testing using a raw socket. + +raw_open(Config) -> + Transport = case config(type, Config) of + tcp -> gen_tcp; + ssl -> ssl + end, + {_, Opts} = lists:keyfind(opts, 1, Config), + {ok, Socket} = Transport:connect("localhost", config(port, Config), + [binary, {active, false}, {packet, raw}, + {reuseaddr, true}, {nodelay, true}|Opts]), + {raw_client, Socket, Transport}. + +raw_send({raw_client, Socket, Transport}, Data) -> + Transport:send(Socket, Data). + +raw_recv_head({raw_client, Socket, Transport}) -> + {ok, Data} = Transport:recv(Socket, 0, 5000), + raw_recv_head(Socket, Transport, Data). + +raw_recv_head(Socket, Transport, Buffer) -> + case binary:match(Buffer, <<"\r\n\r\n">>) of + nomatch -> + {ok, Data} = Transport:recv(Socket, 0, 5000), + raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>); + {_, _} -> + Buffer + end. + +raw_expect_recv({raw_client, Socket, Transport}, Expect) -> + {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 5000), + ok. diff --git a/test/eunit_SUITE.erl b/test/eunit_SUITE.erl index 76c5a30..1d817c6 100644 --- a/test/eunit_SUITE.erl +++ b/test/eunit_SUITE.erl @@ -13,16 +13,7 @@ %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -module(eunit_SUITE). - --include_lib("common_test/include/ct.hrl"). - -%% ct. --export([all/0]). - -%% Tests. --export([eunit/1]). - -%% ct. +-compile(export_all). all() -> [eunit]. diff --git a/test/handlers/long_polling_h.erl b/test/handlers/long_polling_h.erl new file mode 100644 index 0000000..21f1d4d --- /dev/null +++ b/test/handlers/long_polling_h.erl @@ -0,0 +1,27 @@ +%% This module implements a loop handler for long-polling. +%% It starts by sending itself a message after 200ms, +%% then sends another after that for a total of 3 messages. +%% When it receives the last message, it sends a 102 reply back. + +-module(long_polling_h). +-behaviour(cowboy_loop_handler). + +-export([init/3]). +-export([info/3]). +-export([terminate/3]). + +init(_, Req, _) -> + erlang:send_after(200, self(), timeout), + {loop, Req, 2, 5000, hibernate}. + +info(timeout, Req, 0) -> + {ok, Req2} = cowboy_req:reply(102, Req), + {ok, Req2, 0}; +info(timeout, Req, Count) -> + erlang:send_after(200, self(), timeout), + {loop, Req, Count - 1, hibernate}. + +terminate({normal, shutdown}, _, 0) -> + ok; +terminate({error, overflow}, _, _) -> + ok. diff --git a/test/handlers/loop_handler_body_h.erl b/test/handlers/loop_handler_body_h.erl new file mode 100644 index 0000000..db69b02 --- /dev/null +++ b/test/handlers/loop_handler_body_h.erl @@ -0,0 +1,24 @@ +%% This module implements a loop handler that reads +%% the request body after sending itself a message, +%% checks that its size is exactly 100000 bytes, +%% then sends a 200 reply back. + +-module(loop_handler_body_h). +-behaviour(cowboy_loop_handler). + +-export([init/3]). +-export([info/3]). +-export([terminate/3]). + +init(_, Req, _) -> + self() ! timeout, + {loop, Req, undefined, 5000, hibernate}. + +info(timeout, Req, State) -> + {ok, Body, Req2} = cowboy_req:body(Req), + 100000 = byte_size(Body), + {ok, Req3} = cowboy_req:reply(200, Req2), + {ok, Req3, State}. + +terminate({normal, shutdown}, _, _) -> + ok. diff --git a/test/handlers/loop_handler_timeout_h.erl b/test/handlers/loop_handler_timeout_h.erl new file mode 100644 index 0000000..1125046 --- /dev/null +++ b/test/handlers/loop_handler_timeout_h.erl @@ -0,0 +1,23 @@ +%% This module implements a loop handler that sends +%% itself a timeout that will intentionally arrive +%% too late, as it configures itself to only wait +%% 200ms before closing the connection in init/3. +%% This results in a 204 reply being sent back by Cowboy. + +-module(loop_handler_timeout_h). +-behaviour(cowboy_loop_handler). + +-export([init/3]). +-export([info/3]). +-export([terminate/3]). + +init(_, Req, _) -> + erlang:send_after(1000, self(), timeout), + {loop, Req, undefined, 200, hibernate}. + +info(timeout, Req, State) -> + {ok, Req2} = cowboy_req:reply(500, Req), + {ok, Req2, State}. + +terminate({normal, timeout}, _, _) -> + ok. diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index 4b93c67..35d4b06 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -16,7 +16,15 @@ -module(http_SUITE). -compile(export_all). --include_lib("common_test/include/ct.hrl"). +-import(cowboy_test, [config/2]). +-import(cowboy_test, [gun_open/1]). +-import(cowboy_test, [gun_monitor_open/1]). +-import(cowboy_test, [gun_monitor_open/2]). +-import(cowboy_test, [gun_is_gone/2]). +-import(cowboy_test, [raw_open/1]). +-import(cowboy_test, [raw_send/2]). +-import(cowboy_test, [raw_recv_head/1]). +-import(cowboy_test, [raw_expect_recv/2]). %% ct. @@ -34,69 +42,10 @@ all() -> ]. groups() -> - Tests = [ - check_raw_status, - check_status, - chunked_response, - echo_body, - echo_body_max_length, - echo_body_qs, - echo_body_qs_max_length, - error_chain_handle_after_reply, - error_chain_handle_before_reply, - error_handle_after_reply, - error_init_after_reply, - error_init_reply_handle_error, - headers_dupe, - http10_chunkless, - http10_hostless, - keepalive_max, - keepalive_nl, - keepalive_stream_loop, - multipart, - multipart_large, - nc_rand, - nc_zero, - pipeline, - pipeline_long_polling, - rest_bad_accept, - rest_bad_content_type, - rest_expires, - rest_keepalive, - rest_keepalive_post, - rest_missing_get_callbacks, - rest_missing_put_callbacks, - rest_nodelete, - rest_options_default, - rest_param_all, - rest_patch, - rest_post_charset, - rest_postonly, - rest_resource_etags, - rest_resource_etags_if_none_match, - set_resp_body, - set_resp_header, - set_resp_overwrite, - slowloris, - slowloris2, - static_attribute_etag, - static_function_etag, - static_mimetypes_function, - static_specify_file, - static_specify_file_catchall, - static_test_file, - static_test_file_css, - stream_body_set_resp, - stream_body_set_resp_close, - stream_body_set_resp_chunked, - stream_body_set_resp_chunked10, - streamed_response, - te_chunked, - te_chunked_chopped, - te_chunked_delayed, - te_chunked_split_body, - te_chunked_split_crlf, - te_identity + Tests = cowboy_test:all(?MODULE) -- [ + onrequest, onrequest_reply, onrequest_hook, + onresponse_crash, onresponse_reply, onresponse_capitalize, + parse_host, set_env_dispatch ], [ {http, [parallel], Tests}, @@ -123,124 +72,83 @@ groups() -> ]. init_per_suite(Config) -> - application:start(crypto), - application:start(asn1), - application:start(public_key), - application:start(ssl), - application:start(ranch), - application:start(cowlib), - application:start(gun), - application:start(cowboy), - Dir = ?config(priv_dir, Config) ++ "/static", + Dir = config(priv_dir, Config) ++ "/static", ct_helper:create_static_dir(Dir), [{static_dir, Dir}|Config]. end_per_suite(Config) -> - Dir = ?config(static_dir, Config), - ct_helper:delete_static_dir(Dir), - application:stop(cowboy), - application:stop(gun), - application:stop(cowlib), - application:stop(ranch), - application:stop(ssl), - application:stop(public_key), - application:stop(asn1), - application:stop(crypto), - ok. - -init_tcp_group(Ref, ProtoOpts, Config) -> - Transport = ranch_tcp, - {ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [ + ct_helper:delete_static_dir(config(static_dir, Config)). + +init_per_group(Name = http, Config) -> + cowboy_test:init_http(Name, [ + {env, [{dispatch, init_dispatch(Config)}]} + ], Config); +init_per_group(Name = https, Config) -> + cowboy_test:init_https(Name, [ + {env, [{dispatch, init_dispatch(Config)}]} + ], Config); +init_per_group(Name = http_compress, Config) -> + cowboy_test:init_http(Name, [ {env, [{dispatch, init_dispatch(Config)}]}, - {max_keepalive, 50}, - {timeout, 500} - |ProtoOpts]), - Port = ranch:get_port(Ref), - [{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config]. - -init_ssl_group(Ref, ProtoOpts, Config) -> - Transport = ranch_ssl, - {_, Cert, Key} = ct_helper:make_certs(), - Opts = [{cert, Cert}, {key, Key}], - {ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [ + {compress, true} + ], Config); +init_per_group(Name = https_compress, Config) -> + cowboy_test:init_https(Name, [ {env, [{dispatch, init_dispatch(Config)}]}, - {max_keepalive, 50}, - {timeout, 500} - |ProtoOpts]), - Port = ranch:get_port(Ref), - [{type, ssl}, {port, Port}, {opts, Opts}, {transport, Transport}|Config]. - -init_per_group(http, Config) -> - init_tcp_group(http, [], Config); -init_per_group(https, Config) -> - init_ssl_group(https, [], Config); -init_per_group(http_compress, Config) -> - init_tcp_group(http_compress, [{compress, true}], Config); -init_per_group(https_compress, Config) -> - init_ssl_group(https_compress, [{compress, true}], Config); + {compress, true} + ], Config); %% Most, if not all of these, should be in separate test suites. init_per_group(onrequest, Config) -> - Transport = ranch_tcp, {ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [ {env, [{dispatch, init_dispatch(Config)}]}, {max_keepalive, 50}, - {onrequest, fun onrequest_hook/1}, + {onrequest, fun do_onrequest_hook/1}, {timeout, 500} ]), Port = ranch:get_port(onrequest), - [{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []}, - {transport, Transport}|Config]; + [{type, tcp}, {port, Port}, {opts, []}|Config]; init_per_group(onresponse, Config) -> - Transport = ranch_tcp, {ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [ {env, [{dispatch, init_dispatch(Config)}]}, {max_keepalive, 50}, - {onresponse, fun onresponse_hook/4}, + {onresponse, fun do_onresponse_hook/4}, {timeout, 500} ]), Port = ranch:get_port(onresponse), - [{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []}, - {transport, Transport}|Config]; + [{type, tcp}, {port, Port}, {opts, []}|Config]; init_per_group(onresponse_capitalize, Config) -> - Transport = ranch_tcp, {ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [ {env, [{dispatch, init_dispatch(Config)}]}, {max_keepalive, 50}, - {onresponse, fun onresponse_capitalize_hook/4}, + {onresponse, fun do_onresponse_capitalize_hook/4}, {timeout, 500} ]), Port = ranch:get_port(onresponse_capitalize), - [{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []}, - {transport, Transport}|Config]; + [{type, tcp}, {port, Port}, {opts, []}|Config]; init_per_group(parse_host, Config) -> - Transport = ranch_tcp, Dispatch = cowboy_router:compile([ {'_', [ {"/req_attr", http_req_attr, []} ]} ]), - {ok, _} = cowboy:start_http(http, 100, [{port, 0}], [ + {ok, _} = cowboy:start_http(parse_host, 100, [{port, 0}], [ {env, [{dispatch, Dispatch}]}, {max_keepalive, 50}, {timeout, 500} ]), - Port = ranch:get_port(http), - [{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []}, - {transport, Transport}|Config]; + Port = ranch:get_port(parse_host), + [{type, tcp}, {port, Port}, {opts, []}|Config]; init_per_group(set_env, Config) -> - Transport = ranch_tcp, {ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [ {env, [{dispatch, []}]}, {max_keepalive, 50}, {timeout, 500} ]), Port = ranch:get_port(set_env), - [{scheme, <<"http">>}, {type, tcp}, {port, Port}, {opts, []}, - {transport, Transport}|Config]. + [{type, tcp}, {port, Port}, {opts, []}|Config]. end_per_group(Name, _) -> - cowboy:stop_listener(Name), - ok. + ok = cowboy:stop_listener(Name). %% Dispatch configuration. @@ -250,7 +158,6 @@ init_dispatch(Config) -> {"/chunked_response", http_chunked, []}, {"/streamed_response", http_streamed, []}, {"/init_shutdown", http_init_shutdown, []}, - {"/long_polling", http_long_polling, []}, {"/headers/dupe", http_handler, [{headers, [{<<"connection">>, <<"close">>}]}]}, {"/set_resp/header", http_set_resp, @@ -270,18 +177,18 @@ init_dispatch(Config) -> {reply, set_resp_chunked}, {body, [<<"stream_body">>, <<"_set_resp_chunked">>]}]}, {"/static/[...]", cowboy_static, - {dir, ?config(static_dir, Config)}}, + {dir, config(static_dir, Config)}}, {"/static_mimetypes_function/[...]", cowboy_static, - {dir, ?config(static_dir, Config), - [{mimetypes, ?MODULE, mimetypes_text_html}]}}, + {dir, config(static_dir, Config), + [{mimetypes, ?MODULE, do_mimetypes_text_html}]}}, {"/handler_errors", http_errors, []}, {"/static_attribute_etag/[...]", cowboy_static, - {dir, ?config(static_dir, Config)}}, + {dir, config(static_dir, Config)}}, {"/static_function_etag/[...]", cowboy_static, - {dir, ?config(static_dir, Config), - [{etag, ?MODULE, etag_gen}]}}, + {dir, config(static_dir, Config), + [{etag, ?MODULE, do_etag_gen}]}}, {"/static_specify_file/[...]", cowboy_static, - {file, ?config(static_dir, Config) ++ "/style.css"}}, + {file, config(static_dir, Config) ++ "/style.css"}}, {"/multipart", http_multipart, []}, {"/multipart/large", http_multipart_stream, []}, {"/echo/body", http_echo_body, []}, @@ -301,82 +208,22 @@ init_dispatch(Config) -> {"/resetags", rest_resource_etags, []}, {"/rest_expires", rest_expires, []}, {"/rest_empty_resource", rest_empty_resource, []}, - {"/loop_recv", http_loop_recv, []}, {"/loop_stream_recv", http_loop_stream_recv, []}, - {"/loop_timeout", http_loop_timeout, []}, {"/", http_handler, []} ]} ]). -etag_gen(_, _, _) -> +%% Callbacks. + +do_etag_gen(_, _, _) -> {strong, <<"etag">>}. -mimetypes_text_html(_) -> +do_mimetypes_text_html(_) -> <<"text/html">>. -%% Support functions for testing using Gun. - -gun_open(Config) -> - gun_open(Config, []). - -gun_open(Config, Opts) -> - {_, Port} = lists:keyfind(port, 1, Config), - {_, Type} = lists:keyfind(type, 1, Config), - {ok, ConnPid} = gun:open("localhost", Port, [{retry, 0}, {type, Type}|Opts]), - ConnPid. - -gun_monitor_open(Config) -> - gun_monitor_open(Config, []). - -gun_monitor_open(Config, Opts) -> - ConnPid = gun_open(Config, Opts), - {ConnPid, monitor(process, ConnPid)}. - -gun_is_gone(ConnPid) -> - gun_is_gone(ConnPid, monitor(process, ConnPid)). - -gun_is_gone(ConnPid, MRef) -> - receive {'DOWN', MRef, process, ConnPid, gone} -> ok - after 500 -> error(timeout) end. - -%% Support functions for testing using a raw socket. - -raw_open(Config) -> - {_, Port} = lists:keyfind(port, 1, Config), - {_, Type} = lists:keyfind(type, 1, Config), - Transport = case Type of - tcp -> gen_tcp; - ssl -> ssl - end, - {_, Opts} = lists:keyfind(opts, 1, Config), - {ok, Socket} = Transport:connect("localhost", Port, - [binary, {active, false}, {packet, raw}, - {reuseaddr, true}, {nodelay, true}|Opts]), - {raw_client, Socket, Transport}. - -raw_send({raw_client, Socket, Transport}, Data) -> - Transport:send(Socket, Data). - -raw_recv_head({raw_client, Socket, Transport}) -> - {ok, Data} = Transport:recv(Socket, 0, 5000), - raw_recv_head(Socket, Transport, Data). - -raw_recv_head(Socket, Transport, Buffer) -> - case binary:match(Buffer, <<"\r\n\r\n">>) of - nomatch -> - {ok, Data} = Transport:recv(Socket, 0, 5000), - raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>); - {_, _} -> - Buffer - end. - -raw_expect_recv({raw_client, Socket, Transport}, Expect) -> - {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 5000), - ok. - %% Convenience functions. -quick_raw(Data, Config) -> +do_raw(Data, Config) -> Client = raw_open(Config), ok = raw_send(Client, Data), case catch raw_recv_head(Client) of @@ -384,7 +231,7 @@ quick_raw(Data, Config) -> Resp -> element(2, cow_http:parse_status_line(Resp)) end. -quick_get(Path, Config) -> +do_get(Path, Config) -> ConnPid = gun_open(Config), Ref = gun:get(ConnPid, Path), {response, _, Status, _} = gun:await(ConnPid, Ref), @@ -416,14 +263,10 @@ The document has moved <A HREF=\"http://www.google.co.il/\">here</A>. </BODY></HTML>", Tests = [ - {102, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n" - "Content-Length: 5000\r\n\r\n", 0:5000/unit:8 >>}, {200, ["GET / HTTP/1.0\r\nHost: localhost\r\n" "Set-Cookie: ", HugeCookie, "\r\n\r\n"]}, {200, "\r\n\r\n\r\n\r\n\r\nGET / HTTP/1.1\r\nHost: localhost\r\n\r\n"}, {200, "GET http://proxy/ HTTP/1.1\r\nHost: localhost\r\n\r\n"}, - {200, <<"POST /loop_recv HTTP/1.1\r\nHost: localhost\r\n" - "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>}, {400, "\n"}, {400, "Garbage\r\n\r\n"}, {400, "\r\n\r\n\r\n\r\n\r\n\r\n"}, @@ -437,8 +280,6 @@ The document has moved {408, "GET / HTTP/1.1\r\nHost: localhost\r\n\r"}, {414, Huge}, {400, "GET / HTTP/1.1\r\n" ++ Huge}, - {500, <<"GET /long_polling HTTP/1.1\r\nHost: localhost\r\n" - "Content-Length: 100000\r\n\r\n", 0:100000/unit:8 >>}, {505, "GET / HTTP/1.2\r\nHost: localhost\r\n\r\n"}, {closed, ""}, {closed, "\r\n"}, @@ -446,17 +287,15 @@ The document has moved {closed, "GET / HTTP/1.1"} ], _ = [{Status, Packet} = begin - Ret = quick_raw(Packet, Config), + Ret = do_raw(Packet, Config), {Ret, Packet} end || {Status, Packet} <- Tests], ok. check_status(Config) -> Tests = [ - {102, "/long_polling"}, {200, "/"}, {200, "/simple"}, - {204, "/loop_timeout"}, {400, "/static/%2f"}, {400, "/static/%2e"}, {400, "/static/%2e%2e"}, @@ -470,7 +309,7 @@ check_status(Config) -> {666, "/init_shutdown"} ], _ = [{Status, URL} = begin - Ret = quick_get(URL, Config), + Ret = do_get(URL, Config), {Ret, URL} end || {Status, URL} <- Tests]. @@ -566,19 +405,23 @@ http10_chunkless(Config) -> gun_is_gone(ConnPid, MRef). http10_hostless(Config) -> - Port10 = ?config(port, Config) + 10, - Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)), - ranch:start_listener(Name, 5, - ?config(transport, Config), ?config(opts, Config) ++ [{port, Port10}], + Name = http10_hostless, + Port10 = config(port, Config) + 10, + Transport = case config(type, Config) of + tcp -> ranch_tcp; + ssl -> ranch_ssl + end, + ranch:start_listener(Name, 5, Transport, + config(opts, Config) ++ [{port, Port10}], cowboy_protocol, [ {env, [{dispatch, cowboy_router:compile([ {'_', [{"/http1.0/hostless", http_handler, []}]}])}]}, {max_keepalive, 50}, {timeout, 500}] ), - 200 = quick_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n", + 200 = do_raw("GET /http1.0/hostless HTTP/1.0\r\n\r\n", [{port, Port10}|Config]), - cowboy:stop_listener(http10). + cowboy:stop_listener(http10_hostless). keepalive_max(Config) -> {ConnPid, MRef} = gun_monitor_open(Config), @@ -642,6 +485,28 @@ multipart(Config) -> ], ok. +multipart_chunked(Config) -> + ConnPid = gun_open(Config), + Body = << + "This is a preamble." + "\r\n--OHai\r\nX-Name:answer\r\n\r\n42" + "\r\n--OHai\r\nServer:Cowboy\r\n\r\nIt rocks!\r\n" + "\r\n--OHai--\r\n" + "This is an epilogue." + >>, + Ref = gun:post(ConnPid, "/multipart", [ + {<<"content-type">>, <<"multipart/x-makes-no-sense; boundary=OHai">>}, + {<<"transfer-encoding">>, <<"chunked">>}]), + gun:data(ConnPid, Ref, fin, Body), + {response, nofin, 200, _} = gun:await(ConnPid, Ref), + {ok, RespBody} = gun:await_body(ConnPid, Ref), + Parts = binary_to_term(RespBody), + Parts = [ + {[{<<"x-name">>, <<"answer">>}], <<"42">>}, + {[{<<"server">>, <<"Cowboy">>}], <<"It rocks!\r\n">>} + ], + ok. + multipart_large(Config) -> ConnPid = gun_open(Config), Boundary = "----------", @@ -656,7 +521,7 @@ multipart_large(Config) -> {response, fin, 200, _} = gun:await(ConnPid, Ref), ok. -nc_reqs(Config, Input) -> +do_nc(Config, Input) -> Cat = os:find_executable("cat"), Nc = os:find_executable("nc"), case {Cat, Nc} of @@ -666,18 +531,17 @@ nc_reqs(Config, Input) -> {skip, {notfound, nc}}; _Good -> %% Throw garbage at the server then check if it's still up. - {port, Port} = lists:keyfind(port, 1, Config), - StrPort = integer_to_list(Port), + StrPort = integer_to_list(config(port, Config)), [os:cmd("cat " ++ Input ++ " | nc localhost " ++ StrPort) || _ <- lists:seq(1, 100)], - 200 = quick_get("/", Config) + 200 = do_get("/", Config) end. nc_rand(Config) -> - nc_reqs(Config, "/dev/urandom"). + do_nc(Config, "/dev/urandom"). nc_zero(Config) -> - nc_reqs(Config, "/dev/zero"). + do_nc(Config, "/dev/zero"). onrequest(Config) -> ConnPid = gun_open(Config), @@ -696,7 +560,7 @@ onrequest_reply(Config) -> ok. %% Hook for the above onrequest tests. -onrequest_hook(Req) -> +do_onrequest_hook(Req) -> case cowboy_req:qs_val(<<"reply">>, Req) of {undefined, Req2} -> cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2); @@ -714,7 +578,7 @@ onresponse_capitalize(Config) -> ok. %% Hook for the above onresponse_capitalize test. -onresponse_capitalize_hook(Status, Headers, Body, Req) -> +do_onresponse_capitalize_hook(Status, Headers, Body, Req) -> Headers2 = [{cowboy_bstr:capitalize_token(N), V} || {N, V} <- Headers], {ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req), @@ -734,7 +598,7 @@ onresponse_reply(Config) -> ok. %% Hook for the above onresponse tests. -onresponse_hook(_, Headers, _, Req) -> +do_onresponse_hook(_, Headers, _, Req) -> {ok, Req2} = cowboy_req:reply( <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req), Req2. @@ -765,12 +629,6 @@ pipeline(Config) -> _ = [{response, nofin, 200, _} = gun:await(ConnPid, Ref) || Ref <- Refs], ok. -pipeline_long_polling(Config) -> - ConnPid = gun_open(Config), - Refs = [gun:get(ConnPid, "/long_polling") || _ <- lists:seq(1, 2)], - _ = [{response, fin, 102, _} = gun:await(ConnPid, Ref) || Ref <- Refs], - ok. - rest_param_all(Config) -> ConnPid = gun_open(Config), %% Accept without param. @@ -1117,9 +975,9 @@ te_chunked(Config) -> {ok, Body} = gun:await_body(ConnPid, Ref), ok. -body_to_chunks(_, <<>>, Acc) -> +do_body_to_chunks(_, <<>>, Acc) -> lists:reverse([<<"0\r\n\r\n">>|Acc]); -body_to_chunks(ChunkSize, Body, Acc) -> +do_body_to_chunks(ChunkSize, Body, Acc) -> BodySize = byte_size(Body), ChunkSize2 = case BodySize < ChunkSize of true -> BodySize; @@ -1127,12 +985,12 @@ body_to_chunks(ChunkSize, Body, Acc) -> end, << Chunk:ChunkSize2/binary, Rest/binary >> = Body, ChunkSizeBin = list_to_binary(integer_to_list(ChunkSize2, 16)), - body_to_chunks(ChunkSize, Rest, + do_body_to_chunks(ChunkSize, Rest, [<< ChunkSizeBin/binary, "\r\n", Chunk/binary, "\r\n" >>|Acc]). te_chunked_chopped(Config) -> Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Body2 = iolist_to_binary(body_to_chunks(50, Body, [])), + Body2 = iolist_to_binary(do_body_to_chunks(50, Body, [])), ConnPid = gun_open(Config), Ref = gun:post(ConnPid, "/echo/body", [{<<"transfer-encoding">>, <<"chunked">>}]), @@ -1146,7 +1004,7 @@ te_chunked_chopped(Config) -> te_chunked_delayed(Config) -> Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Chunks = body_to_chunks(50, Body, []), + Chunks = do_body_to_chunks(50, Body, []), ConnPid = gun_open(Config), Ref = gun:post(ConnPid, "/echo/body", [{<<"transfer-encoding">>, <<"chunked">>}]), @@ -1160,7 +1018,7 @@ te_chunked_delayed(Config) -> te_chunked_split_body(Config) -> Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Chunks = body_to_chunks(50, Body, []), + Chunks = do_body_to_chunks(50, Body, []), ConnPid = gun_open(Config), Ref = gun:post(ConnPid, "/echo/body", [{<<"transfer-encoding">>, <<"chunked">>}]), @@ -1184,7 +1042,7 @@ te_chunked_split_body(Config) -> te_chunked_split_crlf(Config) -> Body = list_to_binary(io_lib:format("~p", [lists:seq(1, 100)])), - Chunks = body_to_chunks(50, Body, []), + Chunks = do_body_to_chunks(50, Body, []), ConnPid = gun_open(Config), Ref = gun:post(ConnPid, "/echo/body", [{<<"transfer-encoding">>, <<"chunked">>}]), diff --git a/test/http_SUITE_data/http_errors.erl b/test/http_SUITE_data/http_errors.erl index 8831362..35ac3bd 100644 --- a/test/http_SUITE_data/http_errors.erl +++ b/test/http_SUITE_data/http_errors.erl @@ -9,30 +9,28 @@ init({_Transport, http}, Req, _Opts) -> case_init(Case, Req1). case_init(<<"init_before_reply">> = Case, _Req) -> + cowboy_error_h:ignore(?MODULE, case_init, 2), erlang:error(Case); - case_init(<<"init_after_reply">> = Case, Req) -> + cowboy_error_h:ignore(?MODULE, case_init, 2), {ok, _Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req), erlang:error(Case); - case_init(<<"init_reply_handle_error">> = Case, Req) -> {ok, Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req), {ok, Req1, Case}; - case_init(<<"handle_before_reply">> = Case, Req) -> {ok, Req, Case}; - case_init(<<"handle_after_reply">> = Case, Req) -> {ok, Req, Case}. - handle(_Req, <<"init_reply_handle_error">> = Case) -> + cowboy_error_h:ignore(?MODULE, handle, 2), erlang:error(Case); - handle(_Req, <<"handle_before_reply">> = Case) -> + cowboy_error_h:ignore(?MODULE, handle, 2), erlang:error(Case); - handle(Req, <<"handle_after_reply">> = Case) -> + cowboy_error_h:ignore(?MODULE, handle, 2), {ok, _Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req), erlang:error(Case). diff --git a/test/http_SUITE_data/http_long_polling.erl b/test/http_SUITE_data/http_long_polling.erl deleted file mode 100644 index ad4e66e..0000000 --- a/test/http_SUITE_data/http_long_polling.erl +++ /dev/null @@ -1,24 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(http_long_polling). --behaviour(cowboy_http_handler). --export([init/3, handle/2, info/3, terminate/3]). - -init({_Transport, http}, Req, _Opts) -> - erlang:send_after(500, self(), timeout), - {loop, Req, 5, 5000, hibernate}. - -handle(_Req, _State) -> - exit(badarg). - -info(timeout, Req, 0) -> - {ok, Req2} = cowboy_req:reply(102, Req), - {ok, Req2, 0}; -info(timeout, Req, State) -> - erlang:send_after(500, self(), timeout), - {loop, Req, State - 1, hibernate}. - -terminate({normal, shutdown}, _, _) -> - ok; -terminate({error, overflow}, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_loop_recv.erl b/test/http_SUITE_data/http_loop_recv.erl deleted file mode 100644 index d0577f0..0000000 --- a/test/http_SUITE_data/http_loop_recv.erl +++ /dev/null @@ -1,18 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(http_loop_recv). --behaviour(cowboy_loop_handler). --export([init/3, info/3, terminate/3]). - -init({_, http}, Req, _) -> - self() ! recv_timeout, - {loop, Req, undefined, 500, hibernate}. - -info(recv_timeout, Req, State) -> - {ok, Body, Req1} = cowboy_req:body(Req), - 100000 = byte_size(Body), - {ok, Req2} = cowboy_req:reply(200, Req1), - {ok, Req2, State}. - -terminate({normal, shutdown}, _, _) -> - ok. diff --git a/test/http_SUITE_data/http_loop_timeout.erl b/test/http_SUITE_data/http_loop_timeout.erl deleted file mode 100644 index dd3472c..0000000 --- a/test/http_SUITE_data/http_loop_timeout.erl +++ /dev/null @@ -1,16 +0,0 @@ -%% Feel free to use, reuse and abuse the code in this file. - --module(http_loop_timeout). --behaviour(cowboy_loop_handler). --export([init/3, info/3, terminate/3]). - -init({_, http}, Req, _) -> - erlang:send_after(1000, self(), error_timeout), - {loop, Req, undefined, 500, hibernate}. - -info(error_timeout, Req, State) -> - {ok, Req2} = cowboy_req:reply(500, Req), - {ok, Req2, State}. - -terminate({normal, timeout}, _, _) -> - ok. diff --git a/test/http_SUITE_data/rest_missing_callbacks.erl b/test/http_SUITE_data/rest_missing_callbacks.erl index 171c856..94bfbbd 100644 --- a/test/http_SUITE_data/rest_missing_callbacks.erl +++ b/test/http_SUITE_data/rest_missing_callbacks.erl @@ -11,11 +11,13 @@ allowed_methods(Req, State) -> {[<<"GET">>, <<"PUT">>], Req, State}. content_types_accepted(Req, State) -> + cowboy_error_h:ignore(cowboy_rest, process_content_type, 3), {[ {<<"application/json">>, put_application_json} ], Req, State}. content_types_provided(Req, State) -> + cowboy_error_h:ignore(cowboy_rest, set_resp_body, 2), {[ {<<"text/plain">>, get_text_plain} ], Req, State}. diff --git a/test/http_SUITE_data/rest_resource_etags.erl b/test/http_SUITE_data/rest_resource_etags.erl index 43f1e05..2652f57 100644 --- a/test/http_SUITE_data/rest_resource_etags.erl +++ b/test/http_SUITE_data/rest_resource_etags.erl @@ -18,8 +18,10 @@ generate_etag(Req, State) -> {<<"\"etag-header-value\"">>, Req2, State}; %% Invalid return values from generate_etag/2. {<<"binary-strong-unquoted">>, Req2} -> + cowboy_error_h:ignore(cowboy_http, quoted_string, 2), {<<"etag-header-value">>, Req2, State}; {<<"binary-weak-unquoted">>, Req2} -> + cowboy_error_h:ignore(cowboy_http, quoted_string, 2), {<<"W/etag-header-value">>, Req2, State} end. diff --git a/test/loop_handler_SUITE.erl b/test/loop_handler_SUITE.erl new file mode 100644 index 0000000..5f69490 --- /dev/null +++ b/test/loop_handler_SUITE.erl @@ -0,0 +1,87 @@ +%% Copyright (c) 2011-2014, Loïc Hoguin <[email protected]> +%% +%% Permission to use, copy, modify, and/or distribute this software for any +%% purpose with or without fee is hereby granted, provided that the above +%% copyright notice and this permission notice appear in all copies. +%% +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + +-module(loop_handler_SUITE). +-compile(export_all). + +-import(cowboy_test, [config/2]). +-import(cowboy_test, [doc/1]). +-import(cowboy_test, [gun_open/1]). + +%% ct. + +all() -> + cowboy_test:common_all(). + +groups() -> + cowboy_test:common_groups(cowboy_test:all(?MODULE)). + +init_per_group(Name, Config) -> + cowboy_test:init_common_groups(Name, Config, ?MODULE). + +end_per_group(Name, _) -> + cowboy:stop_listener(Name). + +%% Dispatch configuration. + +init_dispatch(_) -> + cowboy_router:compile([{'_', [ + {"/long_polling", long_polling_h, []}, + {"/loop_body", loop_handler_body_h, []}, + {"/loop_timeout", loop_handler_timeout_h, []} + ]}]). + +%% Tests. + +long_polling(Config) -> + doc("Simple long-polling."), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/long_polling"), + {response, fin, 102, _} = gun:await(ConnPid, Ref), + ok. + +long_polling_body(Config) -> + doc("Long-polling with a body that falls within the configurable limits."), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/long_polling", [], << 0:5000/unit:8 >>), + {response, fin, 102, _} = gun:await(ConnPid, Ref), + ok. + +long_polling_body_too_large(Config) -> + doc("Long-polling with a body that exceeds the configurable limits."), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/long_polling", [], << 0:100000/unit:8 >>), + {response, fin, 500, _} = gun:await(ConnPid, Ref), + ok. + +long_polling_pipeline(Config) -> + doc("Pipeline of long-polling calls."), + ConnPid = gun_open(Config), + Refs = [gun:get(ConnPid, "/long_polling") || _ <- lists:seq(1, 2)], + _ = [{response, fin, 102, _} = gun:await(ConnPid, Ref) || Ref <- Refs], + ok. + +loop_body(Config) -> + doc("Check that a loop handler can read the request body in info/3."), + ConnPid = gun_open(Config), + Ref = gun:post(ConnPid, "/loop_body", [], << 0:100000/unit:8 >>), + {response, fin, 200, _} = gun:await(ConnPid, Ref), + ok. + +loop_timeout(Config) -> + doc("Ensure that the loop handler timeout results in a 204 response."), + ConnPid = gun_open(Config), + Ref = gun:get(ConnPid, "/loop_timeout"), + {response, fin, 204, _} = gun:await(ConnPid, Ref), + ok. diff --git a/test/spdy_SUITE.erl b/test/spdy_SUITE.erl index a7e1a7e..2e9c7e4 100644 --- a/test/spdy_SUITE.erl +++ b/test/spdy_SUITE.erl @@ -13,21 +13,10 @@ %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -module(spdy_SUITE). +-compile(export_all). --include_lib("common_test/include/ct.hrl"). - -%% ct. --export([all/0]). --export([groups/0]). --export([init_per_suite/1]). --export([end_per_suite/1]). --export([init_per_group/2]). --export([end_per_group/2]). - -%% Tests. --export([check_status/1]). --export([echo_body/1]). --export([echo_body_multi/1]). +-import(cowboy_test, [config/2]). +-import(cowboy_test, [gun_monitor_open/1]). %% ct. @@ -35,55 +24,28 @@ all() -> [{group, spdy}]. groups() -> - [{spdy, [], [ - check_status, - echo_body, - echo_body_multi - ]}]. + [{spdy, [], cowboy_test:all(?MODULE)}]. init_per_suite(Config) -> case proplists:get_value(ssl_app, ssl:versions()) of Version when Version < "5.2.1" -> {skip, "No NPN support in SSL application."}; _ -> - application:start(crypto), - application:start(cowlib), - application:start(ranch), - application:start(cowboy), - application:start(asn1), - application:start(public_key), - application:start(ssl), - application:start(gun), - Dir = ?config(priv_dir, Config) ++ "/static", + Dir = config(priv_dir, Config) ++ "/static", ct_helper:create_static_dir(Dir), [{static_dir, Dir}|Config] end. end_per_suite(Config) -> - Dir = ?config(static_dir, Config), - ct_helper:delete_static_dir(Dir), - application:stop(gun), - application:stop(ssl), - application:stop(public_key), - application:stop(asn1), - application:stop(cowboy), - application:stop(ranch), - application:stop(cowlib), - application:stop(crypto), - ok. + ct_helper:delete_static_dir(config(static_dir, Config)). init_per_group(Name, Config) -> - {_, Cert, Key} = ct_helper:make_certs(), - Opts = [{cert, Cert}, {key, Key}], - {ok, _} = cowboy:start_spdy(Name, 100, Opts ++ [{port, 0}], [ + cowboy_test:init_spdy(Name, [ {env, [{dispatch, init_dispatch(Config)}]} - ]), - Port = ranch:get_port(Name), - [{port, Port}|Config]. + ], Config). end_per_group(Name, _) -> - cowboy:stop_listener(Name), - ok. + cowboy:stop_listener(Name). %% Dispatch configuration. @@ -91,7 +53,7 @@ init_dispatch(Config) -> cowboy_router:compile([ {"localhost", [ {"/static/[...]", cowboy_static, - {dir, ?config(static_dir, Config)}}, + {dir, config(static_dir, Config)}}, {"/echo/body", http_echo_body, []}, {"/chunked", http_chunked, []}, {"/", http_handler, []} @@ -100,12 +62,7 @@ init_dispatch(Config) -> %% Convenience functions. -gun_monitor_open(Config) -> - {_, Port} = lists:keyfind(port, 1, Config), - {ok, ConnPid} = gun:open("localhost", Port, [{retry, 0}]), - {ConnPid, monitor(process, ConnPid)}. - -quick_get(ConnPid, MRef, Host, Path) -> +do_get(ConnPid, MRef, Host, Path) -> StreamRef = gun:get(ConnPid, Path, [{":host", Host}]), {response, IsFin, Status, _} = gun:await(ConnPid, StreamRef, MRef), {IsFin, Status}. @@ -123,7 +80,7 @@ check_status(Config) -> ], {ConnPid, MRef} = gun_monitor_open(Config), _ = [{Status, Fin, Host, Path} = begin - {IsFin, Ret} = quick_get(ConnPid, MRef, Host, Path), + {IsFin, Ret} = do_get(ConnPid, MRef, Host, Path), {Ret, IsFin, Host, Path} end || {Status, Fin, Host, Path} <- Tests], gun:close(ConnPid). diff --git a/test/ws_SUITE.erl b/test/ws_SUITE.erl index cdb336a..77c82f6 100644 --- a/test/ws_SUITE.erl +++ b/test/ws_SUITE.erl @@ -13,94 +13,53 @@ %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -module(ws_SUITE). +-compile(export_all). --include_lib("common_test/include/ct.hrl"). - -%% ct. --export([all/0]). --export([groups/0]). --export([init_per_suite/1]). --export([end_per_suite/1]). --export([init_per_group/2]). --export([end_per_group/2]). - -%% Tests. --export([ws0/1]). --export([ws8/1]). --export([ws8_init_shutdown/1]). --export([ws8_single_bytes/1]). --export([ws13/1]). --export([ws_deflate/1]). --export([ws_deflate_chunks/1]). --export([ws_deflate_fragments/1]). --export([ws_send_close/1]). --export([ws_send_close_payload/1]). --export([ws_send_many/1]). --export([ws_text_fragments/1]). --export([ws_timeout_hibernate/1]). --export([ws_timeout_cancel/1]). --export([ws_timeout_reset/1]). --export([ws_upgrade_with_opts/1]). +-import(cowboy_test, [config/2]). %% ct. all() -> - [{group, ws}]. + [{group, autobahn}, {group, ws}]. groups() -> - BaseTests = [ - ws0, - ws8, - ws8_init_shutdown, - ws8_single_bytes, - ws13, - ws_deflate, - ws_deflate_chunks, - ws_deflate_fragments, - ws_send_close, - ws_send_close_payload, - ws_send_many, - ws_text_fragments, - ws_timeout_hibernate, - ws_timeout_cancel, - ws_timeout_reset, - ws_upgrade_with_opts - ], - [{ws, [parallel], BaseTests}]. + BaseTests = cowboy_test:all(?MODULE) -- [autobahn_fuzzingclient], + [{autobahn, [], [autobahn_fuzzingclient]}, {ws, [parallel], BaseTests}]. init_per_suite(Config) -> - application:start(crypto), - application:start(cowlib), - application:start(ranch), - application:start(cowboy), Config. -end_per_suite(_Config) -> - application:stop(cowboy), - application:stop(ranch), - application:stop(cowlib), - application:stop(crypto), - ok. - -init_per_group(ws, Config) -> - cowboy:start_http(ws, 100, [{port, 0}], [ +init_per_group(Name = autobahn, Config) -> + %% Some systems have it named pip2. + Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"), + case string:str(Out, "autobahntestsuite") of + 0 -> + ct:print("Skipping the autobahn group because the " + "Autobahn Test Suite is not installed.~nTo install it, " + "please follow the instructions on this page:~n~n " + "http://autobahn.ws/testsuite/installation.html"), + {skip, "Autobahn Test Suite not installed."}; + _ -> + {ok, _} = cowboy:start_http(Name, 100, [{port, 33080}], [ + {env, [{dispatch, init_dispatch()}]}]), + Config + end; +init_per_group(Name = ws, Config) -> + cowboy_test:init_http(Name, [ {env, [{dispatch, init_dispatch()}]}, {compress, true} - ]), - Port = ranch:get_port(ws), - [{port, Port}|Config]. + ], Config). end_per_group(Listener, _Config) -> - cowboy:stop_listener(Listener), - ok. + cowboy:stop_listener(Listener). %% Dispatch configuration. init_dispatch() -> cowboy_router:compile([ {"localhost", [ - {"/ws_echo_timer", ws_echo_timer, []}, {"/ws_echo", ws_echo, []}, + {"/ws_echo_timer", ws_echo_timer, []}, {"/ws_init_shutdown", ws_init_shutdown, []}, {"/ws_send_many", ws_send_many, [ {sequence, [ @@ -127,7 +86,21 @@ init_dispatch() -> ]} ]). -%% ws and wss. +%% Tests. + +autobahn_fuzzingclient(Config) -> + Out = os:cmd("cd " ++ config(priv_dir, Config) + ++ " && wstest -m fuzzingclient -s " + ++ config(data_dir, Config) ++ "client.json"), + Report = config(priv_dir, Config) ++ "reports/servers/index.html", + ct:log("<h2><a href=\"~s\">Full report</a></h2>~n", [Report]), + ct:print("Autobahn Test Suite report: file://~s~n", [Report]), + ct:log("~s~n", [Out]), + {ok, HTML} = file:read_file(Report), + case length(binary:matches(HTML, <<"case_failed">>)) > 2 of + true -> error(failed); + false -> ok + end. %% We do not support hixie76 anymore. ws0(Config) -> @@ -163,7 +136,7 @@ ws8(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -223,7 +196,7 @@ ws8_single_bytes(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -283,7 +256,7 @@ ws13(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -335,7 +308,7 @@ ws_deflate(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -346,7 +319,7 @@ ws_deflate(Config) -> Mask = 16#11223344, Hello = << 242, 72, 205, 201, 201, 7, 0 >>, - MaskedHello = websocket_mask(Hello, Mask, <<>>), + MaskedHello = do_mask(Hello, Mask, <<>>), % send compressed text frame containing the Hello string ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, @@ -377,7 +350,7 @@ ws_deflate_chunks(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -388,7 +361,7 @@ ws_deflate_chunks(Config) -> Mask = 16#11223344, Hello = << 242, 72, 205, 201, 201, 7, 0 >>, - MaskedHello = websocket_mask(Hello, Mask, <<>>), + MaskedHello = do_mask(Hello, Mask, <<>>), % send compressed text frame containing the Hello string ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, @@ -422,7 +395,7 @@ ws_deflate_fragments(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -437,9 +410,9 @@ ws_deflate_fragments(Config) -> % send compressed text frame containing the Hello string % as 2 separate fragments ok = gen_tcp:send(Socket, << 0:1, 1:1, 0:2, 1:4, 1:1, 4:7, Mask:32, - (websocket_mask(binary:part(Hello, 0, 4), Mask, <<>>))/binary >>), + (do_mask(binary:part(Hello, 0, 4), Mask, <<>>))/binary >>), ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 0:4, 1:1, 3:7, Mask:32, - (websocket_mask(binary:part(Hello, 4, 3), Mask, <<>>))/binary >>), + (do_mask(binary:part(Hello, 4, 3), Mask, <<>>))/binary >>), % receive compressed text frame containing the Hello string {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, Hello/binary >>} = gen_tcp:recv(Socket, 0, 6000), @@ -465,7 +438,7 @@ ws_send_close(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -494,7 +467,7 @@ ws_send_close_payload(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -523,7 +496,7 @@ ws_send_many(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -555,7 +528,7 @@ ws_text_fragments(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -609,7 +582,7 @@ ws_timeout_hibernate(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -636,7 +609,7 @@ ws_timeout_cancel(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -663,7 +636,7 @@ ws_timeout_reset(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -696,7 +669,7 @@ ws_upgrade_with_opts(Config) -> {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000), {ok, {http_response, {1, 1}, 101, "Switching Protocols"}, Rest} = erlang:decode_packet(http, Handshake, []), - [Headers, <<>>] = websocket_headers( + [Headers, <<>>] = do_decode_headers( erlang:decode_packet(httph, Rest, []), []), {'Connection', "Upgrade"} = lists:keyfind('Connection', 1, Headers), {'Upgrade', "websocket"} = lists:keyfind('Upgrade', 1, Headers), @@ -711,27 +684,27 @@ ws_upgrade_with_opts(Config) -> %% Internal. -websocket_headers({ok, http_eoh, Rest}, Acc) -> +do_decode_headers({ok, http_eoh, Rest}, Acc) -> [Acc, Rest]; -websocket_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) -> +do_decode_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) -> F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end, - websocket_headers(erlang:decode_packet(httph, Rest, []), + do_decode_headers(erlang:decode_packet(httph, Rest, []), [{F(Key), Value}|Acc]). -websocket_mask(<<>>, _, Unmasked) -> - Unmasked; -websocket_mask(<< O:32, Rest/bits >>, MaskKey, Acc) -> +do_mask(<<>>, _, Acc) -> + Acc; +do_mask(<< O:32, Rest/bits >>, MaskKey, Acc) -> T = O bxor MaskKey, - websocket_mask(Rest, MaskKey, << Acc/binary, T:32 >>); -websocket_mask(<< O:24 >>, MaskKey, Acc) -> + do_mask(Rest, MaskKey, << Acc/binary, T:32 >>); +do_mask(<< O:24 >>, MaskKey, Acc) -> << MaskKey2:24, _:8 >> = << MaskKey:32 >>, T = O bxor MaskKey2, << Acc/binary, T:24 >>; -websocket_mask(<< O:16 >>, MaskKey, Acc) -> +do_mask(<< O:16 >>, MaskKey, Acc) -> << MaskKey2:16, _:16 >> = << MaskKey:32 >>, T = O bxor MaskKey2, << Acc/binary, T:16 >>; -websocket_mask(<< O:8 >>, MaskKey, Acc) -> +do_mask(<< O:8 >>, MaskKey, Acc) -> << MaskKey2:8, _:24 >> = << MaskKey:32 >>, T = O bxor MaskKey2, << Acc/binary, T:8 >>. diff --git a/test/ws_SUITE_data/client.json b/test/ws_SUITE_data/client.json new file mode 100644 index 0000000..7899503 --- /dev/null +++ b/test/ws_SUITE_data/client.json @@ -0,0 +1,14 @@ +{ + "options": {"failByDrop": false}, + "enable-ssl": false, + + "servers": [{ + "agent": "Cowboy", + "url": "ws://localhost:33080/ws_echo", + "options": {"version": 18} + }], + + "cases": ["*"], + "exclude-cases": [], + "exclude-agent-cases": {} +} |