use std::path::PathBuf;
use reqwest::Client;
use crate::config::constants::DEFAULT_USER_AGENT;
pub use domain_status_cli::{FailOn, LogFormat, LogLevel};
#[must_use]
pub fn log_level_filter(level: &LogLevel) -> log::LevelFilter {
match level {
LogLevel::Error => log::LevelFilter::Error,
LogLevel::Warn => log::LevelFilter::Warn,
LogLevel::Info => log::LevelFilter::Info,
LogLevel::Debug => log::LevelFilter::Debug,
LogLevel::Trace => log::LevelFilter::Trace,
}
}
#[derive(Clone, Debug, Default)]
pub struct ScanDependencyOverrides {
pub http_client: Option<Client>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ConfigValidationError {
pub field: String,
pub message: String,
}
impl std::fmt::Display for ConfigValidationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Invalid config '{}': {}", self.field, self.message)
}
}
impl std::error::Error for ConfigValidationError {}
#[derive(Clone)]
pub struct Config {
pub file: PathBuf,
pub log_level: LogLevel,
#[doc(hidden)]
pub log_level_filter_override: Option<log::LevelFilter>,
pub log_format: LogFormat,
pub db_path: PathBuf,
pub max_concurrency: usize,
pub timeout_seconds: u64,
pub user_agent: String,
pub rate_limit_rps: u32,
pub fingerprints: Option<String>,
pub geoip: Option<String>,
pub status_port: Option<u16>,
pub enable_whois: bool,
pub cache_dir: Option<PathBuf>,
pub scan_external_scripts: bool,
pub fail_on: FailOn,
pub fail_on_pct_threshold: u8,
pub log_file: Option<PathBuf>,
#[doc(hidden)]
#[allow(clippy::type_complexity)]
pub progress_callback: Option<std::sync::Arc<dyn Fn(usize, usize, usize, usize) + Send + Sync>>,
#[doc(hidden)]
pub dependency_overrides: Option<ScanDependencyOverrides>,
#[doc(hidden)]
pub allow_localhost_for_tests: bool,
pub drain_timeout_secs: u64,
pub allow_degraded_fingerprints: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
file: PathBuf::from("urls.txt"),
log_level: LogLevel::Info,
log_level_filter_override: None,
log_format: LogFormat::Plain,
db_path: PathBuf::from("./domain_status.db"),
max_concurrency: 30,
timeout_seconds: 10,
user_agent: DEFAULT_USER_AGENT.to_string(),
rate_limit_rps: 15,
fingerprints: None,
geoip: None,
status_port: None,
enable_whois: true,
cache_dir: None,
scan_external_scripts: false,
fail_on: FailOn::Never,
fail_on_pct_threshold: 10,
log_file: None,
progress_callback: None,
dependency_overrides: None,
allow_localhost_for_tests: false,
drain_timeout_secs: 10,
allow_degraded_fingerprints: false,
}
}
}
impl std::fmt::Debug for Config {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Config")
.field("file", &self.file)
.field("log_level", &self.log_level)
.field("log_level_filter_override", &self.log_level_filter_override)
.field("log_format", &self.log_format)
.field("db_path", &self.db_path)
.field("max_concurrency", &self.max_concurrency)
.field("timeout_seconds", &self.timeout_seconds)
.field("user_agent", &self.user_agent)
.field("rate_limit_rps", &self.rate_limit_rps)
.field("fingerprints", &self.fingerprints)
.field("geoip", &self.geoip)
.field("status_port", &self.status_port)
.field("enable_whois", &self.enable_whois)
.field("cache_dir", &self.cache_dir)
.field("scan_external_scripts", &self.scan_external_scripts)
.field("fail_on", &self.fail_on)
.field("fail_on_pct_threshold", &self.fail_on_pct_threshold)
.field("log_file", &self.log_file)
.field(
"progress_callback",
&self.progress_callback.as_ref().map(|_| "<callback>"),
)
.field(
"dependency_overrides",
&self.dependency_overrides.as_ref().map(|_| "<overrides>"),
)
.field("allow_localhost_for_tests", &self.allow_localhost_for_tests)
.field("drain_timeout_secs", &self.drain_timeout_secs)
.field(
"allow_degraded_fingerprints",
&self.allow_degraded_fingerprints,
)
.finish()
}
}
impl Config {
pub const MAX_CONCURRENCY: usize = 500;
pub const MAX_RATE_LIMIT_RPS: u32 = 100;
pub fn validate(&self) -> Result<(), ConfigValidationError> {
if self.max_concurrency == 0 {
return Err(ConfigValidationError {
field: "max_concurrency".to_string(),
message: "must be greater than 0".to_string(),
});
}
if self.max_concurrency > Self::MAX_CONCURRENCY {
return Err(ConfigValidationError {
field: "max_concurrency".to_string(),
message: format!(
"must be <= {} to prevent resource exhaustion",
Self::MAX_CONCURRENCY
),
});
}
if self.rate_limit_rps > Self::MAX_RATE_LIMIT_RPS {
return Err(ConfigValidationError {
field: "rate_limit_rps".to_string(),
message: format!(
"must be <= {} to prevent overwhelming targets",
Self::MAX_RATE_LIMIT_RPS
),
});
}
if self.timeout_seconds == 0 {
return Err(ConfigValidationError {
field: "timeout_seconds".to_string(),
message: "must be greater than 0".to_string(),
});
}
if self.fail_on_pct_threshold > 100 {
return Err(ConfigValidationError {
field: "fail_on_pct_threshold".to_string(),
message: "must be between 0 and 100".to_string(),
});
}
if self.user_agent.trim().is_empty() {
return Err(ConfigValidationError {
field: "user_agent".to_string(),
message: "cannot be empty".to_string(),
});
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_log_level_conversion() {
assert_eq!(
crate::config::log_level_filter(&LogLevel::Error),
log::LevelFilter::Error
);
assert_eq!(
crate::config::log_level_filter(&LogLevel::Warn),
log::LevelFilter::Warn
);
assert_eq!(
crate::config::log_level_filter(&LogLevel::Info),
log::LevelFilter::Info
);
assert_eq!(
crate::config::log_level_filter(&LogLevel::Debug),
log::LevelFilter::Debug
);
assert_eq!(
crate::config::log_level_filter(&LogLevel::Trace),
log::LevelFilter::Trace
);
}
#[test]
fn test_log_level_ordering() {
let error = crate::config::log_level_filter(&LogLevel::Error);
let warn = crate::config::log_level_filter(&LogLevel::Warn);
let info = crate::config::log_level_filter(&LogLevel::Info);
let debug = crate::config::log_level_filter(&LogLevel::Debug);
let trace = crate::config::log_level_filter(&LogLevel::Trace);
assert!(error < warn);
assert!(warn < info);
assert!(info < debug);
assert!(debug < trace);
}
#[test]
fn test_config_default_values() {
let config = Config::default();
assert_eq!(config.max_concurrency, 30);
assert_eq!(config.timeout_seconds, 10);
assert_eq!(config.rate_limit_rps, 15);
assert_eq!(config.fail_on, FailOn::Never);
assert_eq!(config.fail_on_pct_threshold, 10);
assert!(config.enable_whois);
assert!(config.fingerprints.is_none());
assert!(config.geoip.is_none());
assert!(config.status_port.is_none());
}
#[test]
fn test_config_default_paths() {
let config = Config::default();
assert_eq!(config.file, PathBuf::from("urls.txt"));
assert_eq!(config.db_path, PathBuf::from("./domain_status.db"));
}
#[test]
fn test_config_default_user_agent() {
let config = Config::default();
assert!(!config.user_agent.is_empty());
assert!(config.user_agent.contains("Mozilla"));
}
#[test]
fn test_fail_on_variants() {
assert_ne!(FailOn::Never, FailOn::AnyFailure);
assert_ne!(FailOn::AnyFailure, FailOn::PctGreaterThan);
assert_ne!(FailOn::PctGreaterThan, FailOn::Never);
}
#[test]
fn test_config_validate_default_is_valid() {
let config = Config::default();
assert!(config.validate().is_ok());
}
#[test]
fn test_config_validate_max_concurrency_zero() {
let config = Config {
max_concurrency: 0,
..Default::default()
};
let err = config.validate().unwrap_err();
assert_eq!(err.field, "max_concurrency");
assert!(err.message.contains("greater than 0"));
}
#[test]
fn test_config_validate_max_concurrency_too_high() {
let config = Config {
max_concurrency: Config::MAX_CONCURRENCY + 1,
..Default::default()
};
let err = config.validate().unwrap_err();
assert_eq!(err.field, "max_concurrency");
assert!(err.message.contains("resource exhaustion"));
}
#[test]
fn test_config_validate_max_concurrency_at_limit() {
let config = Config {
max_concurrency: Config::MAX_CONCURRENCY,
..Default::default()
};
assert!(config.validate().is_ok());
}
#[test]
fn test_config_validate_rate_limit_zero_is_valid() {
let config = Config {
rate_limit_rps: 0,
..Default::default()
};
assert!(config.validate().is_ok());
}
#[test]
fn test_config_validate_rate_limit_too_high() {
let config = Config {
rate_limit_rps: Config::MAX_RATE_LIMIT_RPS + 1,
..Default::default()
};
let err = config.validate().unwrap_err();
assert_eq!(err.field, "rate_limit_rps");
assert!(err.message.contains("overwhelming"));
}
#[test]
fn test_config_validate_rate_limit_at_limit() {
let config = Config {
rate_limit_rps: Config::MAX_RATE_LIMIT_RPS,
..Default::default()
};
assert!(config.validate().is_ok());
}
#[test]
fn test_config_validate_timeout_zero() {
let config = Config {
timeout_seconds: 0,
..Default::default()
};
let err = config.validate().unwrap_err();
assert_eq!(err.field, "timeout_seconds");
assert!(err.message.contains("greater than 0"));
}
#[test]
fn test_config_validate_fail_on_pct_threshold_over_100() {
let config = Config {
fail_on_pct_threshold: 101,
..Default::default()
};
let err = config.validate().unwrap_err();
assert_eq!(err.field, "fail_on_pct_threshold");
assert!(err.message.contains("between 0 and 100"));
}
#[test]
fn test_config_validate_fail_on_pct_threshold_boundary() {
let config_zero = Config {
fail_on_pct_threshold: 0,
..Default::default()
};
assert!(config_zero.validate().is_ok());
let config_hundred = Config {
fail_on_pct_threshold: 100,
..Default::default()
};
assert!(config_hundred.validate().is_ok());
}
#[test]
fn test_config_validate_empty_user_agent() {
let config = Config {
user_agent: "".to_string(),
..Default::default()
};
let err = config.validate().unwrap_err();
assert_eq!(err.field, "user_agent");
assert!(err.message.contains("empty"));
}
#[test]
fn test_config_validate_whitespace_user_agent() {
let config = Config {
user_agent: " ".to_string(),
..Default::default()
};
let err = config.validate().unwrap_err();
assert_eq!(err.field, "user_agent");
}
#[test]
fn test_config_validation_error_display() {
let err = ConfigValidationError {
field: "test_field".to_string(),
message: "test message".to_string(),
};
let display = format!("{}", err);
assert!(display.contains("test_field"));
assert!(display.contains("test message"));
}
#[test]
fn test_config_validation_error_error_trait() {
let err = ConfigValidationError {
field: "test_field".to_string(),
message: "test message".to_string(),
};
let error_ref: &dyn std::error::Error = &err;
assert_eq!(
error_ref.to_string(),
"Invalid config 'test_field': test message"
);
}
#[test]
fn test_config_debug_formatting() {
let config = Config {
max_concurrency: 50,
rate_limit_rps: 25,
enable_whois: true,
cache_dir: None,
status_port: Some(8080),
..Default::default()
};
let debug_str = format!("{:?}", config);
assert!(debug_str.contains("Config"));
assert!(debug_str.contains("max_concurrency"));
assert!(debug_str.contains("50"));
assert!(
debug_str.contains("progress_callback: None"),
"unset callback must debug as None, got: {debug_str}"
);
assert!(
!debug_str.contains("<callback>"),
"unset callback must not render as <callback>, got: {debug_str}"
);
}
#[test]
fn test_config_debug_with_callback() {
use std::sync::Arc;
let callback =
Arc::new(|_completed: usize, _failed: usize, _skipped: usize, _total: usize| {});
let config = Config {
progress_callback: Some(callback),
..Default::default()
};
let debug_str = format!("{:?}", config);
assert!(
debug_str.contains("<callback>"),
"set callback must render as <callback>, got: {debug_str}"
);
assert!(
!debug_str.contains("0x"),
"set callback must not expose a function pointer, got: {debug_str}"
);
}
#[test]
fn test_config_validate_all_fields_valid() {
let config = Config {
max_concurrency: Config::MAX_CONCURRENCY,
rate_limit_rps: Config::MAX_RATE_LIMIT_RPS,
timeout_seconds: 1,
fail_on_pct_threshold: 100,
user_agent: "Valid User Agent".to_string(),
..Default::default()
};
assert!(
config.validate().is_ok(),
"All valid boundary values should pass"
);
}
#[test]
fn test_config_validate_multiple_errors_first_one_returned() {
let config = Config {
max_concurrency: 0, timeout_seconds: 0, user_agent: "".to_string(), ..Default::default()
};
let err = config.validate().unwrap_err();
assert_eq!(err.field, "max_concurrency");
}
use proptest::prelude::*;
proptest! {
#[test]
fn test_config_validation_concurrency(n in 1u32..=500) {
let config = Config {
max_concurrency: n as usize,
..Default::default()
};
prop_assert!(config.validate().is_ok(),
"Concurrency 1-500 should be valid");
}
#[test]
fn test_config_validation_concurrency_invalid(n in 501u32..1000) {
let config = Config {
max_concurrency: n as usize,
..Default::default()
};
prop_assert!(config.validate().is_err(),
"Concurrency > 500 should be invalid");
}
#[test]
fn test_config_validation_rate_limit(n in 0u32..=100) {
let config = Config {
rate_limit_rps: n,
..Default::default()
};
prop_assert!(config.validate().is_ok(),
"Rate limit 0-100 should be valid");
}
#[test]
fn test_config_validation_rate_limit_invalid(n in 101u32..1000) {
let config = Config {
rate_limit_rps: n,
..Default::default()
};
prop_assert!(config.validate().is_err(),
"Rate limit > 100 should be invalid");
}
#[test]
fn test_config_validation_fail_on_pct(pct in 0u8..=100) {
let config = Config {
fail_on_pct_threshold: pct,
..Default::default()
};
prop_assert!(config.validate().is_ok(),
"Fail on pct 0-100 should be valid");
}
#[test]
fn test_config_validation_fail_on_pct_invalid(pct in 101u8..=255) {
let config = Config {
fail_on_pct_threshold: pct,
..Default::default()
};
prop_assert!(config.validate().is_err(),
"Fail on pct > 100 should be invalid");
}
#[test]
fn test_config_validation_timeout(timeout in 1u64..3600) {
let config = Config {
timeout_seconds: timeout,
..Default::default()
};
prop_assert!(config.validate().is_ok(),
"Timeout >= 1 should be valid");
}
#[test]
fn test_config_validation_user_agent_nonempty(agent in "[a-zA-Z0-9]+( [a-zA-Z0-9]+){0,10}") {
let config = Config {
user_agent: agent.clone(),
..Default::default()
};
if !agent.trim().is_empty() {
prop_assert!(config.validate().is_ok(),
"Non-empty user agent should be valid");
}
}
}
}