ws_handler_SUITE.erl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. %% Copyright (c) 2018, Loïc Hoguin <essen@ninenines.eu>
  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. -module(ws_handler_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(cowboy_test, [gun_open/1]).
  20. -import(cowboy_test, [gun_down/1]).
  21. %% ct.
  22. all() ->
  23. [{group, ws}, {group, ws_hibernate}].
  24. %% @todo Test against HTTP/2 too.
  25. groups() ->
  26. AllTests = ct_helper:all(?MODULE),
  27. [{ws, [parallel], AllTests}, {ws_hibernate, [parallel], AllTests}].
  28. init_per_group(Name, Config) ->
  29. cowboy_test:init_http(Name, #{
  30. env => #{dispatch => init_dispatch(Name)}
  31. }, Config).
  32. end_per_group(Name, _) ->
  33. cowboy:stop_listener(Name).
  34. %% Dispatch configuration.
  35. init_dispatch(Name) ->
  36. RunOrHibernate = case Name of
  37. ws -> run;
  38. ws_hibernate -> hibernate
  39. end,
  40. cowboy_router:compile([{'_', [
  41. {"/init", ws_init_commands_h, RunOrHibernate},
  42. {"/handle", ws_handle_commands_h, RunOrHibernate},
  43. {"/info", ws_info_commands_h, RunOrHibernate},
  44. {"/active", ws_active_commands_h, RunOrHibernate},
  45. {"/deflate", ws_deflate_commands_h, RunOrHibernate},
  46. {"/set_options", ws_set_options_commands_h, RunOrHibernate},
  47. {"/shutdown_reason", ws_shutdown_reason_commands_h, RunOrHibernate}
  48. ]}]).
  49. %% Support functions for testing using Gun.
  50. gun_open_ws(Config, Path, Commands) ->
  51. ConnPid = gun_open(Config),
  52. StreamRef = gun:ws_upgrade(ConnPid, Path, [
  53. {<<"x-commands">>, base64:encode(term_to_binary(Commands))}
  54. ]),
  55. receive
  56. {gun_upgrade, ConnPid, StreamRef, [<<"websocket">>], _} ->
  57. {ok, ConnPid, StreamRef};
  58. {gun_response, ConnPid, _, _, Status, Headers} ->
  59. exit({ws_upgrade_failed, Status, Headers});
  60. {gun_error, ConnPid, StreamRef, Reason} ->
  61. exit({ws_upgrade_failed, Reason})
  62. after 1000 ->
  63. error(timeout)
  64. end.
  65. receive_ws(ConnPid, StreamRef) ->
  66. receive
  67. {gun_ws, ConnPid, StreamRef, Frame} ->
  68. {ok, Frame}
  69. after 1000 ->
  70. {error, timeout}
  71. end.
  72. ensure_handle_is_called(ConnPid, StreamRef, "/handle") ->
  73. gun:ws_send(ConnPid, StreamRef, {text, <<"Necessary to trigger websocket_handle/2.">>});
  74. ensure_handle_is_called(_, _, _) ->
  75. ok.
  76. %% Tests.
  77. websocket_init_nothing(Config) ->
  78. doc("Nothing happens when websocket_init/1 returns no commands."),
  79. do_nothing(Config, "/init").
  80. websocket_handle_nothing(Config) ->
  81. doc("Nothing happens when websocket_handle/2 returns no commands."),
  82. do_nothing(Config, "/handle").
  83. websocket_info_nothing(Config) ->
  84. doc("Nothing happens when websocket_info/2 returns no commands."),
  85. do_nothing(Config, "/info").
  86. do_nothing(Config, Path) ->
  87. {ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, []),
  88. ensure_handle_is_called(ConnPid, StreamRef, Path),
  89. {error, timeout} = receive_ws(ConnPid, StreamRef),
  90. ok.
  91. websocket_init_invalid(Config) ->
  92. doc("The connection must be closed when websocket_init/1 returns an invalid command."),
  93. do_invalid(Config, "/init").
  94. websocket_handle_invalid(Config) ->
  95. doc("The connection must be closed when websocket_handle/2 returns an invalid command."),
  96. do_invalid(Config, "/init").
  97. websocket_info_invalid(Config) ->
  98. doc("The connection must be closed when websocket_info/2 returns an invalid command."),
  99. do_invalid(Config, "/info").
  100. do_invalid(Config, Path) ->
  101. {ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, bad),
  102. ensure_handle_is_called(ConnPid, StreamRef, Path),
  103. gun_down(ConnPid).
  104. websocket_init_one_frame(Config) ->
  105. doc("A single frame is received when websocket_init/1 returns it as a command."),
  106. do_one_frame(Config, "/init").
  107. websocket_handle_one_frame(Config) ->
  108. doc("A single frame is received when websocket_handle/2 returns it as a command."),
  109. do_one_frame(Config, "/handle").
  110. websocket_info_one_frame(Config) ->
  111. doc("A single frame is received when websocket_info/2 returns it as a command."),
  112. do_one_frame(Config, "/info").
  113. do_one_frame(Config, Path) ->
  114. {ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [
  115. {text, <<"One frame!">>}
  116. ]),
  117. ensure_handle_is_called(ConnPid, StreamRef, Path),
  118. {ok, {text, <<"One frame!">>}} = receive_ws(ConnPid, StreamRef),
  119. ok.
  120. websocket_init_many_frames(Config) ->
  121. doc("Multiple frames are received when websocket_init/1 returns them as commands."),
  122. do_many_frames(Config, "/init").
  123. websocket_handle_many_frames(Config) ->
  124. doc("Multiple frames are received when websocket_handle/2 returns them as commands."),
  125. do_many_frames(Config, "/handle").
  126. websocket_info_many_frames(Config) ->
  127. doc("Multiple frames are received when websocket_info/2 returns them as commands."),
  128. do_many_frames(Config, "/info").
  129. do_many_frames(Config, Path) ->
  130. {ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [
  131. {text, <<"One frame!">>},
  132. {binary, <<"Two frames!">>}
  133. ]),
  134. ensure_handle_is_called(ConnPid, StreamRef, Path),
  135. {ok, {text, <<"One frame!">>}} = receive_ws(ConnPid, StreamRef),
  136. {ok, {binary, <<"Two frames!">>}} = receive_ws(ConnPid, StreamRef),
  137. ok.
  138. websocket_init_close_frame(Config) ->
  139. doc("A single close frame is received when websocket_init/1 returns it as a command."),
  140. do_close_frame(Config, "/init").
  141. websocket_handle_close_frame(Config) ->
  142. doc("A single close frame is received when websocket_handle/2 returns it as a command."),
  143. do_close_frame(Config, "/handle").
  144. websocket_info_close_frame(Config) ->
  145. doc("A single close frame is received when websocket_info/2 returns it as a command."),
  146. do_close_frame(Config, "/info").
  147. do_close_frame(Config, Path) ->
  148. {ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [close]),
  149. ensure_handle_is_called(ConnPid, StreamRef, Path),
  150. {ok, close} = receive_ws(ConnPid, StreamRef),
  151. gun_down(ConnPid).
  152. websocket_init_many_frames_then_close_frame(Config) ->
  153. doc("Multiple frames are received followed by a close frame "
  154. "when websocket_init/1 returns them as commands."),
  155. do_many_frames_then_close_frame(Config, "/init").
  156. websocket_handle_many_frames_then_close_frame(Config) ->
  157. doc("Multiple frames are received followed by a close frame "
  158. "when websocket_handle/2 returns them as commands."),
  159. do_many_frames_then_close_frame(Config, "/handle").
  160. websocket_info_many_frames_then_close_frame(Config) ->
  161. doc("Multiple frames are received followed by a close frame "
  162. "when websocket_info/2 returns them as commands."),
  163. do_many_frames_then_close_frame(Config, "/info").
  164. do_many_frames_then_close_frame(Config, Path) ->
  165. {ok, ConnPid, StreamRef} = gun_open_ws(Config, Path, [
  166. {text, <<"One frame!">>},
  167. {binary, <<"Two frames!">>},
  168. close
  169. ]),
  170. ensure_handle_is_called(ConnPid, StreamRef, Path),
  171. {ok, {text, <<"One frame!">>}} = receive_ws(ConnPid, StreamRef),
  172. {ok, {binary, <<"Two frames!">>}} = receive_ws(ConnPid, StreamRef),
  173. {ok, close} = receive_ws(ConnPid, StreamRef),
  174. gun_down(ConnPid).
  175. websocket_active_false(Config) ->
  176. doc("The {active, false} command stops receiving data from the socket. "
  177. "The {active, true} command reenables it."),
  178. {ok, ConnPid, StreamRef} = gun_open_ws(Config, "/active", []),
  179. gun:ws_send(ConnPid, StreamRef, {text, <<"Not received until the handler enables active again.">>}),
  180. {error, timeout} = receive_ws(ConnPid, StreamRef),
  181. {ok, {text, <<"Not received until the handler enables active again.">>}}
  182. = receive_ws(ConnPid, StreamRef),
  183. ok.
  184. websocket_deflate_false(Config) ->
  185. doc("The {deflate, false} command temporarily disables compression. "
  186. "The {deflate, true} command reenables it."),
  187. %% We disable context takeover so that the compressed data
  188. %% does not change across all frames.
  189. {ok, Socket, Headers} = ws_SUITE:do_handshake("/deflate",
  190. "Sec-WebSocket-Extensions: permessage-deflate; server_no_context_takeover\r\n", Config),
  191. {_, "permessage-deflate; server_no_context_takeover"}
  192. = lists:keyfind("sec-websocket-extensions", 1, Headers),
  193. %% The handler receives a compressed "Hello" frame and
  194. %% sends back a compressed or uncompressed echo intermittently.
  195. Mask = 16#11223344,
  196. CompressedHello = <<242, 72, 205, 201, 201, 7, 0>>,
  197. MaskedHello = ws_SUITE:do_mask(CompressedHello, Mask, <<>>),
  198. %% First echo is compressed.
  199. ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>),
  200. {ok, <<1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary>>} = gen_tcp:recv(Socket, 0, 6000),
  201. %% Second echo is not compressed when it is received back.
  202. ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>),
  203. {ok, <<1:1, 0:3, 1:4, 0:1, 5:7, "Hello">>} = gen_tcp:recv(Socket, 0, 6000),
  204. %% Third echo is compressed again.
  205. ok = gen_tcp:send(Socket, <<1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary>>),
  206. {ok, <<1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary>>} = gen_tcp:recv(Socket, 0, 6000),
  207. %% Client-initiated close.
  208. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
  209. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  210. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  211. ok.
  212. websocket_deflate_ignore_if_not_negotiated(Config) ->
  213. doc("The {deflate, boolean()} commands are ignored "
  214. "when compression was not negotiated."),
  215. {ok, ConnPid, StreamRef} = gun_open_ws(Config, "/deflate", []),
  216. _ = [begin
  217. gun:ws_send(ConnPid, StreamRef, {text, <<"Hello.">>}),
  218. {ok, {text, <<"Hello.">>}} = receive_ws(ConnPid, StreamRef)
  219. end || _ <- lists:seq(1, 10)],
  220. ok.
  221. websocket_set_options_idle_timeout(Config) ->
  222. doc("The idle_timeout option can be modified using the "
  223. "command {set_options, Opts} at runtime."),
  224. ConnPid = gun_open(Config),
  225. StreamRef = gun:ws_upgrade(ConnPid, "/set_options"),
  226. receive
  227. {gun_upgrade, ConnPid, StreamRef, [<<"websocket">>], _} ->
  228. ok;
  229. {gun_response, ConnPid, _, _, Status, Headers} ->
  230. exit({ws_upgrade_failed, Status, Headers});
  231. {gun_error, ConnPid, StreamRef, Reason} ->
  232. exit({ws_upgrade_failed, Reason})
  233. after 1000 ->
  234. error(timeout)
  235. end,
  236. %% We don't send anything for a short while and confirm
  237. %% that idle_timeout does not trigger.
  238. {error, timeout} = gun:await(ConnPid, StreamRef, 2000),
  239. %% Trigger the change in idle_timeout and confirm that
  240. %% the connection gets closed soon after.
  241. gun:ws_send(ConnPid, StreamRef, {text, <<"idle_timeout_short">>}),
  242. receive
  243. {gun_down, ConnPid, _, _, _} ->
  244. ok
  245. after 2000 ->
  246. error(timeout)
  247. end.
  248. websocket_shutdown_reason(Config) ->
  249. doc("The command {shutdown_reason, any()} can be used to "
  250. "change the shutdown reason of a Websocket connection."),
  251. ConnPid = gun_open(Config),
  252. StreamRef = gun:ws_upgrade(ConnPid, "/shutdown_reason", [
  253. {<<"x-test-pid">>, pid_to_list(self())}
  254. ]),
  255. {upgrade, [<<"websocket">>], _} = gun:await(ConnPid, StreamRef),
  256. WsPid = receive {ws_pid, P} -> P after 1000 -> error(timeout) end,
  257. MRef = monitor(process, WsPid),
  258. WsPid ! {self(), {?MODULE, ?FUNCTION_NAME}},
  259. receive
  260. {'DOWN', MRef, process, WsPid, {shutdown, {?MODULE, ?FUNCTION_NAME}}} ->
  261. ok
  262. after 1000 ->
  263. error(timeout)
  264. end.