use super::*;
use crate::testing::acquire_test_state_lock;
#[test]
fn pii_test_custom_secret_patterns_match_strings() {
let _guard = crate::testing::acquire_test_state_lock();
reset_secret_patterns_for_tests();
register_secret_pattern(
"custom-secret",
Regex::new("ULTRA_CUSTOM_SECRET::ALPHA\\|BETA\\|2026").expect("regex must compile"),
);
assert_eq!(
redact_if_secret("prefix-ULTRA_CUSTOM_SECRET::ALPHA|BETA|2026-suffix"),
Some("***".to_string()),
);
assert_eq!(
redact_if_secret("benign descriptive text that is intentionally long enough"),
None,
);
reset_secret_patterns_for_tests();
}
#[test]
fn pii_test_max_depth_one_leaves_nested_values_untouched() {
let _guard = acquire_test_state_lock();
replace_pii_rules(Vec::new());
let payload = serde_json::json!({
"outer": {
"password": "secret-value",
"nested": { "token": "abc123" }
}
});
let result = sanitize_payload(&payload, true, 1);
assert_eq!(result, payload);
replace_pii_rules(Vec::new());
}
#[test]
fn pii_test_max_depth_boundary_keeps_array_elements_untouched() {
let _guard = acquire_test_state_lock();
replace_pii_rules(Vec::new());
let payload = serde_json::json!({
"users": [
{ "password": "secret-one" },
{ "token": "secret-two" }
]
});
let result = sanitize_payload(&payload, true, 2);
assert_eq!(result, payload);
replace_pii_rules(Vec::new());
}
#[test]
fn pii_test_a_span_with_too_few_segments_is_not_path_shaped() {
assert!(!looks_like_path("usr/local"));
}
#[test]
fn pii_test_a_span_of_long_wordless_segments_is_not_path_shaped() {
assert!(!looks_like_path("ABCDEFGHIJ/1234567890/KLMNOPQRST"));
}
#[test]
fn pii_test_a_span_of_short_lowercase_words_is_path_shaped() {
assert!(looks_like_path("usr/local/lib"));
}
#[test]
fn pii_test_a_path_with_exactly_half_wordy_segments_is_path_shaped() {
assert!(looks_like_path("usr/local/AB12/CD34"));
}
#[test]
fn pii_test_a_path_with_one_wordy_segment_in_three_is_not_path_shaped() {
assert!(!looks_like_path("usr/AB12/CD34"));
}
#[test]
fn pii_test_a_token_matched_by_two_patterns_is_redacted_once() {
let _guard = crate::testing::acquire_test_state_lock();
reset_secret_patterns_for_tests();
let token = "AKIAIOSFODNN7EXAMPLE0123456789abcdef0123456789abcdef";
assert_eq!(redact_if_secret(token), Some("***".to_string()));
assert_eq!(
redact_if_secret(&format!("a {token} b")),
Some("a *** b".to_string())
);
}
#[test]
fn pii_test_spans_found_out_of_order_are_all_redacted() {
let _guard = crate::testing::acquire_test_state_lock();
reset_secret_patterns_for_tests();
let hex = "a".repeat(44);
let aws = "AKIAIOSFODNN7EXAMPLE";
assert_eq!(
redact_if_secret(&format!("{hex} {aws}")),
Some("*** ***".to_string())
);
}
#[test]
fn pii_test_a_credential_glued_to_a_prefix_is_removed_whole() {
let _guard = crate::testing::acquire_test_state_lock();
reset_secret_patterns_for_tests();
let secret = "AKIAIOSFODNN7EXAMPLE";
assert_eq!(
redact_if_secret(&format!("abcde{secret} tail")),
Some("*** tail".to_string())
);
}