http2_SUITE.erl 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. %% Copyright (c) 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(http2_SUITE).
  15. -compile(export_all).
  16. -compile(nowarn_export_all).
  17. -import(ct_helper, [config/2]).
  18. -import(ct_helper, [doc/1]).
  19. -import(ct_helper, [get_remote_pid_tcp/1]).
  20. -import(cowboy_test, [gun_open/1]).
  21. all() -> [{group, clear}].
  22. groups() -> [{clear, [parallel], ct_helper:all(?MODULE)}].
  23. init_routes(_) -> [
  24. {"localhost", [
  25. {"/", hello_h, []},
  26. {"/echo/:key", echo_h, []},
  27. {"/resp_iolist_body", resp_iolist_body_h, []}
  28. ]}
  29. ].
  30. %% Do a prior knowledge handshake (function originally copied from rfc7540_SUITE).
  31. do_handshake(Config) ->
  32. do_handshake(#{}, Config).
  33. do_handshake(Settings, Config) ->
  34. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  35. %% Send a valid preface.
  36. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(Settings)]),
  37. %% Receive the server preface.
  38. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  39. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  40. %% Send the SETTINGS ack.
  41. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  42. %% Receive the SETTINGS ack.
  43. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  44. {ok, Socket}.
  45. idle_timeout(Config) ->
  46. doc("Terminate when the idle timeout is reached."),
  47. ProtoOpts = #{
  48. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  49. idle_timeout => 1000
  50. },
  51. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  52. Port = ranch:get_port(?FUNCTION_NAME),
  53. try
  54. {ok, Socket} = do_handshake([{port, Port}|Config]),
  55. timer:sleep(1000),
  56. %% Receive a GOAWAY frame back with NO_ERROR.
  57. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 1000)
  58. after
  59. cowboy:stop_listener(?FUNCTION_NAME)
  60. end.
  61. idle_timeout_infinity(Config) ->
  62. doc("Ensure the idle_timeout option accepts the infinity value."),
  63. ProtoOpts = #{
  64. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  65. idle_timeout => infinity
  66. },
  67. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  68. Port = ranch:get_port(?FUNCTION_NAME),
  69. try
  70. {ok, Socket} = do_handshake([{port, Port}|Config]),
  71. timer:sleep(1000),
  72. %% Don't receive a GOAWAY frame.
  73. {error, timeout} = gen_tcp:recv(Socket, 17, 1000)
  74. after
  75. cowboy:stop_listener(?FUNCTION_NAME)
  76. end.
  77. idle_timeout_reset_on_data(Config) ->
  78. doc("Terminate when the idle timeout is reached."),
  79. ProtoOpts = #{
  80. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  81. idle_timeout => 1000
  82. },
  83. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  84. Port = ranch:get_port(?FUNCTION_NAME),
  85. try
  86. {ok, Socket} = do_handshake([{port, Port}|Config]),
  87. %% We wait a little, send a PING, receive a PING ack.
  88. {error, timeout} = gen_tcp:recv(Socket, 17, 500),
  89. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  90. {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000),
  91. %% Again.
  92. {error, timeout} = gen_tcp:recv(Socket, 17, 500),
  93. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  94. {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000),
  95. %% And one more time.
  96. {error, timeout} = gen_tcp:recv(Socket, 17, 500),
  97. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  98. {ok, <<8:24, 6:8, 0:7, 1:1, 0:96>>} = gen_tcp:recv(Socket, 17, 1000),
  99. %% The connection goes away soon after we stop sending data.
  100. timer:sleep(1000),
  101. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 1000)
  102. after
  103. cowboy:stop_listener(?FUNCTION_NAME)
  104. end.
  105. inactivity_timeout(Config) ->
  106. doc("Terminate when the inactivity timeout is reached."),
  107. ProtoOpts = #{
  108. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  109. inactivity_timeout => 1000
  110. },
  111. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  112. Port = ranch:get_port(?FUNCTION_NAME),
  113. try
  114. {ok, Socket} = do_handshake([{port, Port}|Config]),
  115. receive after 1000 -> ok end,
  116. %% Receive a GOAWAY frame back with an INTERNAL_ERROR.
  117. {ok, << _:24, 7:8, _:72, 2:32 >>} = gen_tcp:recv(Socket, 17, 1000)
  118. after
  119. cowboy:stop_listener(?FUNCTION_NAME)
  120. end.
  121. initial_connection_window_size(Config) ->
  122. doc("Confirm a WINDOW_UPDATE frame is sent when the configured "
  123. "connection window is larger than the default."),
  124. ConfiguredSize = 100000,
  125. ProtoOpts = #{
  126. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  127. initial_connection_window_size => ConfiguredSize
  128. },
  129. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  130. Port = ranch:get_port(?FUNCTION_NAME),
  131. try
  132. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}]),
  133. %% Send a valid preface.
  134. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  135. %% Receive the server preface.
  136. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  137. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  138. %% Receive a WINDOW_UPDATE frame incrementing the connection window to 100000.
  139. {ok, <<4:24, 8:8, 0:41, Size:31>>} = gen_tcp:recv(Socket, 13, 1000),
  140. ConfiguredSize = Size + 65535
  141. after
  142. cowboy:stop_listener(?FUNCTION_NAME)
  143. end.
  144. max_frame_size_sent(Config) ->
  145. doc("Confirm that frames sent by Cowboy are limited in size "
  146. "by the max_frame_size_sent configuration value."),
  147. MaxFrameSize = 20000,
  148. ProtoOpts = #{
  149. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  150. max_frame_size_sent => MaxFrameSize
  151. },
  152. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  153. Port = ranch:get_port(?FUNCTION_NAME),
  154. try
  155. {ok, Socket} = do_handshake(#{max_frame_size => MaxFrameSize + 10000},
  156. [{port, Port}|Config]),
  157. %% Send a request with a 30000 bytes body.
  158. {HeadersBlock, _} = cow_hpack:encode([
  159. {<<":method">>, <<"POST">>},
  160. {<<":scheme">>, <<"http">>},
  161. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  162. {<<":path">>, <<"/echo/read_body">>}
  163. ]),
  164. ok = gen_tcp:send(Socket, [
  165. cow_http2:headers(1, nofin, HeadersBlock),
  166. cow_http2:data(1, nofin, <<0:16384/unit:8>>),
  167. cow_http2:data(1, fin, <<0:13616/unit:8>>)
  168. ]),
  169. %% Receive a HEADERS frame as a response.
  170. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = case gen_tcp:recv(Socket, 9, 1000) of
  171. %% We received a WINDOW_UPDATE first. Skip it and the next.
  172. {ok, <<4:24, 8:8, 0:40>>} ->
  173. {ok, _} = gen_tcp:recv(Socket, 4 + 13, 1000),
  174. gen_tcp:recv(Socket, 9, 1000);
  175. Res ->
  176. Res
  177. end,
  178. {ok, _} = gen_tcp:recv(Socket, SkipLen, 6000),
  179. %% The DATA frames following must have lengths of 20000
  180. %% and then 10000 due to the limit.
  181. {ok, <<20000:24, 0:8, _:40, _:20000/unit:8>>} = gen_tcp:recv(Socket, 20009, 6000),
  182. {ok, <<10000:24, 0:8, _:40, _:10000/unit:8>>} = gen_tcp:recv(Socket, 10009, 6000)
  183. after
  184. cowboy:stop_listener(?FUNCTION_NAME)
  185. end.
  186. preface_timeout_infinity(Config) ->
  187. doc("Ensure infinity for preface_timeout is accepted."),
  188. ProtoOpts = #{
  189. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  190. preface_timeout => infinity
  191. },
  192. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  193. Port = ranch:get_port(?FUNCTION_NAME),
  194. try
  195. {ok, Socket} = do_handshake([{port, Port}|Config]),
  196. Pid = get_remote_pid_tcp(Socket),
  197. Ref = erlang:monitor(process, Pid),
  198. receive
  199. {'DOWN', Ref, process, Pid, Reason} ->
  200. error(Reason)
  201. after 1000 ->
  202. ok
  203. end
  204. after
  205. cowboy:stop_listener(?FUNCTION_NAME)
  206. end.
  207. resp_iolist_body(Config) ->
  208. doc("Regression test when response bodies are iolists that "
  209. "include improper lists, empty lists and empty binaries. "
  210. "The original issue failed to split the body into frames properly."),
  211. ProtoOpts = #{
  212. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  213. },
  214. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  215. Port = ranch:get_port(?FUNCTION_NAME),
  216. try
  217. ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
  218. Ref = gun:get(ConnPid, "/resp_iolist_body"),
  219. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  220. {_, BinLen} = lists:keyfind(<<"content-length">>, 1, RespHeaders),
  221. Len = binary_to_integer(BinLen),
  222. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  223. Len = iolist_size(RespBody),
  224. gun:close(ConnPid)
  225. after
  226. cowboy:stop_listener(?FUNCTION_NAME)
  227. end.
  228. settings_timeout_infinity(Config) ->
  229. doc("Ensure infinity for settings_timeout is accepted."),
  230. ProtoOpts = #{
  231. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  232. settings_timeout => infinity
  233. },
  234. {ok, _} = cowboy:start_clear(?FUNCTION_NAME, [{port, 0}], ProtoOpts),
  235. Port = ranch:get_port(?FUNCTION_NAME),
  236. try
  237. {ok, Socket} = do_handshake([{port, Port}|Config]),
  238. Pid = get_remote_pid_tcp(Socket),
  239. Ref = erlang:monitor(process, Pid),
  240. receive
  241. {'DOWN', Ref, process, Pid, Reason} ->
  242. error(Reason)
  243. after 1000 ->
  244. ok
  245. end
  246. after
  247. cowboy:stop_listener(?FUNCTION_NAME)
  248. end.