#[derive(Debug, Clone)]
pub struct AmbientFinding {
pub file: &'static str,
pub line: u32,
pub evidence_pattern: &'static str,
pub category: AmbientCategory,
pub severity: Severity,
pub description: &'static str,
pub exempt: bool,
pub exemption_reason: Option<&'static str>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AmbientCategory {
Time,
Spawn,
Entropy,
Io,
Env,
Output,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Severity {
Low,
Medium,
High,
Critical,
}
pub const KNOWN_FINDINGS: &[AmbientFinding] = &[
AmbientFinding {
file: "time/driver.rs",
line: 38,
evidence_pattern: "std::time::Instant::now()",
category: AmbientCategory::Time,
severity: Severity::Low,
description: "WallClock epoch initialization",
exempt: true,
exemption_reason: Some("Timer driver is the time provider"),
},
AmbientFinding {
file: "runtime/blocking_pool.rs",
line: 62,
evidence_pattern: "Instant::now()",
category: AmbientCategory::Time,
severity: Severity::Low,
description: "Instant::now() in blocking pool timeout",
exempt: true,
exemption_reason: Some("Blocking pool operates outside async runtime"),
},
AmbientFinding {
file: "time/sleep.rs",
line: 623,
evidence_pattern: "std::thread::spawn",
category: AmbientCategory::Spawn,
severity: Severity::Medium,
description: "Fallback timer thread in Sleep::poll()",
exempt: true,
exemption_reason: Some("Documented fallback; used only when no timer driver"),
},
AmbientFinding {
file: "runtime/blocking_pool.rs",
line: 972,
evidence_pattern: "thread::Builder::new()",
category: AmbientCategory::Spawn,
severity: Severity::Low,
description: "Worker thread spawning in blocking pool",
exempt: true,
exemption_reason: Some("Blocking pool requires real OS threads by design"),
},
AmbientFinding {
file: "web/debug.rs",
line: 134,
evidence_pattern: "TcpListener::bind",
category: AmbientCategory::Io,
severity: Severity::Medium,
description: "TcpListener::bind in DebugServer::start()",
exempt: true,
exemption_reason: Some("Debug server is intentionally outside runtime"),
},
];
#[must_use]
pub fn count_by_severity(severity: Severity) -> usize {
validate_audit_operation_authorized("count_by_severity");
KNOWN_FINDINGS
.iter()
.filter(|f| f.severity == severity && !f.exempt)
.count()
}
#[must_use]
pub fn unresolved_count() -> usize {
validate_audit_operation_authorized("unresolved_count");
KNOWN_FINDINGS.iter().filter(|f| !f.exempt).count()
}
pub fn validate_audit_system_security() -> Result<(), String> {
let mut warnings = Vec::new();
let critical_exempt_count = KNOWN_FINDINGS
.iter()
.filter(|f| f.severity == Severity::Critical && f.exempt)
.count();
if critical_exempt_count > 2 {
warnings.push(format!(
"Suspicious: {} critical findings marked exempt",
critical_exempt_count
));
}
let audit_violations = KNOWN_FINDINGS
.iter()
.filter(|f| f.file.starts_with("audit/") && !f.exempt)
.count();
if audit_violations == 0 {
warnings.push("Suspicious: No audit system violations in KNOWN_FINDINGS".to_string());
}
for finding in KNOWN_FINDINGS {
if finding.exempt {
match finding.exemption_reason {
None => warnings.push(format!(
"Exempt finding without justification: {}:{}",
finding.file, finding.line
)),
Some(reason) if reason.len() < 15 => warnings.push(format!(
"Weak exemption justification at {}:{}: '{}'",
finding.file, finding.line, reason
)),
_ => {} }
}
}
if warnings.is_empty() {
Ok(())
} else {
Err(warnings.join("; "))
}
}
pub fn create_constrained_audit_context(
source_region: crate::types::RegionId,
target_region: crate::types::RegionId,
) -> Result<(), String> {
if source_region == target_region {
return Ok(());
}
validate_audit_operation_authorized("create_constrained_audit_context");
eprintln!(
"AUDIT: Cross-region audit from {:?} to {:?}",
source_region, target_region
);
Ok(())
}
pub const GREP_PATTERNS: &[(&str, AmbientCategory)] = &[
(r"Instant::now\(\)", AmbientCategory::Time),
(r"SystemTime::now\(\)", AmbientCategory::Time),
(r"std::time::Instant::now\(\)", AmbientCategory::Time),
(r"std::time::SystemTime::now\(\)", AmbientCategory::Time),
(r"core::time::Instant::now\(\)", AmbientCategory::Time),
(r"core::time::SystemTime::now\(\)", AmbientCategory::Time),
(r"Clock::now\(\)", AmbientCategory::Time),
(r"WallTime::now\(\)", AmbientCategory::Time),
(r"Time::now\(\)", AmbientCategory::Time),
(r"std::thread::spawn", AmbientCategory::Spawn),
(r"thread::spawn", AmbientCategory::Spawn),
(r"thread::Builder", AmbientCategory::Spawn),
(r"std::thread::Builder", AmbientCategory::Spawn),
(r"core::thread::spawn", AmbientCategory::Spawn),
(r"Thread::spawn", AmbientCategory::Spawn), (r"getrandom::", AmbientCategory::Entropy),
(r"rand::thread_rng", AmbientCategory::Entropy),
(r"rand::random\(\)", AmbientCategory::Entropy),
(
r"std::collections::hash_map::RandomState",
AmbientCategory::Entropy,
),
(r"Rng::", AmbientCategory::Entropy), (r"std::net::TcpListener", AmbientCategory::Io),
(r"std::net::TcpStream", AmbientCategory::Io),
(r"std::net::UdpSocket", AmbientCategory::Io),
(r"std::fs::File::open", AmbientCategory::Io),
(r"std::fs::File::create", AmbientCategory::Io),
(r"std::fs::OpenOptions", AmbientCategory::Io),
(r"std::fs::read\(", AmbientCategory::Io),
(r"std::fs::write\(", AmbientCategory::Io),
(r"File::open\(", AmbientCategory::Io),
(r"File::create\(", AmbientCategory::Io),
(r"TcpListener::", AmbientCategory::Io),
(r"TcpStream::", AmbientCategory::Io),
(r"env::var\(", AmbientCategory::Env),
(r"env::var_os\(", AmbientCategory::Env),
(r"env::vars\(", AmbientCategory::Env),
(r"env::set_var\(", AmbientCategory::Env),
(r"env::remove_var\(", AmbientCategory::Env),
(r"std::env::var\(", AmbientCategory::Env),
(r"std::env::var_os\(", AmbientCategory::Env),
(r"std::env::vars\(", AmbientCategory::Env),
(r"std::env::set_var\(", AmbientCategory::Env),
(r"std::env::remove_var\(", AmbientCategory::Env),
(r"println!\(", AmbientCategory::Output),
(r"eprintln!\(", AmbientCategory::Output),
(r"print!\(", AmbientCategory::Output),
(r"eprint!\(", AmbientCategory::Output),
(r"dbg!\(", AmbientCategory::Output),
];
pub const SUSPICIOUS_ALIAS_PATTERNS: &[(&str, AmbientCategory)] = &[
(r"use.*Instant\s+as\s+\w+", AmbientCategory::Time),
(r"use.*SystemTime\s+as\s+\w+", AmbientCategory::Time),
(r"use.*time::Instant\s+as\s+\w+", AmbientCategory::Time),
(r"use.*time::SystemTime\s+as\s+\w+", AmbientCategory::Time),
(r"use.*thread::spawn\s+as\s+\w+", AmbientCategory::Spawn),
(r"use.*thread::Builder\s+as\s+\w+", AmbientCategory::Spawn),
(r"use.*std::thread\s+as\s+\w+", AmbientCategory::Spawn),
(r"use.*getrandom\s+as\s+\w+", AmbientCategory::Entropy),
(r"use.*thread_rng\s+as\s+\w+", AmbientCategory::Entropy),
(r"use.*rand\s+as\s+\w+", AmbientCategory::Entropy),
(r"use.*std::fs\s+as\s+\w+", AmbientCategory::Io),
(r"use.*std::net\s+as\s+\w+", AmbientCategory::Io),
(r"use.*File\s+as\s+\w+", AmbientCategory::Io),
(r"use.*std::env\s+as\s+\w+", AmbientCategory::Env),
];
pub fn detect_ambient_violations(source_code: &str) -> Vec<AmbientViolation> {
validate_audit_operation_authorized("detect_ambient_violations");
detect_ambient_violations_impl(source_code)
}
fn detect_ambient_violations_impl(source_code: &str) -> Vec<AmbientViolation> {
let mut violations = Vec::new();
let mut line_filter = AmbientDetectionLineFilter::default();
let mut suspicious_aliases: Vec<(String, AmbientCategory)> = Vec::new();
let lines: Vec<&str> = source_code.lines().collect();
for (line_num, line) in lines.iter().enumerate() {
let Some(code_line) = line_filter.sanitized_non_test_line(line) else {
continue;
};
for (pattern, category) in matching_grep_patterns(&code_line) {
violations.push(AmbientViolation {
line_number: line_num + 1,
line_content: (*line).to_string(),
pattern: pattern.to_string(),
category,
violation_type: ViolationType::DirectUsage,
});
}
for (alias, category) in &suspicious_aliases {
if let Some(pattern) = ambient_alias_usage_pattern(&code_line, alias, *category) {
violations.push(AmbientViolation {
line_number: line_num + 1,
line_content: (*line).to_string(),
pattern,
category: *category,
violation_type: ViolationType::IndirectUsage,
});
}
}
let mut alias_categories_seen = Vec::new();
for (pattern, category) in SUSPICIOUS_ALIAS_PATTERNS {
let regex_pattern = pattern.trim_start_matches("r\"").trim_end_matches('"');
if code_line.contains("use") && code_line.contains("as") {
let is_suspicious = match category {
AmbientCategory::Time => {
code_line.contains("Instant")
|| code_line.contains("SystemTime")
|| code_line.contains("time::")
}
AmbientCategory::Spawn => {
code_line.contains("thread::") || code_line.contains("spawn")
}
AmbientCategory::Entropy => {
code_line.contains("getrandom")
|| code_line.contains("thread_rng")
|| code_line.contains("rand")
}
AmbientCategory::Io => {
code_line.contains("std::fs")
|| code_line.contains("std::net")
|| code_line.contains("File")
}
AmbientCategory::Env => {
code_line.contains("std::env") || code_line.contains("env::")
}
AmbientCategory::Output => false, };
if is_suspicious && !alias_categories_seen.contains(category) {
alias_categories_seen.push(*category);
if let Some(alias) = alias_identifier_after_as(&code_line) {
let already_tracked =
suspicious_aliases
.iter()
.any(|(known_alias, known_category)| {
known_alias == &alias && known_category == category
});
if !already_tracked {
suspicious_aliases.push((alias, *category));
}
}
violations.push(AmbientViolation {
line_number: line_num + 1,
line_content: (*line).to_string(),
pattern: regex_pattern.to_string(),
category: *category,
violation_type: ViolationType::SuspiciousAlias,
});
}
}
}
}
violations
}
fn grep_pattern_literal(pattern: &str) -> String {
pattern.replace(r"\(", "(").replace(r"\)", ")")
}
fn line_contains_grep_literal(line: &str, literal: &str) -> bool {
line.match_indices(literal).any(|(idx, _)| {
let before = line[..idx].chars().next_back();
!matches!(before, Some(ch) if ch.is_ascii_alphanumeric() || ch == '_')
})
}
fn ambient_pattern_key(literal: &str, category: AmbientCategory) -> &'static str {
match category {
AmbientCategory::Time => {
if literal.contains("Instant::now") {
"time_instant_now"
} else if literal.contains("SystemTime::now") {
"time_system_time_now"
} else if literal.contains("Clock::now") {
"time_clock_now"
} else if literal.contains("WallTime::now") {
"time_wall_time_now"
} else {
"time_other"
}
}
AmbientCategory::Spawn => {
if literal.contains("thread::spawn") || literal.contains("Thread::spawn") {
"spawn_thread_spawn"
} else if literal.contains("thread::Builder") {
"spawn_thread_builder"
} else {
"spawn_other"
}
}
AmbientCategory::Entropy => {
if literal.contains("getrandom") {
"entropy_getrandom"
} else if literal.contains("thread_rng") {
"entropy_thread_rng"
} else if literal.contains("rand::random") {
"entropy_rand_random"
} else if literal.contains("RandomState") {
"entropy_random_state"
} else if literal.contains("Rng::") {
"entropy_rng_trait"
} else {
"entropy_other"
}
}
AmbientCategory::Io => {
if literal.contains("TcpListener") {
"io_tcp_listener"
} else if literal.contains("TcpStream") {
"io_tcp_stream"
} else if literal.contains("UdpSocket") {
"io_udp_socket"
} else if literal.contains("File::open") {
"io_file_open"
} else if literal.contains("File::create") {
"io_file_create"
} else if literal.contains("OpenOptions") {
"io_open_options"
} else if literal.contains("fs::read") {
"io_fs_read"
} else if literal.contains("fs::write") {
"io_fs_write"
} else {
"io_other"
}
}
AmbientCategory::Env => {
if literal.contains("var_os") {
"env_var_os"
} else if literal.contains("vars") {
"env_vars"
} else if literal.contains("set_var") {
"env_set_var"
} else if literal.contains("remove_var") {
"env_remove_var"
} else if literal.contains("var(") {
"env_var"
} else {
"env_other"
}
}
AmbientCategory::Output => {
if literal.contains("println!") {
"output_println"
} else if literal.contains("eprintln!") {
"output_eprintln"
} else if literal.contains("print!") {
"output_print"
} else if literal.contains("eprint!") {
"output_eprint"
} else if literal.contains("dbg!") {
"output_dbg"
} else {
"output_other"
}
}
}
}
fn matching_grep_patterns(line: &str) -> Vec<(&'static str, AmbientCategory)> {
let mut patterns: Vec<_> = GREP_PATTERNS
.iter()
.map(|(pattern, category)| (*pattern, grep_pattern_literal(pattern), *category))
.collect();
patterns.sort_by(|(_, lhs, _), (_, rhs, _)| rhs.len().cmp(&lhs.len()));
let mut matches = Vec::new();
let mut matched_literals: Vec<String> = Vec::new();
let mut matched_keys: Vec<(AmbientCategory, &'static str)> = Vec::new();
for (pattern, literal, category) in patterns {
let operation_key = ambient_pattern_key(&literal, category);
let already_covered = matched_literals.iter().any(|matched| {
matched.ends_with(literal.as_str()) || literal.ends_with(matched.as_str())
}) || matched_keys.iter().any(|(matched_category, matched_key)| {
*matched_category == category && *matched_key == operation_key
});
if !already_covered && line_contains_grep_literal(line, &literal) {
matched_literals.push(literal);
matched_keys.push((category, operation_key));
matches.push((pattern, category));
}
}
matches
}
fn alias_identifier_after_as(line: &str) -> Option<String> {
let (_, alias_and_rest) = line.split_once(" as ")?;
let alias: String = alias_and_rest
.trim_start()
.chars()
.take_while(|ch| ch.is_ascii_alphanumeric() || *ch == '_')
.collect();
if alias.is_empty() { None } else { Some(alias) }
}
fn ambient_alias_usage_pattern(
line: &str,
alias: &str,
category: AmbientCategory,
) -> Option<String> {
let qualified_methods: &[&str] = match category {
AmbientCategory::Time => &["now"],
AmbientCategory::Spawn => &["spawn", "new"],
AmbientCategory::Entropy => &["random", "thread_rng", "fill", "fill_bytes"],
AmbientCategory::Io => &["open", "create", "read", "write", "connect", "bind"],
AmbientCategory::Env => &["var", "var_os", "vars", "set_var", "remove_var"],
AmbientCategory::Output => &[],
};
for method in qualified_methods {
let pattern = format!("{alias}::{method}(");
if line_contains_alias_call(line, &pattern) {
return Some(pattern);
}
}
if matches!(
category,
AmbientCategory::Spawn | AmbientCategory::Entropy | AmbientCategory::Env
) {
let pattern = format!("{alias}(");
if line_contains_alias_call(line, &pattern) {
return Some(pattern);
}
}
None
}
fn line_contains_alias_call(line: &str, pattern: &str) -> bool {
line.match_indices(pattern).any(|(idx, _)| {
let before = line[..idx].chars().next_back();
!matches!(before, Some(ch) if ch.is_ascii_alphanumeric() || ch == '_')
})
}
fn ambient_raw_string_start(bytes: &[u8], idx: usize) -> Option<(usize, usize)> {
let mut cursor = idx;
if bytes.get(cursor) == Some(&b'b') {
cursor += 1;
}
if bytes.get(cursor) != Some(&b'r') {
return None;
}
let mut hash_count = 0;
cursor += 1;
while bytes.get(cursor) == Some(&b'#') {
hash_count += 1;
cursor += 1;
}
if bytes.get(cursor) == Some(&b'"') {
Some((cursor + 1, hash_count))
} else {
None
}
}
#[derive(Default)]
struct AmbientDetectionSanitizerState {
in_block_comment: bool,
in_string: bool,
raw_hashes: Option<usize>,
}
fn strip_comments_and_literals_for_detection(
line: &str,
state: &mut AmbientDetectionSanitizerState,
) -> String {
let bytes = line.as_bytes();
let mut out = String::with_capacity(line.len());
let mut idx = 0;
while idx < bytes.len() {
if state.in_block_comment {
if idx + 1 < bytes.len() && bytes[idx] == b'*' && bytes[idx + 1] == b'/' {
state.in_block_comment = false;
idx += 2;
} else {
idx += 1;
}
continue;
}
if let Some(hash_count) = state.raw_hashes {
if bytes[idx] == b'"'
&& idx + 1 + hash_count <= bytes.len()
&& bytes[idx + 1..idx + 1 + hash_count]
.iter()
.all(|b| *b == b'#')
{
state.raw_hashes = None;
idx += 1 + hash_count;
} else {
idx += 1;
}
continue;
}
if state.in_string {
match bytes[idx] {
b'\\' => idx = (idx + 2).min(bytes.len()),
b'"' => {
state.in_string = false;
idx += 1;
}
_ => idx += 1,
}
continue;
}
if idx + 1 < bytes.len() && bytes[idx] == b'/' && bytes[idx + 1] == b'/' {
break;
}
if idx + 1 < bytes.len() && bytes[idx] == b'/' && bytes[idx + 1] == b'*' {
state.in_block_comment = true;
idx += 2;
continue;
}
if let Some((next_idx, hash_count)) = ambient_raw_string_start(bytes, idx) {
state.raw_hashes = Some(hash_count);
idx = next_idx;
continue;
}
if bytes[idx] == b'"' {
state.in_string = true;
idx += 1;
continue;
}
out.push(bytes[idx] as char);
idx += 1;
}
out
}
#[derive(Default)]
struct AmbientDetectionLineFilter {
pending_cfg_test: bool,
pending_cfg_test_attr_depth: i32,
pending_cfg_test_item_body: bool,
in_cfg_test_item: bool,
cfg_test_depth: i32,
cfg_test_sanitizer_state: AmbientDetectionSanitizerState,
sanitizer_state: AmbientDetectionSanitizerState,
}
impl AmbientDetectionLineFilter {
fn sanitized_non_test_line(&mut self, line: &str) -> Option<String> {
let trimmed = line.trim();
if self.in_cfg_test_item {
self.advance_cfg_test_depth(line);
return None;
}
if self.pending_cfg_test_item_body {
self.start_or_continue_cfg_test_item(line);
return None;
}
if is_test_only_attribute(trimmed) {
self.pending_cfg_test = true;
self.pending_cfg_test_attr_depth = 0;
return None;
}
if self.pending_cfg_test {
if trimmed.is_empty() {
return None;
}
if self.pending_cfg_test_attr_depth > 0 || trimmed.starts_with("#[") {
self.pending_cfg_test_attr_depth += attribute_delimiter_delta(trimmed);
self.pending_cfg_test_attr_depth = self.pending_cfg_test_attr_depth.max(0);
return None;
}
self.pending_cfg_test = false;
self.pending_cfg_test_attr_depth = 0;
if starts_cfg_test_item(trimmed) {
self.start_or_continue_cfg_test_item(line);
return None;
}
}
let stripped = strip_comments_and_literals_for_detection(line, &mut self.sanitizer_state);
if stripped.trim().is_empty() {
None
} else {
Some(stripped)
}
}
fn start_or_continue_cfg_test_item(&mut self, line: &str) {
let depth = brace_delta_for_detection(line, &mut self.cfg_test_sanitizer_state);
if depth > 0 {
self.in_cfg_test_item = true;
self.cfg_test_depth = depth;
self.pending_cfg_test_item_body = false;
} else {
self.pending_cfg_test_item_body = !line.trim_end().ends_with(';');
}
}
fn advance_cfg_test_depth(&mut self, line: &str) {
self.cfg_test_depth += brace_delta_for_detection(line, &mut self.cfg_test_sanitizer_state);
if self.cfg_test_depth <= 0 {
self.in_cfg_test_item = false;
self.cfg_test_depth = 0;
self.cfg_test_sanitizer_state = AmbientDetectionSanitizerState::default();
}
}
}
fn brace_delta_for_detection(line: &str, state: &mut AmbientDetectionSanitizerState) -> i32 {
let stripped = strip_comments_and_literals_for_detection(line, state);
stripped.chars().fold(0, |depth, ch| match ch {
'{' => depth + 1,
'}' => depth - 1,
_ => depth,
})
}
fn starts_cfg_test_item(trimmed: &str) -> bool {
trimmed.starts_with("mod ")
|| trimmed.starts_with("pub mod ")
|| trimmed.starts_with("fn ")
|| trimmed.starts_with("pub fn ")
|| trimmed.starts_with("async fn ")
|| trimmed.starts_with("pub async fn ")
|| trimmed.starts_with("impl ")
}
fn is_test_only_attribute(trimmed: &str) -> bool {
trimmed == "#[cfg(test)]"
|| trimmed.starts_with("#[cfg(all(test")
|| trimmed.starts_with("#[cfg(any(test")
|| trimmed == "#[test]"
|| trimmed.starts_with("#[tokio::test")
}
fn attribute_delimiter_delta(line: &str) -> i32 {
line.chars().fold(0, |depth, ch| match ch {
'[' | '(' => depth + 1,
']' | ')' => depth - 1,
_ => depth,
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AmbientViolation {
pub line_number: usize,
pub line_content: String,
pub pattern: String,
pub category: AmbientCategory,
pub violation_type: ViolationType,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ViolationType {
DirectUsage,
SuspiciousAlias,
IndirectUsage,
}
fn validate_audit_operation_authorized(operation_name: &str) {
assert!(
!operation_name.is_empty(),
"Invalid audit operation: empty operation name"
);
validate_known_findings_integrity();
validate_audit_system_integrity();
}
fn validate_known_findings_integrity() {
let mut critical_exempt_count = 0;
let mut audit_system_findings = 0;
for finding in KNOWN_FINDINGS {
if finding.severity == Severity::Critical && finding.exempt {
critical_exempt_count += 1;
}
if finding.file.starts_with("audit/") {
audit_system_findings += 1;
}
if finding.exempt {
match finding.exemption_reason {
None => panic!(
"KNOWN_FINDINGS tampering detected: exempt finding without reason at {}:{}",
finding.file, finding.line
),
Some(reason) if reason.len() < 10 => panic!(
"KNOWN_FINDINGS tampering detected: insufficient exemption reason at {}:{}: '{}'",
finding.file, finding.line, reason
),
_ => {} }
}
}
if critical_exempt_count > 2 {
eprintln!(
"WARNING: High number of exempt critical findings ({}), possible KNOWN_FINDINGS tampering",
critical_exempt_count
);
}
if audit_system_findings == 0 {
eprintln!(
"WARNING: No audit system findings in KNOWN_FINDINGS, possible tampering to hide violations"
);
}
}
fn validate_audit_system_integrity() {
let _audit_source_patterns = [
(
"std::fs::read_to_string",
"Audit system using direct file access",
),
("println!", "Audit system using ambient output"),
("eprintln!", "Audit system using ambient error output"),
];
assert!(
!KNOWN_FINDINGS.is_empty(),
"KNOWN_FINDINGS is empty - possible audit system tampering"
);
assert!(
!GREP_PATTERNS.is_empty(),
"GREP_PATTERNS is empty - possible audit system tampering"
);
}
#[cfg(test)]
pub use tests::scan_categories_for_contract_fixture;
#[cfg(test)]
mod tests {
#![allow(
clippy::pedantic,
clippy::nursery,
clippy::expect_fun_call,
clippy::map_unwrap_or,
clippy::cast_possible_wrap,
clippy::future_not_send
)]
use super::*;
#[test]
fn known_findings_are_documented() {
assert!(
!KNOWN_FINDINGS.is_empty(),
"Findings list should not be empty"
);
}
#[test]
fn critical_findings_resolved() {
let critical = count_by_severity(Severity::Critical);
assert!(
critical == 0,
"Expected zero non-exempt critical findings, got {critical}"
);
}
#[test]
fn exempt_findings_have_reasons() {
for finding in KNOWN_FINDINGS {
if finding.exempt {
assert!(
finding.exemption_reason.is_some(),
"Exempt finding in {} has no reason",
finding.file
);
}
}
}
#[test]
fn grep_patterns_cover_all_categories() {
let categories: std::collections::HashSet<_> =
GREP_PATTERNS.iter().map(|(_, cat)| *cat).collect();
assert!(categories.contains(&AmbientCategory::Time));
assert!(categories.contains(&AmbientCategory::Spawn));
assert!(categories.contains(&AmbientCategory::Entropy));
assert!(categories.contains(&AmbientCategory::Io));
assert!(categories.contains(&AmbientCategory::Env));
assert!(categories.contains(&AmbientCategory::Output));
}
#[test]
fn unresolved_count_tracks_non_exempt() {
let unresolved = unresolved_count();
let total = KNOWN_FINDINGS.len();
let exempt = KNOWN_FINDINGS.iter().filter(|f| f.exempt).count();
assert_eq!(unresolved, total - exempt);
}
#[test]
fn severity_ordering() {
assert!(Severity::Low < Severity::Medium);
assert!(Severity::Medium < Severity::Critical);
}
#[test]
fn validate_audit_system_security_detects_tampering() {
let result = super::validate_audit_system_security();
match result {
Ok(()) => {
println!("Audit system security validation passed");
}
Err(warnings) => {
println!("Audit system security warnings: {}", warnings);
assert!(
!warnings.is_empty(),
"Warnings should not be empty if validation failed"
);
assert!(
warnings.len() > 10,
"Warning messages should be descriptive"
);
}
}
}
#[test]
fn capability_validation_prevents_unauthorized_operations() {
super::validate_audit_operation_authorized("test_operation");
super::validate_known_findings_integrity();
super::validate_audit_system_integrity();
}
#[test]
fn cross_region_audit_context_validation() {
let source_region = crate::types::RegionId::new_for_test(1, 0);
let target_region = crate::types::RegionId::new_for_test(2, 0);
let same_result = super::create_constrained_audit_context(source_region, source_region);
assert!(same_result.is_ok(), "Same-region audit should be allowed");
let cross_result = super::create_constrained_audit_context(source_region, target_region);
assert!(
cross_result.is_ok(),
"Cross-region audit should be validated and logged"
);
}
#[test]
fn count_functions_include_capability_validation() {
let warning_count = super::count_by_severity(Severity::Medium);
let unresolved_count = super::unresolved_count();
assert!(warning_count <= 100, "Warning count should be reasonable");
assert!(
unresolved_count <= 100,
"Unresolved count should be reasonable"
);
}
#[test]
fn detect_direct_ambient_violations() {
let source = r#"
fn bad_function() {
let now = std::time::Instant::now(); // Should be detected
println!("Current time: {:?}", now); // Should be detected
}
fn good_function(cx: &Cx) {
let now = cx.time().instant_now(); // Should NOT be detected
}
#[cfg(test)]
fn test_code() {
let now = Instant::now(); // Should NOT be detected (test code)
}
"#;
let violations = detect_ambient_violations(source);
assert_eq!(violations.len(), 2);
let instant_violation = violations
.iter()
.find(|v| v.line_content.contains("Instant::now"))
.unwrap();
assert_eq!(instant_violation.category, AmbientCategory::Time);
assert_eq!(instant_violation.violation_type, ViolationType::DirectUsage);
let println_violation = violations
.iter()
.find(|v| v.line_content.contains("println!"))
.unwrap();
assert_eq!(println_violation.category, AmbientCategory::Output);
assert_eq!(println_violation.violation_type, ViolationType::DirectUsage);
}
#[test]
fn detect_alias_bypasses() {
let source = r#"
use std::time::Instant as Clock; // Suspicious alias
use std::thread::spawn as fork; // Suspicious alias
use std::fs::File as FileHandle; // Suspicious alias
fn bad_function() {
let now = Clock::now(); // Would bypass simple grep
fork(|| { /* work */ }); // Would bypass simple grep
let _f = FileHandle::open("/etc/passwd").unwrap(); // Would bypass simple grep
}
fn good_function(cx: &Cx) {
let now = cx.time().instant_now(); // Proper capability usage
}
"#;
let violations = detect_ambient_violations(source);
let alias_violations: Vec<_> = violations
.iter()
.filter(|v| v.violation_type == ViolationType::SuspiciousAlias)
.collect();
assert!(!alias_violations.is_empty(), "Should detect alias bypasses");
assert!(
alias_violations
.iter()
.any(|v| v.category == AmbientCategory::Time)
);
assert!(
alias_violations
.iter()
.any(|v| v.category == AmbientCategory::Spawn)
);
let indirect_violations: Vec<_> = violations
.iter()
.filter(|v| v.violation_type == ViolationType::IndirectUsage)
.collect();
assert!(
indirect_violations.iter().any(|v| {
v.category == AmbientCategory::Time && v.line_content.contains("Clock::now")
}),
"Should detect use of time alias"
);
assert!(
indirect_violations
.iter()
.any(|v| v.category == AmbientCategory::Spawn && v.line_content.contains("fork")),
"Should detect use of spawn alias"
);
assert!(
indirect_violations.iter().any(|v| {
v.category == AmbientCategory::Io && v.line_content.contains("FileHandle::open")
}),
"Should detect use of IO alias"
);
}
#[test]
fn enhanced_patterns_catch_module_path_variants() {
let source = r#"
fn bad_variants() {
let now1 = std::time::Instant::now(); // std:: prefix
let now2 = core::time::Instant::now(); // core:: prefix
let now3 = Instant::now(); // unqualified
std::thread::spawn(|| {}); // std:: prefix
thread::spawn(|| {}); // unqualified
std::env::var("HOME").ok(); // std:: prefix
env::var("PATH").ok(); // unqualified
}
"#;
let violations = detect_ambient_violations(source);
let time_violations: Vec<_> = violations
.iter()
.filter(|v| v.category == AmbientCategory::Time)
.collect();
assert_eq!(time_violations.len(), 3, "Should catch all time variants");
let spawn_violations: Vec<_> = violations
.iter()
.filter(|v| v.category == AmbientCategory::Spawn)
.collect();
assert_eq!(spawn_violations.len(), 2, "Should catch all spawn variants");
let env_violations: Vec<_> = violations
.iter()
.filter(|v| v.category == AmbientCategory::Env)
.collect();
assert_eq!(env_violations.len(), 2, "Should catch all env variants");
}
#[test]
fn enhanced_patterns_exclude_comments_and_tests() {
let source = r#"
// This is a comment with Instant::now() that should be ignored
/* Block comment with std::thread::spawn that should be ignored */
fn production_code() {
let now = Instant::now(); // Should be detected
}
#[cfg(test)]
mod tests {
fn test_function() {
let now = Instant::now(); // Should NOT be detected
thread::spawn(|| {}); // Should NOT be detected
}
}
"#;
let violations = detect_ambient_violations(source);
assert_eq!(violations.len(), 1);
assert!(violations[0].line_content.contains("Instant::now"));
assert_eq!(violations[0].category, AmbientCategory::Time);
}
#[test]
fn enhanced_patterns_exclude_split_brace_cfg_test_functions() {
let source = r#"
fn production_code() {
let now = Instant::now(); // Should be detected
}
#[cfg(test)]
fn test_code()
{
let now = Instant::now(); // Should NOT be detected
thread::spawn(|| {}); // Should NOT be detected
}
"#;
let violations = detect_ambient_violations(source);
assert_eq!(
violations.len(),
1,
"cfg(test) functions with split braces must be skipped"
);
assert!(violations[0].line_content.contains("Instant::now"));
assert_eq!(violations[0].category, AmbientCategory::Time);
}
#[test]
fn enhanced_patterns_exclude_cfg_test_modules_with_literal_braces() {
let source = r##"
fn production_code() {
let now = Instant::now(); // Should be detected
}
#[cfg(test)]
mod tests {
fn test_fixture() {
let _json = r#"{"nested": {"brace": true}}"#;
let _text = "closing brace } inside a string";
eprintln!("test-only output");
let now = Instant::now(); // Should NOT be detected
}
}
"##;
let violations = detect_ambient_violations(source);
assert_eq!(
violations.len(),
1,
"cfg(test) modules with literal braces must stay skipped"
);
assert!(violations[0].line_content.contains("Instant::now"));
assert_eq!(violations[0].category, AmbientCategory::Time);
}
#[test]
fn violation_type_classification() {
let source = r#"
use std::time::Instant as Clock;
fn test_function() {
let now = Instant::now(); // DirectUsage
let now2 = Clock::now(); // Would be caught by enhanced patterns
}
"#;
let violations = detect_ambient_violations(source);
let direct_usage = violations.iter().any(|v| {
v.violation_type == ViolationType::DirectUsage
&& v.line_content.contains("Instant::now")
});
assert!(direct_usage, "Should detect direct usage");
let alias_usage = violations.iter().any(|v| {
v.violation_type == ViolationType::SuspiciousAlias
&& v.line_content.contains("as Clock")
});
assert!(alias_usage, "Should detect suspicious alias");
}
use std::path::{Path, PathBuf};
const EXEMPT_PREFIXES: &[&str] = &[
"util/entropy.rs",
"fs/",
"time/driver.rs",
"runtime/blocking_pool.rs",
"web/debug.rs",
"lab/",
"test_logging.rs",
"test_utils.rs",
"test_ndjson.rs",
"obligation/conformance_runner.rs",
"audit/",
"bin/",
"cli/",
"atp/benchmark/",
];
const PRISTINE_MODULES: &[&str] = &["cx", "obligation", "plan"];
const AMBIENT_VIOLATION_CEILING: usize = 564;
fn src_root() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("src")
}
fn collect_rs_files(dir: &Path) -> Vec<PathBuf> {
let mut files = Vec::new();
let Ok(entries) = std::fs::read_dir(dir) else {
return files;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
files.extend(collect_rs_files(&path));
} else if path.extension().is_some_and(|e| e == "rs") {
files.push(path);
}
}
files
}
fn is_exempt(rel_path: &str) -> bool {
EXEMPT_PREFIXES.iter().any(|p| rel_path.starts_with(p)) || is_test_surface(rel_path)
}
fn is_test_surface(rel_path: &str) -> bool {
let file_name = Path::new(rel_path)
.file_name()
.and_then(|name| name.to_str())
.unwrap_or(rel_path);
file_name.ends_with("_tests.rs")
|| file_name.ends_with("_test.rs")
|| file_name.ends_with("_conformance.rs")
|| file_name.ends_with("_conformance_tests.rs")
|| file_name.ends_with("_metamorphic.rs")
|| file_name.ends_with("_metamorphic_tests.rs")
|| file_name.ends_with("_testing.rs")
|| file_name.ends_with("_mutations.rs")
|| file_name.ends_with("_verification_suite.rs")
|| file_name.ends_with("_audit.rs")
|| file_name.ends_with("_e2e_tests.rs")
|| file_name.starts_with("real_") && file_name.ends_with(".rs")
}
fn pattern_to_literal(pattern: &str) -> String {
pattern.replace(r"\(", "(").replace(r"\)", ")")
}
fn line_contains_literal(line: &str, literal: &str) -> bool {
line.match_indices(literal).any(|(idx, _)| {
let before = line[..idx].chars().next_back();
!matches!(before, Some(ch) if ch.is_ascii_alphanumeric() || ch == '_')
})
}
fn raw_string_start(bytes: &[u8], idx: usize) -> Option<(usize, usize)> {
let mut cursor = idx;
if bytes.get(cursor) == Some(&b'b') {
cursor += 1;
}
if bytes.get(cursor) != Some(&b'r') {
return None;
}
let mut hash_count = 0;
cursor += 1;
while bytes.get(cursor) == Some(&b'#') {
hash_count += 1;
cursor += 1;
}
if bytes.get(cursor) == Some(&b'"') {
Some((cursor + 1, hash_count))
} else {
None
}
}
#[derive(Default)]
struct ScanSanitizerState {
in_block_comment: bool,
in_string: bool,
raw_hashes: Option<usize>,
}
fn strip_comments_and_literals(line: &str, state: &mut ScanSanitizerState) -> String {
let bytes = line.as_bytes();
let mut out = String::with_capacity(line.len());
let mut idx = 0;
while idx < bytes.len() {
if state.in_block_comment {
if idx + 1 < bytes.len() && bytes[idx] == b'*' && bytes[idx + 1] == b'/' {
state.in_block_comment = false;
idx += 2;
} else {
idx += 1;
}
continue;
}
if let Some(hash_count) = state.raw_hashes {
if bytes[idx] == b'"'
&& idx + 1 + hash_count <= bytes.len()
&& bytes[idx + 1..idx + 1 + hash_count]
.iter()
.all(|b| *b == b'#')
{
state.raw_hashes = None;
idx += 1 + hash_count;
} else {
idx += 1;
}
continue;
}
if state.in_string {
match bytes[idx] {
b'\\' => idx = (idx + 2).min(bytes.len()),
b'"' => {
state.in_string = false;
idx += 1;
}
_ => idx += 1,
}
continue;
}
if idx + 1 < bytes.len() && bytes[idx] == b'/' && bytes[idx + 1] == b'/' {
break;
}
if idx + 1 < bytes.len() && bytes[idx] == b'/' && bytes[idx + 1] == b'*' {
state.in_block_comment = true;
idx += 2;
continue;
}
if let Some((next_idx, hash_count)) = raw_string_start(bytes, idx) {
state.raw_hashes = Some(hash_count);
idx = next_idx;
continue;
}
if bytes[idx] == b'"' {
state.in_string = true;
idx += 1;
continue;
}
out.push(bytes[idx] as char);
idx += 1;
}
out
}
fn non_test_lines(content: &str) -> Vec<(usize, String)> {
let mut result = Vec::new();
let mut in_cfg_test_item = false;
let mut brace_depth: i32 = 0;
let mut pending_cfg_test = false;
let mut pending_cfg_test_attr_depth: i32 = 0;
let mut pending_cfg_test_item_body = false;
let mut cfg_test_sanitizer_state = ScanSanitizerState::default();
let mut sanitizer_state = ScanSanitizerState::default();
for (idx, line) in content.lines().enumerate() {
let trimmed = line.trim();
if in_cfg_test_item {
brace_depth += brace_delta_for_scan(line, &mut cfg_test_sanitizer_state);
if brace_depth <= 0 {
in_cfg_test_item = false;
brace_depth = 0;
cfg_test_sanitizer_state = ScanSanitizerState::default();
}
continue;
}
if pending_cfg_test_item_body {
let delta = brace_delta_for_scan(line, &mut cfg_test_sanitizer_state);
if delta > 0 {
in_cfg_test_item = true;
brace_depth = delta;
pending_cfg_test_item_body = false;
} else if trimmed.ends_with(';') {
pending_cfg_test_item_body = false;
}
continue;
}
if is_test_only_attribute(trimmed) {
pending_cfg_test = true;
pending_cfg_test_attr_depth = 0;
continue;
}
if pending_cfg_test {
if starts_cfg_test_item(trimmed) {
pending_cfg_test = false;
pending_cfg_test_attr_depth = 0;
cfg_test_sanitizer_state = ScanSanitizerState::default();
let delta = brace_delta_for_scan(line, &mut cfg_test_sanitizer_state);
if delta > 0 {
in_cfg_test_item = true;
brace_depth = delta;
} else if !trimmed.ends_with(';') {
pending_cfg_test_item_body = true;
}
continue;
}
if trimmed.is_empty() {
continue;
}
if pending_cfg_test_attr_depth > 0 || trimmed.starts_with("#[") {
pending_cfg_test_attr_depth += attribute_delimiter_delta(trimmed);
pending_cfg_test_attr_depth = pending_cfg_test_attr_depth.max(0);
continue;
}
if !trimmed.starts_with('#') {
pending_cfg_test = false;
pending_cfg_test_attr_depth = 0;
}
}
if trimmed.starts_with("//") && !sanitizer_state.in_block_comment {
continue;
}
let stripped = strip_comments_and_literals(line, &mut sanitizer_state);
if stripped.trim().is_empty() {
continue;
}
result.push((idx + 1, stripped));
}
result
}
fn brace_delta_for_scan(line: &str, state: &mut ScanSanitizerState) -> i32 {
let stripped = strip_comments_and_literals(line, state);
stripped.chars().fold(0, |depth, ch| match ch {
'{' => depth + 1,
'}' => depth - 1,
_ => depth,
})
}
struct Violation {
file: String,
line: usize,
pattern: String,
category: AmbientCategory,
}
impl std::fmt::Display for Violation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
" {}:{} — {:?} ({})",
self.file, self.line, self.category, self.pattern
)
}
}
fn scan_source(rel: &str, content: &str) -> Vec<Violation> {
let mut violations = Vec::new();
if is_exempt(rel) {
return violations;
}
let lines = non_test_lines(content);
for (line_num, line_text) in &lines {
for (pattern, category) in matching_grep_patterns(line_text) {
violations.push(Violation {
file: rel.to_string(),
line: *line_num,
pattern: pattern_to_literal(pattern),
category,
});
}
}
violations
}
#[must_use]
pub fn scan_categories_for_contract_fixture(rel: &str, content: &str) -> Vec<AmbientCategory> {
scan_source(rel, content)
.into_iter()
.map(|violation| violation.category)
.collect()
}
fn scan_directory(dir: &Path, root: &Path) -> Vec<Violation> {
let mut violations = Vec::new();
for file_path in collect_rs_files(dir) {
let rel = file_path
.strip_prefix(root)
.unwrap()
.to_string_lossy()
.replace('\\', "/");
let Ok(content) = std::fs::read_to_string(&file_path) else {
continue;
};
violations.extend(scan_source(&rel, &content));
}
violations
}
fn format_violations(vs: &[Violation]) -> String {
vs.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>()
.join("\n")
}
fn format_violation_sample(vs: &[Violation], limit: usize) -> String {
let mut rendered = vs
.iter()
.take(limit)
.map(std::string::ToString::to_string)
.collect::<Vec<_>>();
if vs.len() > limit {
rendered.push(format!(" ... {} more violation(s)", vs.len() - limit));
}
rendered.join("\n")
}
fn has_non_test_match_near_line(
content: &str,
pattern: &str,
expected_line: u32,
max_line_distance: u32,
) -> bool {
let literal = pattern_to_literal(pattern);
non_test_lines(content).into_iter().any(|(line_num, line)| {
expected_line.abs_diff(line_num as u32) <= max_line_distance
&& line_contains_literal(&line, &literal)
})
}
#[test]
fn pristine_modules_have_no_ambient_authority() {
let root = src_root();
for module in PRISTINE_MODULES {
let module_dir = root.join(module);
let violations = scan_directory(&module_dir, &root);
assert!(
violations.is_empty(),
"Pristine module '{module}' has {} ambient authority violation(s):\n{}",
violations.len(),
format_violations(&violations),
);
}
}
#[test]
fn known_findings_reference_real_code() {
let root = src_root();
let mut audit_failures = Vec::new();
for finding in KNOWN_FINDINGS {
let path = root.join(finding.file);
let content = match std::fs::read_to_string(&path) {
Ok(content) => content,
Err(err) => {
audit_failures.push(format!(
"KNOWN_FINDINGS references unreadable file: src/{} ({})",
finding.file, err
));
continue; }
};
let has_nearby_match =
has_non_test_match_near_line(&content, finding.evidence_pattern, finding.line, 30);
if !has_nearby_match {
audit_failures.push(format!(
"KNOWN_FINDINGS entry '{}' at src/{}:{} — \
no matching non-test evidence pattern '{}' found within ±30 lines. Stale entry?",
finding.description, finding.file, finding.line, finding.evidence_pattern,
));
}
}
assert!(
audit_failures.is_empty(),
"Ambient authority audit failures ({}):{}",
audit_failures.len(),
audit_failures
.iter()
.fold(String::new(), |mut acc, failure| {
acc.push_str("\n - ");
acc.push_str(failure);
acc
})
);
}
#[test]
fn grep_patterns_catch_each_finding_category() {
for finding in KNOWN_FINDINGS {
let covered = GREP_PATTERNS
.iter()
.any(|(_, cat)| *cat == finding.category);
assert!(
covered,
"Finding '{}' with category {:?} has no grep pattern coverage",
finding.description, finding.category,
);
}
}
#[test]
fn exempt_findings_are_in_recognized_provider_paths() {
let provider_paths: &[&str] = &[
"time/driver.rs",
"time/sleep.rs",
"runtime/blocking_pool.rs",
"web/debug.rs",
"util/entropy.rs",
"fs/",
];
for finding in KNOWN_FINDINGS.iter().filter(|f| f.exempt) {
let in_provider = provider_paths.iter().any(|p| finding.file.starts_with(p));
assert!(
in_provider,
"Exempt finding '{}' in src/{} is not in a recognized \
provider path. Either remove the exemption or add the \
path to provider_paths.",
finding.description, finding.file,
);
}
}
#[test]
fn ambient_authority_does_not_regress() {
let root = src_root();
let violations = scan_directory(&root, &root);
assert!(
violations.len() <= AMBIENT_VIOLATION_CEILING,
"Ambient authority count ({}) exceeds ceiling ({}).\n\
Either remove the ambient authority usage or, if intentional,\n\
add it to KNOWN_FINDINGS and bump AMBIENT_VIOLATION_CEILING.\n\
Violation sample:\n{}",
violations.len(),
AMBIENT_VIOLATION_CEILING,
format_violation_sample(&violations, 80),
);
}
#[test]
fn non_test_lines_filter_skips_cfg_test_modules() {
let source = "\
fn real_code() {
Instant::now();
}
#[cfg(test)]
mod tests {
fn test_code() {
Instant::now();
}
}
";
let lines = non_test_lines(source);
let text: Vec<&str> = lines.iter().map(|(_, l)| l.as_str()).collect();
assert!(
text.iter().any(|l| l.contains("real_code")),
"Should include production code"
);
assert!(
!text.iter().any(|l| l.contains("test_code")),
"Should exclude #[cfg(test)] module code"
);
}
#[test]
fn non_test_lines_filter_keeps_skipping_after_nested_test_functions() {
let source = "\
fn real_code() {
Instant::now();
}
#[cfg(test)]
mod tests {
#[test]
fn first_test() {
Instant::now();
}
fn helper_after_test() {
std::net::TcpListener::bind(\"127.0.0.1:0\").unwrap();
}
}
";
let lines = non_test_lines(source);
let text: Vec<&str> = lines.iter().map(|(_, l)| l.as_str()).collect();
assert!(
text.iter().any(|l| l.contains("real_code")),
"Should include production code"
);
assert!(
!text.iter().any(|l| l.contains("helper_after_test")),
"Should exclude the full #[cfg(test)] module after nested #[test] functions"
);
}
#[test]
fn non_test_lines_filter_skips_cfg_test_functions_with_split_braces() {
let source = "\
fn real_code() {
Instant::now();
}
#[cfg(test)]
fn test_code()
{
Instant::now();
}
";
let lines = non_test_lines(source);
let text: Vec<&str> = lines.iter().map(|(_, l)| l.as_str()).collect();
assert!(
text.iter().any(|l| l.contains("real_code")),
"Should include production code"
);
assert!(
!text.iter().any(|l| l.contains("test_code")),
"Should exclude #[cfg(test)] function signatures"
);
assert_eq!(
text.iter()
.filter(|line| line.contains("Instant::now"))
.count(),
1,
"Should exclude #[cfg(test)] function bodies"
);
}
#[test]
fn non_test_lines_filter_skips_cfg_test_modules_with_literal_braces() {
let source = r##"
fn real_code() {
Instant::now();
}
#[cfg(test)]
mod tests {
fn fixture_code() {
let _json = r#"{"nested": {"brace": true}}"#;
let _text = "closing brace } inside a string";
eprintln!("test-only output");
Instant::now();
}
}
"##;
let lines = non_test_lines(source);
let text: Vec<&str> = lines.iter().map(|(_, l)| l.as_str()).collect();
assert!(
text.iter().any(|line| line.contains("real_code")),
"Should include production code"
);
assert!(
!text.iter().any(|line| line.contains("fixture_code")),
"Should exclude cfg(test) modules even when fixtures contain braces"
);
assert_eq!(
text.iter()
.filter(|line| line.contains("Instant::now"))
.count(),
1,
"Should exclude test-only Instant::now after brace-heavy literals"
);
assert!(
!text.iter().any(|line| line.contains("eprintln!(")),
"Should exclude test-only output after brace-heavy literals"
);
}
#[test]
fn non_test_lines_filter_skips_comments() {
let source = "\
// Instant::now() in a comment
/// Instant::now() in a doc comment
//! Instant::now() in a module doc
let x = Instant::now();
";
let lines = non_test_lines(source);
assert_eq!(
lines
.iter()
.filter(|(_, l)| l.contains("Instant::now"))
.count(),
1,
"Should have exactly one non-comment Instant::now() line"
);
}
#[test]
fn non_test_lines_filter_skips_block_comments_and_strings() {
let source = r##"
/*
Instant::now();
eprintln!("inside block comment");
*/
fn production_code() {
let _normal = "Instant::now()";
let _multiline = "SystemTime::now()
eprintln!(\"inside multiline string\")";
let _raw = r#"eprintln!("inside raw string")"#;
let _raw_multiline = r#"
std::thread::spawn(|| ());
"#;
Instant::now();
}
"##;
let lines = non_test_lines(source);
let instant_count = lines
.iter()
.filter(|(_, line)| line.contains("Instant::now"))
.count();
let output_count = lines
.iter()
.filter(|(_, line)| line.contains("eprintln!("))
.count();
assert_eq!(
instant_count, 1,
"only executable Instant::now should remain"
);
assert_eq!(
output_count, 0,
"string/comment output patterns should be ignored"
);
}
#[test]
fn top_level_test_surfaces_are_exempt_from_scanning() {
assert!(is_exempt("real_integration_scenarios_e2e_tests.rs"));
assert!(is_exempt("raptorq_rfc6330_conformance_tests.rs"));
assert!(is_exempt("runtime/sharded_state_conformance.rs"));
assert!(is_exempt("cancel_cx_runtime_channel_metamorphic_tests.rs"));
assert!(is_exempt("runtime/scheduler/edf_priority_metamorphic.rs"));
assert!(is_exempt("grpc_server_deadline_cancel_audit.rs"));
assert!(is_exempt("integration_mutation_testing.rs"));
assert!(!is_exempt("service/runtime.rs"));
}
#[test]
fn scanner_allows_documented_test_surface_ambient_output() {
let source = r#"
fn test_helper() {
std::env::var("ASUPERSYNC_TEST_LOG").ok();
eprintln!("stage log");
}
"#;
let violations = scan_source("real_integration_scenarios_e2e_tests.rs", source);
assert!(
violations.is_empty(),
"test-surface carve-outs should not count as production ambient authority: {}",
format_violations(&violations)
);
}
#[test]
fn scanner_rejects_production_env_and_output_patterns() {
let source = r#"
fn production_path() {
let _ = std::env::var("ASUPERSYNC_RUNTIME_FLAG");
eprintln!("bypasses structured tracing");
}
"#;
let violations = scan_source("service/runtime.rs", source);
assert!(
violations
.iter()
.any(|v| v.category == AmbientCategory::Env),
"expected env access violation, got: {}",
format_violations(&violations)
);
assert!(
violations
.iter()
.any(|v| v.category == AmbientCategory::Output),
"expected output violation, got: {}",
format_violations(&violations)
);
}
#[test]
fn scanner_ignores_production_pattern_literals() {
let source = r##"
fn production_path() {
let _message = "std::env::var(\"NOPE\")";
let _raw = r#"eprintln!("stage log")"#;
/* std::thread::spawn(|| ()); */
}
"##;
let violations = scan_source("service/runtime.rs", source);
assert!(
violations.is_empty(),
"string and block-comment literals should not trip the scanner: {}",
format_violations(&violations)
);
}
#[test]
fn output_patterns_do_not_double_count_stderr_macros_as_stdout() {
let source = r#"
fn production_path() {
eprintln!("stderr");
}
"#;
let violations = scan_source("service/runtime.rs", source);
let output_patterns: Vec<_> = violations
.iter()
.filter(|v| v.category == AmbientCategory::Output)
.map(|v| v.pattern.as_str())
.collect();
assert_eq!(output_patterns, vec!["eprintln!("]);
}
#[test]
fn ambient_finding_debug_clone() {
let f = &KNOWN_FINDINGS[0];
let dbg = format!("{f:?}");
assert!(dbg.contains("AmbientFinding"), "{dbg}");
let cloned = f.clone();
assert_eq!(format!("{cloned:?}"), dbg);
}
#[test]
fn ambient_category_debug_clone_copy_hash() {
use std::collections::HashSet;
let c = AmbientCategory::Time;
let dbg = format!("{c:?}");
assert!(dbg.contains("Time"), "{dbg}");
let copied = c;
let cloned = c;
assert_eq!(copied, cloned);
let mut set = HashSet::new();
set.insert(c);
assert!(set.contains(&AmbientCategory::Time));
}
#[test]
fn severity_debug_clone_copy() {
let s = Severity::Medium;
let dbg = format!("{s:?}");
assert!(dbg.contains("Medium"), "{dbg}");
let copied = s;
let cloned = s;
assert_eq!(copied, cloned);
}
}