use std::io::Write;
use std::sync::{Arc, Mutex};
use crate::csv::{self, Dialect, ScanEnd};
use crate::csv_rows::{self, RecordIndex};
use crate::decompress::{self, Archive, Kind, Packer};
use crate::encoding::Codec;
use crate::file::{OpenMode, Source};
use crate::index;
use crate::jobs::{CancelToken, Progress};
use crate::text::rows::SEG;
use crate::text::TextBuf;
pub(crate) fn rounds(n: u64) -> u64 {
let k = std::env::var("BIGTEXT_FUZZ_ROUNDS")
.ok()
.and_then(|v| v.parse::<u64>().ok())
.unwrap_or(1);
n * k.max(1)
}
struct Rng(u64);
impl Rng {
fn next(&mut self) -> u64 {
self.0 ^= self.0 << 13;
self.0 ^= self.0 >> 7;
self.0 ^= self.0 << 17;
self.0
}
fn below(&mut self, n: u64) -> u64 {
self.next() % n.max(1)
}
}
fn noise(r: &mut Rng, pieces: &[&[u8]], len: usize) -> Vec<u8> {
let mut v = Vec::with_capacity(len + 64);
while v.len() < len {
if r.below(50) == 0 {
v.push(r.next() as u8);
} else {
v.extend_from_slice(pieces[r.below(pieces.len() as u64) as usize]);
}
}
v
}
fn noise_upto(r: &mut Rng, pieces: &[&[u8]], max: u64) -> Vec<u8> {
let n = r.below(max) as usize;
noise(r, pieces, n)
}
fn buf_with(data: &[u8]) -> (tempfile::NamedTempFile, TextBuf) {
let mut f = tempfile::NamedTempFile::new().unwrap();
f.write_all(data).unwrap();
f.flush().unwrap();
let src = Arc::new(Source::open(f.path(), OpenMode::Auto).unwrap());
let codec = Codec::utf8();
let idx = index::build_blocking(&src, codec);
let mut t = TextBuf::new(src, idx, codec);
t.blocking = true;
(f, t)
}
const CSV_PIECES: &[&[u8]] = &[
b"a",
b"12",
b",",
b",",
b";",
b"\"",
b"\"\"",
b"\n",
b"\r\n",
b"\r",
b"\t",
b" ",
"\u{171}".as_bytes(),
"\u{65e5}".as_bytes(),
b"\"x,y\"",
b"\"q\nw\"",
];
#[test]
fn csv_records_and_rows_tile_any_bytes() {
let cancel = CancelToken::new();
for seed in 1..=rounds(40) {
let mut r = Rng(seed * 0x9E37_79B9);
let size = if seed % 5 == 0 {
3 * SEG as usize
} else {
r.below(20_000) as usize
};
let data = noise(&mut r, CSV_PIECES, size);
let (_f, buf) = buf_with(&data);
let len = buf.len();
let d = Dialect::new(b',');
let mut next = d.first(&buf);
let end = csv::for_each_record(&buf, d, &cancel, None, |rec| {
assert_eq!(rec.content.start, next, "seed {seed}");
assert!(
rec.content.end <= rec.next && rec.next <= len,
"seed {seed}"
);
for f in rec.fields {
assert!(rec.content.start <= f.start && f.end <= rec.content.end);
}
next = rec.next;
true
});
assert_eq!(end, ScanEnd::Done);
let _ = csv::detect_table(&buf.codec, &data, true, b',', 0);
let out = Mutex::new(Arc::new(RecordIndex::empty(d)));
csv_rows::index_records(&buf, d, &cancel, None, &out, &|| {});
let idx = out.lock().unwrap().clone();
let mut rows = vec![csv_rows::row_at(&buf, &idx, 0).unwrap()];
while let Some(row) = csv_rows::next_row(&buf, &idx, rows.last().unwrap()).unwrap() {
assert!(row.start > rows.last().unwrap().start, "seed {seed}");
rows.push(row);
}
for w in rows.windows(2) {
assert_eq!(w[0].seg.next, w[1].start, "seed {seed}");
assert!(w[0].seg.next - w[0].start <= 2 * SEG + 16);
}
for _ in 0..50 {
let p = r.below(len + 1);
let row = csv_rows::row_at(&buf, &idx, p).unwrap();
assert!(
row.start <= p && (p < row.seg.next || row.seg.next == len),
"seed {seed} at {p}"
);
}
}
}
const SQL_PIECES: &[&[u8]] = &[
b"CREATE TABLE ",
b"`t`",
b"\"public\".\"u\"",
b" (id int, v text);\n",
b"INSERT INTO ",
b" VALUES (1,'a;b'),(2,'c\\'d');\n",
b"'",
b"\\'",
b";",
b"\n",
b"-- note\n",
b"/*",
b"*/",
b"/*!40101 SET NAMES utf8 */;\n",
b"DELIMITER ;;\n",
b"DELIMITER ;\n",
b"$$",
b"$f$",
b"COPY public.u (id, v) FROM stdin;\n",
b"1\tx\n",
b"\\.\n",
b"SET @saved_cs = 1;\n",
b"\\restrict abc\n",
b"ALTER TABLE ONLY u ADD CONSTRAINT f FOREIGN KEY (id) REFERENCES t(id);\n",
b"CREATE VIEW w AS SELECT 1;\n",
b"DROP TABLE IF EXISTS `t`;\n",
b"BEGIN",
b"END;;\n",
b" ",
];
#[test]
fn sql_dump_scan_survives_any_dump() {
let cancel = CancelToken::new();
for seed in 1..=rounds(200) {
let mut r = Rng(seed * 0x2545_F491);
let data = noise_upto(&mut r, SQL_PIECES, 30_000);
let (_f, buf) = buf_with(&data);
let len = buf.len();
let Ok(Some(dump)) = crate::sqldump::scan(&buf, &cancel, None, |_| {}) else {
continue;
};
let inside = |r: &std::ops::Range<u64>| r.start <= r.end && r.end <= len;
for t in &dump.tables {
assert!(t.parts.iter().all(|p| inside(&p.range)), "seed {seed}");
}
assert!(dump
.header
.iter()
.chain(&dump.prelude)
.chain(&dump.other)
.all(|p| inside(&p.range)));
assert!(inside(&dump.footer), "seed {seed}");
let all: Vec<usize> = (0..dump.tables.len()).collect();
let mut out = Vec::new();
crate::sqldump::write_tables(
&buf,
&dump,
&all,
crate::sqldump::Extract::WHOLE,
&mut out,
&cancel,
None,
)
.unwrap();
}
}
const LOG_PIECES: &[&[u8]] = &[
b"10.0.0.1 - - ",
b"[13/Nov/2026:10:00:00 +0000] ",
b"\"GET /a?b=1 HTTP/1.1\" ",
b"200 ",
b"1234 ",
b"\"-\" ",
b"\"Mozilla/5.0\"",
b"0.012",
b"<34>",
b"<165>1 ",
b"Nov 13 10:00:00 ",
b"host ",
b"app[123]: ",
b"2026-11-13T10:00:00.123Z ",
b"#Fields: date time cs-uri-stem sc-status\n",
b"https ",
b"\n",
b" ",
b"\"",
b"-",
];
#[test]
fn log_formats_survive_any_lines() {
for seed in 1..=rounds(300) {
let mut r = Rng(seed * 0x1234_5679);
let text = noise_upto(&mut r, LOG_PIECES, 4000);
let lines: Vec<&[u8]> = text.split(|&b| b == b'\n').collect();
for line in &lines {
let _ = crate::timestamp::parse_bytes(line);
let _ = crate::patterns::template(line);
}
if let Some(f) = crate::logformat::Format::guess(&lines) {
let mut s = crate::logformat::Splitter::new(&f);
for line in &lines {
let _ = s.split(line);
}
}
}
}
const JSON_PIECES: &[&[u8]] = &[
b"{",
b"}",
b"[",
b"]",
b":",
b",",
b"\"k\"",
b"\"\\u00e9\"",
b"\"\\",
b"\"\\uD83D\"",
b"1",
b"-2.5e3",
b"true",
b"null",
b" ",
b"\n",
b"\"open",
b"0x",
b"01",
];
#[test]
fn json_check_survives_any_bytes() {
let cancel = CancelToken::new();
for seed in 1..=rounds(200) {
let mut r = Rng(seed * 0x0BAD_5EED);
let data = noise_upto(&mut r, JSON_PIECES, 8000);
let (_f, buf) = buf_with(&data);
let _ = crate::json_check::check_document(&buf, &cancel, None);
}
}
#[test]
fn queries_and_expressions_survive_any_text() {
let q: &[&[u8]] = &[
b".a", b".b.c", b" == ", b" != ", b" > ", b" <= ", b"\"x\"", b"1", b" and ", b" or ",
b"not ", b"(", b")", b"[0]", b" ~ ", b"\"", b".", b" ",
];
let e: &[&[u8]] = &[
b"{a}", b"{b}", b"{", b"}", b"+", b"-", b"*", b"/", b"(", b")", b"substr(", b",", b"1",
b"\"s\"", b"upper(", b" ", b"if(", b">", b"\"",
];
let names = vec!["a".to_string(), "b".to_string()];
for seed in 1..=rounds(2000) {
let mut r = Rng(seed * 0x7F4A_7C15);
let text = String::from_utf8_lossy(&noise_upto(&mut r, q, 60)).into_owned();
let _ = crate::jsonq::FieldQuery::parse(&text);
let text = String::from_utf8_lossy(&noise_upto(&mut r, e, 60)).into_owned();
let _ = crate::csv_expr::Expr::compile(&text, &names);
let _ = crate::timestamp::parse(&text);
}
}
#[test]
fn redaction_survives_any_line() {
let pieces: &[&[u8]] = &[
b"token=",
b"Bearer ",
b"@",
b"example.com",
b"\"password\": \"",
b"\"",
b"?key=",
b"&",
b"10.0.0.",
b"::",
b"eyJ",
b".",
b" ",
b"Cookie: ",
"\u{171}".as_bytes(),
b"\\",
b"Authorization: ",
b"a",
b"1",
b"ghs_",
b"github_pat_",
b"AKIA",
b"xoxe.xoxb-1-",
b"sk_live_",
b"AIza",
b"-",
b"_",
];
let opts = crate::redact::Options {
ips: true,
custom: vec![r"(\d+)".into()],
..Default::default()
};
let mut r = crate::redact::Redactor::new(&opts, [9; 16]).unwrap();
for seed in 1..=rounds(500) {
let mut rng = Rng(seed * 0x51ED_2701);
let line = noise_upto(&mut rng, pieces, 400);
let mut out = Vec::new();
r.line(&line, &mut out);
}
}
fn guarded(what: String, f: impl FnOnce() + Send + 'static) {
use std::sync::mpsc::RecvTimeoutError;
let (done, wait) = std::sync::mpsc::channel();
let t = std::thread::spawn(move || {
f();
let _ = done.send(());
});
match wait.recv_timeout(std::time::Duration::from_secs(60)) {
Ok(()) => t.join().unwrap(),
Err(RecvTimeoutError::Disconnected) => panic!("{what}: panicked"),
Err(RecvTimeoutError::Timeout) => panic!("{what}: still running after a minute"),
}
}
struct Capped(usize);
impl Write for Capped {
fn write(&mut self, b: &[u8]) -> std::io::Result<usize> {
if b.len() > self.0 {
return Err(std::io::Error::other("more than the test takes"));
}
self.0 -= b.len();
Ok(b.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
fn damage(r: &mut Rng, packed: &[u8]) -> Vec<u8> {
let mut v = packed.to_vec();
let how = r.below(3);
if how != 1 {
for _ in 0..=r.below(4) {
let n = v.len() as u64;
if n == 0 {
break;
}
let i = match r.below(4) {
0 => r.below(n.min(64)),
1 => n - 1 - r.below(n.min(32)),
_ => r.below(n),
} as usize;
v[i] = if r.below(2) == 0 {
v[i] ^ (1 << r.below(8))
} else {
r.next() as u8
};
}
}
if how != 0 {
let cut = r.below(v.len() as u64) as usize;
v.truncate(cut);
}
v
}
fn pack(data: &[u8], kind: Option<Kind>) -> Vec<u8> {
let mut p = Packer::new(Vec::new(), kind).unwrap();
p.write_all(data).unwrap();
p.finish().unwrap()
}
#[test]
fn damaged_compressed_files_fail_cleanly() {
let dir = tempfile::tempdir().unwrap();
for seed in 1..=rounds(4) {
let mut r = Rng(seed * 0x6A09_E667);
let text = noise_upto(&mut r, LOG_PIECES, 20_000);
for kind in Kind::ALL {
let packed = pack(&text, Some(kind));
for v in 0..8 {
let path = dir
.path()
.join(format!("{seed}-{v}.log.{}", kind.extension()));
std::fs::write(&path, damage(&mut r, &packed)).unwrap();
guarded(format!("{} seed {seed} case {v}", kind.name()), move || {
let _ = decompress::detect(&path);
let _ = decompress::estimate_unpacked(&path, kind);
let _ = decompress::unpack_to(
&path,
kind,
&mut Capped(8 << 20),
&CancelToken::new(),
&Progress::default(),
);
});
}
}
}
}
fn zip_of(files: &[(&str, Vec<u8>)]) -> Vec<u8> {
let mut z = zip::ZipWriter::new(std::io::Cursor::new(Vec::new()));
for (name, data) in files {
z.start_file(*name, zip::write::SimpleFileOptions::default())
.unwrap();
z.write_all(data).unwrap();
}
z.finish().unwrap().into_inner()
}
fn tar_of(files: &[(&str, Vec<u8>)]) -> Vec<u8> {
let mut b = tar::Builder::new(Vec::new());
for (name, data) in files {
let mut h = tar::Header::new_gnu();
h.set_size(data.len() as u64);
h.set_mode(0o644);
h.set_mtime(1_700_000_000);
h.set_cksum();
b.append_data(&mut h, name, &data[..]).unwrap();
}
b.into_inner().unwrap()
}
#[test]
fn damaged_archives_fail_cleanly() {
let dir = tempfile::tempdir().unwrap();
for seed in 1..=rounds(3) {
let mut r = Rng(seed * 0xBB67_AE85);
let files = [
("logs/app.log", noise_upto(&mut r, LOG_PIECES, 6000)),
("logs/old/db.log", noise_upto(&mut r, LOG_PIECES, 6000)),
];
let tar = tar_of(&files);
let mut archives = vec![("bundle.zip".to_string(), Archive::Zip, zip_of(&files))];
for kind in [None].into_iter().chain(Kind::ALL.map(Some)) {
let name = match kind {
None => "bundle.tar".to_string(),
Some(k) => format!("bundle.tar.{}", k.extension()),
};
archives.push((name, Archive::Tar(kind), pack(&tar, kind)));
}
for (name, archive, bytes) in archives {
for v in 0..6 {
let path = dir.path().join(format!("{seed}-{v}-{name}"));
std::fs::write(&path, damage(&mut r, &bytes)).unwrap();
guarded(format!("{name} seed {seed} case {v}"), move || {
let cancel = CancelToken::new();
let progress = Progress::default();
let _ = decompress::detect_archive(&path);
let listed = decompress::list_members(&path, archive, &cancel, &progress);
let mut names: Vec<String> = match listed {
Ok(Some(m)) => m.into_iter().take(3).map(|m| m.name).collect(),
_ => Vec::new(),
};
names.push("logs/app.log".into());
for name in names {
let unpacked =
decompress::unpack_member(&path, archive, &name, &cancel, &progress);
if let Ok(Some(out)) = unpacked {
decompress::remove_temp(&out);
assert!(!out.exists(), "{} left behind", out.display());
}
}
});
}
}
}
}