feat: improve test coverage and fix critical crypto bug - Translate all Chinese comments to English in utils/ip.rs and config/constants/app.rs - Add comprehensive test suite for crypto/encdec/id.rs module (14 new tests) - Fix critical bug in Argon2 key generation that was returning all-zero keys - Improve test coverage for IP utilities and configuration constants - Ensure all test cases follow English naming conventions and meaningful descriptions

This commit is contained in:
overtrue
2025-05-25 13:53:59 +08:00
parent e838f4292c
commit bc398ccf1b
4 changed files with 265 additions and 55 deletions
+25 -25
View File
@@ -96,7 +96,7 @@ mod tests {
#[test]
fn test_app_basic_constants() {
// 测试应用基本常量
// Test application basic constants
assert_eq!(APP_NAME, "RustFs");
assert!(!APP_NAME.is_empty(), "App name should not be empty");
assert!(!APP_NAME.contains(' '), "App name should not contain spaces");
@@ -110,7 +110,7 @@ mod tests {
#[test]
fn test_logging_constants() {
// 测试日志相关常量
// Test logging related constants
assert_eq!(DEFAULT_LOG_LEVEL, "info");
assert!(["trace", "debug", "info", "warn", "error"].contains(&DEFAULT_LOG_LEVEL),
"Log level should be a valid tracing level");
@@ -127,7 +127,7 @@ mod tests {
#[test]
fn test_environment_constants() {
// 测试环境相关常量
// Test environment related constants
assert_eq!(ENVIRONMENT, "production");
assert!(["development", "staging", "production", "test"].contains(&ENVIRONMENT),
"Environment should be a standard environment name");
@@ -135,7 +135,7 @@ mod tests {
#[test]
fn test_connection_constants() {
// 测试连接相关常量
// Test connection related constants
assert_eq!(MAX_CONNECTIONS, 100);
assert!(MAX_CONNECTIONS > 0, "Max connections should be positive");
assert!(MAX_CONNECTIONS <= 10000, "Max connections should be reasonable");
@@ -147,7 +147,7 @@ mod tests {
#[test]
fn test_security_constants() {
// 测试安全相关常量
// Test security related constants
assert_eq!(DEFAULT_ACCESS_KEY, "rustfsadmin");
assert!(!DEFAULT_ACCESS_KEY.is_empty(), "Access key should not be empty");
assert!(DEFAULT_ACCESS_KEY.len() >= 8, "Access key should be at least 8 characters");
@@ -156,14 +156,14 @@ mod tests {
assert!(!DEFAULT_SECRET_KEY.is_empty(), "Secret key should not be empty");
assert!(DEFAULT_SECRET_KEY.len() >= 8, "Secret key should be at least 8 characters");
// 在生产环境中,访问密钥和秘密密钥应该不同
// 这里是默认值,所以相同是可以接受的,但应该在文档中警告
// In production environment, access key and secret key should be different
// These are default values, so being the same is acceptable, but should be warned in documentation
println!("Warning: Default access key and secret key are the same. Change them in production!");
}
#[test]
fn test_file_path_constants() {
// 测试文件路径相关常量
// Test file path related constants
assert_eq!(DEFAULT_OBS_CONFIG, "./deploy/config/obs.toml");
assert!(DEFAULT_OBS_CONFIG.ends_with(".toml"), "Config file should be TOML format");
assert!(!DEFAULT_OBS_CONFIG.is_empty(), "Config path should not be empty");
@@ -177,14 +177,14 @@ mod tests {
#[test]
fn test_port_constants() {
// 测试端口相关常量
// Test port related constants
assert_eq!(DEFAULT_PORT, 9000);
assert!(DEFAULT_PORT > 1024, "Default port should be above reserved range");
// u16类型自动保证端口在有效范围内(0-65535)
// u16 type automatically ensures port is in valid range (0-65535)
assert_eq!(DEFAULT_CONSOLE_PORT, 9002);
assert!(DEFAULT_CONSOLE_PORT > 1024, "Console port should be above reserved range");
// u16类型自动保证端口在有效范围内(0-65535)
// u16 type automatically ensures port is in valid range (0-65535)
assert_ne!(DEFAULT_PORT, DEFAULT_CONSOLE_PORT,
"Main port and console port should be different");
@@ -192,7 +192,7 @@ mod tests {
#[test]
fn test_address_constants() {
// 测试地址相关常量
// Test address related constants
assert_eq!(DEFAULT_ADDRESS, ":9000");
assert!(DEFAULT_ADDRESS.starts_with(':'), "Address should start with colon");
assert!(DEFAULT_ADDRESS.contains(&DEFAULT_PORT.to_string()),
@@ -209,7 +209,7 @@ mod tests {
#[test]
fn test_const_str_concat_functionality() {
// 测试const_str::concat宏的功能
// Test const_str::concat macro functionality
let expected_address = format!(":{}", DEFAULT_PORT);
assert_eq!(DEFAULT_ADDRESS, expected_address);
@@ -219,7 +219,7 @@ mod tests {
#[test]
fn test_string_constants_validity() {
// 测试字符串常量的有效性
// Test validity of string constants
let string_constants = [
APP_NAME,
VERSION,
@@ -244,7 +244,7 @@ mod tests {
#[test]
fn test_numeric_constants_validity() {
// 测试数值常量的有效性
// Test validity of numeric constants
assert!(SAMPLE_RATIO.is_finite(), "Sample ratio should be finite");
assert!(!SAMPLE_RATIO.is_nan(), "Sample ratio should not be NaN");
@@ -258,42 +258,42 @@ mod tests {
#[test]
fn test_security_best_practices() {
// 测试安全最佳实践
// Test security best practices
// 这些是默认值,在生产环境中应该被更改
// These are default values, should be changed in production environments
println!("Security Warning: Default credentials detected!");
println!("Access Key: {}", DEFAULT_ACCESS_KEY);
println!("Secret Key: {}", DEFAULT_SECRET_KEY);
println!("These should be changed in production environments!");
// 验证密钥长度符合最低安全要求
// Verify that key lengths meet minimum security requirements
assert!(DEFAULT_ACCESS_KEY.len() >= 8, "Access key should be at least 8 characters");
assert!(DEFAULT_SECRET_KEY.len() >= 8, "Secret key should be at least 8 characters");
// 检查默认凭据是否包含常见的不安全模式
// Check if default credentials contain common insecure patterns
let _insecure_patterns = ["admin", "password", "123456", "default"];
let _access_key_lower = DEFAULT_ACCESS_KEY.to_lowercase();
let _secret_key_lower = DEFAULT_SECRET_KEY.to_lowercase();
// 注意:这里可以添加更多的安全检查逻辑
// 例如检查密钥是否包含不安全的模式
// Note: More security check logic can be added here
// For example, check if keys contain insecure patterns
}
#[test]
fn test_configuration_consistency() {
// 测试配置的一致性
// Test configuration consistency
// 版本一致性
// Version consistency
assert_eq!(VERSION, SERVICE_VERSION, "Application version should match service version");
// 端口不冲突
// Port conflict check
let ports = [DEFAULT_PORT, DEFAULT_CONSOLE_PORT];
let mut unique_ports = std::collections::HashSet::new();
for port in &ports {
assert!(unique_ports.insert(port), "Port {} is duplicated", port);
}
// 地址格式一致性
// Address format consistency
assert_eq!(DEFAULT_ADDRESS, format!(":{}", DEFAULT_PORT));
assert_eq!(DEFAULT_CONSOLE_ADDRESS, format!(":{}", DEFAULT_CONSOLE_PORT));
}