129 lines
3.6 KiB
Rust
129 lines
3.6 KiB
Rust
//! A cheap, deterministic stand-in for fuzzing that runs on stable: build
|
|
//! many `.http` files from realistic line fragments and check the parser's
|
|
//! invariants on each.
|
|
|
|
use reqlite_core::httpfile::HttpFile;
|
|
use reqlite_core::Header;
|
|
|
|
const FRAGMENTS: &[&str] = &[
|
|
"###",
|
|
"### Title",
|
|
"### spaced title ",
|
|
"",
|
|
"",
|
|
" ",
|
|
"# comment",
|
|
"// comment",
|
|
"# @name x",
|
|
"# @timeout 5",
|
|
"# @no-redirect",
|
|
"# @weird thing",
|
|
"@host = example.com",
|
|
"@ bad",
|
|
"GET http://{{host}}/a",
|
|
"POST /b HTTP/1.1",
|
|
"PUT https://x/y?z=1",
|
|
"https://bare.example",
|
|
"DELETE",
|
|
" ?a=1",
|
|
" &b=2",
|
|
" /more",
|
|
"Content-Type: application/json",
|
|
"Content-Type: multipart/form-data; boundary=B",
|
|
"X-A: 1",
|
|
"# X-Off: 2",
|
|
"not a header",
|
|
"{",
|
|
" \"a\": 1",
|
|
"}",
|
|
"< ./file.json",
|
|
"<@ ./tpl.json",
|
|
"--B",
|
|
"Content-Disposition: form-data; name=\"f\"",
|
|
"--B--",
|
|
"> {%",
|
|
" client.log(1);",
|
|
"%}",
|
|
"> ./h.js",
|
|
"<> ./prev.json",
|
|
"{{var}}",
|
|
"\u{feff}bom",
|
|
"ünïcödé: ✓",
|
|
"\t\ttabs",
|
|
];
|
|
|
|
/// xorshift64*: deterministic, no dependencies.
|
|
struct Rng(u64);
|
|
|
|
impl Rng {
|
|
fn next(&mut self) -> u64 {
|
|
self.0 ^= self.0 >> 12;
|
|
self.0 ^= self.0 << 25;
|
|
self.0 ^= self.0 >> 27;
|
|
self.0.wrapping_mul(0x2545_f491_4f6c_dd1d)
|
|
}
|
|
fn below(&mut self, n: usize) -> usize {
|
|
(self.next() % n as u64) as usize
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn invariants_hold_for_generated_files() {
|
|
// REQLITE_FUZZ_CASES=200000 for a longer search.
|
|
let cases: usize = std::env::var("REQLITE_FUZZ_CASES")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(3000);
|
|
let seed = std::env::var("REQLITE_FUZZ_SEED")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(0x5eed_1234_abcd_0001);
|
|
let mut rng = Rng(seed);
|
|
for case in 0..cases {
|
|
let lines = rng.below(40);
|
|
let eol = if rng.below(4) == 0 { "\r\n" } else { "\n" };
|
|
let mut text = String::new();
|
|
for _ in 0..lines {
|
|
text.push_str(FRAGMENTS[rng.below(FRAGMENTS.len())]);
|
|
text.push_str(eol);
|
|
}
|
|
if rng.below(3) == 0 {
|
|
text.truncate(text.trim_end_matches(['\r', '\n']).len());
|
|
}
|
|
|
|
let f = HttpFile::parse(text.clone());
|
|
assert_eq!(f.write(), text, "case {case}: round trip\n{text:?}");
|
|
for i in 0..f.request_count() {
|
|
let r = f.request(i).unwrap().clone();
|
|
assert_eq!(
|
|
f.replace_request(i, &r).unwrap(),
|
|
text,
|
|
"case {case}: identity edit #{i}\n{text:?}"
|
|
);
|
|
|
|
// A real edit changes only this request's span.
|
|
let mut edited = r.clone();
|
|
edited.headers.push(Header::new("X-Fuzz", "1"));
|
|
let out = f.replace_request(i, &edited).unwrap();
|
|
assert_eq!(
|
|
&out[..r.source.start],
|
|
&text[..r.source.start],
|
|
"case {case}: prefix\n{text:?}"
|
|
);
|
|
assert!(out.ends_with(&text[r.source.end..]), "case {case}: suffix\n{text:?}");
|
|
let g = HttpFile::parse(out);
|
|
assert!(
|
|
g.requests().any(|q| q.header("X-Fuzz") == Some("1")),
|
|
"case {case}: edit visible\n{text:?}"
|
|
);
|
|
|
|
let src = f.request_source(i).unwrap().to_string();
|
|
assert_eq!(
|
|
f.replace_request_text(i, &src).unwrap(),
|
|
text,
|
|
"case {case}: source round trip\n{text:?}"
|
|
);
|
|
}
|
|
}
|
|
}
|