use oxcache::{redact_cache_key, redact_connection_string, redact_field, redact_value, Redacted};
#[test]
fn test_redact_value_normal_string() {
assert_eq!(redact_value("password123", 3), "****123");
assert_eq!(redact_value("longpassword", 5), "****sword");
assert_eq!(redact_value("secret_key_value", 4), "****alue");
}
#[test]
fn test_redact_value_short_string() {
assert_eq!(redact_value("abc", 4), "***");
assert_eq!(redact_value("test", 4), "****");
assert_eq!(redact_value("a", 4), "*");
assert_eq!(redact_value("ab", 4), "**");
}
#[test]
fn test_redact_value_empty_string() {
assert_eq!(redact_value("", 4), "");
assert_eq!(redact_value("", 0), "");
}
#[test]
fn test_redact_value_zero_visible() {
assert_eq!(redact_value("password", 0), "****");
assert_eq!(redact_value("test", 0), "****");
}
#[test]
fn test_redact_value_unicode() {
assert_eq!(redact_value("密码abc123", 3), "****123");
assert_eq!(redact_value("hello世界test", 4), "****test");
}
#[test]
fn test_redact_value_long_string() {
let long_value = "a".repeat(10000);
let result = redact_value(&long_value, 4);
assert!(result.starts_with("****"));
assert!(result.ends_with("aaaa"));
assert_eq!(result.len(), 8); }
#[test]
fn test_redact_connection_string_with_password() {
assert_eq!(
redact_connection_string("redis://:mypassword@localhost:6379"),
"redis://:****@localhost:6379"
);
assert_eq!(
redact_connection_string("redis://user:mypassword@localhost:6379"),
"redis://user:****@localhost:6379"
);
}
#[test]
fn test_redact_connection_string_without_password() {
assert_eq!(
redact_connection_string("redis://user@localhost:6379"),
"redis://user:****@localhost:6379"
);
assert_eq!(
redact_connection_string("redis://localhost:6379"),
"redis://localhost:6379"
);
}
#[test]
fn test_redact_connection_string_different_protocols() {
assert_eq!(
redact_connection_string("mysql://admin:secret123@db.example.com:3306"),
"mysql://admin:****@db.example.com:3306"
);
assert_eq!(
redact_connection_string("postgresql://user:pass@localhost:5432"),
"postgresql://user:****@localhost:5432"
);
}
#[test]
fn test_redact_connection_string_no_auth() {
assert_eq!(
redact_connection_string("redis://localhost:6379"),
"redis://localhost:6379"
);
assert_eq!(
redact_connection_string("http://example.com/api"),
"http://example.com/api"
);
}
#[test]
fn test_redact_connection_string_complex_password() {
let result = redact_connection_string("redis://user:p@ss:w0rd@localhost:6379");
assert!(result.contains("@localhost:6379"));
assert!(result.contains("****"));
}
#[test]
fn test_redact_connection_string_empty() {
assert_eq!(redact_connection_string(""), "");
}
#[test]
fn test_redact_cache_key_sensitive() {
assert_eq!(redact_cache_key("user_token_abc123"), "****c123");
assert_eq!(redact_cache_key("access_token_xyz"), "****_xyz");
assert_eq!(redact_cache_key("password_reset_key"), "****_key");
assert_eq!(redact_cache_key("secret_api_data"), "****data");
assert_eq!(redact_cache_key("api_key_value"), "****alue");
assert_eq!(redact_cache_key("session_user_123"), "****_123");
}
#[test]
fn test_redact_cache_key_non_sensitive() {
assert_eq!(redact_cache_key("user_profile_123"), "user_profile_123");
assert_eq!(redact_cache_key("cache_data_item"), "cache_data_item");
assert_eq!(redact_cache_key("product_info_456"), "product_info_456");
}
#[test]
fn test_redact_cache_key_long_key() {
let long_key = "a".repeat(150);
let result = redact_cache_key(&long_key);
assert_eq!(result.len(), 100);
assert!(result.ends_with("..."));
}
#[test]
fn test_redact_cache_key_exact_100_chars() {
let key = "a".repeat(100);
let result = redact_cache_key(&key);
assert_eq!(result.len(), 100);
assert!(!result.ends_with("..."));
}
#[test]
fn test_redact_cache_key_case_insensitive() {
let result1 = redact_cache_key("USER_TOKEN_123");
assert!(result1.starts_with("****"));
let result2 = redact_cache_key("Api_Key_Data");
assert!(result2.starts_with("****"));
let result3 = redact_cache_key("SECRET_VALUE");
assert!(result3.starts_with("****"));
}
#[test]
fn test_redact_field_sensitive() {
assert_eq!(redact_field("password", "secret123"), "****t123");
assert_eq!(redact_field("api_key", "abc123xyz"), "****3xyz");
assert_eq!(redact_field("access_token", "token_value"), "****alue");
assert_eq!(redact_field("private_key", "key_data"), "****data");
}
#[test]
fn test_redact_field_non_sensitive() {
assert_eq!(redact_field("username", "john_doe"), "john_doe");
assert_eq!(redact_field("email", "user@example.com"), "user@example.com");
assert_eq!(redact_field("name", "Alice"), "Alice");
}
#[test]
fn test_redact_field_partial_match() {
assert_eq!(redact_field("user_password", "pass123"), "****s123");
assert_eq!(redact_field("auth_token_value", "xyz"), "***");
}
#[test]
fn test_redacted_wrapper() {
let redacted = Redacted::new("secret_value");
assert_eq!(redacted.to_string(), "****alue");
}
#[test]
fn test_redacted_wrapper_custom_visible() {
let redacted = Redacted::new("secret_value").with_visible_chars(6);
assert_eq!(redacted.to_string(), "****_value");
let redacted = Redacted::new("password123").with_visible_chars(0);
assert_eq!(redacted.to_string(), "****");
}
#[test]
fn test_redacted_debug() {
let redacted = Redacted::new("test_value");
let debug_str = format!("{:?}", redacted);
assert!(debug_str.starts_with('"'));
assert!(debug_str.ends_with('"'));
}
#[test]
fn test_redacted_short_value() {
let redacted = Redacted::new("ab");
assert_eq!(redacted.to_string(), "**");
}