Unify integration test environment and add valid config validation

- Create shared tests/common.rs with TestEnvironment setup
- Simplify gemini_test_client.py to single-request client
- Refactor config validation tests to use common setup
- Add test_valid_config_startup for complete server validation
- Fix clippy warning in main.rs
- Remove unused code and consolidate test infrastructure
This commit is contained in:
Jeena 2026-01-16 23:59:54 +00:00
parent 3e490d85ef
commit 01bcda10d0
5 changed files with 219 additions and 295 deletions

View file

@ -61,7 +61,7 @@ async fn main() {
} }
// Load and parse config // Load and parse config
let config = match config::load_config(&config_path) { let config = match config::load_config(config_path) {
Ok(config) => config, Ok(config) => config,
Err(e) => { Err(e) => {
eprintln!("Error: Failed to parse config file '{}': {}", config_path, e); eprintln!("Error: Failed to parse config file '{}': {}", config_path, e);

59
tests/common.rs Normal file
View file

@ -0,0 +1,59 @@
use std::path::{Path, PathBuf};
use tempfile::TempDir;
pub struct TestEnvironment {
pub temp_dir: TempDir,
pub config_path: PathBuf,
pub cert_path: PathBuf,
pub key_path: PathBuf,
pub content_path: PathBuf,
pub port: u16,
}
pub fn setup_test_environment() -> TestEnvironment {
let temp_dir = TempDir::new().unwrap();
let config_path = temp_dir.path().join("config.toml");
let cert_path = temp_dir.path().join("cert.pem");
let key_path = temp_dir.path().join("key.pem");
let content_path = temp_dir.path().join("content");
// Create content directory and file
std::fs::create_dir(&content_path).unwrap();
std::fs::write(content_path.join("test.gmi"), "# Test Gemini content\n").unwrap();
// Generate test certificates
generate_test_certificates(temp_dir.path());
// Use a unique port based on process ID to avoid conflicts
let port = 1967 + (std::process::id() % 1000) as u16;
TestEnvironment {
temp_dir,
config_path,
cert_path,
key_path,
content_path,
port,
}
}
fn generate_test_certificates(temp_dir: &Path) {
use std::process::Command;
let cert_path = temp_dir.join("cert.pem");
let key_path = temp_dir.join("key.pem");
let status = Command::new("openssl")
.args(&[
"req", "-x509", "-newkey", "rsa:2048",
"-keyout", &key_path.to_string_lossy(),
"-out", &cert_path.to_string_lossy(),
"-days", "1",
"-nodes",
"-subj", "/CN=localhost"
])
.status()
.unwrap();
assert!(status.success(), "Failed to generate test certificates");
}

View file

@ -1,5 +1,6 @@
mod common;
use std::process::Command; use std::process::Command;
use std::env;
#[test] #[test]
fn test_missing_config_file() { fn test_missing_config_file() {
@ -17,83 +18,100 @@ fn test_missing_config_file() {
#[test] #[test]
fn test_missing_hostname() { fn test_missing_hostname() {
let temp_dir = env::temp_dir().join(format!("pollux_test_config_{}", std::process::id())); let env = common::setup_test_environment();
std::fs::create_dir_all(&temp_dir).unwrap(); let config_content = format!(r#"
let config_path = temp_dir.join("config.toml"); root = "{}"
std::fs::write(&config_path, r#" cert = "{}"
root = "/tmp" key = "{}"
cert = "cert.pem" bind_host = "127.0.0.1"
key = "key.pem" "#, env.content_path.display(), env.cert_path.display(), env.key_path.display());
bind_host = "0.0.0.0" std::fs::write(&env.config_path, config_content).unwrap();
"#).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_pollux")) let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config") .arg("--config")
.arg(&config_path) .arg(&env.config_path)
.output() .output()
.unwrap(); .unwrap();
assert!(!output.status.success()); assert!(!output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap(); let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("'hostname' field is required")); assert!(stderr.contains("'hostname' field is required"));
assert!(stderr.contains("hostname = \"your.domain.com\"")); assert!(stderr.contains("Add: hostname = \"your.domain.com\""));
// Cleanup
let _ = std::fs::remove_dir_all(&temp_dir);
} }
#[test] #[test]
fn test_nonexistent_root_directory() { fn test_nonexistent_root_directory() {
let temp_dir = env::temp_dir().join(format!("pollux_test_config_{}", std::process::id())); let env = common::setup_test_environment();
std::fs::create_dir_all(&temp_dir).unwrap(); let config_content = format!(r#"
let config_path = temp_dir.join("config.toml");
std::fs::write(&config_path, r#"
root = "/definitely/does/not/exist" root = "/definitely/does/not/exist"
cert = "cert.pem" cert = "{}"
key = "key.pem" key = "{}"
hostname = "example.com" hostname = "example.com"
bind_host = "0.0.0.0" bind_host = "127.0.0.1"
"#).unwrap(); "#, env.cert_path.display(), env.key_path.display());
std::fs::write(&env.config_path, config_content).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_pollux")) let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config") .arg("--config")
.arg(config_path) .arg(&env.config_path)
.output() .output()
.unwrap(); .unwrap();
// Cleanup
let _ = std::fs::remove_dir_all(&temp_dir);
assert!(!output.status.success()); assert!(!output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap(); let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("Root directory '/definitely/does/not/exist' does not exist")); assert!(stderr.contains("Error: Root directory '/definitely/does/not/exist' does not exist"));
assert!(stderr.contains("Create the directory and add your Gemini files")); assert!(stderr.contains("Create the directory and add your Gemini files (.gmi, .txt, images)"));
} }
#[test] #[test]
fn test_missing_certificate_file() { fn test_missing_certificate_file() {
let temp_dir = env::temp_dir().join(format!("pollux_test_config_{}", std::process::id())); let env = common::setup_test_environment();
std::fs::create_dir_all(&temp_dir).unwrap(); let config_content = format!(r#"
let config_path = temp_dir.join("config.toml"); root = "{}"
std::fs::write(&config_path, r#"
root = "/tmp"
cert = "/nonexistent/cert.pem" cert = "/nonexistent/cert.pem"
key = "key.pem" key = "{}"
hostname = "example.com" hostname = "example.com"
bind_host = "0.0.0.0" bind_host = "127.0.0.1"
"#).unwrap(); "#, env.content_path.display(), env.key_path.display());
std::fs::write(&env.config_path, config_content).unwrap();
let output = Command::new(env!("CARGO_BIN_EXE_pollux")) let output = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config") .arg("--config")
.arg(&config_path) .arg(&env.config_path)
.output() .output()
.unwrap(); .unwrap();
assert!(!output.status.success()); assert!(!output.status.success());
let stderr = String::from_utf8(output.stderr).unwrap(); let stderr = String::from_utf8(output.stderr).unwrap();
assert!(stderr.contains("Certificate file '/nonexistent/cert.pem' does not exist")); assert!(stderr.contains("Error: Certificate file '/nonexistent/cert.pem' does not exist"));
assert!(stderr.contains("Generate or obtain TLS certificates")); assert!(stderr.contains("Generate or obtain TLS certificates for your domain"));
}
// Cleanup
let _ = std::fs::remove_dir_all(&temp_dir); #[test]
fn test_valid_config_startup() {
let env = common::setup_test_environment();
let config_content = format!(r#"
root = "{}"
cert = "{}"
key = "{}"
hostname = "localhost"
bind_host = "127.0.0.1"
port = {}
"#, env.content_path.display(), env.cert_path.display(), env.key_path.display(), env.port);
std::fs::write(&env.config_path, config_content).unwrap();
let mut server_process = Command::new(env!("CARGO_BIN_EXE_pollux"))
.arg("--config")
.arg(&env.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();
} }

View file

@ -1,195 +1,71 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
Gemini Test Client Simple Gemini Test Client
A simple Gemini protocol client for testing Gemini servers. Makes a single Gemini request and prints the status line.
Used by integration tests to validate server behavior. Used by integration tests for rate limiting validation.
Usage: Usage: python3 tests/gemini_test_client.py gemini://host:port/path
python3 tests/gemini_test_client.py --url gemini://example.com/ --timeout 10
""" """
import argparse import sys
import socket import socket
import ssl import ssl
import time
import multiprocessing
from concurrent.futures import ProcessPoolExecutor, as_completed
def parse_args(): def main():
"""Parse command line arguments""" if len(sys.argv) != 2:
parser = argparse.ArgumentParser(description='Test Gemini rate limiting with concurrent requests') print("Usage: python3 gemini_test_client.py <gemini-url>", file=sys.stderr)
parser.add_argument('--limit', type=int, default=3, sys.exit(1)
help='Number of concurrent requests to send (default: 3)')
parser.add_argument('--host', default='localhost',
help='Server host (default: localhost)')
parser.add_argument('--port', type=int, default=1965,
help='Server port (default: 1965)')
parser.add_argument('--delay', type=float, default=0.1,
help='Delay between request start and connection close (default: 0.1s)')
parser.add_argument('--timeout', type=float, default=5.0,
help='Socket timeout in seconds (default: 5.0)')
parser.add_argument('--url', default='gemini://localhost/big-file.mkv',
help='Gemini URL to request (default: gemini://localhost/big-file.mkv)')
args = parser.parse_args() url = sys.argv[1]
# Validation # Parse URL (basic parsing)
if args.limit < 1: if not url.startswith('gemini://'):
parser.error("Limit must be at least 1") print("Error: URL must start with gemini://", file=sys.stderr)
if args.limit > 10000: sys.exit(1)
parser.error("Limit too high (max 10000 for safety)")
if args.delay < 0:
parser.error("Delay must be non-negative")
if args.timeout <= 0:
parser.error("Timeout must be positive")
return args url_parts = url[9:].split('/', 1) # Remove gemini://
host_port = url_parts[0]
path = '/' + url_parts[1] if len(url_parts) > 1 else '/'
if ':' in host_port:
host, port = host_port.rsplit(':', 1)
port = int(port)
else:
host = host_port
port = 1965
def send_gemini_request(host, port, url, delay, timeout):
"""Send one Gemini request with proper error handling"""
try: try:
# Create SSL context # Create SSL connection
context = ssl.create_default_context() context = ssl.create_default_context()
context.check_hostname = False context.check_hostname = False
context.verify_mode = ssl.CERT_NONE context.verify_mode = ssl.CERT_NONE
# Connect with timeout sock = socket.create_connection((host, port), timeout=5.0)
sock = socket.create_connection((host, port), timeout=timeout)
ssl_sock = context.wrap_socket(sock, server_hostname=host) ssl_sock = context.wrap_socket(sock, server_hostname=host)
# Send request # Send request
request = f"{url}\r\n".encode('utf-8') request = f"{url}\r\n"
ssl_sock.send(request) ssl_sock.send(request.encode('utf-8'))
# Read response with timeout # Read response header
ssl_sock.settimeout(timeout) response = b''
response = ssl_sock.recv(1024) while b'\r\n' not in response and len(response) < 1024:
data = ssl_sock.recv(1)
if not response: if not data:
return "Error: Empty response" break
response += data
status = response.decode('utf-8', errors='ignore').split('\r\n')[0]
# Keep connection alive briefly if requested
if delay > 0:
time.sleep(delay)
ssl_sock.close() ssl_sock.close()
return status
except socket.timeout: if response:
return "Error: Timeout" status_line = response.decode('utf-8', errors='ignore').split('\r\n')[0]
except ConnectionRefusedError: print(status_line)
return "Error: Connection refused" else:
print("Error: No response")
except Exception as e: except Exception as e:
return f"Error: {e}" print(f"Error: {e}")
def main():
"""Run concurrent requests"""
args = parse_args()
if args.limit == 1:
print("Testing single request (debug mode)...")
start_time = time.time()
result = send_gemini_request(args.host, args.port, args.url, args.delay, args.timeout)
end_time = time.time()
duration = end_time - start_time
print(f"Result: {result}")
print(".2f")
return
print(f"Testing rate limiting with {args.limit} concurrent requests (using multiprocessing)...")
print(f"Server: {args.host}:{args.port}")
print(f"URL: {args.url}")
print(f"Delay: {args.delay}s, Timeout: {args.timeout}s")
print()
start_time = time.time()
# Use ProcessPoolExecutor for true parallelism (bypasses GIL)
results = []
max_workers = min(args.limit, multiprocessing.cpu_count() * 4) # Limit workers to avoid system overload
with ProcessPoolExecutor(max_workers=max_workers) as executor:
futures = [
executor.submit(send_gemini_request, args.host, args.port,
args.url, args.delay, args.timeout)
for _ in range(args.limit)
]
for future in as_completed(futures):
results.append(future.result())
elapsed = time.time() - start_time
# Analyze results
status_counts = {}
connection_refused = 0
timeouts = 0
other_errors = []
for result in results:
if "Connection refused" in result:
connection_refused += 1
elif "Timeout" in result:
timeouts += 1
elif result.startswith("Error"):
other_errors.append(result)
else:
status_counts[result] = status_counts.get(result, 0) + 1
# Print results
print("Results:")
for status, count in sorted(status_counts.items()):
print(f" {status}: {count}")
if connection_refused > 0:
print(f" Connection refused: {connection_refused} (server overloaded)")
if timeouts > 0:
print(f" Timeouts: {timeouts} (server unresponsive)")
if other_errors:
print(f" Other errors: {len(other_errors)}")
for error in other_errors[:3]:
print(f" {error}")
if len(other_errors) > 3:
print(f" ... and {len(other_errors) - 3} more")
print()
print(".2f")
# Success criteria for rate limiting
success_20 = status_counts.get("20 application/octet-stream", 0)
rate_limited_41 = status_counts.get("41 Server unavailable", 0)
total_successful = success_20 + rate_limited_41 + connection_refused
total_processed = total_successful + timeouts
print(f"\nAnalysis:")
print(f" Total requests sent: {args.limit}")
print(f" Successfully processed: {total_successful}")
print(f" Timeouts (server unresponsive): {timeouts}")
if args.limit == 1:
# Single request should succeed
if success_20 == 1 and timeouts == 0:
print("✅ PASS: Single request works correctly")
else:
print("❌ FAIL: Single request failed")
elif rate_limited_41 > 0 and success_20 > 0:
# We have both successful responses and 41 rate limited responses
print("✅ PASS: Rate limiting detected!")
print(f" {success_20} requests succeeded")
print(f" {rate_limited_41} requests rate limited with 41 response")
print(" Mixed results indicate rate limiting is working correctly")
elif success_20 == args.limit and timeouts == 0:
# All requests succeeded
print("⚠️ All requests succeeded - rate limiting may not be triggered")
print(" This could mean:")
print(" - Requests are not truly concurrent")
print(" - Processing is too fast for overlap")
print(" - Need longer delays or more concurrent requests")
else:
print("❓ UNCLEAR: Check server logs and test parameters")
print(" May need to adjust --limit, delays, or server configuration")
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -1,37 +1,10 @@
use std::process::Command; mod common;
struct TestEnvironment { #[test]
temp_dir: std::path::PathBuf, fn test_rate_limiting_with_concurrent_requests() {
config_file: std::path::PathBuf, let env = common::setup_test_environment();
content_file: std::path::PathBuf,
port: u16,
}
impl Drop for TestEnvironment { // Create config with rate limiting enabled
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.temp_dir);
}
}
fn setup_test_environment() -> Result<TestEnvironment, Box<dyn std::error::Error>> {
use std::env;
// Create unique temp directory for this test
let temp_dir = env::temp_dir().join(format!("pollux_test_{}", std::process::id()));
std::fs::create_dir_all(&temp_dir)?;
// Generate test certificates
generate_test_certificates(&temp_dir)?;
// Create test content file
let content_file = temp_dir.join("test.gmi");
std::fs::write(&content_file, "# Test Gemini content\n")?;
// Use a unique port based on process ID to avoid conflicts
let port = 1967 + (std::process::id() % 1000) as u16;
// Create config file
let config_file = temp_dir.join("config.toml");
let config_content = format!(r#" let config_content = format!(r#"
root = "{}" root = "{}"
cert = "{}" cert = "{}"
@ -40,53 +13,51 @@ fn setup_test_environment() -> Result<TestEnvironment, Box<dyn std::error::Error
bind_host = "127.0.0.1" bind_host = "127.0.0.1"
port = {} port = {}
max_concurrent_requests = 1 max_concurrent_requests = 1
"#, temp_dir.display(), temp_dir.join("cert.pem").display(), temp_dir.join("key.pem").display(), port); "#, env.content_path.display(), env.cert_path.display(), env.key_path.display(), env.port);
std::fs::write(&config_file, config_content)?; std::fs::write(&env.config_path, config_content).unwrap();
Ok(TestEnvironment { // Start server binary with test delay to simulate processing time
temp_dir, let mut server_process = std::process::Command::new(env!("CARGO_BIN_EXE_pollux"))
config_file, .arg("--config")
content_file, .arg(&env.config_path)
port, .arg("--test-processing-delay")
}) .arg("1") // 1 second delay per request
} .spawn()
.expect("Failed to start server");
fn generate_test_certificates(temp_dir: &std::path::Path) -> Result<(), Box<dyn std::error::Error>> { // Wait for server to start
use std::process::Command; std::thread::sleep(std::time::Duration::from_millis(500));
let cert_path = temp_dir.join("cert.pem"); // Spawn 5 concurrent client processes
let key_path = temp_dir.join("key.pem"); let mut handles = vec![];
for _ in 0..5 {
let status = Command::new("openssl") let url = format!("gemini://localhost:{}/test.gmi", env.port);
.args(&[ let handle = std::thread::spawn(move || {
"req", "-x509", "-newkey", "rsa:2048", std::process::Command::new("python3")
"-keyout", &key_path.to_string_lossy(), .arg("tests/gemini_test_client.py")
"-out", &cert_path.to_string_lossy(), .arg(url)
"-days", "1", .output()
"-nodes", });
"-subj", "/CN=localhost" handles.push(handle);
])
.status()?;
if !status.success() {
return Err("Failed to generate test certificates with openssl".into());
} }
Ok(()) // Collect results
} let mut results = vec![];
for handle in handles {
let output = handle.join().unwrap().unwrap();
let status = String::from_utf8(output.stdout).unwrap();
results.push(status.trim().to_string());
}
#[test] // Kill server
fn test_rate_limiting_with_concurrent_requests() { let _ = server_process.kill();
// For now, skip the complex concurrent testing
// The test infrastructure is in place, but full integration testing
// requires more robust isolation and timing controls
println!("Skipping rate limiting integration test - infrastructure ready for future implementation");
}
fn python_available() -> bool { // Analyze results
std::process::Command::new("python3") let success_count = results.iter().filter(|r| r.starts_with("20")).count();
.arg("--version") let rate_limited_count = results.iter().filter(|r| r.starts_with("41")).count();
.output()
.map(|output| output.status.success()) // Validation
.unwrap_or(false) assert!(success_count >= 1, "At least 1 request should succeed, got results: {:?}", results);
assert!(rate_limited_count >= 1, "At least 1 request should be rate limited, got results: {:?}", results);
assert_eq!(success_count + rate_limited_count, 5, "All requests should get valid responses, got results: {:?}", results);
} }