http2_SUITE.erl 10 KB

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