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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
%% Copyright (c) 2018, 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(ws_handler_SUITE).
-compile(export_all).
-compile(nowarn_export_all).
-import(ct_helper, [config/2]).
-import(ct_helper, [doc/1]).
-import(cowboy_test, [gun_open/1]).
-import(cowboy_test, [gun_down/1]).
%% ct.
all() ->
[{group, ws}, {group, ws_hibernate}].
%% @todo Test against HTTP/2 too.
groups() ->
AllTests = ct_helper:all(?MODULE),
[{ws, [parallel], AllTests}, {ws_hibernate, [parallel], AllTests}].
init_per_group(Name, Config) ->
cowboy_test:init_http(Name, #{
env => #{dispatch => init_dispatch(Name)}
}, Config).
end_per_group(Name, _) ->
cowboy:stop_listener(Name).
%% Dispatch configuration.
init_dispatch(Name) ->
RunOrHibernate = case Name of
ws -> run;
ws_hibernate -> hibernate
end,
cowboy_router:compile([{'_', [
{"/init", ws_init_commands_h, RunOrHibernate},
{"/handle", ws_handle_commands_h, RunOrHibernate},
{"/info", ws_info_commands_h, RunOrHibernate},
{"/active", ws_active_commands_h, RunOrHibernate},
{"/deflate", ws_deflate_commands_h, RunOrHibernate},
{"/set_options", ws_set_options_commands_h, RunOrHibernate}
]}]).
%% Support functions for testing using Gun.
gun_open_ws(Config, Path, Commands) ->
ConnPid = gun_open(Config),
StreamRef = gun:ws_upgrade(ConnPid, Path, [
{<<"x-commands">>, base64:encode(term_to_binary(Commands))}
]),
receive
{gun_upgrade, ConnPid, StreamRef, [<<"websocket">>], _} ->
{ok, ConnPid, StreamRef};
{gun_response, ConnPid, _, _, Status, Headers} ->
exit({ws_upgrade_failed, Status, Headers});
{gun_error, ConnPid, StreamRef, Reason} ->
exit({ws_upgrade_failed, Reason})
after 1000 ->
error(timeout)
end.
receive_ws(ConnPid, StreamRef) ->
receive
{gun_ws, ConnPid, StreamRef, Frame} ->
{ok, Frame}
after 1000 ->
{error, timeout}
end.
ensure_handle_is_called(ConnPid, "/handle") ->
gun:ws_send(ConnPid, {text, <<"Necessary to trigger websocket_handle/2.">>});
ensure_handle_is_called(_, _) ->
ok.
%% Tests.
websocket_init_nothing(Config) ->
doc("Nothing happens when websocket_init/1 returns no commands."),
do_nothing(Config, "/init").
websocket_handle_nothing(Config) ->
doc("Nothing happens when websocket_handle/2 returns no commands."),
do_nothing(Config, "/handle").
websocket_info_nothing(Config) ->
doc("Nothing happens when websocket_info/2 returns no commands."),
do_nothing(Config, "/info").
do_nothing(Config, Path) ->
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, []),
ensure_handle_is_called(ConnPid, Path),
{error, timeout} = receive_ws(ConnPid, StreamRef),
ok.
websocket_init_invalid(Config) ->
doc("The connection must be closed when websocket_init/1 returns an invalid command."),
do_invalid(Config, "/init").
websocket_handle_invalid(Config) ->
doc("The connection must be closed when websocket_handle/2 returns an invalid command."),
do_invalid(Config, "/init").
websocket_info_invalid(Config) ->
doc("The connection must be closed when websocket_info/2 returns an invalid command."),
do_invalid(Config, "/info").
do_invalid(Config, Path) ->
{ok, ConnPid, _} = gun_open_ws(Config, Path, bad),
ensure_handle_is_called(ConnPid, Path),
gun_down(ConnPid).
websocket_init_one_frame(Config) ->
doc("A single frame is received when websocket_init/1 returns it as a command."),
do_one_frame(Config, "/init").
websocket_handle_one_frame(Config) ->
doc("A single frame is received when websocket_handle/2 returns it as a command."),
do_one_frame(Config, "/handle").
websocket_info_one_frame(Config) ->
doc("A single frame is received when websocket_info/2 returns it as a command."),
do_one_frame(Config, "/info").
do_one_frame(Config, Path) ->
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [
{text, <<"One frame!">>}
]),
ensure_handle_is_called(ConnPid, Path),
{ok, {text, <<"One frame!">>}} = receive_ws(ConnPid, StreamRef),
ok.
websocket_init_many_frames(Config) ->
doc("Multiple frames are received when websocket_init/1 returns them as commands."),
do_many_frames(Config, "/init").
websocket_handle_many_frames(Config) ->
doc("Multiple frames are received when websocket_handle/2 returns them as commands."),
do_many_frames(Config, "/handle").
websocket_info_many_frames(Config) ->
doc("Multiple frames are received when websocket_info/2 returns them as commands."),
do_many_frames(Config, "/info").
do_many_frames(Config, Path) ->
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [
{text, <<"One frame!">>},
{binary, <<"Two frames!">>}
]),
ensure_handle_is_called(ConnPid, Path),
{ok, {text, <<"One frame!">>}} = receive_ws(ConnPid, StreamRef),
{ok, {binary, <<"Two frames!">>}} = receive_ws(ConnPid, StreamRef),
ok.
websocket_init_close_frame(Config) ->
doc("A single close frame is received when websocket_init/1 returns it as a command."),
do_close_frame(Config, "/init").
websocket_handle_close_frame(Config) ->
doc("A single close frame is received when websocket_handle/2 returns it as a command."),
do_close_frame(Config, "/handle").
websocket_info_close_frame(Config) ->
doc("A single close frame is received when websocket_info/2 returns it as a command."),
do_close_frame(Config, "/info").
do_close_frame(Config, Path) ->
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [close]),
ensure_handle_is_called(ConnPid, Path),
{ok, close} = receive_ws(ConnPid, StreamRef),
gun_down(ConnPid).
websocket_init_many_frames_then_close_frame(Config) ->
doc("Multiple frames are received followed by a close frame "
"when websocket_init/1 returns them as commands."),
do_many_frames_then_close_frame(Config, "/init").
websocket_handle_many_frames_then_close_frame(Config) ->
doc("Multiple frames are received followed by a close frame "
"when websocket_handle/2 returns them as commands."),
do_many_frames_then_close_frame(Config, "/handle").
websocket_info_many_frames_then_close_frame(Config) ->
doc("Multiple frames are received followed by a close frame "
"when websocket_info/2 returns them as commands."),
do_many_frames_then_close_frame(Config, "/info").
do_many_frames_then_close_frame(Config, Path) ->
{ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [
{text, <<"One frame!">>},
{binary, <<"Two frames!">>},
close
]),
ensure_handle_is_called(ConnPid, Path),
{ok, {text, <<"One frame!">>}} = receive_ws(ConnPid, StreamRef),
{ok, {binary, <<"Two frames!">>}} = receive_ws(ConnPid, StreamRef),
{ok, close} = receive_ws(ConnPid, StreamRef),
gun_down(ConnPid).
websocket_active_false(Config) ->
doc("The {active, false} command stops receiving data from the socket. "
"The {active, true} command reenables it."),
{ok, ConnPid, StreamRef} = gun_open_ws(Config, "/active", []),
gun:ws_send(ConnPid, {text, <<"Not received until the handler enables active again.">>}),
{error, timeout} = receive_ws(ConnPid, StreamRef),
{ok, {text, <<"Not received until the handler enables active again.">>}}
= receive_ws(ConnPid, StreamRef),
ok.
websocket_deflate_false(Config) ->
doc("The {deflate, false} command temporarily disables compression. "
"The {deflate, true} command reenables it."),
%% We disable context takeover so that the compressed data
%% does not change across all frames.
{ok, Socket, Headers} = ws_SUITE:do_handshake("/deflate",
"Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover\r\n", Config),
{_, "permessage-deflate; server_no_context_takeover"}
= lists:keyfind("sec-websocket-extensions", 1, Headers),
%% The handler receives a compressed "Hello" frame and
%% sends back a compressed or uncompressed echo intermittently.
Mask = 16#11223344,
CompressedHello = <<242, 72, 205, 201, 201, 7, 0>>,
MaskedHello = ws_SUITE:do_mask(CompressedHello, Mask, <<>>),
%% First echo is compressed.
ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>),
{ok, <<1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary>>} = gen_tcp:recv(Socket, 0, 6000),
%% Second echo is not compressed when it is received back.
ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>),
{ok, <<1:1, 0:3, 1:4, 0:1, 5:7, "Hello">>} = gen_tcp:recv(Socket, 0, 6000),
%% Third echo is compressed again.
ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>),
{ok, <<1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary>>} = gen_tcp:recv(Socket, 0, 6000),
%% Client-initiated close.
ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
{ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
{error, closed} = gen_tcp:recv(Socket, 0, 6000),
ok.
websocket_deflate_ignore_if_not_negotiated(Config) ->
doc("The {deflate, boolean()} commands are ignored "
"when compression was not negotiated."),
{ok, ConnPid, StreamRef} = gun_open_ws(Config, "/deflate", []),
_ = [begin
gun:ws_send(ConnPid, {text, <<"Hello.">>}),
{ok, {text, <<"Hello.">>}} = receive_ws(ConnPid, StreamRef)
end || _ <- lists:seq(1, 10)],
ok.
websocket_set_options_idle_timeout(Config) ->
doc("The idle_timeout option can be modified using the "
"command {set_options, Opts} at runtime."),
ConnPid = gun_open(Config),
StreamRef = gun:ws_upgrade(ConnPid, "/set_options"),
receive
{gun_upgrade, ConnPid, StreamRef, [<<"websocket">>], _} ->
ok;
{gun_response, ConnPid, _, _, Status, Headers} ->
exit({ws_upgrade_failed, Status, Headers});
{gun_error, ConnPid, StreamRef, Reason} ->
exit({ws_upgrade_failed, Reason})
after 1000 ->
error(timeout)
end,
%% We don't send anything for a short while and confirm
%% that idle_timeout does not trigger.
{error, timeout} = gun:await(ConnPid, StreamRef, 2000),
%% Trigger the change in idle_timeout and confirm that
%% the connection gets closed soon after.
gun:ws_send(ConnPid, {text, <<"idle_timeout_short">>}),
receive
{gun_down, ConnPid, _, _, _, _} ->
ok
after 2000 ->
error(timeout)
end.
|