http2_SUITE.erl 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. inactivity_timeout(Config) ->
  47. doc("Terminate when the inactivity timeout is reached."),
  48. ProtoOpts = #{
  49. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  50. inactivity_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. receive after 1000 -> ok end,
  56. %% Receive a GOAWAY frame back with an INTERNAL_ERROR.
  57. {ok, << _:24, 7:8, _:72, 2:32 >>} = gen_tcp:recv(Socket, 17, 1000),
  58. ok.
  59. initial_connection_window_size(Config) ->
  60. doc("Confirm a WINDOW_UPDATE frame is sent when the configured "
  61. "connection window is larger than the default."),
  62. ConfiguredSize = 100000,
  63. ProtoOpts = #{
  64. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  65. initial_connection_window_size => ConfiguredSize
  66. },
  67. {ok, _} = cowboy:start_clear(name(), [{port, 0}], ProtoOpts),
  68. Port = ranch:get_port(name()),
  69. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}]),
  70. %% Send a valid preface.
  71. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  72. %% Receive the server preface.
  73. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  74. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  75. %% Receive a WINDOW_UPDATE frame incrementing the connection window to 100000.
  76. {ok, <<4:24, 8:8, 0:41, Size:31>>} = gen_tcp:recv(Socket, 13, 1000),
  77. ConfiguredSize = Size + 65535,
  78. ok.
  79. max_frame_size_sent(Config) ->
  80. doc("Confirm that frames sent by Cowboy are limited in size "
  81. "by the max_frame_size_sent configuration value."),
  82. MaxFrameSize = 20000,
  83. ProtoOpts = #{
  84. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  85. max_frame_size_sent => MaxFrameSize
  86. },
  87. {ok, _} = cowboy:start_clear(name(), [{port, 0}], ProtoOpts),
  88. Port = ranch:get_port(name()),
  89. {ok, Socket} = do_handshake(#{max_frame_size => MaxFrameSize + 10000}, [{port, Port}|Config]),
  90. %% Send a request with a 30000 bytes body.
  91. {HeadersBlock, _} = cow_hpack:encode([
  92. {<<":method">>, <<"POST">>},
  93. {<<":scheme">>, <<"http">>},
  94. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  95. {<<":path">>, <<"/echo/read_body">>}
  96. ]),
  97. ok = gen_tcp:send(Socket, [
  98. cow_http2:headers(1, nofin, HeadersBlock),
  99. cow_http2:data(1, nofin, <<0:16384/unit:8>>),
  100. cow_http2:data(1, fin, <<0:13616/unit:8>>)
  101. ]),
  102. %% Receive a HEADERS frame as a response.
  103. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = case gen_tcp:recv(Socket, 9, 1000) of
  104. %% We received a WINDOW_UPDATE first. Skip it and the next.
  105. {ok, <<4:24, 8:8, 0:40>>} ->
  106. {ok, _} = gen_tcp:recv(Socket, 4 + 13, 1000),
  107. gen_tcp:recv(Socket, 9, 1000);
  108. Res ->
  109. Res
  110. end,
  111. {ok, _} = gen_tcp:recv(Socket, SkipLen, 6000),
  112. %% The DATA frames following must have lengths of 20000
  113. %% and then 10000 due to the limit.
  114. {ok, <<20000:24, 0:8, _:40, _:20000/unit:8>>} = gen_tcp:recv(Socket, 20009, 6000),
  115. {ok, <<10000:24, 0:8, _:40, _:10000/unit:8>>} = gen_tcp:recv(Socket, 10009, 6000),
  116. %% Stop the listener.
  117. cowboy:stop_listener(name()).
  118. preface_timeout_infinity(Config) ->
  119. doc("Ensure infinity for preface_timeout is accepted."),
  120. ProtoOpts = #{
  121. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  122. preface_timeout => infinity
  123. },
  124. {ok, _} = cowboy:start_clear(name(), [{port, 0}], ProtoOpts),
  125. Port = ranch:get_port(name()),
  126. {ok, Socket} = do_handshake([{port, Port}|Config]),
  127. Pid = get_remote_pid_tcp(Socket),
  128. Ref = erlang:monitor(process, Pid),
  129. receive
  130. {'DOWN', Ref, process, Pid, Reason} ->
  131. error(Reason)
  132. after 1000 ->
  133. cowboy:stop_listener(name())
  134. end.
  135. resp_iolist_body(Config) ->
  136. doc("Regression test when response bodies are iolists that "
  137. "include improper lists, empty lists and empty binaries. "
  138. "The original issue failed to split the body into frames properly."),
  139. ProtoOpts = #{
  140. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  141. },
  142. {ok, _} = cowboy:start_clear(name(), [{port, 0}], ProtoOpts),
  143. Port = ranch:get_port(name()),
  144. ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
  145. Ref = gun:get(ConnPid, "/resp_iolist_body"),
  146. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  147. {_, BinLen} = lists:keyfind(<<"content-length">>, 1, RespHeaders),
  148. Len = binary_to_integer(BinLen),
  149. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  150. Len = iolist_size(RespBody),
  151. gun:close(ConnPid).
  152. settings_timeout_infinity(Config) ->
  153. doc("Ensure infinity for settings_timeout is accepted."),
  154. ProtoOpts = #{
  155. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  156. settings_timeout => infinity
  157. },
  158. {ok, _} = cowboy:start_clear(name(), [{port, 0}], ProtoOpts),
  159. Port = ranch:get_port(name()),
  160. {ok, Socket} = do_handshake([{port, Port}|Config]),
  161. Pid = get_remote_pid_tcp(Socket),
  162. Ref = erlang:monitor(process, Pid),
  163. receive
  164. {'DOWN', Ref, process, Pid, Reason} ->
  165. error(Reason)
  166. after 1000 ->
  167. cowboy:stop_listener(name())
  168. end.