rfc7540_SUITE.erl 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. %% Copyright (c) 2016-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(rfc7540_SUITE).
  15. -compile(export_all).
  16. -import(ct_helper, [config/2]).
  17. -import(ct_helper, [doc/1]).
  18. -import(cowboy_test, [raw_open/1]).
  19. -import(cowboy_test, [raw_send/2]).
  20. -import(cowboy_test, [raw_recv_head/1]).
  21. -import(cowboy_test, [raw_recv/3]).
  22. all() -> [{group, clear}, {group, tls}].
  23. groups() ->
  24. Modules = ct_helper:all(?MODULE),
  25. Clear = [M || M <- Modules, lists:sublist(atom_to_list(M), 4) =/= "alpn"] -- [prior_knowledge_reject_tls],
  26. TLS = [M || M <- Modules, lists:sublist(atom_to_list(M), 4) =:= "alpn"] ++ [prior_knowledge_reject_tls],
  27. [{clear, [parallel], Clear}, {tls, [parallel], TLS}].
  28. init_per_group(Name = clear, Config) ->
  29. cowboy_test:init_http(Name = clear, #{
  30. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  31. }, Config);
  32. init_per_group(Name = tls, Config) ->
  33. cowboy_test:init_http2(Name = tls, #{
  34. env => #{dispatch => cowboy_router:compile(init_routes(Config))}
  35. }, Config).
  36. end_per_group(Name, _) ->
  37. ok = cowboy:stop_listener(Name).
  38. init_routes(_) -> [
  39. {"localhost", [
  40. {"/", hello_h, []},
  41. {"/echo/:key", echo_h, []}
  42. ]}
  43. ].
  44. %% Starting HTTP/2 for "http" URIs.
  45. http_upgrade_ignore_h2(Config) ->
  46. doc("An h2 token in an Upgrade field must be ignored. (RFC7540 3.2)"),
  47. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  48. ok = gen_tcp:send(Socket, [
  49. "GET / HTTP/1.1\r\n"
  50. "Host: localhost\r\n"
  51. "Connection: Upgrade, HTTP2-Settings\r\n"
  52. "Upgrade: h2\r\n"
  53. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  54. "\r\n"]),
  55. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  56. ok.
  57. http_upgrade_ignore_if_http_10(Config) ->
  58. doc("The Upgrade header must be ignored if part of an HTTP/1.0 request. (RFC7230 6.7)"),
  59. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  60. ok = gen_tcp:send(Socket, [
  61. "GET / HTTP/1.0\r\n"
  62. "Host: localhost\r\n"
  63. "Connection: Upgrade, HTTP2-Settings\r\n"
  64. "Upgrade: h2c\r\n"
  65. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  66. "\r\n"]),
  67. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  68. ok.
  69. http_upgrade_ignore_missing_upgrade_in_connection(Config) ->
  70. doc("The Upgrade header must be listed in the "
  71. "Connection header field. (RFC7230 6.7)"),
  72. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  73. ok = gen_tcp:send(Socket, [
  74. "GET / HTTP/1.1\r\n"
  75. "Host: localhost\r\n"
  76. "Connection: HTTP2-Settings\r\n"
  77. "Upgrade: h2c\r\n"
  78. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  79. "\r\n"]),
  80. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  81. ok.
  82. http_upgrade_ignore_missing_http2_settings_in_connection(Config) ->
  83. doc("The HTTP2-Settings header must be listed in the "
  84. "Connection header field. (RFC7540 3.2.1, RFC7230 6.7)"),
  85. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  86. ok = gen_tcp:send(Socket, [
  87. "GET / HTTP/1.1\r\n"
  88. "Host: localhost\r\n"
  89. "Connection: Upgrade\r\n"
  90. "Upgrade: h2c\r\n"
  91. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  92. "\r\n"]),
  93. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  94. ok.
  95. http_upgrade_ignore_zero_http2_settings_header(Config) ->
  96. doc("The HTTP Upgrade request must include "
  97. "exactly one HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
  98. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  99. ok = gen_tcp:send(Socket, [
  100. "GET / HTTP/1.1\r\n"
  101. "Host: localhost\r\n"
  102. "Connection: Upgrade, HTTP2-Settings\r\n"
  103. "Upgrade: h2c\r\n"
  104. "\r\n"]),
  105. {ok, <<"HTTP/1.1 200">>} = gen_tcp:recv(Socket, 12, 1000),
  106. ok.
  107. http_upgrade_reject_two_http2_settings_header(Config) ->
  108. doc("The HTTP Upgrade request must include "
  109. "exactly one HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
  110. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  111. ok = gen_tcp:send(Socket, [
  112. "GET / HTTP/1.1\r\n"
  113. "Host: localhost\r\n"
  114. "Connection: Upgrade, HTTP2-Settings\r\n"
  115. "Upgrade: h2c\r\n"
  116. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  117. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  118. "\r\n"]),
  119. {ok, <<"HTTP/1.1 400">>} = gen_tcp:recv(Socket, 12, 1000),
  120. ok.
  121. http_upgrade_reject_bad_http2_settings_header(Config) ->
  122. doc("The HTTP Upgrade request must include "
  123. "a valid HTTP2-Settings header field (RFC7540 3.2, RFC7540 3.2.1)"),
  124. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  125. ok = gen_tcp:send(Socket, [
  126. "GET / HTTP/1.1\r\n"
  127. "Host: localhost\r\n"
  128. "Connection: Upgrade, HTTP2-Settings\r\n"
  129. "Upgrade: h2c\r\n"
  130. %% We send a full SETTINGS frame on purpose.
  131. "HTTP2-Settings: ", base64:encode(cow_http2:settings(#{})), "\r\n",
  132. "\r\n"]),
  133. {ok, <<"HTTP/1.1 400">>} = gen_tcp:recv(Socket, 12, 1000),
  134. ok.
  135. %% Match directly for now.
  136. do_recv_101(Socket) ->
  137. {ok, <<
  138. "HTTP/1.1 101 Switching Protocols\r\n"
  139. "connection: Upgrade\r\n"
  140. "upgrade: h2c\r\n"
  141. "\r\n"
  142. >>} = gen_tcp:recv(Socket, 71, 1000),
  143. ok.
  144. http_upgrade_101(Config) ->
  145. doc("A 101 response must be sent on successful upgrade "
  146. "to HTTP/2 when using the HTTP Upgrade mechanism. (RFC7540 3.2, RFC7230 6.7)"),
  147. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  148. ok = gen_tcp:send(Socket, [
  149. "GET / HTTP/1.1\r\n"
  150. "Host: localhost\r\n"
  151. "Connection: Upgrade, HTTP2-Settings\r\n"
  152. "Upgrade: h2c\r\n"
  153. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  154. "\r\n"]),
  155. ok = do_recv_101(Socket),
  156. ok.
  157. http_upgrade_server_preface(Config) ->
  158. doc("The first frame after the upgrade must be a "
  159. "SETTINGS frame for the server connection preface. (RFC7540 3.2, RFC7540 3.5, RFC7540 6.5)"),
  160. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  161. ok = gen_tcp:send(Socket, [
  162. "GET / HTTP/1.1\r\n"
  163. "Host: localhost\r\n"
  164. "Connection: Upgrade, HTTP2-Settings\r\n"
  165. "Upgrade: h2c\r\n"
  166. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  167. "\r\n"]),
  168. ok = do_recv_101(Socket),
  169. %% Receive the server preface.
  170. {ok, << _:24, 4:8, 0:40 >>} = gen_tcp:recv(Socket, 9, 1000),
  171. ok.
  172. http_upgrade_client_preface_timeout(Config) ->
  173. doc("Clients negotiating HTTP/2 and not sending a preface in "
  174. "a timely manner must be disconnected."),
  175. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  176. ok = gen_tcp:send(Socket, [
  177. "GET / HTTP/1.1\r\n"
  178. "Host: localhost\r\n"
  179. "Connection: Upgrade, HTTP2-Settings\r\n"
  180. "Upgrade: h2c\r\n"
  181. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  182. "\r\n"]),
  183. ok = do_recv_101(Socket),
  184. %% Receive the server preface.
  185. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  186. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  187. %% Receive the response to the initial HTTP/1.1 request.
  188. {ok, << HeadersSkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  189. {ok, _} = gen_tcp:recv(Socket, HeadersSkipLen, 1000),
  190. {ok, << DataSkipLen:24, 0:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  191. {ok, _} = gen_tcp:recv(Socket, DataSkipLen, 1000),
  192. %% Do not send the preface. Wait for the server to disconnect us.
  193. {error, closed} = gen_tcp:recv(Socket, 9, 6000),
  194. ok.
  195. http_upgrade_reject_missing_client_preface(Config) ->
  196. doc("Servers must treat an invalid connection preface as a "
  197. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  198. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  199. ok = gen_tcp:send(Socket, [
  200. "GET / HTTP/1.1\r\n"
  201. "Host: localhost\r\n"
  202. "Connection: Upgrade, HTTP2-Settings\r\n"
  203. "Upgrade: h2c\r\n"
  204. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  205. "\r\n"]),
  206. ok = do_recv_101(Socket),
  207. %% Send a SETTINGS frame directly instead of the proper preface.
  208. ok = gen_tcp:send(Socket, cow_http2:settings(#{})),
  209. %% Receive the server preface.
  210. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  211. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  212. %% We expect the server to close the connection when it receives a bad preface.
  213. %% The server may however have already started sending the response to the
  214. %% initial HTTP/1.1 request.
  215. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  216. case gen_tcp:recv(Socket, 9, 1000) of
  217. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  218. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  219. [headers|Acc];
  220. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  221. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  222. [data|Acc];
  223. {error, _} ->
  224. [closed|Acc]
  225. end
  226. end, [], [1, 2, 3])),
  227. case Received of
  228. [closed|_] -> ok;
  229. [headers, closed|_] -> ok;
  230. [headers, data, closed] -> ok
  231. end.
  232. http_upgrade_reject_invalid_client_preface(Config) ->
  233. doc("Servers must treat an invalid connection preface as a "
  234. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  235. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  236. ok = gen_tcp:send(Socket, [
  237. "GET / HTTP/1.1\r\n"
  238. "Host: localhost\r\n"
  239. "Connection: Upgrade, HTTP2-Settings\r\n"
  240. "Upgrade: h2c\r\n"
  241. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  242. "\r\n"]),
  243. ok = do_recv_101(Socket),
  244. %% Send a slightly incorrect preface.
  245. ok = gen_tcp:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
  246. %% Receive the server preface.
  247. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  248. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  249. %% We expect the server to close the connection when it receives a bad preface.
  250. %% The server may however have already started sending the response to the
  251. %% initial HTTP/1.1 request.
  252. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  253. case gen_tcp:recv(Socket, 9, 1000) of
  254. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  255. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  256. [headers|Acc];
  257. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  258. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  259. [data|Acc];
  260. {error, _} ->
  261. [closed|Acc]
  262. end
  263. end, [], [1, 2, 3])),
  264. case Received of
  265. [closed|_] -> ok;
  266. [headers, closed|_] -> ok;
  267. [headers, data, closed] -> ok
  268. end.
  269. http_upgrade_reject_missing_client_preface_settings(Config) ->
  270. doc("Servers must treat an invalid connection preface as a "
  271. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  272. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  273. ok = gen_tcp:send(Socket, [
  274. "GET / HTTP/1.1\r\n"
  275. "Host: localhost\r\n"
  276. "Connection: Upgrade, HTTP2-Settings\r\n"
  277. "Upgrade: h2c\r\n"
  278. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  279. "\r\n"]),
  280. ok = do_recv_101(Socket),
  281. %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
  282. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
  283. %% Receive the server preface.
  284. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  285. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  286. %% We expect the server to close the connection when it receives a bad preface.
  287. %% The server may however have already started sending the response to the
  288. %% initial HTTP/1.1 request.
  289. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  290. case gen_tcp:recv(Socket, 9, 1000) of
  291. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  292. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  293. [headers|Acc];
  294. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  295. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  296. [data|Acc];
  297. {error, _} ->
  298. [closed|Acc]
  299. end
  300. end, [], [1, 2, 3])),
  301. case Received of
  302. [closed|_] -> ok;
  303. [headers, closed|_] -> ok;
  304. [headers, data, closed] -> ok
  305. end.
  306. http_upgrade_reject_invalid_client_preface_settings(Config) ->
  307. doc("Servers must treat an invalid connection preface as a "
  308. "connection error of type PROTOCOL_ERROR. (RFC7540 3.2, RFC7540 3.5)"),
  309. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  310. ok = gen_tcp:send(Socket, [
  311. "GET / HTTP/1.1\r\n"
  312. "Host: localhost\r\n"
  313. "Connection: Upgrade, HTTP2-Settings\r\n"
  314. "Upgrade: h2c\r\n"
  315. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  316. "\r\n"]),
  317. ok = do_recv_101(Socket),
  318. %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
  319. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", << 0:24, 4:8, 0:9, 1:31 >>]),
  320. %% Receive the server preface.
  321. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  322. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  323. %% We expect the server to close the connection when it receives a bad preface.
  324. %% The server may however have already started sending the response to the
  325. %% initial HTTP/1.1 request.
  326. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  327. case gen_tcp:recv(Socket, 9, 1000) of
  328. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  329. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  330. [headers|Acc];
  331. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  332. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  333. [data|Acc];
  334. {error, _} ->
  335. [closed|Acc]
  336. end
  337. end, [], [1, 2, 3])),
  338. case Received of
  339. [closed|_] -> ok;
  340. [headers, closed|_] -> ok;
  341. [headers, data, closed] -> ok
  342. end.
  343. http_upgrade_accept_client_preface_empty_settings(Config) ->
  344. doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.2, RFC7540 3.5)"),
  345. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  346. ok = gen_tcp:send(Socket, [
  347. "GET / HTTP/1.1\r\n"
  348. "Host: localhost\r\n"
  349. "Connection: Upgrade, HTTP2-Settings\r\n"
  350. "Upgrade: h2c\r\n"
  351. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  352. "\r\n"]),
  353. ok = do_recv_101(Socket),
  354. %% Send a valid preface sequence except followed by an empty SETTINGS frame.
  355. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  356. %% Receive the server preface.
  357. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  358. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  359. %% Receive the SETTINGS ack.
  360. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  361. ok.
  362. http_upgrade_client_preface_settings_ack_timeout(Config) ->
  363. doc("The SETTINGS frames sent by the client must be acknowledged. (RFC7540 3.5, RFC7540 6.5.3)"),
  364. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  365. ok = gen_tcp:send(Socket, [
  366. "GET / HTTP/1.1\r\n"
  367. "Host: localhost\r\n"
  368. "Connection: Upgrade, HTTP2-Settings\r\n"
  369. "Upgrade: h2c\r\n"
  370. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  371. "\r\n"]),
  372. ok = do_recv_101(Socket),
  373. %% Send a valid preface.
  374. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  375. %% Receive the server preface.
  376. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  377. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  378. %% Receive the SETTINGS ack.
  379. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  380. %% Do not ack the server preface. Expect a GOAWAY with reason SETTINGS_TIMEOUT.
  381. {ok, << _:24, 7:8, _:72, 4:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  382. ok.
  383. %% @todo We need a successful test with actual options in HTTP2-Settings.
  384. %% SETTINGS_MAX_FRAME_SIZE is probably the easiest to test. The relevant
  385. %% RFC quote is:
  386. %%
  387. %% 3.2.1
  388. %% A server decodes and interprets these values as it would any other
  389. %% SETTINGS frame. Explicit acknowledgement of these settings
  390. %% (Section 6.5.3) is not necessary, since a 101 response serves as
  391. %% implicit acknowledgement.
  392. %% @todo We need to test an upgrade with a request body. It is probably
  393. %% worth having a configuration value for how much we accept while still
  394. %% upgrading (if too big, we would just stay on HTTP/1.1). We therefore
  395. %% needs a test for when the body is small enough, and one for when the
  396. %% body is larger than we accept. The relevant RFC quote is:
  397. %%
  398. %% 3.2
  399. %% Requests that contain a payload body MUST be sent in their entirety
  400. %% before the client can send HTTP/2 frames. This means that a large
  401. %% request can block the use of the connection until it is completely
  402. %% sent.
  403. %% @todo We should definitely have a test with OPTIONS. The relevant
  404. %% RFC quote is:
  405. %%
  406. %% 3.2
  407. %% If concurrency of an initial request with subsequent requests is
  408. %% important, an OPTIONS request can be used to perform the upgrade to
  409. %% HTTP/2, at the cost of an additional round trip.
  410. %% @todo If we ever handle priority, we need to check that the initial
  411. %% HTTP/1.1 request has default priority. The relevant RFC quote is:
  412. %%
  413. %% 3.2
  414. %% The HTTP/1.1 request that is sent prior to upgrade is assigned a
  415. %% stream identifier of 1 (see Section 5.1.1) with default priority
  416. %% values (Section 5.3.5).
  417. http_upgrade_response(Config) ->
  418. doc("A response must be sent to the initial HTTP/1.1 request "
  419. "after switching to HTTP/2. The response must use "
  420. "the stream identifier 1. (RFC7540 3.2)"),
  421. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  422. ok = gen_tcp:send(Socket, [
  423. "GET / HTTP/1.1\r\n"
  424. "Host: localhost\r\n"
  425. "Connection: Upgrade, HTTP2-Settings\r\n"
  426. "Upgrade: h2c\r\n"
  427. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  428. "\r\n"]),
  429. ok = do_recv_101(Socket),
  430. %% Send a valid preface.
  431. %% @todo Use non-empty SETTINGS here. Just because.
  432. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  433. %% Receive the server preface.
  434. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  435. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  436. %% Send the SETTINGS ack.
  437. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  438. %% Receive the SETTINGS ack, and the response HEADERS and DATA (Stream ID 1).
  439. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  440. case gen_tcp:recv(Socket, 9, 1000) of
  441. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  442. [settings_ack|Acc];
  443. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  444. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  445. [headers|Acc];
  446. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  447. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  448. [data|Acc]
  449. end
  450. end, [], [1, 2, 3])),
  451. case Received of
  452. [settings_ack, headers, data] -> ok;
  453. [headers, settings_ack, data] -> ok;
  454. [headers, data, settings_ack] -> ok
  455. end.
  456. http_upgrade_response_half_closed(Config) ->
  457. doc("The stream for the initial HTTP/1.1 request is half-closed. (RFC7540 3.2)"),
  458. %% Try sending more data after the upgrade and get an error.
  459. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  460. ok = gen_tcp:send(Socket, [
  461. "GET / HTTP/1.1\r\n"
  462. "Host: localhost\r\n"
  463. "Connection: Upgrade, HTTP2-Settings\r\n"
  464. "Upgrade: h2c\r\n"
  465. "HTTP2-Settings: ", base64:encode(cow_http2:settings_payload(#{})), "\r\n",
  466. "\r\n"]),
  467. ok = do_recv_101(Socket),
  468. %% Send a valid preface followed by an unexpected DATA frame.
  469. ok = gen_tcp:send(Socket, [
  470. "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n",
  471. cow_http2:settings(#{}),
  472. cow_http2:data(1, fin, <<"Unexpected DATA frame.">>)
  473. ]),
  474. %% Receive the server preface.
  475. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  476. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  477. %% Skip the SETTINGS ack, receive the response HEADERS, DATA and RST_STREAM (streamid 1).
  478. Received = lists:reverse(lists:foldl(fun(_, Acc) ->
  479. case gen_tcp:recv(Socket, 9, 1000) of
  480. {ok, << 0:24, 4:8, 1:8, 0:32 >>} ->
  481. Acc;
  482. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} ->
  483. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  484. [headers|Acc];
  485. {ok, << SkipLen:24, 0:8, _:8, 1:32 >>} ->
  486. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  487. [data|Acc];
  488. {ok, << 4:24, 3:8, 0:8, 1:32 >>} ->
  489. %% We expect a STREAM_CLOSED reason.
  490. {ok, << 5:32 >>} = gen_tcp:recv(Socket, 4, 1000),
  491. [rst_stream|Acc];
  492. {error, _} ->
  493. %% Can be timeouts, ignore them.
  494. Acc
  495. end
  496. end, [], [1, 2, 3, 4])),
  497. case Received of
  498. [rst_stream] -> ok;
  499. [headers, rst_stream] -> ok;
  500. [headers, data, rst_stream] -> ok
  501. end.
  502. %% Starting HTTP/2 for "https" URIs.
  503. alpn_ignore_h2c(Config) ->
  504. doc("An h2c ALPN protocol identifier must be ignored. (RFC7540 3.3)"),
  505. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  506. [{alpn_advertised_protocols, [<<"h2c">>, <<"http/1.1">>]}, binary, {active, false}]),
  507. {ok, <<"http/1.1">>} = ssl:negotiated_protocol(Socket),
  508. ok.
  509. alpn_server_preface(Config) ->
  510. doc("The first frame must be a SETTINGS frame "
  511. "for the server connection preface. (RFC7540 3.3, RFC7540 3.5, RFC7540 6.5)"),
  512. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  513. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  514. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  515. %% Receive the server preface.
  516. {ok, << _:24, 4:8, 0:40 >>} = ssl:recv(Socket, 9, 1000),
  517. ok.
  518. alpn_client_preface_timeout(Config) ->
  519. doc("Clients negotiating HTTP/2 and not sending a preface in "
  520. "a timely manner must be disconnected."),
  521. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  522. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  523. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  524. %% Receive the server preface.
  525. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  526. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  527. %% Do not send the preface. Wait for the server to disconnect us.
  528. {error, closed} = ssl:recv(Socket, 3, 6000),
  529. ok.
  530. alpn_reject_missing_client_preface(Config) ->
  531. doc("Servers must treat an invalid connection preface as a "
  532. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  533. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  534. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  535. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  536. %% Send a SETTINGS frame directly instead of the proper preface.
  537. ok = ssl:send(Socket, cow_http2:settings(#{})),
  538. %% Receive the server preface.
  539. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  540. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  541. %% We expect the server to close the connection when it receives a bad preface.
  542. {error, closed} = ssl:recv(Socket, 3, 1000),
  543. ok.
  544. alpn_reject_invalid_client_preface(Config) ->
  545. doc("Servers must treat an invalid connection preface as a "
  546. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  547. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  548. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  549. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  550. %% Send a slightly incorrect preface.
  551. ok = ssl:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
  552. %% Receive the server preface.
  553. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  554. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  555. %% We expect the server to close the connection when it receives a bad preface.
  556. {error, closed} = ssl:recv(Socket, 3, 1000),
  557. ok.
  558. alpn_reject_missing_client_preface_settings(Config) ->
  559. doc("Servers must treat an invalid connection preface as a "
  560. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  561. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  562. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  563. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  564. %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
  565. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
  566. %% Receive the server preface.
  567. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  568. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  569. %% We expect the server to close the connection when it receives a bad preface.
  570. {error, closed} = ssl:recv(Socket, 3, 1000),
  571. ok.
  572. alpn_reject_invalid_client_preface_settings(Config) ->
  573. doc("Servers must treat an invalid connection preface as a "
  574. "connection error of type PROTOCOL_ERROR. (RFC7540 3.3, RFC7540 3.5)"),
  575. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  576. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  577. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  578. %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
  579. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", << 0:24, 4:8, 0:9, 1:31 >>]),
  580. %% Receive the server preface.
  581. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  582. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  583. %% We expect the server to close the connection when it receives a bad preface.
  584. {error, closed} = ssl:recv(Socket, 3, 1000),
  585. ok.
  586. alpn_accept_client_preface_empty_settings(Config) ->
  587. doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.3, RFC7540 3.5)"),
  588. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  589. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  590. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  591. %% Send a valid preface sequence except followed by an empty SETTINGS frame.
  592. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  593. %% Receive the server preface.
  594. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  595. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  596. %% Receive the SETTINGS ack.
  597. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  598. ok.
  599. alpn_client_preface_settings_ack_timeout(Config) ->
  600. doc("Failure to acknowledge the server's SETTINGS frame "
  601. "results in a SETTINGS_TIMEOUT connection error. (RFC7540 3.5, RFC7540 6.5.3)"),
  602. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  603. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  604. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  605. %% Send a valid preface.
  606. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  607. %% Receive the server preface.
  608. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  609. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  610. %% Receive the SETTINGS ack.
  611. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  612. %% Do not ack the server preface. Expect a GOAWAY with reason SETTINGS_TIMEOUT.
  613. {ok, << _:24, 7:8, _:72, 4:32 >>} = ssl:recv(Socket, 17, 6000),
  614. ok.
  615. alpn(Config) ->
  616. doc("Successful ALPN negotiation. (RFC7540 3.3)"),
  617. {ok, Socket} = ssl:connect("localhost", config(port, Config),
  618. [{alpn_advertised_protocols, [<<"h2">>]}, binary, {active, false}]),
  619. {ok, <<"h2">>} = ssl:negotiated_protocol(Socket),
  620. %% Send a valid preface.
  621. %% @todo Use non-empty SETTINGS here. Just because.
  622. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  623. %% Receive the server preface.
  624. {ok, << Len:24 >>} = ssl:recv(Socket, 3, 1000),
  625. {ok, << 4:8, 0:40, _:Len/binary >>} = ssl:recv(Socket, 6 + Len, 1000),
  626. %% Send the SETTINGS ack.
  627. ok = ssl:send(Socket, cow_http2:settings_ack()),
  628. %% Receive the SETTINGS ack.
  629. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = ssl:recv(Socket, 9, 1000),
  630. %% Wait until after the SETTINGS ack timeout was supposed to trigger.
  631. receive after 6000 -> ok end,
  632. %% Send a PING.
  633. ok = ssl:send(Socket, cow_http2:ping(0)),
  634. %% Receive a PING ack back, indicating the connection is still up.
  635. {ok, << 8:24, 6:8, 0:7, 1:1, 0:96 >>} = ssl:recv(Socket, 17, 1000),
  636. ok.
  637. %% Starting HTTP/2 with prior knowledge.
  638. prior_knowledge_reject_tls(Config) ->
  639. doc("Implementations that support HTTP/2 over TLS must use ALPN. (RFC7540 3.4)"),
  640. {ok, Socket} = ssl:connect("localhost", config(port, Config), [binary, {active, false}]),
  641. %% Send a valid preface.
  642. ok = ssl:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  643. %% We expect the server to send an HTTP 400 error
  644. %% when trying to use HTTP/2 without going through ALPN negotiation.
  645. {ok, <<"HTTP/1.1 400">>} = ssl:recv(Socket, 12, 1000),
  646. ok.
  647. prior_knowledge_server_preface(Config) ->
  648. doc("The first frame must be a SETTINGS frame "
  649. "for the server connection preface. (RFC7540 3.4, RFC7540 3.5, RFC7540 6.5)"),
  650. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  651. %% Send a valid preface.
  652. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  653. %% Receive the server preface.
  654. {ok, << _:24, 4:8, 0:40 >>} = gen_tcp:recv(Socket, 9, 1000),
  655. ok.
  656. %% Note: the client preface timeout doesn't apply in this case,
  657. %% so we don't test it. An HTTP/1.1 client that does not send
  658. %% a request in a timely manner will get disconnected by the
  659. %% HTTP protocol code, not by HTTP/2's.
  660. %% Note: the test that starts by sending a SETTINGS frame is
  661. %% redundant with tests sending garbage on the connection.
  662. %% From the point of view of an HTTP/1.1 connection, a
  663. %% SETTINGS frame is indistinguishable from garbage.
  664. prior_knowledge_reject_invalid_client_preface(Config) ->
  665. doc("An incorrect preface is an invalid HTTP/1.1 request. (RFC7540 3.4)"),
  666. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  667. %% Send a slightly incorrect preface.
  668. ok = gen_tcp:send(Socket, "PRI * HTTP/2.0\r\n\r\nSM: Value\r\n\r\n"),
  669. %% We propagate to HTTP/2 after checking only the request-line.
  670. %% The server then sends its preface before checking the full client preface.
  671. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  672. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  673. %% We expect the server to close the connection when it receives a bad preface.
  674. {error, closed} = gen_tcp:recv(Socket, 9, 1000),
  675. ok.
  676. prior_knowledge_reject_missing_client_preface_settings(Config) ->
  677. doc("Servers must treat an invalid connection preface as a "
  678. "connection error of type PROTOCOL_ERROR. (RFC7540 3.4, RFC7540 3.5)"),
  679. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  680. %% Send a valid preface sequence except followed by a PING instead of a SETTINGS frame.
  681. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:ping(0)]),
  682. %% Receive the server preface.
  683. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  684. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  685. %% We expect the server to close the connection when it receives a bad preface.
  686. {error, closed} = gen_tcp:recv(Socket, 9, 1000),
  687. ok.
  688. prior_knowledge_reject_invalid_client_preface_settings(Config) ->
  689. doc("Servers must treat an invalid connection preface as a "
  690. "connection error of type PROTOCOL_ERROR. (RFC7540 3.4, RFC7540 3.5)"),
  691. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  692. %% Send a valid preface sequence except followed by a badly formed SETTINGS frame.
  693. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", << 0:24, 4:8, 0:9, 1:31 >>]),
  694. %% Receive the server preface.
  695. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  696. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  697. %% We expect the server to close the connection when it receives a bad preface.
  698. {error, closed} = gen_tcp:recv(Socket, 9, 1000),
  699. ok.
  700. prior_knowledge_accept_client_preface_empty_settings(Config) ->
  701. doc("The SETTINGS frame in the client preface may be empty. (RFC7540 3.4, RFC7540 3.5)"),
  702. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  703. %% Send a valid preface sequence except followed by an empty SETTINGS frame.
  704. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  705. %% Receive the server preface.
  706. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  707. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  708. %% Receive the SETTINGS ack.
  709. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  710. ok.
  711. prior_knowledge_client_preface_settings_ack_timeout(Config) ->
  712. doc("The SETTINGS frames sent by the client must be acknowledged. (RFC7540 3.5, RFC7540 6.5.3)"),
  713. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  714. %% Send a valid preface.
  715. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  716. %% Receive the server preface.
  717. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  718. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  719. %% Receive the SETTINGS ack.
  720. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  721. %% Do not ack the server preface. Expect a GOAWAY with reason SETTINGS_TIMEOUT.
  722. {ok, << _:24, 7:8, _:72, 4:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  723. ok.
  724. %% Do a prior knowledge handshake.
  725. do_handshake(Config) ->
  726. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  727. %% Send a valid preface.
  728. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  729. %% Receive the server preface.
  730. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  731. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  732. %% Send the SETTINGS ack.
  733. ok = gen_tcp:send(Socket, cow_http2:settings_ack()),
  734. %% Receive the SETTINGS ack.
  735. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  736. {ok, Socket}.
  737. prior_knowledge(Config) ->
  738. doc("Streams can be initiated after a successful HTTP/2 connection "
  739. "with prior knowledge of server capabilities. (RFC7540 3.4)"),
  740. %% @todo Use non-empty SETTINGS here. Just because.
  741. {ok, Socket} = do_handshake(Config),
  742. %% Wait until after the SETTINGS ack timeout was supposed to trigger.
  743. receive after 6000 -> ok end,
  744. %% Send a PING.
  745. ok = gen_tcp:send(Socket, cow_http2:ping(0)),
  746. %% Receive a PING ack back, indicating the connection is still up.
  747. {ok, << 8:24, 6:8, 0:7, 1:1, 0:96 >>} = gen_tcp:recv(Socket, 17, 1000),
  748. ok.
  749. %% @todo If we ever add an option to disable HTTP/2, we need to check
  750. %% the following things:
  751. %% * HTTP/1.1 Upgrade returns an HTTP/1.1 response (3.2)
  752. %% * HTTP/1.1 Upgrade errors out if the client sends HTTP/2 frames
  753. %% without waiting for the 101 response (3.2, 3.5)
  754. %% * Prior knowledge handshake fails (3.4)
  755. %% * ALPN selects HTTP/1.1 (3.3)
  756. %% Frame size.
  757. max_frame_size_allow_exactly_default(Config) ->
  758. doc("All implementations must allow frame sizes of at least 16384. (RFC7540 4.1, RFC7540 4.2)"),
  759. {ok, Socket} = do_handshake(Config),
  760. %% Send a POST request with a DATA frame of exactly 16384 bytes.
  761. {HeadersBlock, _} = cow_hpack:encode([
  762. {<<":method">>, <<"POST">>},
  763. {<<":scheme">>, <<"http">>},
  764. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  765. {<<":path">>, <<"/echo/read_body">>}
  766. ]),
  767. ok = gen_tcp:send(Socket, [
  768. cow_http2:headers(1, nofin, HeadersBlock),
  769. cow_http2:data(1, fin, << 0:16384/unit:8 >>)
  770. ]),
  771. %% Receive a response with the same DATA frame.
  772. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  773. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  774. {ok, << 16384:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  775. {ok, << 0:16384/unit:8 >>} = gen_tcp:recv(Socket, 16384, 1000),
  776. ok.
  777. max_frame_size_reject_larger_than_default(Config) ->
  778. doc("A FRAME_SIZE_ERROR connection error must be sent when receiving "
  779. "frames larger than the default 16384 length. (RFC7540 4.1, RFC7540 4.2)"),
  780. {ok, Socket} = do_handshake(Config),
  781. %% Send a POST request with a DATA frame larger than 16384 bytes.
  782. {HeadersBlock, _} = cow_hpack:encode([
  783. {<<":method">>, <<"POST">>},
  784. {<<":scheme">>, <<"http">>},
  785. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  786. {<<":path">>, <<"/echo/read_body">>}
  787. ]),
  788. ok = gen_tcp:send(Socket, [
  789. cow_http2:headers(1, nofin, HeadersBlock),
  790. cow_http2:data(1, fin, << 0:16385/unit:8 >>)
  791. ]),
  792. %% Receive a FRAME_SIZE_ERROR connection error.
  793. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  794. ok.
  795. %% @todo We need configurable SETTINGS in Cowboy for these tests.
  796. %% max_frame_size_config_reject_too_small(Config) ->
  797. %% doc("SETTINGS_MAX_FRAME_SIZE configuration values smaller than "
  798. %% "16384 must be rejected. (RFC7540 6.5.2)"),
  799. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  800. %% todo.
  801. %%
  802. %% max_frame_size_config_reject_too_large(Config) ->
  803. %% doc("SETTINGS_MAX_FRAME_SIZE configuration values larger than "
  804. %% "16777215 must be rejected. (RFC7540 6.5.2)"),
  805. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  806. %% todo.
  807. %%
  808. %% max_frame_size_allow_exactly_custom(Config) ->
  809. %% doc("An endpoint that sets SETTINGS_MAX_FRAME_SIZE must allow frames "
  810. %% "of up to that size. (RFC7540 4.2, RFC7540 6.5.2)"),
  811. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  812. %% todo.
  813. %%
  814. %% max_frame_size_reject_larger_than_custom(Config) ->
  815. %% doc("An endpoint that sets SETTINGS_MAX_FRAME_SIZE must reject frames "
  816. %% "of up to that size with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.5.2)"),
  817. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  818. %% todo.
  819. %% @todo How do I test this?
  820. %%
  821. %% max_frame_size_client_default_respect_limits(Config) ->
  822. %% doc("The server must not send frame sizes of more "
  823. %% "than 16384 by default. (RFC7540 4.1, RFC7540 4.2)"),
  824. %% This is about the client sending a SETTINGS frame.
  825. max_frame_size_client_override_reject_too_small(Config) ->
  826. doc("A SETTINGS_MAX_FRAME_SIZE smaller than 16384 must be rejected "
  827. "with a PROTOCOL_ERROR connection error. (RFC7540 6.5.2)"),
  828. {ok, Socket} = do_handshake(Config),
  829. %% Send a SETTINGS frame with a SETTINGS_MAX_FRAME_SIZE lower than 16384.
  830. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:40, 5:16, 16383:32 >>),
  831. %% Receive a PROTOCOL_ERROR connection error.
  832. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  833. ok.
  834. %% This is about the client sending a SETTINGS frame.
  835. max_frame_size_client_override_reject_too_large(Config) ->
  836. doc("A SETTINGS_MAX_FRAME_SIZE larger than 16777215 must be rejected "
  837. "with a PROTOCOL_ERROR connection error. (RFC7540 6.5.2)"),
  838. {ok, Socket} = do_handshake(Config),
  839. %% Send a SETTINGS frame with a SETTINGS_MAX_FRAME_SIZE larger than 16777215.
  840. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:40, 5:16, 16777216:32 >>),
  841. %% Receive a PROTOCOL_ERROR connection error.
  842. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  843. ok.
  844. %% @todo How do I test this?
  845. %%
  846. %% max_frame_size_client_custom_respect_limits(Config) ->
  847. %% doc("The server must not send frame sizes of more than "
  848. %% "client's advertised limits. (RFC7540 4.1, RFC7540 4.2)"),
  849. %% I am using FRAME_SIZE_ERROR here because the information in the
  850. %% frame header tells us this frame is at least 1 byte long, while
  851. %% the given length is smaller; i.e. it is too small to contain
  852. %% mandatory frame data (the pad length).
  853. data_reject_frame_size_0_padded_flag(Config) ->
  854. doc("DATA frames of size 0 with the PADDED flag set must be rejected "
  855. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.1)"),
  856. {ok, Socket} = do_handshake(Config),
  857. %% Send a POST request with an incorrect padded DATA frame size.
  858. {HeadersBlock, _} = cow_hpack:encode([
  859. {<<":method">>, <<"POST">>},
  860. {<<":scheme">>, <<"http">>},
  861. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  862. {<<":path">>, <<"/echo/read_body">>}
  863. ]),
  864. ok = gen_tcp:send(Socket, [
  865. cow_http2:headers(1, nofin, HeadersBlock),
  866. << 0:24, 0:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31 >>
  867. ]),
  868. %% Receive a FRAME_SIZE_ERROR connection error.
  869. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  870. ok.
  871. %% This case on the other hand is noted specifically in the RFC
  872. %% as being a PROTOCOL_ERROR. It can be thought of as the Pad Length
  873. %% being incorrect, rather than the frame size.
  874. data_reject_frame_size_too_small_padded_flag(Config) ->
  875. doc("DATA frames with Pad Length >= Length must be rejected "
  876. "with a PROTOCOL_ERROR connection error. (RFC7540 6.1)"),
  877. {ok, Socket} = do_handshake(Config),
  878. %% Send a POST request with an incorrect padded DATA frame size.
  879. {HeadersBlock, _} = cow_hpack:encode([
  880. {<<":method">>, <<"POST">>},
  881. {<<":scheme">>, <<"http">>},
  882. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  883. {<<":path">>, <<"/echo/read_body">>}
  884. ]),
  885. ok = gen_tcp:send(Socket, [
  886. cow_http2:headers(1, nofin, HeadersBlock),
  887. << 10:24, 0:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:80 >>
  888. ]),
  889. %% Receive a PROTOCOL_ERROR connection error.
  890. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  891. ok.
  892. headers_reject_frame_size_0_padded_flag(Config) ->
  893. doc("HEADERS frames of size 0 with the PADDED flag set must be rejected "
  894. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  895. {ok, Socket} = do_handshake(Config),
  896. %% Send a padded HEADERS frame with an incorrect size.
  897. ok = gen_tcp:send(Socket, << 0:24, 1:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31 >>),
  898. %% Receive a FRAME_SIZE_ERROR connection error.
  899. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  900. ok.
  901. headers_reject_frame_size_too_small_padded_flag(Config) ->
  902. doc("HEADERS frames with no priority flag and Pad Length >= Length "
  903. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 6.2)"),
  904. {ok, Socket} = do_handshake(Config),
  905. %% Send a padded HEADERS frame with an incorrect size.
  906. ok = gen_tcp:send(Socket, << 10:24, 1:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:80 >>),
  907. %% Receive a PROTOCOL_ERROR connection error.
  908. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  909. ok.
  910. headers_reject_frame_size_too_small_priority_flag(Config) ->
  911. doc("HEADERS frames of size smaller than 5 with the PRIORITY flag set must be rejected "
  912. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  913. {ok, Socket} = do_handshake(Config),
  914. %% Send a HEADERS frame with priority set and an incorrect size.
  915. ok = gen_tcp:send(Socket, << 4:24, 1:8,
  916. 0:2, 1:1, 0:4, 1:1, 0:1, 1:31, 0:1, 3:31, 0:8 >>),
  917. %% Receive a FRAME_SIZE_ERROR connection error.
  918. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  919. ok.
  920. headers_reject_frame_size_5_padded_and_priority_flags(Config) ->
  921. doc("HEADERS frames of size smaller than 6 with the PADDED "
  922. "and PRIORITY flags set must be rejected "
  923. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  924. {ok, Socket} = do_handshake(Config),
  925. %% Send a padded HEADERS frame with an incorrect size.
  926. ok = gen_tcp:send(Socket, << 5:24, 1:8,
  927. 0:2, 1:1, 0:1, 1:1, 0:2, 1:1, 0:1, 1:31, 0:8, 0:1, 3:31, 0:8 >>),
  928. %% Receive a FRAME_SIZE_ERROR connection error.
  929. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  930. ok.
  931. headers_reject_frame_size_too_small_padded_and_priority_flags(Config) ->
  932. doc("HEADERS frames of size smaller than Length+6 with the PADDED and PRIORITY flags set "
  933. "must be rejected with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  934. {ok, Socket} = do_handshake(Config),
  935. %% Send a padded HEADERS frame with an incorrect size.
  936. ok = gen_tcp:send(Socket, << 15:24, 1:8,
  937. 0:2, 1:1, 0:1, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:1, 3:31, 0:8, 0:80 >>),
  938. %% Receive a PROTOCOL_ERROR connection error.
  939. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  940. ok.
  941. priority_reject_frame_size_too_small(Config) ->
  942. doc("PRIORITY frames of size smaller than 5 must be rejected "
  943. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.3)"),
  944. {ok, Socket} = do_handshake(Config),
  945. %% Send a PRIORITY frame with an incorrect size.
  946. ok = gen_tcp:send(Socket, << 4:24, 2:8, 0:9, 1:31, 0:1, 3:31, 0:8 >>),
  947. %% Receive a FRAME_SIZE_ERROR stream error.
  948. {ok, << _:24, 3:8, _:40, 6:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  949. ok.
  950. priority_reject_frame_size_too_large(Config) ->
  951. doc("PRIORITY frames of size larger than 5 must be rejected "
  952. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.3)"),
  953. {ok, Socket} = do_handshake(Config),
  954. %% Send a PRIORITY frame with an incorrect size.
  955. ok = gen_tcp:send(Socket, << 6:24, 2:8, 0:9, 1:31, 0:1, 3:31, 0:16 >>),
  956. %% Receive a FRAME_SIZE_ERROR stream error.
  957. {ok, << _:24, 3:8, _:40, 6:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  958. ok.
  959. rst_stream_reject_frame_size_too_small(Config) ->
  960. doc("RST_STREAM frames of size smaller than 4 must be rejected "
  961. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.4)"),
  962. {ok, Socket} = do_handshake(Config),
  963. %% Send a request and reset it immediately.
  964. {HeadersBlock, _} = cow_hpack:encode([
  965. {<<":method">>, <<"GET">>},
  966. {<<":scheme">>, <<"http">>},
  967. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  968. {<<":path">>, <<"/">>}
  969. ]),
  970. ok = gen_tcp:send(Socket, [
  971. cow_http2:headers(1, fin, HeadersBlock),
  972. << 3:24, 3:8, 0:9, 1:31, 8:32 >>
  973. ]),
  974. %% Receive a FRAME_SIZE_ERROR connection error.
  975. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  976. ok.
  977. rst_stream_reject_frame_size_too_large(Config) ->
  978. doc("RST_STREAM frames of size larger than 4 must be rejected "
  979. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.4)"),
  980. {ok, Socket} = do_handshake(Config),
  981. %% Send a request and reset it immediately.
  982. {HeadersBlock, _} = cow_hpack:encode([
  983. {<<":method">>, <<"GET">>},
  984. {<<":scheme">>, <<"http">>},
  985. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  986. {<<":path">>, <<"/">>}
  987. ]),
  988. ok = gen_tcp:send(Socket, [
  989. cow_http2:headers(1, fin, HeadersBlock),
  990. << 5:24, 3:8, 0:9, 1:31, 8:32 >>
  991. ]),
  992. %% Receive a FRAME_SIZE_ERROR connection error.
  993. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  994. ok.
  995. settings_reject_bad_frame_size(Config) ->
  996. doc("SETTINGS frames must have a size multiple of 6 or be rejected "
  997. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.5)"),
  998. {ok, Socket} = do_handshake(Config),
  999. %% Send a SETTINGS frame with an incorrect size.
  1000. ok = gen_tcp:send(Socket, << 5:24, 4:8, 0:40, 1:16, 4096:32 >>),
  1001. %% Receive a FRAME_SIZE_ERROR connection error.
  1002. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1003. ok.
  1004. settings_ack_reject_non_empty_frame_size(Config) ->
  1005. doc("SETTINGS frames with the ACK flag set and a non-empty payload "
  1006. "must be rejected with a FRAME_SIZE_ERROR connection error (RFC7540 4.2, RFC7540 6.5)"),
  1007. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  1008. %% Send a valid preface.
  1009. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  1010. %% Receive the server preface.
  1011. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  1012. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  1013. %% Send a SETTINGS ack with a payload.
  1014. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:7, 1:1, 0:32, 1:16, 4096:32 >>),
  1015. %% Receive the SETTINGS ack.
  1016. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1017. %% Receive a FRAME_SIZE_ERROR connection error.
  1018. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1019. ok.
  1020. %% Note that clients are not supposed to send PUSH_PROMISE frames.
  1021. %% However when they do, we need to be able to parse it in order
  1022. %% to reject it, and so these errors may still occur.
  1023. push_promise_reject_frame_size_too_small(Config) ->
  1024. doc("PUSH_PROMISE frames of size smaller than 4 must be rejected "
  1025. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1026. {ok, Socket} = do_handshake(Config),
  1027. %% Send a PUSH_PROMISE frame with an incorrect size.
  1028. {HeadersBlock, _} = cow_hpack:encode([
  1029. {<<":method">>, <<"GET">>},
  1030. {<<":scheme">>, <<"http">>},
  1031. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1032. {<<":path">>, <<"/">>}
  1033. ]),
  1034. ok = gen_tcp:send(Socket, [
  1035. << 3:24, 5:8, 0:5, 1:1, 0:3, 1:31, 0:1, 3:31 >>,
  1036. HeadersBlock
  1037. ]),
  1038. %% Receive a FRAME_SIZE_ERROR connection error.
  1039. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1040. ok.
  1041. push_promise_reject_frame_size_4_padded_flag(Config) ->
  1042. doc("PUSH_PROMISE frames of size smaller than 5 with the PADDED flag set must be rejected "
  1043. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1044. {ok, Socket} = do_handshake(Config),
  1045. %% Send a PUSH_PROMISE frame with an incorrect size.
  1046. {HeadersBlock, _} = cow_hpack:encode([
  1047. {<<":method">>, <<"GET">>},
  1048. {<<":scheme">>, <<"http">>},
  1049. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1050. {<<":path">>, <<"/">>}
  1051. ]),
  1052. ok = gen_tcp:send(Socket, [
  1053. << 4:24, 5:8, 0:4, 1:1, 1:1, 0:3, 1:31, 0:1, 0:8, 3:31 >>,
  1054. HeadersBlock
  1055. ]),
  1056. %% Receive a FRAME_SIZE_ERROR connection error.
  1057. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1058. ok.
  1059. push_promise_reject_frame_size_too_small_padded_flag(Config) ->
  1060. doc("PUSH_PROMISE frames of size smaller than Length+5 with the PADDED flag set "
  1061. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1062. {ok, Socket} = do_handshake(Config),
  1063. %% Send a PUSH_PROMISE frame with an incorrect size.
  1064. {HeadersBlock, _} = cow_hpack:encode([
  1065. {<<":method">>, <<"GET">>},
  1066. {<<":scheme">>, <<"http">>},
  1067. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1068. {<<":path">>, <<"/">>}
  1069. ]),
  1070. ok = gen_tcp:send(Socket, [
  1071. << 14:24, 5:8, 0:4, 1:1, 1:1, 0:3, 1:31, 10:8, 0:1, 3:31 >>,
  1072. HeadersBlock,
  1073. << 0:80 >>
  1074. ]),
  1075. %% Receive a PROTOCOL_ERROR connection error.
  1076. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1077. ok.
  1078. ping_reject_frame_size_too_small(Config) ->
  1079. doc("PING frames of size smaller than 8 must be rejected "
  1080. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.7)"),
  1081. {ok, Socket} = do_handshake(Config),
  1082. %% Send a PING frame with an incorrect size.
  1083. ok = gen_tcp:send(Socket, << 7:24, 6:8, 0:40, 0:56 >>),
  1084. %% Receive a FRAME_SIZE_ERROR connection error.
  1085. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1086. ok.
  1087. ping_reject_frame_size_too_large(Config) ->
  1088. doc("PING frames of size larger than 8 must be rejected "
  1089. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.7)"),
  1090. {ok, Socket} = do_handshake(Config),
  1091. %% Send a PING frame with an incorrect size.
  1092. ok = gen_tcp:send(Socket, << 9:24, 6:8, 0:40, 0:72 >>),
  1093. %% Receive a FRAME_SIZE_ERROR connection error.
  1094. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1095. ok.
  1096. goaway_reject_frame_size_too_small(Config) ->
  1097. doc("GOAWAY frames of size smaller than 8 must be rejected "
  1098. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.8)"),
  1099. {ok, Socket} = do_handshake(Config),
  1100. %% Send a GOAWAY frame with an incorrect size.
  1101. ok = gen_tcp:send(Socket, << 7:24, 7:8, 0:40, 0:56 >>),
  1102. %% Receive a FRAME_SIZE_ERROR connection error.
  1103. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1104. ok.
  1105. goaway_allow_frame_size_too_large(Config) ->
  1106. doc("GOAWAY frames of size larger than 8 must be allowed. (RFC7540 6.8)"),
  1107. {ok, Socket} = do_handshake(Config),
  1108. %% Send a GOAWAY frame with debug data.
  1109. ok = gen_tcp:send(Socket, << 12:24, 7:8, 0:40, 0:64, 99999:32 >>),
  1110. %% Receive a GOAWAY frame back.
  1111. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1112. ok.
  1113. window_update_reject_frame_size_too_small(Config) ->
  1114. doc("WINDOW_UPDATE frames of size smaller than 4 must be rejected "
  1115. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.9)"),
  1116. {ok, Socket} = do_handshake(Config),
  1117. %% Send a WINDOW_UPDATE frame with an incorrect size.
  1118. ok = gen_tcp:send(Socket, << 3:24, 8:8, 0:40, 1000:24 >>),
  1119. %% Receive a FRAME_SIZE_ERROR connection error.
  1120. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1121. ok.
  1122. window_update_reject_frame_size_too_large(Config) ->
  1123. doc("WINDOW_UPDATE frames of size larger than 4 must be rejected "
  1124. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.9)"),
  1125. {ok, Socket} = do_handshake(Config),
  1126. %% Send a WINDOW_UPDATE frame with an incorrect size.
  1127. ok = gen_tcp:send(Socket, << 5:24, 8:8, 0:40, 1000:40 >>),
  1128. %% Receive a FRAME_SIZE_ERROR connection error.
  1129. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1130. ok.
  1131. %% Note: There is no particular limits on the size of CONTINUATION frames,
  1132. %% they can go from 0 to SETTINGS_MAX_FRAME_SIZE.