-
Notifications
You must be signed in to change notification settings - Fork 0
Fuzz the HTTP endpoint, make sure it never hangs. #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. 8000
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys | ||
import string | ||
|
||
from amaranth.sim import Simulator | ||
|
||
from .simple_led_http import SimpleLedHttp | ||
from stream_fixtures import StreamCollector | ||
from hypothesis import given, strategies as st, settings, Phase, Verbosity | ||
from hypothesis.errors import InvalidArgument | ||
|
||
def run_fuzz_http_request(method, path, headers, body): | ||
""" | ||
Same test as in simple_http_fuzz_test, but for manually re-running failures. | ||
""" | ||
dut = SimpleLedHttp() | ||
sim = Simulator(dut) | ||
sim.add_clock(1e-6) | ||
|
||
header = "".join(f"{k} : {v}\r\n" for k,v in headers.items()) | ||
|
||
input = f"{method} {path} HTTP/1.0\r\n{header}\r\n\r\n{body}" | ||
sys.stderr.write(f"Testing with {input}") | ||
|
||
|
||
async def driver(ctx): | ||
ctx.set(dut.session.inbound.active, 1) | ||
await ctx.tick().until(dut.session.outbound.active) | ||
in_stream = dut.session.inbound.data | ||
ctx.set(in_stream.valid, 1) | ||
idx = 0 | ||
while idx < len(input): | ||
ctx.set(in_stream.payload, ord(input[idx])) | ||
if ctx.get(in_stream.ready): | ||
idx += 1 | ||
await ctx.tick() | ||
ctx.set(dut.session.inbound.active, 0) | ||
await ctx.tick().until(~dut.session.outbound.active) | ||
assert not ctx.get(dut.session.outbound.data.valid) | ||
await ctx.tick() | ||
|
||
sim.add_testbench(driver) | ||
collector = StreamCollector(stream=dut.session.outbound.data) | ||
sim.add_process(collector.collect()) | ||
with sim.write_vcd(sys.stdout): | ||
sim.run_until(0.01) | ||
|
||
# All we're really checking is that every packet gets _some_ kind of response. | ||
assert len(collector) != 0 | ||
|
||
def test_simple_get(): | ||
run_fuzz_http_request("POST", "/led", {}, "") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import sys | ||
import string | ||
|
||
from amaranth.sim import Simulator | ||
|
||
from .simple_led_http import SimpleLedHttp | ||
from stream_fixtures import StreamCollector | ||
from hypothesis import given, strategies as st, settings, Phase, Verbosity | ||
from hypothesis.errors import InvalidArgument | ||
|
||
st_methods = st.sampled_from(["GET", "POST", "PUT", "DELETE", "BREW"]) | ||
st_paths = st.sampled_from(["/", "/led", "/count", "/coffee", "/asdf"]) | ||
|
||
st_header_names = st.sampled_from(["Host", "User-Agent", "Content-Type", | ||
"Content-Length", "Accept", "Accept-Additions" "Cookie"]) | ||
st_header_values = st.text( | ||
alphabet=st.characters(codec='utf-8', exclude_characters="\r\n"), | ||
min_size=1, | ||
max_size=32) | ||
st_headers = st.dictionaries(st_header_names, st_header_values, min_size=0, max_size=10) | ||
|
||
st_bodies = st.text( | ||
alphabet=st.characters(codec='utf-8'), | ||
min_size=0, | ||
max_size=256) | ||
|
||
@settings( | ||
max_examples=2, # Increase for more testing. | ||
verbosity=Verbosity.normal, | ||
deadline=None, | ||
) | ||
@given( | ||
method=st_methods, | ||
path=st_paths, | ||
headers=st_headers, | ||
body=st_bodies | ||
) | ||
def test_fuzz_http_request(method, path, headers, body): | ||
dut = SimpleLedHttp() | ||
sim = Simulator(dut) | ||
sim.add_clock(1e-6) | ||
|
||
header = "".join(f"{k} : {v}\r\n" for k,v in headers.items()) | ||
|
||
input = f"{method} {path} HTTP/1.0\r\n{header}\r\n\r\n{body}" | ||
sys.stderr.write(f"Testing with {input}") | ||
|
||
|
||
async def driver(ctx): | ||
ctx.set(dut.session.inbound.active, 1) | ||
await ctx.tick().until(dut.session.outbound.active) | ||
in_stream = dut.session.inbound.data | ||
ctx.set(in_stream.valid, 1) | ||
idx = 0 | ||
while idx < len(input): | ||
ctx.set(in_stream.payload, ord(input[idx])) | ||
if ctx.get(in_stream.ready): | ||
idx += 1 | ||
await ctx.tick() | ||
ctx.set(dut.session.inbound.active, 0) | ||
await ctx.tick().until(~dut.session.outbound.active) | ||
assert not ctx.get(dut.session.outbound.data.valid) | ||
await ctx.tick() | ||
|
||
sim.add_testbench(driver) | ||
collector = StreamCollector(stream=dut.session.outbound.data) | ||
sim.add_process(collector.collect()) | ||
sim.run_until(0.01) | ||
|
||
# All we're really checking is that every packet gets _some_ kind of response. | ||
sys.stderr.write(f"Got response {collector.body}") | ||
|
||
assert len(collector) != 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.