Debian Codegraph @

test/cowboy_test.erl

raw
1 %% Copyright (c) Loïc Hoguin <[email protected]>
2 %%
3 %% Permission to use, copy, modify, and/or distribute this software for any
4 %% purpose with or without fee is hereby granted, provided that the above
5 %% copyright notice and this permission notice appear in all copies.
6 %%
7 %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13 %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 -module(cowboy_test).
16 -compile(export_all).
17 -compile(nowarn_export_all).
18
19 -import(ct_helper, [config/2]).
20
21 %% Listeners initialization.
22
23 init_http(Ref, ProtoOpts, Config) ->
24 {ok, _} = cowboy:start_clear(Ref, [{port, 0}], ProtoOpts),
25 Port = ranch:get_port(Ref),
26 [{ref, Ref}, {type, tcp}, {protocol, http}, {port, Port}, {opts, []}|Config].
27
28 init_https(Ref, ProtoOpts, Config) ->
29 Opts = ct_helper:get_certs_from_ets(),
30 {ok, _} = cowboy:start_tls(Ref, Opts ++ [{port, 0}, {verify, verify_none}], ProtoOpts),
31 Port = ranch:get_port(Ref),
32 [{ref, Ref}, {type, ssl}, {protocol, http}, {port, Port}, {opts, Opts}|Config].
33
34 init_http2(Ref, ProtoOpts, Config) ->
35 Opts = ct_helper:get_certs_from_ets(),
36 {ok, _} = cowboy:start_tls(Ref, Opts ++ [{port, 0}, {verify, verify_none}], ProtoOpts),
37 Port = ranch:get_port(Ref),
38 [{ref, Ref}, {type, ssl}, {protocol, http2}, {port, Port}, {opts, Opts}|Config].
39
40 %% @todo This will probably require TransOpts as argument.
41 init_http3(Ref, ProtoOpts, Config) ->
42 %% @todo Quicer does not currently support non-file cert/key,
43 %% so we use quicer test certificates for now.
44 %% @todo Quicer also does not support cacerts which means
45 %% we currently have no authentication based security.
46 DataDir = filename:dirname(filename:dirname(config(data_dir, Config)))
47 ++ "/rfc9114_SUITE_data",
48 TransOpts = #{
49 socket_opts => [
50 {certfile, DataDir ++ "/server.pem"},
51 {keyfile, DataDir ++ "/server.key"}
52 ]
53 },
54 {ok, Listener} = cowboy:start_quic(Ref, TransOpts, ProtoOpts),
55 {ok, {_, Port}} = quicer:sockname(Listener),
56 %% @todo Keep listener information around in a better place.
57 persistent_term:put({cowboy_test_quic, Ref}, Listener),
58 [{ref, Ref}, {type, quic}, {protocol, http3}, {port, Port}, {opts, TransOpts}|Config].
59
60 stop_group(Ref) ->
61 case persistent_term:get({cowboy_test_quic, Ref}, undefined) of
62 undefined ->
63 cowboy:stop_listener(Ref);
64 Listener ->
65 quicer:close_listener(Listener)
66 end.
67
68 %% Common group of listeners used by most suites.
69
70 common_all() ->
71 All = [
72 {group, http},
73 {group, https},
74 {group, h2},
75 {group, h2c},
76 % {group, h3},
77 {group, http_compress},
78 {group, https_compress},
79 {group, h2_compress},
80 {group, h2c_compress}
81 % {group, h3_compress}
82 ],
83 %% Don't run HTTP/3 tests on Windows for now.
84 case os:type() of
85 {win32, _} ->
86 All -- [{group, h3}, {group, h3_compress}];
87 _ ->
88 All
89 end.
90
91 common_groups(Tests) ->
92 Parallel = case os:getenv("NO_PARALLEL") of
93 false -> parallel;
94 _ -> no_parallel
95 end,
96 common_groups(Tests, Parallel).
97
98 common_groups(Tests, Parallel) ->
99 Opts = case Parallel of
100 parallel -> [parallel];
101 no_parallel -> []
102 end,
103 Groups = [
104 {http, Opts, Tests},
105 {https, Opts, Tests},
106 {h2, Opts, Tests},
107 {h2c, Opts, Tests},
108 {h3, Opts, Tests},
109 {http_compress, Opts, Tests},
110 {https_compress, Opts, Tests},
111 {h2_compress, Opts, Tests},
112 {h2c_compress, Opts, Tests},
113 {h3_compress, Opts, Tests}
114 ],
115 %% Don't run HTTP/3 tests on Windows for now.
116 case os:type() of
117 {win32, _} ->
118 Groups -- [{h3, Opts, Tests}, {h3_compress, Opts, Tests}];
119 _ ->
120 Groups
121 end.
122
123 init_common_groups(Name, Config, Mod) ->
124 init_common_groups(Name, Config, Mod, #{}).
125
126 init_common_groups(Name = http, Config, Mod, ProtoOpts) ->
127 init_http(Name, ProtoOpts#{
128 env => #{dispatch => Mod:init_dispatch(Config)}
129 }, [{flavor, vanilla}|Config]);
130 init_common_groups(Name = https, Config, Mod, ProtoOpts) ->
131 init_https(Name, ProtoOpts#{
132 env => #{dispatch => Mod:init_dispatch(Config)}
133 }, [{flavor, vanilla}|Config]);
134 init_common_groups(Name = h2, Config, Mod, ProtoOpts) ->
135 init_http2(Name, ProtoOpts#{
136 env => #{dispatch => Mod:init_dispatch(Config)}
137 }, [{flavor, vanilla}|Config]);
138 init_common_groups(Name = h2c, Config, Mod, ProtoOpts) ->
139 Config1 = init_http(Name, ProtoOpts#{
140 env => #{dispatch => Mod:init_dispatch(Config)}
141 }, [{flavor, vanilla}|Config]),
142 lists:keyreplace(protocol, 1, Config1, {protocol, http2});
143 init_common_groups(Name = h3, Config, Mod, ProtoOpts) ->
144 init_http3(Name, ProtoOpts#{
145 env => #{dispatch => Mod:init_dispatch(Config)}
146 }, [{flavor, vanilla}|Config]);
147 init_common_groups(Name = http_compress, Config, Mod, ProtoOpts) ->
148 init_http(Name, ProtoOpts#{
149 env => #{dispatch => Mod:init_dispatch(Config)},
150 stream_handlers => [cowboy_compress_h, cowboy_stream_h]
151 }, [{flavor, compress}|Config]);
152 init_common_groups(Name = https_compress, Config, Mod, ProtoOpts) ->
153 init_https(Name, ProtoOpts#{
154 env => #{dispatch => Mod:init_dispatch(Config)},
155 stream_handlers => [cowboy_compress_h, cowboy_stream_h]
156 }, [{flavor, compress}|Config]);
157 init_common_groups(Name = h2_compress, Config, Mod, ProtoOpts) ->
158 init_http2(Name, ProtoOpts#{
159 env => #{dispatch => Mod:init_dispatch(Config)},
160 stream_handlers => [cowboy_compress_h, cowboy_stream_h]
161 }, [{flavor, compress}|Config]);
162 init_common_groups(Name = h2c_compress, Config, Mod, ProtoOpts) ->
163 Config1 = init_http(Name, ProtoOpts#{
164 env => #{dispatch => Mod:init_dispatch(Config)},
165 stream_handlers => [cowboy_compress_h, cowboy_stream_h]
166 }, [{flavor, compress}|Config]),
167 lists:keyreplace(protocol, 1, Config1, {protocol, http2});
168 init_common_groups(Name = h3_compress, Config, Mod, ProtoOpts) ->
169 init_http3(Name, ProtoOpts#{
170 env => #{dispatch => Mod:init_dispatch(Config)},
171 stream_handlers => [cowboy_compress_h, cowboy_stream_h]
172 }, [{flavor, compress}|Config]).
173
174 %% Support functions for testing using Gun.
175
176 gun_open(Config) ->
177 gun_open(Config, #{}).
178
179 gun_open(Config, Opts) ->
180 TlsOpts = case proplists:get_value(no_cert, Config, false) of
181 true -> [{verify, verify_none}];
182 false -> ct_helper:get_certs_from_ets() %% @todo Wrong in current quicer.
183 end,
184 {ok, ConnPid} = gun:open("localhost", config(port, Config), Opts#{
185 retry => 0,
186 transport => config(type, Config),
187 tls_opts => TlsOpts,
188 protocols => [config(protocol, Config)]
189 }),
190 ConnPid.
191
192 gun_down(ConnPid) ->
193 receive {gun_down, ConnPid, _, _, _} -> ok
194 after 500 -> error(timeout) end.
195
196 %% Support functions for testing using a raw socket.
197
198 raw_open(Config) ->
199 Transport = case config(type, Config) of
200 tcp -> gen_tcp;
201 ssl -> ssl
202 end,
203 {_, Opts} = lists:keyfind(opts, 1, Config),
204 {ok, Socket} = Transport:connect("localhost", config(port, Config),
205 [binary, {active, false}, {packet, raw},
206 {reuseaddr, true}, {nodelay, true}|Opts]),
207 {raw_client, Socket, Transport}.
208
209 raw_send({raw_client, Socket, Transport}, Data) ->
210 Transport:send(Socket, Data).
211
212 raw_recv_head({raw_client, Socket, Transport}) ->
213 {ok, Data} = Transport:recv(Socket, 0, 10000),
214 raw_recv_head(Socket, Transport, Data).
215
216 raw_recv_head(Socket, Transport, Buffer) ->
217 case binary:match(Buffer, <<"\r\n\r\n">>) of
218 nomatch ->
219 {ok, Data} = Transport:recv(Socket, 0, 10000),
220 raw_recv_head(Socket, Transport, << Buffer/binary, Data/binary >>);
221 {_, _} ->
222 Buffer
223 end.
224
225 raw_recv_rest({raw_client, _, _}, Length, Buffer) when Length =:= byte_size(Buffer) ->
226 Buffer;
227 raw_recv_rest({raw_client, Socket, Transport}, Length, Buffer) when Length > byte_size(Buffer) ->
228 {ok, Data} = Transport:recv(Socket, Length - byte_size(Buffer), 10000),
229 << Buffer/binary, Data/binary >>.
230
231 raw_recv({raw_client, Socket, Transport}, Length, Timeout) ->
232 Transport:recv(Socket, Length, Timeout).
233
234 raw_expect_recv({raw_client, _, _}, <<>>) ->
235 ok;
236 raw_expect_recv({raw_client, Socket, Transport}, Expect) ->
237 {ok, Expect} = Transport:recv(Socket, iolist_size(Expect), 10000),
238 ok.