http2_SUITE.erl 6.0 KB

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