ws_SUITE.erl 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. %% Copyright (c) 2011-2017, 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_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. %% ct.
  20. all() ->
  21. [{group, autobahn}, {group, ws}].
  22. groups() ->
  23. BaseTests = ct_helper:all(?MODULE) -- [autobahn_fuzzingclient],
  24. [{autobahn, [], [autobahn_fuzzingclient]}, {ws, [parallel], BaseTests}].
  25. init_per_group(Name = autobahn, Config) ->
  26. %% Some systems have it named pip2.
  27. Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
  28. case string:str(Out, "autobahntestsuite") of
  29. 0 ->
  30. ct:print("Skipping the autobahn group because the "
  31. "Autobahn Test Suite is not installed.~nTo install it, "
  32. "please follow the instructions on this page:~n~n "
  33. "http://autobahn.ws/testsuite/installation.html"),
  34. {skip, "Autobahn Test Suite not installed."};
  35. _ ->
  36. {ok, _} = cowboy:start_clear(Name, [{port, 33080}], #{
  37. env => #{dispatch => init_dispatch()}
  38. }),
  39. Config
  40. end;
  41. init_per_group(Name = ws, Config) ->
  42. cowboy_test:init_http(Name, #{
  43. env => #{dispatch => init_dispatch()}
  44. }, Config).
  45. end_per_group(Listener, _Config) ->
  46. cowboy:stop_listener(Listener).
  47. %% Dispatch configuration.
  48. init_dispatch() ->
  49. cowboy_router:compile([
  50. {"localhost", [
  51. {"/ws_echo", ws_echo, []},
  52. {"/ws_echo_timer", ws_echo_timer, []},
  53. {"/ws_init", ws_init_h, []},
  54. {"/ws_init_shutdown", ws_init_shutdown, []},
  55. {"/ws_send_many", ws_send_many, [
  56. {sequence, [
  57. {text, <<"one">>},
  58. {text, <<"two">>},
  59. {text, <<"seven!">>}]}
  60. ]},
  61. {"/ws_send_close", ws_send_many, [
  62. {sequence, [
  63. {text, <<"send">>},
  64. close,
  65. {text, <<"won't be received">>}]}
  66. ]},
  67. {"/ws_send_close_payload", ws_send_many, [
  68. {sequence, [
  69. {text, <<"send">>},
  70. {close, 1001, <<"some text!">>},
  71. {text, <<"won't be received">>}]}
  72. ]},
  73. {"/ws_subprotocol", ws_subprotocol, []},
  74. {"/terminate", ws_terminate_h, []},
  75. {"/ws_timeout_hibernate", ws_timeout_hibernate, []},
  76. {"/ws_timeout_cancel", ws_timeout_cancel, []},
  77. {"/ws_max_frame_size", ws_max_frame_size, []}
  78. ]}
  79. ]).
  80. %% Tests.
  81. autobahn_fuzzingclient(Config) ->
  82. doc("Autobahn test suite for the Websocket protocol."),
  83. Self = self(),
  84. spawn_link(fun() -> do_start_port(Config, Self) end),
  85. receive autobahn_exit -> ok end,
  86. ct:log("<h2><a href=\"log_private/reports/servers/index.html\">Full report</a></h2>~n"),
  87. Report = config(priv_dir, Config) ++ "reports/servers/index.html",
  88. ct:print("Autobahn Test Suite report: file://~s~n", [Report]),
  89. {ok, HTML} = file:read_file(Report),
  90. case length(binary:matches(HTML, <<"case_failed">>)) > 2 of
  91. true -> error(failed);
  92. false -> ok
  93. end.
  94. do_start_port(Config, Pid) ->
  95. Port = open_port({spawn, "wstest -m fuzzingclient -s " ++ config(data_dir, Config) ++ "client.json"},
  96. [{line, 10000}, {cd, config(priv_dir, Config)}, binary, eof]),
  97. do_receive_infinity(Port, Pid).
  98. do_receive_infinity(Port, Pid) ->
  99. receive
  100. {Port, {data, {eol, Line}}} ->
  101. io:format(user, "~s~n", [Line]),
  102. do_receive_infinity(Port, Pid);
  103. {Port, eof} ->
  104. Pid ! autobahn_exit
  105. end.
  106. unlimited_connections(Config) ->
  107. doc("Websocket connections are not limited. The connections "
  108. "are removed from the count after the handshake completes."),
  109. _ = [begin
  110. spawn_link(fun() -> do_connect_and_loop(Config) end),
  111. timer:sleep(1)
  112. end || _ <- lists:seq(1, 3000)],
  113. timer:sleep(1000),
  114. %% We have at least 3000 client and 3000 server sockets.
  115. true = length(erlang:ports()) > 6000,
  116. %% Ranch thinks we have no connections.
  117. 0 = ranch_server:count_connections(ws),
  118. ok.
  119. do_connect_and_loop(Config) ->
  120. {ok, Socket, _} = do_handshake("/ws_echo", Config),
  121. do_loop(Socket).
  122. do_loop(Socket) ->
  123. %% Masked text hello echoed back clear by the server.
  124. Mask = 16#37fa213d,
  125. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  126. ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  127. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  128. timer:sleep(1000),
  129. do_loop(Socket).
  130. ws0(Config) ->
  131. doc("Websocket version 0 (hixie-76 draft) is no longer supported."),
  132. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  133. ok = gen_tcp:send(Socket,
  134. "GET /ws_echo_timer HTTP/1.1\r\n"
  135. "Host: localhost\r\n"
  136. "Connection: Upgrade\r\n"
  137. "Upgrade: WebSocket\r\n"
  138. "Origin: http://localhost\r\n"
  139. "Sec-Websocket-Key1: Y\" 4 1Lj!957b8@0H756!i\r\n"
  140. "Sec-Websocket-Key2: 1711 M;4\\74 80<6\r\n"
  141. "\r\n"),
  142. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  143. {ok, {http_response, {1, 1}, 400, _}, _} = erlang:decode_packet(http, Handshake, []),
  144. ok.
  145. ws7(Config) ->
  146. doc("Websocket version 7 (draft) is supported."),
  147. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  148. ok = gen_tcp:send(Socket, [
  149. "GET /ws_echo_timer HTTP/1.1\r\n"
  150. "Host: localhost\r\n"
  151. "Connection: Upgrade\r\n"
  152. "Upgrade: websocket\r\n"
  153. "Sec-WebSocket-Origin: http://localhost\r\n"
  154. "Sec-WebSocket-Version: 7\r\n"
  155. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  156. "\r\n"]),
  157. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  158. {ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []),
  159. [Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
  160. {_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  161. {_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  162. {_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
  163. do_ws_version(Socket).
  164. ws8(Config) ->
  165. doc("Websocket version 8 (draft) is supported."),
  166. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  167. ok = gen_tcp:send(Socket, [
  168. "GET /ws_echo_timer HTTP/1.1\r\n"
  169. "Host: localhost\r\n"
  170. "Connection: Upgrade\r\n"
  171. "Upgrade: websocket\r\n"
  172. "Sec-WebSocket-Origin: http://localhost\r\n"
  173. "Sec-WebSocket-Version: 8\r\n"
  174. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  175. "\r\n"]),
  176. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  177. {ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []),
  178. [Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
  179. {_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  180. {_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  181. {_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
  182. do_ws_version(Socket).
  183. ws13(Config) ->
  184. doc("Websocket version 13 (RFC) is supported."),
  185. {ok, Socket, _} = do_handshake("/ws_echo_timer", Config),
  186. do_ws_version(Socket).
  187. do_ws_version(Socket) ->
  188. %% Masked text hello echoed back clear by the server.
  189. Mask = 16#37fa213d,
  190. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  191. ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  192. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  193. %% Empty binary frame echoed back.
  194. ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 0:7, 0:32 >>),
  195. {ok, << 1:1, 0:3, 2:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  196. %% Masked binary hello echoed back clear by the server.
  197. ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  198. {ok, << 1:1, 0:3, 2:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  199. %% Frames sent on timer by the handler.
  200. {ok, << 1:1, 0:3, 1:4, 0:1, 14:7, "websocket_init" >>} = gen_tcp:recv(Socket, 0, 6000),
  201. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000),
  202. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000),
  203. {ok, << 1:1, 0:3, 1:4, 0:1, 16:7, "websocket_handle" >>} = gen_tcp:recv(Socket, 0, 6000),
  204. %% Client-initiated ping/pong.
  205. ok = gen_tcp:send(Socket, << 1:1, 0:3, 9:4, 1:1, 0:7, 0:32 >>),
  206. {ok, << 1:1, 0:3, 10:4, 0:8 >>} = 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. ws_init_return_ok(Config) ->
  213. doc("Handler does nothing."),
  214. {ok, Socket, _} = do_handshake("/ws_init?ok", Config),
  215. %% The handler does nothing; nothing should happen here.
  216. {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
  217. ok.
  218. ws_init_return_ok_hibernate(Config) ->
  219. doc("Handler does nothing; hibernates."),
  220. {ok, Socket, _} = do_handshake("/ws_init?ok_hibernate", Config),
  221. %% The handler does nothing; nothing should happen here.
  222. {error, timeout} = gen_tcp:recv(Socket, 0, 1000),
  223. ok.
  224. ws_init_return_reply(Config) ->
  225. doc("Handler sends a text frame just after the handshake."),
  226. {ok, Socket, _} = do_handshake("/ws_init?reply", Config),
  227. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  228. ok.
  229. ws_init_return_reply_hibernate(Config) ->
  230. doc("Handler sends a text frame just after the handshake and then hibernates."),
  231. {ok, Socket, _} = do_handshake("/ws_init?reply_hibernate", Config),
  232. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  233. ok.
  234. ws_init_return_reply_close(Config) ->
  235. doc("Handler closes immediately after the handshake."),
  236. {ok, Socket, _} = do_handshake("/ws_init?reply_close", Config),
  237. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  238. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  239. ok.
  240. ws_init_return_reply_close_hibernate(Config) ->
  241. doc("Handler closes immediately after the handshake, then attempts to hibernate."),
  242. {ok, Socket, _} = do_handshake("/ws_init?reply_close_hibernate", Config),
  243. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  244. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  245. ok.
  246. ws_init_return_reply_many(Config) ->
  247. doc("Handler sends many frames just after the handshake."),
  248. {ok, Socket, _} = do_handshake("/ws_init?reply_many", Config),
  249. %% We catch all frames at once and check them directly.
  250. {ok, <<
  251. 1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
  252. 1:1, 0:3, 2:4, 0:1, 5:7, "World" >>} = gen_tcp:recv(Socket, 14, 6000),
  253. ok.
  254. ws_init_return_reply_many_hibernate(Config) ->
  255. doc("Handler sends many frames just after the handshake and then hibernates."),
  256. {ok, Socket, _} = do_handshake("/ws_init?reply_many_hibernate", Config),
  257. %% We catch all frames at once and check them directly.
  258. {ok, <<
  259. 1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
  260. 1:1, 0:3, 2:4, 0:1, 5:7, "World" >>} = gen_tcp:recv(Socket, 14, 6000),
  261. ok.
  262. ws_init_return_reply_many_close(Config) ->
  263. doc("Handler sends many frames including a close frame just after the handshake."),
  264. {ok, Socket, _} = do_handshake("/ws_init?reply_many_close", Config),
  265. %% We catch all frames at once and check them directly.
  266. {ok, <<
  267. 1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
  268. 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 9, 6000),
  269. ok.
  270. ws_init_return_reply_many_close_hibernate(Config) ->
  271. doc("Handler sends many frames including a close frame just after the handshake and then hibernates."),
  272. {ok, Socket, _} = do_handshake("/ws_init?reply_many_close_hibernate", Config),
  273. %% We catch all frames at once and check them directly.
  274. {ok, <<
  275. 1:1, 0:3, 1:4, 0:1, 5:7, "Hello",
  276. 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 9, 6000),
  277. ok.
  278. ws_init_return_stop(Config) ->
  279. doc("Handler closes immediately after the handshake."),
  280. {ok, Socket, _} = do_handshake("/ws_init?stop", Config),
  281. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  282. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  283. ok.
  284. ws_init_shutdown_before_handshake(Config) ->
  285. doc("Handler stops before Websocket handshake."),
  286. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  287. ok = gen_tcp:send(Socket, [
  288. "GET /ws_init_shutdown HTTP/1.1\r\n"
  289. "Host: localhost\r\n"
  290. "Connection: Upgrade\r\n"
  291. "Origin: http://localhost\r\n"
  292. "Sec-WebSocket-Version: 13\r\n"
  293. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  294. "Upgrade: websocket\r\n"
  295. "\r\n"]),
  296. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  297. {ok, {http_response, {1, 1}, 403, _}, _Rest} = erlang:decode_packet(http, Handshake, []),
  298. ok.
  299. ws_max_frame_size_close(Config) ->
  300. doc("Server closes connection when frame size exceeds max_frame_size option"),
  301. %% max_frame_size is set to 8 bytes in ws_max_frame_size.
  302. {ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
  303. Mask = 16#11223344,
  304. MaskedHello = do_mask(<<"HelloHello">>, Mask, <<>>),
  305. ok = gen_tcp:send(Socket, << 1:1, 0:3, 2:4, 1:1, 10:7, Mask:32, MaskedHello/binary >>),
  306. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  307. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  308. ok.
  309. ws_max_frame_size_final_fragment_close(Config) ->
  310. doc("Server closes connection when final fragmented frame "
  311. "exceeds max_frame_size option"),
  312. %% max_frame_size is set to 8 bytes in ws_max_frame_size.
  313. {ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
  314. Mask = 16#11223344,
  315. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  316. ok = gen_tcp:send(Socket, << 0:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  317. ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  318. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  319. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  320. ok.
  321. ws_max_frame_size_intermediate_fragment_close(Config) ->
  322. doc("Server closes connection when intermediate fragmented frame "
  323. "exceeds max_frame_size option"),
  324. %% max_frame_size is set to 8 bytes in ws_max_frame_size.
  325. {ok, Socket, _} = do_handshake("/ws_max_frame_size", Config),
  326. Mask = 16#11223344,
  327. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  328. ok = gen_tcp:send(Socket, << 0:1, 0:3, 2:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  329. ok = gen_tcp:send(Socket, << 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  330. ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  331. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1009:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  332. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  333. ok.
  334. ws_send_close(Config) ->
  335. doc("Server-initiated close frame ends the connection."),
  336. {ok, Socket, _} = do_handshake("/ws_send_close", Config),
  337. %% We catch all frames at once and check them directly.
  338. {ok, <<
  339. 1:1, 0:3, 1:4, 0:1, 4:7, "send",
  340. 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 8, 6000),
  341. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  342. ok.
  343. ws_send_close_payload(Config) ->
  344. doc("Server-initiated close frame with payload ends the connection."),
  345. {ok, Socket, _} = do_handshake("/ws_send_close_payload", Config),
  346. %% We catch all frames at once and check them directly.
  347. {ok, <<
  348. 1:1, 0:3, 1:4, 0:1, 4:7, "send",
  349. 1:1, 0:3, 8:4, 0:1, 12:7, 1001:16, "some text!" >>} = gen_tcp:recv(Socket, 20, 6000),
  350. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  351. ok.
  352. ws_send_many(Config) ->
  353. doc("Server sends many frames in a single reply."),
  354. {ok, Socket, _} = do_handshake("/ws_send_many", Config),
  355. %% We catch all frames at once and check them directly.
  356. {ok, <<
  357. 1:1, 0:3, 1:4, 0:1, 3:7, "one",
  358. 1:1, 0:3, 1:4, 0:1, 3:7, "two",
  359. 1:1, 0:3, 1:4, 0:1, 6:7, "seven!" >>} = gen_tcp:recv(Socket, 18, 6000),
  360. ok.
  361. ws_single_bytes(Config) ->
  362. doc("Client sends a text frame one byte at a time."),
  363. {ok, Socket, _} = do_handshake("/ws_echo", Config),
  364. %% We sleep between sends to make sure only one byte is sent.
  365. ok = gen_tcp:send(Socket, << 16#81 >>), timer:sleep(100),
  366. ok = gen_tcp:send(Socket, << 16#85 >>), timer:sleep(100),
  367. ok = gen_tcp:send(Socket, << 16#37 >>), timer:sleep(100),
  368. ok = gen_tcp:send(Socket, << 16#fa >>), timer:sleep(100),
  369. ok = gen_tcp:send(Socket, << 16#21 >>), timer:sleep(100),
  370. ok = gen_tcp:send(Socket, << 16#3d >>), timer:sleep(100),
  371. ok = gen_tcp:send(Socket, << 16#7f >>), timer:sleep(100),
  372. ok = gen_tcp:send(Socket, << 16#9f >>), timer:sleep(100),
  373. ok = gen_tcp:send(Socket, << 16#4d >>), timer:sleep(100),
  374. ok = gen_tcp:send(Socket, << 16#51 >>), timer:sleep(100),
  375. ok = gen_tcp:send(Socket, << 16#58 >>),
  376. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  377. ok.
  378. ws_subprotocol(Config) ->
  379. doc("Websocket sub-protocol negotiation."),
  380. {ok, _, Headers} = do_handshake("/ws_subprotocol",
  381. "Sec-WebSocket-Protocol: foo, bar\r\n", Config),
  382. {_, "foo"} = lists:keyfind("sec-websocket-protocol", 1, Headers),
  383. ok.
  384. ws_terminate(Config) ->
  385. doc("The Req object is kept in a more compact form by default."),
  386. {ok, Socket, _} = do_handshake("/terminate",
  387. "x-test-pid: " ++ pid_to_list(self()) ++ "\r\n", Config),
  388. %% Send a close frame.
  389. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
  390. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  391. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  392. %% Confirm terminate/3 was called with a compacted Req.
  393. receive {terminate, _, Req} ->
  394. true = maps:is_key(path, Req),
  395. false = maps:is_key(headers, Req),
  396. ok
  397. after 1000 ->
  398. error(timeout)
  399. end.
  400. ws_terminate_fun(Config) ->
  401. doc("A function can be given to filter the Req object."),
  402. {ok, Socket, _} = do_handshake("/terminate?req_filter",
  403. "x-test-pid: " ++ pid_to_list(self()) ++ "\r\n", Config),
  404. %% Send a close frame.
  405. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
  406. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  407. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  408. %% Confirm terminate/3 was called with a compacted Req.
  409. receive {terminate, _, Req} ->
  410. filtered = Req,
  411. ok
  412. after 1000 ->
  413. error(timeout)
  414. end.
  415. ws_text_fragments(Config) ->
  416. doc("Client sends fragmented text frames."),
  417. {ok, Socket, _} = do_handshake("/ws_echo", Config),
  418. %% Send two "Hello" over two fragments and two sends.
  419. Mask = 16#37fa213d,
  420. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  421. ok = gen_tcp:send(Socket, << 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  422. ok = gen_tcp:send(Socket, << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  423. {ok, << 1:1, 0:3, 1:4, 0:1, 10:7, "HelloHello" >>} = gen_tcp:recv(Socket, 0, 6000),
  424. %% Send three "Hello" over three fragments and one send.
  425. ok = gen_tcp:send(Socket, [
  426. << 0:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
  427. << 0:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>,
  428. << 1:1, 0:3, 0:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>]),
  429. {ok, << 1:1, 0:3, 1:4, 0:1, 15:7, "HelloHelloHello" >>} = gen_tcp:recv(Socket, 0, 6000),
  430. ok.
  431. ws_timeout_hibernate(Config) ->
  432. doc("Server-initiated close on timeout with hibernating process."),
  433. {ok, Socket, _} = do_handshake("/ws_timeout_hibernate", Config),
  434. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  435. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  436. ok.
  437. ws_timeout_no_cancel(Config) ->
  438. doc("Server-initiated timeout is not influenced by reception of Erlang messages."),
  439. {ok, Socket, _} = do_handshake("/ws_timeout_cancel", Config),
  440. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  441. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  442. ok.
  443. ws_timeout_reset(Config) ->
  444. doc("Server-initiated timeout is reset when client sends more data."),
  445. {ok, Socket, _} = do_handshake("/ws_timeout_cancel", Config),
  446. %% Send and receive back a frame a few times.
  447. Mask = 16#37fa213d,
  448. MaskedHello = do_mask(<<"Hello">>, Mask, <<>>),
  449. [begin
  450. ok = gen_tcp:send(Socket, << 1:1, 0:3, 1:4, 1:1, 5:7, Mask:32, MaskedHello/binary >>),
  451. {ok, << 1:1, 0:3, 1:4, 0:1, 5:7, "Hello" >>} = gen_tcp:recv(Socket, 0, 6000),
  452. timer:sleep(500)
  453. end || _ <- [1, 2, 3, 4]],
  454. %% Timeout will occur after we stop sending data.
  455. {ok, << 1:1, 0:3, 8:4, 0:1, 2:7, 1000:16 >>} = gen_tcp:recv(Socket, 0, 6000),
  456. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  457. ok.
  458. ws_webkit_deflate(Config) ->
  459. doc("x-webkit-deflate-frame compression."),
  460. {ok, Socket, Headers} = do_handshake("/ws_echo",
  461. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
  462. {_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
  463. %% Send and receive a compressed "Hello" frame.
  464. Mask = 16#11223344,
  465. CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
  466. MaskedHello = do_mask(CompressedHello, Mask, <<>>),
  467. ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 1:4, 1:1, 7:7, Mask:32, MaskedHello/binary >>),
  468. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
  469. %% Client-initiated close.
  470. ok = gen_tcp:send(Socket, << 1:1, 0:3, 8:4, 1:1, 0:7, 0:32 >>),
  471. {ok, << 1:1, 0:3, 8:4, 0:8 >>} = gen_tcp:recv(Socket, 0, 6000),
  472. {error, closed} = gen_tcp:recv(Socket, 0, 6000),
  473. ok.
  474. ws_webkit_deflate_fragments(Config) ->
  475. doc("Client sends an x-webkit-deflate-frame compressed and fragmented text frame."),
  476. {ok, Socket, Headers} = do_handshake("/ws_echo",
  477. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
  478. {_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
  479. %% Send a compressed "Hello" over two fragments and two sends.
  480. Mask = 16#11223344,
  481. CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
  482. MaskedHello1 = do_mask(binary:part(CompressedHello, 0, 4), Mask, <<>>),
  483. MaskedHello2 = do_mask(binary:part(CompressedHello, 4, 3), Mask, <<>>),
  484. ok = gen_tcp:send(Socket, << 0:1, 1:1, 0:2, 1:4, 1:1, 4:7, Mask:32, MaskedHello1/binary >>),
  485. ok = gen_tcp:send(Socket, << 1:1, 1:1, 0:2, 0:4, 1:1, 3:7, Mask:32, MaskedHello2/binary >>),
  486. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
  487. ok.
  488. ws_webkit_deflate_single_bytes(Config) ->
  489. doc("Client sends an x-webkit-deflate-frame compressed text frame one byte at a time."),
  490. {ok, Socket, Headers} = do_handshake("/ws_echo",
  491. "Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n", Config),
  492. {_, "x-webkit-deflate-frame"} = lists:keyfind("sec-websocket-extensions", 1, Headers),
  493. %% We sleep between sends to make sure only one byte is sent.
  494. Mask = 16#11223344,
  495. CompressedHello = << 242, 72, 205, 201, 201, 7, 0 >>,
  496. MaskedHello = do_mask(CompressedHello, Mask, <<>>),
  497. ok = gen_tcp:send(Socket, << 16#c1 >>), timer:sleep(100),
  498. ok = gen_tcp:send(Socket, << 16#87 >>), timer:sleep(100),
  499. ok = gen_tcp:send(Socket, << 16#11 >>), timer:sleep(100),
  500. ok = gen_tcp:send(Socket, << 16#22 >>), timer:sleep(100),
  501. ok = gen_tcp:send(Socket, << 16#33 >>), timer:sleep(100),
  502. ok = gen_tcp:send(Socket, << 16#44 >>), timer:sleep(100),
  503. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 0)]), timer:sleep(100),
  504. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 1)]), timer:sleep(100),
  505. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 2)]), timer:sleep(100),
  506. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 3)]), timer:sleep(100),
  507. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 4)]), timer:sleep(100),
  508. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 5)]), timer:sleep(100),
  509. ok = gen_tcp:send(Socket, [binary:at(MaskedHello, 6)]),
  510. {ok, << 1:1, 1:1, 0:2, 1:4, 0:1, 7:7, CompressedHello/binary >>} = gen_tcp:recv(Socket, 0, 6000),
  511. ok.
  512. %% Internal.
  513. do_handshake(Path, Config) ->
  514. do_handshake(Path, "", Config).
  515. do_handshake(Path, ExtraHeaders, Config) ->
  516. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config),
  517. [binary, {active, false}]),
  518. ok = gen_tcp:send(Socket, [
  519. "GET ", Path, " HTTP/1.1\r\n"
  520. "Host: localhost\r\n"
  521. "Connection: Upgrade\r\n"
  522. "Origin: http://localhost\r\n"
  523. "Sec-WebSocket-Version: 13\r\n"
  524. "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n"
  525. "Upgrade: websocket\r\n",
  526. ExtraHeaders,
  527. "\r\n"]),
  528. {ok, Handshake} = gen_tcp:recv(Socket, 0, 6000),
  529. {ok, {http_response, {1, 1}, 101, _}, Rest} = erlang:decode_packet(http, Handshake, []),
  530. [Headers, <<>>] = do_decode_headers(erlang:decode_packet(httph, Rest, []), []),
  531. {_, "Upgrade"} = lists:keyfind('Connection', 1, Headers),
  532. {_, "websocket"} = lists:keyfind('Upgrade', 1, Headers),
  533. {_, "s3pPLMBiTxaQ9kYGzzhZRbK+xOo="} = lists:keyfind("sec-websocket-accept", 1, Headers),
  534. {ok, Socket, Headers}.
  535. do_decode_headers({ok, http_eoh, Rest}, Acc) ->
  536. [Acc, Rest];
  537. do_decode_headers({ok, {http_header, _I, Key, _R, Value}, Rest}, Acc) ->
  538. F = fun(S) when is_atom(S) -> S; (S) -> string:to_lower(S) end,
  539. do_decode_headers(erlang:decode_packet(httph, Rest, []), [{F(Key), Value}|Acc]).
  540. do_mask(<<>>, _, Acc) ->
  541. Acc;
  542. do_mask(<< O:32, Rest/bits >>, MaskKey, Acc) ->
  543. T = O bxor MaskKey,
  544. do_mask(Rest, MaskKey, << Acc/binary, T:32 >>);
  545. do_mask(<< O:24 >>, MaskKey, Acc) ->
  546. << MaskKey2:24, _:8 >> = << MaskKey:32 >>,
  547. T = O bxor MaskKey2,
  548. << Acc/binary, T:24 >>;
  549. do_mask(<< O:16 >>, MaskKey, Acc) ->
  550. << MaskKey2:16, _:16 >> = << MaskKey:32 >>,
  551. T = O bxor MaskKey2,
  552. << Acc/binary, T:16 >>;
  553. do_mask(<< O:8 >>, MaskKey, Acc) ->
  554. << MaskKey2:8, _:24 >> = << MaskKey:32 >>,
  555. T = O bxor MaskKey2,
  556. << Acc/binary, T:8 >>.