0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 12:20:24 +00:00

Enable Autobahn Test Suite by default

Includes a variety of small changes that are a first step to
improving the test system heavily.
This commit is contained in:
Loïc Hoguin 2014-04-20 22:20:54 +02:00
parent 806cde9ace
commit 74512fc84c
11 changed files with 244 additions and 514 deletions

View file

@ -23,11 +23,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

2
all.sh
View file

@ -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

View file

@ -1,100 +0,0 @@
%% Copyright (c) 2011, Magnus Klaar <magnus.klaar@gmail.com>
%%
%% 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.

View file

@ -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.

View file

@ -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()

108
test/cowboy_test.erl Normal file
View file

@ -0,0 +1,108 @@
%% Copyright (c) 2014, Loïc Hoguin <essen@ninenines.eu>
%%
%% 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.
stop(Apps) ->
_ = [application:stop(App) || App <- Apps],
ok.
%% Quick configuration value retrieval.
config(Key, Config) ->
{_, Value} = lists:keyfind(Key, 1, Config),
Value.
%% 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_"
]).
%% 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.

View file

@ -14,16 +14,9 @@
-module(eunit_SUITE).
-include_lib("common_test/include/ct.hrl").
%% ct.
-export([all/0]).
%% Tests.
-export([eunit/1]).
%% ct.
all() ->
[eunit].

View file

@ -16,9 +16,15 @@
-module(http_SUITE).
-compile(export_all).
config(Key, Config) ->
{_, Value} = lists:keyfind(Key, 1, Config),
Value.
-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.
@ -36,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},
@ -125,14 +72,7 @@ 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),
cowboy_test:start([cowboy, gun]),
Dir = config(priv_dir, Config) ++ "/static",
ct_helper:create_static_dir(Dir),
[{static_dir, Dir}|Config].
@ -140,15 +80,7 @@ init_per_suite(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.
cowboy_test:stop([cowboy, gun]).
init_tcp_group(Ref, ProtoOpts, Config) ->
Transport = ranch_tcp,
@ -186,7 +118,7 @@ init_per_group(onrequest, Config) ->
{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),
@ -196,7 +128,7 @@ init_per_group(onresponse, Config) ->
{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),
@ -206,7 +138,7 @@ init_per_group(onresponse_capitalize, Config) ->
{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),
@ -270,13 +202,13 @@ init_dispatch(Config) ->
{dir, config(static_dir, Config)}},
{"/static_mimetypes_function/[...]", cowboy_static,
{dir, config(static_dir, Config),
[{mimetypes, ?MODULE, mimetypes_text_html}]}},
[{mimetypes, ?MODULE, do_mimetypes_text_html}]}},
{"/handler_errors", http_errors, []},
{"/static_attribute_etag/[...]", cowboy_static,
{dir, config(static_dir, Config)}},
{"/static_function_etag/[...]", cowboy_static,
{dir, config(static_dir, Config),
[{etag, ?MODULE, etag_gen}]}},
[{etag, ?MODULE, do_etag_gen}]}},
{"/static_specify_file/[...]", cowboy_static,
{file, config(static_dir, Config) ++ "/style.css"}},
{"/multipart", http_multipart, []},
@ -305,72 +237,17 @@ init_dispatch(Config) ->
]}
]).
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) ->
{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) ->
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) ->
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.
%% 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
@ -378,7 +255,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),
@ -440,7 +317,7 @@ 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.
@ -464,7 +341,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].
@ -570,7 +447,7 @@ http10_hostless(Config) ->
{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).
@ -650,7 +527,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
@ -663,14 +540,14 @@ nc_reqs(Config, Input) ->
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),
@ -689,7 +566,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);
@ -707,7 +584,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),
@ -727,7 +604,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.
@ -1110,9 +987,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;
@ -1120,12 +997,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">>}]),
@ -1139,7 +1016,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">>}]),
@ -1153,7 +1030,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">>}]),
@ -1177,7 +1054,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">>}]),

View file

@ -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,42 +24,23 @@ 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",
cowboy_test:start([cowboy, gun]),
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),
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.
cowboy_test:stop([cowboy, gun]).
init_per_group(Name, Config) ->
{_, Cert, Key} = ct_helper:make_certs(),
@ -79,7 +49,7 @@ init_per_group(Name, Config) ->
{env, [{dispatch, init_dispatch(Config)}]}
]),
Port = ranch:get_port(Name),
[{port, Port}|Config].
[{port, Port}, {type, ssl}|Config].
end_per_group(Name, _) ->
cowboy:stop_listener(Name),
@ -91,7 +61,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 +70,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 +88,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).

View file

@ -13,75 +13,41 @@
%% 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),
cowboy_test:start([cowboy]),
Config.
end_per_suite(_Config) ->
application:stop(cowboy),
application:stop(ranch),
application:stop(cowlib),
application:stop(crypto),
ok.
cowboy_test:stop([cowboy]).
init_per_group(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(autobahn, 100, [{port, 33080}], [
{env, [{dispatch, init_dispatch()}]}]),
Config
end;
init_per_group(ws, Config) ->
cowboy:start_http(ws, 100, [{port, 0}], [
{env, [{dispatch, init_dispatch()}]},
@ -99,8 +65,8 @@ end_per_group(Listener, _Config) ->
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 +93,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 +143,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 +203,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 +263,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 +315,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 +326,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 +357,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 +368,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 +402,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 +417,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 +445,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 +474,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 +503,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 +535,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 +589,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 +616,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 +643,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 +676,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 +691,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 >>.

View file

@ -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": {}
}