rfc7540_SUITE.erl 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  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 format.
  757. ignore_unknown_frames(Config) ->
  758. doc("Frames of unknown type must be ignored and discarded. (RFC7540 4.1)"),
  759. {ok, Socket} = do_handshake(Config),
  760. %% Send a POST request with a single DATA frame,
  761. %% and an unknown frame type interleaved.
  762. {HeadersBlock, _} = cow_hpack:encode([
  763. {<<":method">>, <<"POST">>},
  764. {<<":scheme">>, <<"http">>},
  765. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  766. {<<":path">>, <<"/echo/read_body">>}
  767. ]),
  768. ok = gen_tcp:send(Socket, [
  769. cow_http2:headers(1, nofin, HeadersBlock),
  770. << 10:24, 99:8, 0:40, 0:80 >>,
  771. cow_http2:data(1, fin, << 0:100/unit:8 >>)
  772. ]),
  773. %% Receive a response with the same DATA frame.
  774. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  775. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  776. {ok, << 100:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  777. {ok, << 0:100/unit:8 >>} = gen_tcp:recv(Socket, 100, 1000),
  778. ok.
  779. %% Frame size.
  780. max_frame_size_allow_exactly_default(Config) ->
  781. doc("All implementations must allow frame sizes of at least 16384. (RFC7540 4.1, RFC7540 4.2)"),
  782. {ok, Socket} = do_handshake(Config),
  783. %% Send a POST request with a DATA frame of exactly 16384 bytes.
  784. {HeadersBlock, _} = cow_hpack:encode([
  785. {<<":method">>, <<"POST">>},
  786. {<<":scheme">>, <<"http">>},
  787. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  788. {<<":path">>, <<"/echo/read_body">>}
  789. ]),
  790. ok = gen_tcp:send(Socket, [
  791. cow_http2:headers(1, nofin, HeadersBlock),
  792. cow_http2:data(1, fin, << 0:16384/unit:8 >>)
  793. ]),
  794. %% Receive a response with the same DATA frame.
  795. {ok, << SkipLen:24, 1:8, _:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  796. {ok, _} = gen_tcp:recv(Socket, SkipLen, 1000),
  797. {ok, << 16384:24, 0:8, 1:8, 1:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  798. {ok, << 0:16384/unit:8 >>} = gen_tcp:recv(Socket, 16384, 1000),
  799. ok.
  800. max_frame_size_reject_larger_than_default(Config) ->
  801. doc("A FRAME_SIZE_ERROR connection error must be sent when receiving "
  802. "frames larger than the default 16384 length. (RFC7540 4.1, RFC7540 4.2)"),
  803. {ok, Socket} = do_handshake(Config),
  804. %% Send a POST request with a DATA frame larger than 16384 bytes.
  805. {HeadersBlock, _} = cow_hpack:encode([
  806. {<<":method">>, <<"POST">>},
  807. {<<":scheme">>, <<"http">>},
  808. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  809. {<<":path">>, <<"/echo/read_body">>}
  810. ]),
  811. ok = gen_tcp:send(Socket, [
  812. cow_http2:headers(1, nofin, HeadersBlock),
  813. cow_http2:data(1, fin, << 0:16385/unit:8 >>)
  814. ]),
  815. %% Receive a FRAME_SIZE_ERROR connection error.
  816. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  817. ok.
  818. %% @todo We need configurable SETTINGS in Cowboy for these tests.
  819. %% max_frame_size_config_reject_too_small(Config) ->
  820. %% doc("SETTINGS_MAX_FRAME_SIZE configuration values smaller than "
  821. %% "16384 must be rejected. (RFC7540 6.5.2)"),
  822. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  823. %% todo.
  824. %%
  825. %% max_frame_size_config_reject_too_large(Config) ->
  826. %% doc("SETTINGS_MAX_FRAME_SIZE configuration values larger than "
  827. %% "16777215 must be rejected. (RFC7540 6.5.2)"),
  828. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  829. %% todo.
  830. %%
  831. %% max_frame_size_allow_exactly_custom(Config) ->
  832. %% doc("An endpoint that sets SETTINGS_MAX_FRAME_SIZE must allow frames "
  833. %% "of up to that size. (RFC7540 4.2, RFC7540 6.5.2)"),
  834. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  835. %% todo.
  836. %%
  837. %% max_frame_size_reject_larger_than_custom(Config) ->
  838. %% doc("An endpoint that sets SETTINGS_MAX_FRAME_SIZE must reject frames "
  839. %% "of up to that size with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.5.2)"),
  840. %% %% @todo This requires us to have a configurable SETTINGS in Cowboy.
  841. %% todo.
  842. %% @todo How do I test this?
  843. %%
  844. %% max_frame_size_client_default_respect_limits(Config) ->
  845. %% doc("The server must not send frame sizes of more "
  846. %% "than 16384 by default. (RFC7540 4.1, RFC7540 4.2)"),
  847. %% This is about the client sending a SETTINGS frame.
  848. max_frame_size_client_override_reject_too_small(Config) ->
  849. doc("A SETTINGS_MAX_FRAME_SIZE smaller than 16384 must be rejected "
  850. "with a PROTOCOL_ERROR connection error. (RFC7540 6.5.2)"),
  851. {ok, Socket} = do_handshake(Config),
  852. %% Send a SETTINGS frame with a SETTINGS_MAX_FRAME_SIZE lower than 16384.
  853. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:40, 5:16, 16383:32 >>),
  854. %% Receive a PROTOCOL_ERROR connection error.
  855. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  856. ok.
  857. %% This is about the client sending a SETTINGS frame.
  858. max_frame_size_client_override_reject_too_large(Config) ->
  859. doc("A SETTINGS_MAX_FRAME_SIZE larger than 16777215 must be rejected "
  860. "with a PROTOCOL_ERROR connection error. (RFC7540 6.5.2)"),
  861. {ok, Socket} = do_handshake(Config),
  862. %% Send a SETTINGS frame with a SETTINGS_MAX_FRAME_SIZE larger than 16777215.
  863. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:40, 5:16, 16777216:32 >>),
  864. %% Receive a PROTOCOL_ERROR connection error.
  865. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  866. ok.
  867. %% @todo How do I test this?
  868. %%
  869. %% max_frame_size_client_custom_respect_limits(Config) ->
  870. %% doc("The server must not send frame sizes of more than "
  871. %% "client's advertised limits. (RFC7540 4.1, RFC7540 4.2)"),
  872. %% I am using FRAME_SIZE_ERROR here because the information in the
  873. %% frame header tells us this frame is at least 1 byte long, while
  874. %% the given length is smaller; i.e. it is too small to contain
  875. %% mandatory frame data (the pad length).
  876. data_reject_frame_size_0_padded_flag(Config) ->
  877. doc("DATA frames of size 0 with the PADDED flag set must be rejected "
  878. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.1)"),
  879. {ok, Socket} = do_handshake(Config),
  880. %% Send a POST request with an incorrect padded DATA frame size.
  881. {HeadersBlock, _} = cow_hpack:encode([
  882. {<<":method">>, <<"POST">>},
  883. {<<":scheme">>, <<"http">>},
  884. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  885. {<<":path">>, <<"/echo/read_body">>}
  886. ]),
  887. ok = gen_tcp:send(Socket, [
  888. cow_http2:headers(1, nofin, HeadersBlock),
  889. << 0:24, 0:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31 >>
  890. ]),
  891. %% Receive a FRAME_SIZE_ERROR connection error.
  892. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  893. ok.
  894. %% This case on the other hand is noted specifically in the RFC
  895. %% as being a PROTOCOL_ERROR. It can be thought of as the Pad Length
  896. %% being incorrect, rather than the frame size.
  897. data_reject_frame_size_too_small_padded_flag(Config) ->
  898. doc("DATA frames with Pad Length >= Length must be rejected "
  899. "with a PROTOCOL_ERROR connection error. (RFC7540 6.1)"),
  900. {ok, Socket} = do_handshake(Config),
  901. %% Send a POST request with an incorrect padded DATA frame size.
  902. {HeadersBlock, _} = cow_hpack:encode([
  903. {<<":method">>, <<"POST">>},
  904. {<<":scheme">>, <<"http">>},
  905. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  906. {<<":path">>, <<"/echo/read_body">>}
  907. ]),
  908. ok = gen_tcp:send(Socket, [
  909. cow_http2:headers(1, nofin, HeadersBlock),
  910. << 10:24, 0:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31, 10:8, 0:80 >>
  911. ]),
  912. %% Receive a PROTOCOL_ERROR connection error.
  913. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  914. ok.
  915. headers_reject_frame_size_0_padded_flag(Config) ->
  916. doc("HEADERS frames of size 0 with the PADDED flag set must be rejected "
  917. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  918. {ok, Socket} = do_handshake(Config),
  919. %% Send a padded HEADERS frame with an incorrect size.
  920. ok = gen_tcp:send(Socket, << 0:24, 1:8, 0:4, 1:1, 0:2, 1:1, 0:1, 1:31 >>),
  921. %% Receive a FRAME_SIZE_ERROR connection error.
  922. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  923. ok.
  924. headers_reject_frame_size_too_small_padded_flag(Config) ->
  925. doc("HEADERS frames with no priority flag and Pad Length >= Length "
  926. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 6.2)"),
  927. {ok, Socket} = do_handshake(Config),
  928. %% Send a padded HEADERS frame with an incorrect size.
  929. 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 >>),
  930. %% Receive a PROTOCOL_ERROR connection error.
  931. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  932. ok.
  933. headers_reject_frame_size_too_small_priority_flag(Config) ->
  934. doc("HEADERS frames of size smaller than 5 with the PRIORITY flag set must be rejected "
  935. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  936. {ok, Socket} = do_handshake(Config),
  937. %% Send a HEADERS frame with priority set and an incorrect size.
  938. ok = gen_tcp:send(Socket, << 4:24, 1:8,
  939. 0:2, 1:1, 0:4, 1:1, 0:1, 1:31, 0:1, 3:31, 0:8 >>),
  940. %% Receive a FRAME_SIZE_ERROR connection error.
  941. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  942. ok.
  943. headers_reject_frame_size_5_padded_and_priority_flags(Config) ->
  944. doc("HEADERS frames of size smaller than 6 with the PADDED "
  945. "and PRIORITY flags set must be rejected "
  946. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  947. {ok, Socket} = do_handshake(Config),
  948. %% Send a padded HEADERS frame with an incorrect size.
  949. ok = gen_tcp:send(Socket, << 5:24, 1:8,
  950. 0:2, 1:1, 0:1, 1:1, 0:2, 1:1, 0:1, 1:31, 0:8, 0:1, 3:31, 0:8 >>),
  951. %% Receive a FRAME_SIZE_ERROR connection error.
  952. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  953. ok.
  954. headers_reject_frame_size_too_small_padded_and_priority_flags(Config) ->
  955. doc("HEADERS frames of size smaller than Length+6 with the PADDED and PRIORITY flags set "
  956. "must be rejected with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.2)"),
  957. {ok, Socket} = do_handshake(Config),
  958. %% Send a padded HEADERS frame with an incorrect size.
  959. ok = gen_tcp:send(Socket, << 15:24, 1:8,
  960. 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 >>),
  961. %% Receive a PROTOCOL_ERROR connection error.
  962. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  963. ok.
  964. priority_reject_frame_size_too_small(Config) ->
  965. doc("PRIORITY frames of size smaller than 5 must be rejected "
  966. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.3)"),
  967. {ok, Socket} = do_handshake(Config),
  968. %% Send a PRIORITY frame with an incorrect size.
  969. ok = gen_tcp:send(Socket, << 4:24, 2:8, 0:9, 1:31, 0:1, 3:31, 0:8 >>),
  970. %% Receive a FRAME_SIZE_ERROR stream error.
  971. {ok, << _:24, 3:8, _:40, 6:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  972. ok.
  973. priority_reject_frame_size_too_large(Config) ->
  974. doc("PRIORITY frames of size larger than 5 must be rejected "
  975. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.3)"),
  976. {ok, Socket} = do_handshake(Config),
  977. %% Send a PRIORITY frame with an incorrect size.
  978. ok = gen_tcp:send(Socket, << 6:24, 2:8, 0:9, 1:31, 0:1, 3:31, 0:16 >>),
  979. %% Receive a FRAME_SIZE_ERROR stream error.
  980. {ok, << _:24, 3:8, _:40, 6:32 >>} = gen_tcp:recv(Socket, 13, 6000),
  981. ok.
  982. rst_stream_reject_frame_size_too_small(Config) ->
  983. doc("RST_STREAM frames of size smaller than 4 must be rejected "
  984. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.4)"),
  985. {ok, Socket} = do_handshake(Config),
  986. %% Send a request and reset it immediately.
  987. {HeadersBlock, _} = cow_hpack:encode([
  988. {<<":method">>, <<"GET">>},
  989. {<<":scheme">>, <<"http">>},
  990. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  991. {<<":path">>, <<"/">>}
  992. ]),
  993. ok = gen_tcp:send(Socket, [
  994. cow_http2:headers(1, fin, HeadersBlock),
  995. << 3:24, 3:8, 0:9, 1:31, 8:32 >>
  996. ]),
  997. %% Receive a FRAME_SIZE_ERROR connection error.
  998. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  999. ok.
  1000. rst_stream_reject_frame_size_too_large(Config) ->
  1001. doc("RST_STREAM frames of size larger than 4 must be rejected "
  1002. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.4)"),
  1003. {ok, Socket} = do_handshake(Config),
  1004. %% Send a request and reset it immediately.
  1005. {HeadersBlock, _} = cow_hpack:encode([
  1006. {<<":method">>, <<"GET">>},
  1007. {<<":scheme">>, <<"http">>},
  1008. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1009. {<<":path">>, <<"/">>}
  1010. ]),
  1011. ok = gen_tcp:send(Socket, [
  1012. cow_http2:headers(1, fin, HeadersBlock),
  1013. << 5:24, 3:8, 0:9, 1:31, 8:32 >>
  1014. ]),
  1015. %% Receive a FRAME_SIZE_ERROR connection error.
  1016. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1017. ok.
  1018. settings_reject_bad_frame_size(Config) ->
  1019. doc("SETTINGS frames must have a size multiple of 6 or be rejected "
  1020. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.5)"),
  1021. {ok, Socket} = do_handshake(Config),
  1022. %% Send a SETTINGS frame with an incorrect size.
  1023. ok = gen_tcp:send(Socket, << 5:24, 4:8, 0:40, 1:16, 4096:32 >>),
  1024. %% Receive a FRAME_SIZE_ERROR connection error.
  1025. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1026. ok.
  1027. settings_ack_reject_non_empty_frame_size(Config) ->
  1028. doc("SETTINGS frames with the ACK flag set and a non-empty payload "
  1029. "must be rejected with a FRAME_SIZE_ERROR connection error (RFC7540 4.2, RFC7540 6.5)"),
  1030. {ok, Socket} = gen_tcp:connect("localhost", config(port, Config), [binary, {active, false}]),
  1031. %% Send a valid preface.
  1032. ok = gen_tcp:send(Socket, ["PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n", cow_http2:settings(#{})]),
  1033. %% Receive the server preface.
  1034. {ok, << Len:24 >>} = gen_tcp:recv(Socket, 3, 1000),
  1035. {ok, << 4:8, 0:40, _:Len/binary >>} = gen_tcp:recv(Socket, 6 + Len, 1000),
  1036. %% Send a SETTINGS ack with a payload.
  1037. ok = gen_tcp:send(Socket, << 6:24, 4:8, 0:7, 1:1, 0:32, 1:16, 4096:32 >>),
  1038. %% Receive the SETTINGS ack.
  1039. {ok, << 0:24, 4:8, 1:8, 0:32 >>} = gen_tcp:recv(Socket, 9, 1000),
  1040. %% Receive a FRAME_SIZE_ERROR connection error.
  1041. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1042. ok.
  1043. %% Note that clients are not supposed to send PUSH_PROMISE frames.
  1044. %% However when they do, we need to be able to parse it in order
  1045. %% to reject it, and so these errors may still occur.
  1046. push_promise_reject_frame_size_too_small(Config) ->
  1047. doc("PUSH_PROMISE frames of size smaller than 4 must be rejected "
  1048. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1049. {ok, Socket} = do_handshake(Config),
  1050. %% Send a PUSH_PROMISE frame with an incorrect size.
  1051. {HeadersBlock, _} = cow_hpack:encode([
  1052. {<<":method">>, <<"GET">>},
  1053. {<<":scheme">>, <<"http">>},
  1054. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1055. {<<":path">>, <<"/">>}
  1056. ]),
  1057. ok = gen_tcp:send(Socket, [
  1058. << 3:24, 5:8, 0:5, 1:1, 0:3, 1:31, 0:1, 3:31 >>,
  1059. HeadersBlock
  1060. ]),
  1061. %% Receive a FRAME_SIZE_ERROR connection error.
  1062. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1063. ok.
  1064. push_promise_reject_frame_size_4_padded_flag(Config) ->
  1065. doc("PUSH_PROMISE frames of size smaller than 5 with the PADDED flag set must be rejected "
  1066. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1067. {ok, Socket} = do_handshake(Config),
  1068. %% Send a PUSH_PROMISE frame with an incorrect size.
  1069. {HeadersBlock, _} = cow_hpack:encode([
  1070. {<<":method">>, <<"GET">>},
  1071. {<<":scheme">>, <<"http">>},
  1072. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1073. {<<":path">>, <<"/">>}
  1074. ]),
  1075. ok = gen_tcp:send(Socket, [
  1076. << 4:24, 5:8, 0:4, 1:1, 1:1, 0:3, 1:31, 0:1, 0:8, 3:31 >>,
  1077. HeadersBlock
  1078. ]),
  1079. %% Receive a FRAME_SIZE_ERROR connection error.
  1080. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1081. ok.
  1082. push_promise_reject_frame_size_too_small_padded_flag(Config) ->
  1083. doc("PUSH_PROMISE frames of size smaller than Length+5 with the PADDED flag set "
  1084. "must be rejected with a PROTOCOL_ERROR connection error. (RFC7540 4.2, RFC7540 6.6)"),
  1085. {ok, Socket} = do_handshake(Config),
  1086. %% Send a PUSH_PROMISE frame with an incorrect size.
  1087. {HeadersBlock, _} = cow_hpack:encode([
  1088. {<<":method">>, <<"GET">>},
  1089. {<<":scheme">>, <<"http">>},
  1090. {<<":authority">>, <<"localhost">>}, %% @todo Correct port number.
  1091. {<<":path">>, <<"/">>}
  1092. ]),
  1093. ok = gen_tcp:send(Socket, [
  1094. << 14:24, 5:8, 0:4, 1:1, 1:1, 0:3, 1:31, 10:8, 0:1, 3:31 >>,
  1095. HeadersBlock,
  1096. << 0:80 >>
  1097. ]),
  1098. %% Receive a PROTOCOL_ERROR connection error.
  1099. {ok, << _:24, 7:8, _:72, 1:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1100. ok.
  1101. ping_reject_frame_size_too_small(Config) ->
  1102. doc("PING frames of size smaller than 8 must be rejected "
  1103. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.7)"),
  1104. {ok, Socket} = do_handshake(Config),
  1105. %% Send a PING frame with an incorrect size.
  1106. ok = gen_tcp:send(Socket, << 7:24, 6:8, 0:40, 0:56 >>),
  1107. %% Receive a FRAME_SIZE_ERROR connection error.
  1108. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1109. ok.
  1110. ping_reject_frame_size_too_large(Config) ->
  1111. doc("PING frames of size larger than 8 must be rejected "
  1112. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.7)"),
  1113. {ok, Socket} = do_handshake(Config),
  1114. %% Send a PING frame with an incorrect size.
  1115. ok = gen_tcp:send(Socket, << 9:24, 6:8, 0:40, 0:72 >>),
  1116. %% Receive a FRAME_SIZE_ERROR connection error.
  1117. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1118. ok.
  1119. goaway_reject_frame_size_too_small(Config) ->
  1120. doc("GOAWAY frames of size smaller than 8 must be rejected "
  1121. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.8)"),
  1122. {ok, Socket} = do_handshake(Config),
  1123. %% Send a GOAWAY frame with an incorrect size.
  1124. ok = gen_tcp:send(Socket, << 7:24, 7:8, 0:40, 0:56 >>),
  1125. %% Receive a FRAME_SIZE_ERROR connection error.
  1126. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1127. ok.
  1128. goaway_allow_frame_size_too_large(Config) ->
  1129. doc("GOAWAY frames of size larger than 8 must be allowed. (RFC7540 6.8)"),
  1130. {ok, Socket} = do_handshake(Config),
  1131. %% Send a GOAWAY frame with debug data.
  1132. ok = gen_tcp:send(Socket, << 12:24, 7:8, 0:40, 0:64, 99999:32 >>),
  1133. %% Receive a GOAWAY frame back.
  1134. {ok, << _:24, 7:8, _:72, 0:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1135. ok.
  1136. window_update_reject_frame_size_too_small(Config) ->
  1137. doc("WINDOW_UPDATE frames of size smaller than 4 must be rejected "
  1138. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.9)"),
  1139. {ok, Socket} = do_handshake(Config),
  1140. %% Send a WINDOW_UPDATE frame with an incorrect size.
  1141. ok = gen_tcp:send(Socket, << 3:24, 8:8, 0:40, 1000:24 >>),
  1142. %% Receive a FRAME_SIZE_ERROR connection error.
  1143. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1144. ok.
  1145. window_update_reject_frame_size_too_large(Config) ->
  1146. doc("WINDOW_UPDATE frames of size larger than 4 must be rejected "
  1147. "with a FRAME_SIZE_ERROR connection error. (RFC7540 4.2, RFC7540 6.9)"),
  1148. {ok, Socket} = do_handshake(Config),
  1149. %% Send a WINDOW_UPDATE frame with an incorrect size.
  1150. ok = gen_tcp:send(Socket, << 5:24, 8:8, 0:40, 1000:40 >>),
  1151. %% Receive a FRAME_SIZE_ERROR connection error.
  1152. {ok, << _:24, 7:8, _:72, 6:32 >>} = gen_tcp:recv(Socket, 17, 6000),
  1153. ok.
  1154. %% Note: There is no particular limits on the size of CONTINUATION frames,
  1155. %% they can go from 0 to SETTINGS_MAX_FRAME_SIZE.