Skip to content

Commit a6c9e49

Browse files
committed
Generate transfer codings with chunked first and last in the HTTP/1 fuzz
The generated framing headers only used Transfer-Encoding: chunked, so the choice between chunked framing and reading until close was never exercised. Chunked bodies are now also sent with "gzip, chunked", which RFC 9112 section 6.3 frames as chunked, and with "chunked, gzip", whose body is read until the connection closes and includes the chunk framing. Each generated framing carries the body length it gives, and the model checks the body size of every completed response whose length is known: the bytes are unmutated, every initial request was issued, and the response and the ones before it are 1.1 responses to GET or POST with a 200, 404 or 500 status, no 101, and no extra framing headers. A response read until close takes the rest of the bytes, so no response after it has a known size.
1 parent 5677ece commit a6c9e49

1 file changed

Lines changed: 102 additions & 25 deletions

File tree

‎test/mint/http1/fuzz_test.exs‎

Lines changed: 102 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,45 @@ defmodule Mint.HTTP1.FuzzTest do
4242
])
4343
end
4444

45+
# Returns the framing headers with the body length RFC 9112 6.3 gives them:
46+
# an integer, :until_close, or nil when the headers don't describe the body.
47+
# Chunked framing applies only when chunked is the final transfer coding.
4548
defp framing_headers_gen(body) do
4649
derived =
4750
case body do
4851
:none ->
49-
[[], [{"content-length", "0"}]]
52+
[{[], :until_close}, {[{"content-length", "0"}], 0}]
5053

51-
{:cl, _} ->
52-
[[{"content-length", :match}]]
54+
{:cl, bytes} ->
55+
[{[{"content-length", :match}], byte_size(bytes)}]
56+
57+
{:chunked, chunks, _, _} ->
58+
size = chunks |> Enum.map(&byte_size/1) |> Enum.sum()
5359

54-
{:chunked, _, _, _} ->
55-
[[{"transfer-encoding", "chunked"}]]
60+
[
61+
{[{"transfer-encoding", "chunked"}], size},
62+
{[{"transfer-encoding", "gzip, chunked"}], size},
63+
{[{"transfer-encoding", "chunked, gzip"}], :until_close}
64+
]
5665

5766
{:close, _} ->
58-
[[], [{"connection", "close"}]]
67+
[{[], :until_close}, {[{"connection", "close"}], :until_close}]
5968
end
6069

6170
frequency([
6271
{9, member_of(derived)},
6372
{1,
64-
member_of([
65-
[{"content-length", "5"}],
66-
[{"content-length", "-1"}],
67-
[{"content-length", "2, 2"}],
68-
[{"content-length", "3"}, {"content-length", "3"}],
69-
[{"transfer-encoding", "chunked"}, {"content-length", "3"}],
70-
[{"transfer-encoding", "chunked"}]
71-
])}
73+
map(
74+
member_of([
75+
[{"content-length", "5"}],
76+
[{"content-length", "-1"}],
77+
[{"content-length", "2, 2"}],
78+
[{"content-length", "3"}, {"content-length", "3"}],
79+
[{"transfer-encoding", "chunked"}, {"content-length", "3"}],
80+
[{"transfer-encoding", "chunked"}]
81+
]),
82+
&{&1, nil}
83+
)}
7284
])
7385
end
7486

@@ -88,14 +100,16 @@ defmodule Mint.HTTP1.FuzzTest do
88100
{1, member_of(["600", "99", "101", "1000", "20"])}
89101
]),
90102
body <- body_gen(),
91-
framing <- framing_headers_gen(body),
103+
{framing, body_length} <- framing_headers_gen(body),
92104
headers <- list_of(header_gen(), max_length: 3) do
93105
%{
94106
preludes: preludes,
95107
version: version,
96108
status: status,
97-
headers: framing ++ headers,
98-
body: body
109+
framing: framing,
110+
headers: headers,
111+
body: body,
112+
body_length: body_length
99113
}
100114
end
101115
end
@@ -211,8 +225,10 @@ defmodule Mint.HTTP1.FuzzTest do
211225
end
212226
end)
213227

214-
bytes = responses |> Enum.map(&render_response/1) |> IO.iodata_to_binary()
228+
rendered = Enum.map(responses, &render_response/1)
229+
bytes = rendered |> Enum.map(fn {head, body} -> [head, body] end) |> IO.iodata_to_binary()
215230
bytes = mutate(bytes <> scenario.extra, scenario.mutation)
231+
meta = put_expected_body_sizes(meta, refs, scenario, rendered, byte_size(bytes))
216232

217233
state = %{
218234
scenario: scenario,
@@ -249,7 +265,56 @@ defmodule Mint.HTTP1.FuzzTest do
249265
drain_mailbox()
250266
end
251267

252-
defp new_meta(method), do: %{method: method, status: nil, content_length: nil, body_size: 0}
268+
defp new_meta(method) do
269+
%{method: method, status: nil, content_length: nil, body_size: 0, expected_body_size: nil}
270+
end
271+
272+
# The body size of a response is known when the bytes are sent unmutated,
273+
# every initial request was issued, the response and all the ones before it
274+
# are well-formed 1.1 responses to GET or POST without a 101, and their
275+
# framing headers describe their bodies. A body read until close takes every
276+
# byte after the response's header section, so no later response has a
277+
# known size.
278+
defp put_expected_body_sizes(meta, refs, scenario, rendered, total_size) do
279+
if scenario.mutation == nil and length(refs) == length(scenario.methods) do
280+
[refs, scenario.methods, scenario.responses, rendered]
281+
|> Enum.zip()
282+
|> Enum.reduce_while({meta, 0}, fn {ref, {method, _}, response, {head, body}},
283+
{meta, offset} ->
284+
body_start = offset + IO.iodata_length(head)
285+
286+
size =
287+
case response.body_length do
288+
:until_close -> total_size - body_start
289+
other -> other
290+
end
291+
292+
cond do
293+
size == nil or not known_framing?(method, response) ->
294+
{:halt, {meta, offset}}
295+
296+
response.body_length == :until_close ->
297+
{:halt, {put_in(meta[ref].expected_body_size, size), offset}}
298+
299+
true ->
300+
meta = put_in(meta[ref].expected_body_size, size)
301+
{:cont, {meta, body_start + IO.iodata_length(body)}}
302+
end
303+
end)
304+
|> elem(0)
305+
else
306+
meta
307+
end
308+
end
309+
310+
defp known_framing?(method, response) do
311+
method in ["GET", "POST"] and response.version == "1.1" and
312+
response.status in ["200", "404", "500"] and
313+
not Enum.any?(response.preludes, &(elem(&1, 0) == "101")) and
314+
not Enum.any?(response.headers, fn {name, _} ->
315+
String.downcase(name) in ["content-length", "transfer-encoding"]
316+
end)
317+
end
253318

254319
defp describe(state) do
255320
"scenario: #{inspect(state.scenario, limit: :infinity)}\nbytes: #{inspect(state.bytes, limit: :infinity)}\nlog: #{inspect(state.log, limit: :infinity)}"
@@ -497,8 +562,13 @@ defmodule Mint.HTTP1.FuzzTest do
497562
end
498563

499564
defp update_meta(meta, {:done, ref}, _ref_state, state) when is_map_key(meta, ref) do
500-
%{method: method, status: status, content_length: content_length, body_size: body_size} =
501-
meta[ref]
565+
%{
566+
method: method,
567+
status: status,
568+
content_length: content_length,
569+
body_size: body_size,
570+
expected_body_size: expected_body_size
571+
} = meta[ref]
502572

503573
# A 101 response hands the connection to another protocol, whose bytes are
504574
# delivered as data whatever the request method was.
@@ -518,6 +588,11 @@ defmodule Mint.HTTP1.FuzzTest do
518588
"response completed with #{body_size} body bytes but content-length #{content_length}\n#{describe(state)}"
519589
)
520590

591+
expected_body_size != nil and body_size != expected_body_size ->
592+
flunk(
593+
"response completed with #{body_size} body bytes but its framing gives #{expected_body_size}\n#{describe(state)}"
594+
)
595+
521596
true ->
522597
meta
523598
end
@@ -531,6 +606,7 @@ defmodule Mint.HTTP1.FuzzTest do
531606
preludes: preludes,
532607
version: version,
533608
status: status,
609+
framing: framing,
534610
headers: headers,
535611
body: body
536612
}) do
@@ -566,17 +642,18 @@ defmodule Mint.HTTP1.FuzzTest do
566642
]), chunks |> Enum.map(&byte_size/1) |> Enum.sum()}
567643
end
568644

569-
[
645+
head = [
570646
prelude_text,
571647
"HTTP/",
572648
version,
573649
" ",
574650
status,
575651
" Reason\r\n",
576-
render_headers(headers, body_length),
577-
"\r\n",
578-
body_bytes
652+
render_headers(framing ++ headers, body_length),
653+
"\r\n"
579654
]
655+
656+
{head, body_bytes}
580657
end
581658

582659
defp render_headers(headers, body_length) do

0 commit comments

Comments
 (0)