aboutsummaryrefslogtreecommitdiffstats
path: root/test/spdy_SUITE.erl
blob: 469e5d64c67d1916d94a79d01819e0c779ba7dc4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
%% Copyright (c) 2013, 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(spdy_SUITE).

-include_lib("common_test/include/ct.hrl").
-include("../src/cowboy_spdy.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]).

%% ct.

all() ->
	[{group, spdy}].

groups() ->
	[{spdy, [], [
		check_status
	]}].

init_per_suite(Config) ->
	application:start(crypto),
	application:start(ranch),
	application:start(cowboy),
	application:start(asn1),
	application:start(public_key),
	application:start(ssl),
	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(ssl),
	application:stop(public_key),
	application:stop(asn1),
	application:stop(cowboy),
	application:stop(ranch),
	application:stop(crypto),
	ok.

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}], [
		{env, [{dispatch, init_dispatch(Config)}]}
	]),
	Port = ranch:get_port(Name),
	[{port, Port}|Config].

end_per_group(Name, _) ->
	cowboy:stop_listener(Name),
	ok.

%% Dispatch configuration.

init_dispatch(Config) ->
	cowboy_router:compile([
		{"localhost", [
			{"/static/[...]", cowboy_static,
				[{directory, ?config(static_dir, Config)},
				 {mimetypes, [{<<".css">>, [<<"text/css">>]}]}]},
			{"/chunked", http_chunked, []},
			{"/", http_handler, []}
		]}
	]).

%% Convenience functions.

quick_get(Host, Path, ExpectedFlags, Config) ->
	{_, Port} = lists:keyfind(port, 1, Config),
	{ok, Socket} = ssl:connect("localhost", Port, [
		binary, {active, false},
		{client_preferred_next_protocols, client, [<<"spdy/3">>]}
	]),
	{Zdef, Zinf} = zlib_init(),
	ReqHeaders = headers_encode(Zdef, [
		{<<":method">>, <<"GET">>},
		{<<":path">>, list_to_binary(Path)},
		{<<":version">>, <<"HTTP/1.1">>},
		{<<":host">>, list_to_binary(Host)},
		{<<":scheme">>, <<"https">>}
	]),
	ReqLength = 10 + byte_size(ReqHeaders),
	StreamID = 1,
	ok = ssl:send(Socket, << 1:1, 3:15, 1:16, 0:8, ReqLength:24,
		0:1, StreamID:31, 0:1, 0:31, 0:3, 0:5, 0:8, ReqHeaders/binary >>),
	{ok, Packet} = ssl:recv(Socket, 0, 1000),
	<< 1:1, 3:15, 2:16, Flags:8, RespLength:24,
		_:1, StreamID:31, RespHeaders/bits >> = Packet,
	Flags = ExpectedFlags,
	RespLength = 4 + byte_size(RespHeaders),
	[<< NbHeaders:32, Rest/bits >>] = try
		zlib:inflate(Zinf, RespHeaders)
	catch _:_ ->
		ok = zlib:inflateSetDictionary(Zinf, ?ZDICT),
		zlib:inflate(Zinf, <<>>)
	end,
	RespHeaders2 = headers_decode(Zinf, Rest, []),
	NbHeaders = length(RespHeaders2),
	{_, << Status:3/binary, _/bits >>}
		= lists:keyfind(<<":status">>, 1, RespHeaders2),
	StatusCode = list_to_integer(binary_to_list(Status)),
	ok = ssl:close(Socket),
	zlib_terminate(Zdef, Zinf),
	{StatusCode, RespHeaders2}.

zlib_init() ->
	Zdef = zlib:open(),
	ok = zlib:deflateInit(Zdef),
	_ = zlib:deflateSetDictionary(Zdef, ?ZDICT),
	Zinf = zlib:open(),
	ok = zlib:inflateInit(Zinf),
	{Zdef, Zinf}.

zlib_terminate(Zdef, Zinf) ->
	zlib:close(Zdef),
	zlib:close(Zinf).

headers_encode(Zdef, Headers) ->
	NbHeaders = length(Headers),
	Headers2 = << << (begin
		SizeN = byte_size(N),
		SizeV = byte_size(V),
		<< SizeN:32, N/binary, SizeV:32, V/binary >>
	end)/binary >> || {N, V} <- Headers >>,
	Headers3 = << NbHeaders:32, Headers2/binary >>,
	iolist_to_binary(zlib:deflate(Zdef, Headers3, full)).

headers_decode(_, <<>>, Acc) ->
	lists:reverse(Acc);
headers_decode(Zinf, << SizeN:32, Rest/bits >>, Acc) ->
	<< Name:SizeN/binary, SizeV:32, Rest2/bits >> = Rest,
	<< Value:SizeV/binary, Rest3/bits >> = Rest2,
	headers_decode(Zinf, Rest3, [{Name, Value}|Acc]).

%% Tests.

check_status(Config) ->
	Tests = [
		{200, nofin, "localhost", "/"},
		{200, nofin, "localhost", "/chunked"},
		{200, nofin, "localhost", "/static/style.css"},
		{400, fin, "bad-host", "/"},
		{400, fin, "localhost", "bad-path"},
		{404, fin, "localhost", "/this/path/does/not/exist"}
	],
	_ = [{Status, Fin, Host, Path} = begin
		RespFlags = case Fin of fin -> 1; nofin -> 0 end,
		{Ret, _} = quick_get(Host, Path, RespFlags, Config),
		{Ret, Fin, Host, Path}
	end || {Status, Fin, Host, Path} <- Tests].