use oxcache::{log_cache_key, redact_cache_key, redact_connection_string, sanitize_message};
#[test]
fn test_log_cache_key_sensitive_key() {
log_cache_key("info", "Test message", "user_token_abc123");
log_cache_key("debug", "Debug log", "api_key_secret");
log_cache_key("warn", "Warning", "password_reset");
log_cache_key("error", "Error", "session_token_xyz");
}
#[test]
fn test_log_cache_key_non_sensitive() {
log_cache_key("info", "Normal key", "user_profile_123");
log_cache_key("debug", "Cache hit", "product_info_456");
}
#[test]
fn test_log_cache_key_empty() {
log_cache_key("info", "Empty key test", "");
}
#[test]
fn test_log_cache_key_unknown_level() {
log_cache_key("unknown", "Test", "normal_key");
}
#[test]
fn test_sanitize_message_with_connection_string() {
let msg = "Connection: redis://user:secret123@localhost:6379";
let sanitized = sanitize_message(msg);
assert!(!sanitized.contains("secret123"));
assert!(sanitized.contains("**"));
}
#[test]
fn test_sanitize_message_without_connection_string() {
let msg = "User logged in successfully";
let sanitized = sanitize_message(msg);
assert_eq!(sanitized, msg);
}
#[test]
fn test_sanitize_message_empty() {
let sanitized = sanitize_message("");
assert_eq!(sanitized, "");
}
#[test]
fn test_sanitize_message_multiple_connections() {
let msg = "Primary: redis://admin:pass1@host1:6379, Secondary: redis://admin:pass2@host2:6379";
let sanitized = sanitize_message(msg);
assert!(!sanitized.contains("pass1"));
}
#[test]
fn test_sanitize_message_different_protocols() {
let msg = "Database: postgresql://user:dbpass@db.example.com:5432";
let sanitized = sanitize_message(msg);
assert!(!sanitized.contains("dbpass"));
}
#[test]
fn test_sanitize_message_no_at_symbol() {
let msg = "This is a normal message without connection string";
let sanitized = sanitize_message(msg);
assert_eq!(sanitized, msg);
}
#[test]
fn test_sanitize_message_protocol_only() {
let msg = "Visit https://example.com for more info";
let sanitized = sanitize_message(msg);
assert_eq!(sanitized, msg);
}
#[test]
fn test_redact_cache_key_all_patterns() {
let sensitive_keys = [
("user_token", true),
("password_reset", true),
("secret_key", true),
("api_key_value", true),
("apikey_data", true),
("auth_session", true),
("credential_store", true),
("session_id", true),
("cookie_value", true),
("jwt_token", true),
("normal_key", false),
("user_profile", false),
("cache_item", false),
];
for (key, is_sensitive) in sensitive_keys {
let result = redact_cache_key(key);
if is_sensitive {
assert!(
result.starts_with("****"),
"Key '{}' should be redacted, got '{}'",
key,
result
);
} else {
assert_eq!(result, key, "Key '{}' should not be redacted, got '{}'", key, result);
}
}
}
#[test]
fn test_redact_connection_string_edge_cases() {
assert_eq!(redact_connection_string("redis://localhost"), "redis://localhost");
let result = redact_connection_string("redis://user:pass:word@host:6379");
assert!(!result.contains("pass:word"));
assert!(result.contains("****"));
}
#[test]
fn test_secure_info_macro_compiles() {
let _msg = "Test message for secure_info";
}
#[test]
fn test_secure_debug_macro_compiles() {
let _msg = "Test message for secure_debug";
}
#[test]
fn test_sanitize_message_very_long() {
let long_msg = format!("Connection: redis://user:{}@localhost:6379", "a".repeat(10000));
let _sanitized = sanitize_message(&long_msg);
}
#[test]
fn test_sanitize_message_unicode() {
let msg = "连接: redis://用户:密码@本地主机:6379";
let sanitized = sanitize_message(msg);
assert!(!sanitized.is_empty());
}
#[test]
fn test_sanitize_message_special_chars() {
let msg = "Conn: redis://user:p@ss!w0rd#@host:6379";
let sanitized = sanitize_message(msg);
assert!(!sanitized.is_empty());
}