#![allow(clippy::unwrap_used, clippy::expect_used, clippy::pedantic)]
use std::path::{Path, PathBuf};
use sqlite_core::{Database, Value};
use sqlite_forensic::{
audit, audit_journal, carve_all_deleted_records, carve_rollback_journal, carve_with_fragments,
row_histories_with_residue,
};
fn corpus_root() -> Option<PathBuf> {
match std::env::var("SQLITE_FORENSIC_BELKASOFT_CORPUS") {
Ok(p) if !p.is_empty() => {
let path = PathBuf::from(p);
if path.is_dir() {
Some(path)
} else {
eprintln!(
"SKIP belkasoft_robustness: SQLITE_FORENSIC_BELKASOFT_CORPUS={} is not a directory",
path.display()
);
None
}
}
_ => {
eprintln!(
"SKIP belkasoft_robustness: set SQLITE_FORENSIC_BELKASOFT_CORPUS to the extracted \
Belkasoft \"SQLite Exercises\" corpus root to run this real-world robustness sweep"
);
None
}
}
}
fn is_sqlite(path: &Path) -> bool {
use std::io::Read;
let Ok(mut f) = std::fs::File::open(path) else {
return false;
};
let mut magic = [0u8; 16];
matches!(f.read_exact(&mut magic), Ok(())) && &magic == b"SQLite format 3\0"
}
fn all_dbs(root: &Path) -> Vec<PathBuf> {
let mut out = Vec::new();
let mut stack = vec![root.to_path_buf()];
while let Some(dir) = stack.pop() {
let Ok(entries) = std::fs::read_dir(&dir) else {
continue;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
stack.push(path);
continue;
}
if let Some(name) = path.file_name().and_then(|n| n.to_str()) {
if name.ends_with("-wal") || name.ends_with("-shm") || name.ends_with("-journal") {
continue;
}
}
if is_sqlite(&path) {
out.push(path);
}
}
}
out.sort();
out
}
fn assert_record_sane(values: &[Value]) {
assert!(
values.len() <= 100_000,
"carved record has implausible column count {}",
values.len()
);
for v in values {
if let Value::Blob(b) = v {
let _ = b.len();
}
}
}
fn sidecar(path: &Path, suffix: &str) -> PathBuf {
let mut s = path.as_os_str().to_owned();
s.push(suffix);
PathBuf::from(s)
}
#[test]
fn real_messenger_databases_survive_the_pipeline_without_panic() {
let Some(root) = corpus_root() else {
return;
};
let dbs = all_dbs(&root);
assert!(
!dbs.is_empty(),
"SQLITE_FORENSIC_BELKASOFT_CORPUS={} contained no SQLite databases",
root.display()
);
let mut opened = 0usize;
for path in &dbs {
let bytes = std::fs::read(path).expect("db readable");
let wal_path = sidecar(path, "-wal");
let db = if wal_path.exists() {
let wal = std::fs::read(&wal_path).expect("wal sidecar readable");
match Database::open_with_wal(bytes, &wal) {
Ok(db) => db,
Err(_) => continue,
}
} else {
match Database::open(bytes) {
Ok(db) => db,
Err(_) => continue,
}
};
opened += 1;
let tiers = carve_with_fragments(&db);
assert_eq!(
tiers.full,
carve_all_deleted_records(&db),
"{}: carve_with_fragments.full diverged from carve_all_deleted_records",
path.display()
);
for rec in &tiers.full {
assert_record_sane(&rec.values);
}
let anomalies = audit(&db);
assert!(
anomalies.len() <= 1_000_000,
"{}: implausible anomaly count {}",
path.display(),
anomalies.len()
);
let _ = row_histories_with_residue(&db);
let _ = db.live_table_rows();
let _ = db.without_rowid_table_rows();
let _ = db.wal_timeline();
let journal_path = sidecar(path, "-journal");
if journal_path.exists() {
if let Ok(journal) = std::fs::read(&journal_path) {
let recovery = carve_rollback_journal(&db, &journal);
for r in &recovery.deleted {
assert_record_sane(&r.values);
}
let _ = audit_journal(&db, &journal);
}
}
}
assert!(
opened > 0,
"no database under {} opened — the pipeline never ran",
root.display()
);
eprintln!(
"belkasoft_robustness: walked {} db file(s), {opened} opened + survived the pipeline",
dbs.len()
);
}