http2_SUITE.erl 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. {"/resp_iolist_body", resp_iolist_body_h, []}
  27. ]}
  28. ].
  29. %% Do a prior knowledge handshake (function copied from rfc7540_SUITE).
  30. do_handshake(Config) ->
  31. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  32. %% Send a valid preface.
  33. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  34. %% Receive the server preface.
  35. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  36. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  37. %% Send the SETTINGS ack.
  38. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  39. %% Receive the SETTINGS ack.
  40. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  41. {ok, Socket}.
  42. inactivity_timeout(Config) ->
  43. doc("Terminate when the inactivity timeout is reached."),
  44. ProtoOpts = #{
  45. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  46. inactivity_timeout => 1000
  47. },
  48. {ok, _} = cowboy:start_clear(name(), [{port, 0}], ProtoOpts),
  49. Port = ranch:get_port(name()),
  50. {ok, Socket} = do_handshake([{port, Port}|Config]),
  51. receive after 1000 -> ok end,
  52. %% Receive a GOAWAY frame back with an INTERNAL_ERROR.
  53. {ok, << _:24, 7:8, _:72, 2:32 >>} = gen_tcp:recv(Socket, 17, 1000),
  54. ok.
  55. initial_connection_window_size(Config) ->
  56. doc("Confirm a WINDOW_UPDATE frame is sent when the configured "
  57. "connection window is larger than the default."),
  58. ConfiguredSize = 100000,
  59. ProtoOpts = #{
  60. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  61. initial_connection_window_size => ConfiguredSize
  62. },
  63. {ok, _} = cowboy:start_clear(name(), [{port, 0}], ProtoOpts),
  64. Port = ranch:get_port(name()),
  65. {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {active, false}]),
  66. %% Send a valid preface.
  67. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  68. %% Receive the server preface.
  69. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  70. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  71. %% Receive a WINDOW_UPDATE frame incrementing the connection window to 100000.
  72. {ok, <<4:24, 8:8, 0:41, Size:31>>} = gen_tcp:recv(Socket, 13, 1000),
  73. ConfiguredSize = Size + 65535,
  74. ok.
  75. preface_timeout_infinity(Config) ->
  76. doc("Ensure infinity for preface_timeout is accepted."),
  77. ProtoOpts = #{
  78. env => #{dispatch => cowboy_router:compile(init_routes(Config))},
  79. preface_timeout => infinity
  80. },
  81. {ok, Pid} = cowboy:start_clear(name(), [{port, 0}], ProtoOpts),
  82. Ref = erlang:monitor(process, Pid),
  83. Port = ranch:get_port(name()),
  84. {ok, _} = do_handshake([{port, Port}|Config]),
  85. receive
  86. {'DOWN', Ref, process, Pid, Reason} ->
  87. error(Reason)
  88. after 1000 ->
  89. cowboy:stop_listener(preface_timeout_infinity)
  90. end.
  91. resp_iolist_body(Config) ->
  92. doc("Regression test when response bodies are iolists that "
  93. "include improper lists, empty lists and empty binaries. "
  94. "The original issue failed to split the body into frames properly."),
  95. ProtoOpts = #{
  96. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  97. },
  98. {ok, _} = cowboy:start_clear(name(), [{port, 0}], ProtoOpts),
  99. Port = ranch:get_port(name()),
  100. ConnPid = gun_open([{type, tcp}, {protocol, http2}, {port, Port}|Config]),
  101. Ref = gun:get(ConnPid, "/resp_iolist_body"),
  102. {response, nofin, 200, RespHeaders} = gun:await(ConnPid, Ref),
  103. {_, BinLen} = lists:keyfind(<<"content-length">>, 1, RespHeaders),
  104. Len = binary_to_integer(BinLen),
  105. {ok, RespBody} = gun:await_body(ConnPid, Ref),
  106. Len = iolist_size(RespBody),
  107. gun:close(ConnPid).