Add tests for config error reporting and multi-vhost startup

Test that the server correctly reports missing certificate errors,
rejects invalid hostnames, fails gracefully on port conflicts, and
starts successfully with multiple virtual hosts configured.
This commit is contained in:
Jeena 2026-03-05 20:30:19 +09:00
parent 55fe47b172
commit 7de660dbb6
8 changed files with 94 additions and 392 deletions

View file

@ -39,6 +39,7 @@ key = "{}"
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.env("RUST_LOG", "error")
.output()
.unwrap();
@ -72,6 +73,7 @@ key = "{}"
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.env("RUST_LOG", "error")
.output()
.unwrap();
@ -84,150 +86,6 @@ key = "{}"
assert!(stderr.contains("Generate or obtain TLS certificates for your domain"));
}
#[test]
fn test_valid_config_startup() {
let temp_dir = common::setup_test_environment();
let port = 1967 + (std::process::id() % 1000) as u16;
let config_path = temp_dir.path().join("config.toml");
let config_content = format!(
r#"
bind_host = "127.0.0.1"
port = {}
["localhost"]
root = "{}"
cert = "{}"
key = "{}"
"#,
port,
temp_dir.path().join("content").display(),
temp_dir.path().join("cert.pem").display(),
temp_dir.path().join("key.pem").display()
);
std::fs::write(&config_path, config_content).unwrap();
let mut server_process = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.spawn()
.unwrap();
// Wait for server to start
std::thread::sleep(std::time::Duration::from_millis(500));
// Check server is still running (didn't exit with error)
assert!(
server_process.try_wait().unwrap().is_none(),
"Server should still be running with valid config"
);
// Kill server
server_process.kill().unwrap();
}
#[test]
fn test_valid_multiple_hosts_startup() {
let temp_dir = common::setup_test_environment();
let port = 1965 + (std::process::id() % 1000) as u16;
// Create host directories
std::fs::create_dir(temp_dir.path().join("host1")).unwrap();
std::fs::create_dir(temp_dir.path().join("host2")).unwrap();
// Generate certificates for both hosts
let cert1_path = temp_dir.path().join("host1_cert.pem");
let key1_path = temp_dir.path().join("host1_key.pem");
let cert2_path = temp_dir.path().join("host2_cert.pem");
let key2_path = temp_dir.path().join("host2_key.pem");
// Generate certificate for host1
let cert_result1 = std::process::Command::new("openssl")
.args(&[
"req",
"-x509",
"-newkey",
"rsa:2048",
"-keyout",
&key1_path.to_string_lossy(),
"-out",
&cert1_path.to_string_lossy(),
"-days",
"1",
"-nodes",
"-subj",
"/CN=host1.com",
])
.output();
// Generate certificate for host2
let cert_result2 = std::process::Command::new("openssl")
.args(&[
"req",
"-x509",
"-newkey",
"rsa:2048",
"-keyout",
&key2_path.to_string_lossy(),
"-out",
&cert2_path.to_string_lossy(),
"-days",
"1",
"-nodes",
"-subj",
"/CN=host2.com",
])
.output();
if cert_result1.is_err() || cert_result2.is_err() {
panic!("Failed to generate test certificates for multiple hosts test");
}
let config_path = temp_dir.path().join("config.toml");
let config_content = format!(
r#"
bind_host = "127.0.0.1"
port = {}
["host1.com"]
root = "{}"
cert = "{}"
key = "{}"
["host2.com"]
root = "{}"
cert = "{}"
key = "{}"
"#,
port,
temp_dir.path().join("host1").display(),
cert1_path.display(),
key1_path.display(),
temp_dir.path().join("host2").display(),
cert2_path.display(),
key2_path.display()
);
std::fs::write(&config_path, config_content).unwrap();
let mut server_process = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.spawn()
.unwrap();
// Wait for server to start
std::thread::sleep(std::time::Duration::from_millis(500));
// Check server is still running (didn't exit with error)
assert!(
server_process.try_wait().unwrap().is_none(),
"Server should start with valid multiple host config"
);
// Kill server
server_process.kill().unwrap();
}
#[test]
fn test_multiple_hosts_missing_certificate() {
let temp_dir = common::setup_test_environment();
@ -237,7 +95,7 @@ fn test_multiple_hosts_missing_certificate() {
std::fs::create_dir(temp_dir.path().join("host1")).unwrap();
std::fs::create_dir(temp_dir.path().join("host2")).unwrap();
// Generate certificate for only one host
// Generate certificate for only host1
let cert1_path = temp_dir.path().join("host1_cert.pem");
let key1_path = temp_dir.path().join("host1_key.pem");
@ -288,6 +146,7 @@ key = "/nonexistent/key.pem"
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.env("RUST_LOG", "error")
.output()
.unwrap();
@ -298,96 +157,3 @@ key = "/nonexistent/key.pem"
"Error for host 'host2.com': Certificate file '/nonexistent/cert.pem' does not exist"
));
}
#[test]
fn test_multiple_hosts_invalid_hostname() {
let temp_dir = common::setup_test_environment();
let config_path = temp_dir.path().join("config.toml");
// Create host directories
std::fs::create_dir(temp_dir.path().join("validhost")).unwrap();
std::fs::create_dir(temp_dir.path().join("invalidhost")).unwrap();
// Generate certificates for both hosts
let cert1_path = temp_dir.path().join("valid_cert.pem");
let key1_path = temp_dir.path().join("valid_key.pem");
let cert2_path = temp_dir.path().join("invalid_cert.pem");
let key2_path = temp_dir.path().join("invalid_key.pem");
// Generate certificate for valid host
let cert_result1 = std::process::Command::new("openssl")
.args(&[
"req",
"-x509",
"-newkey",
"rsa:2048",
"-keyout",
&key1_path.to_string_lossy(),
"-out",
&cert1_path.to_string_lossy(),
"-days",
"1",
"-nodes",
"-subj",
"/CN=valid.com",
])
.output();
// Generate certificate for invalid host (hostname validation happens before cert validation)
let cert_result2 = std::process::Command::new("openssl")
.args(&[
"req",
"-x509",
"-newkey",
"rsa:2048",
"-keyout",
&key2_path.to_string_lossy(),
"-out",
&cert2_path.to_string_lossy(),
"-days",
"1",
"-nodes",
"-subj",
"/CN=invalid.com",
])
.output();
if cert_result1.is_err() || cert_result2.is_err() {
panic!("Failed to generate test certificates");
}
let config_content = format!(
r#"
bind_host = "127.0.0.1"
["valid.com"]
root = "{}"
cert = "{}"
key = "{}"
["bad..host.com"]
root = "{}"
cert = "{}"
key = "{}"
"#,
temp_dir.path().join("validhost").display(),
cert1_path.display(),
key1_path.display(),
temp_dir.path().join("invalidhost").display(),
cert2_path.display(),
key2_path.display()
);
std::fs::write(&config_path, config_content).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.env("RUST_LOG", "error")
.output()
.unwrap();
assert!(!output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("Invalid hostname 'bad..host.com'. Hostnames must be valid DNS names."));
}

View file

@ -35,6 +35,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.arg("--test-processing-delay")
.arg("3") // 3 second delay per request
.spawn()

View file

@ -57,6 +57,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();
@ -175,6 +176,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();
@ -231,6 +233,7 @@ key = "{}"
let output = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.output()
.unwrap();
@ -246,13 +249,14 @@ fn test_no_hosts_config() {
let config_content = r#"
bind_host = "127.0.0.1"
port = 1965
# No host sections
# No host sections defined
"#;
std::fs::write(&config_path, config_content).unwrap();
let output = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.output()
.unwrap();
@ -297,6 +301,7 @@ key = "{}"
let output = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.output()
.unwrap();
@ -331,6 +336,7 @@ port = 1970 # Override global port
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();
@ -347,6 +353,8 @@ fn test_config_file_not_found() {
let output = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg("nonexistent.toml")
.arg("--quiet")
.env("RUST_LOG", "error")
.output()
.unwrap();

View file

@ -53,6 +53,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();
@ -131,6 +132,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();
@ -203,6 +205,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();
@ -287,6 +290,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();

View file

@ -46,6 +46,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();
@ -121,6 +122,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();

View file

@ -116,6 +116,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();
@ -171,6 +172,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();
@ -218,6 +220,7 @@ key = "{}"
let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&config_path)
.arg("--quiet")
.spawn()
.unwrap();