http2_SUITE.erl 8.8 KB

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