#![cfg(target_os = "linux")]
use adotenv::{parse, parse_with, DiagnosticCode, Dialect, ExpandOptions, ResolveError};
static MEMORY_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn peak_growth(run: impl FnOnce()) -> usize {
let reset = std::fs::write("/proc/self/clear_refs", "5").is_ok();
let before = status_kib(if reset { "VmRSS:" } else { "VmHWM:" });
run();
status_kib("VmHWM:").saturating_sub(before) * 1024
}
fn status_kib(field: &str) -> usize {
let status = std::fs::read_to_string("/proc/self/status").expect("process status is readable");
status
.lines()
.find_map(|line| line.strip_prefix(field))
.and_then(|rest| rest.trim().strip_suffix("kB"))
.and_then(|value| value.trim().parse().ok())
.expect("process status reports the field")
}
#[test]
fn lookup_index_does_not_allocate_for_ignored_bare_keys() {
const LIMIT: usize = 1 << 20;
let _guard = MEMORY_LOCK.lock().unwrap();
let source = "BARE\n".repeat(200_000);
let (document, _) = parse_with(&source, Dialect::NODE_DOTENV);
let growth = peak_growth(|| {
assert_eq!(document.lookup_index().len(), 0);
});
assert!(
growth < LIMIT,
"the lookup index grew the resident set by {growth} bytes"
);
}
#[test]
fn parsing_caps_repeated_diagnostics_within_the_budget() {
const LIMIT: usize = 16 << 20;
let _guard = MEMORY_LOCK.lock().unwrap();
let source = format!("A={}\n", "#".repeat(1_000_000));
let growth = peak_growth(|| {
let (_, diagnostics) = parse(&source);
assert_eq!(diagnostics.len(), 10_001);
assert_eq!(
diagnostics.last().unwrap().code(),
DiagnosticCode::TooManyDiagnostics
);
});
assert!(
growth < LIMIT,
"the bounded diagnostic list grew the resident set by {growth} bytes"
);
}
#[test]
fn resolution_rejects_a_dense_scan_within_the_reference_budget() {
const LIMIT: usize = 32 << 20;
let _guard = MEMORY_LOCK.lock().unwrap();
let value = "${A:-$A}".repeat(100_000);
let source = format!("A={value}\n");
let (doc, _) = parse(&source);
let growth = peak_growth(|| {
assert!(matches!(
doc.resolve(|_| None, &ExpandOptions::portable()),
Err(ResolveError::TooManyReferences {
key,
limit: 65_536,
..
}) if key == "A"
));
});
assert!(
growth < LIMIT,
"the bounded reference scan grew the resident set by {growth} bytes"
);
}
#[test]
fn nested_and_unclosed_forms_use_memory_linear_in_value_length() {
const LIMIT: usize = 64 << 20;
let _guard = MEMORY_LOCK.lock().unwrap();
for bytes in [16 << 10, 64 << 10] {
let unclosed = "$(".repeat(bytes / 2);
let commands = format!("{}{}", "$(".repeat(bytes / 3), ")".repeat(bytes / 3));
let defaults = format!("{}{}", "${A:-".repeat(bytes / 6), "}".repeat(bytes / 6));
for (value, forms) in [
(unclosed, bytes / 2),
(commands, bytes / 3),
(defaults, bytes / 6),
] {
let source = format!("B=1\nA={value}\n");
let (doc, _) = parse(&source);
let growth = peak_growth(|| {
doc.sort().unwrap();
});
assert!(
growth < LIMIT,
"sorting {forms} forms grew the resident set by {growth} bytes"
);
let growth = peak_growth(|| {
let references = doc.unique("A").unwrap().references();
assert_eq!(references.len(), forms);
});
assert!(
growth < LIMIT,
"references for {forms} forms grew the resident set by {growth} bytes"
);
}
}
}
#[test]
fn resolution_depth_budget_bounds_delimiter_scan() {
const LIMIT: usize = 8 << 20;
let _guard = MEMORY_LOCK.lock().unwrap();
for value in [
"$(".repeat(100_000),
"${".repeat(100_000),
format!("${{{}", "$(".repeat(100_000)),
] {
let source = format!("A={value}\n");
let (doc, _) = parse(&source);
let growth = peak_growth(|| {
assert!(matches!(
doc.resolve(
|_| None,
&ExpandOptions::portable()
.with_max_references(2)
.with_max_depth(64)
),
Err(ResolveError::DepthExceeded { limit: 64, .. })
));
});
assert!(
growth < LIMIT,
"the delimiter scan grew the resident set by {growth} bytes"
);
}
}