pollux/tests/common.rs
Jeena 55fe47b172 Replace custom logging with tracing crate and RUST_LOG env var
- Remove custom logging module and init_logging function
- Update main.rs to use tracing_subscriber with EnvFilter
- Remove log_level from global config structure
- Update documentation and tests to use RUST_LOG
- Format long lines in config.rs and test files for better readability
2026-01-22 05:25:46 +00:00

83 lines
2.4 KiB
Rust

use std::path::Path;
#[allow(dead_code)]
pub fn generate_test_certificates_for_host(temp_dir: &Path, hostname: &str) {
let cert_path = temp_dir.join(format!("{}.pem", hostname));
let key_path = temp_dir.join(format!("{}_key.pem", hostname));
// Generate self-signed certificate for testing
// This is a simplified version - in production, use proper certificates
std::fs::write(
&cert_path,
format!(
"-----BEGIN CERTIFICATE-----\nTest cert for {}\n-----END CERTIFICATE-----\n",
hostname
),
)
.unwrap();
std::fs::write(
&key_path,
format!(
"-----BEGIN PRIVATE KEY-----\nTest key for {}\n-----END PRIVATE KEY-----\n",
hostname
),
)
.unwrap();
}
use tempfile::TempDir;
pub fn setup_test_environment() -> TempDir {
let temp_dir = TempDir::new().unwrap();
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());
// Verify certificates were created successfully
let cert_path = temp_dir.path().join("cert.pem");
let key_path = temp_dir.path().join("key.pem");
assert!(cert_path.exists(), "Certificate file was not created");
assert!(key_path.exists(), "Private key file was not created");
temp_dir
}
fn generate_test_certificates(temp_dir: &Path) {
use std::process::Command;
// Generate self-signed certificate for testing
let cert_path = temp_dir.join("cert.pem");
let key_path = temp_dir.join("key.pem");
// Use openssl to generate a test certificate
let output = 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",
])
.output();
match output {
Ok(result) if result.status.success() => {
// Certificate generation successful
}
_ => {
panic!("Failed to generate test certificates with OpenSSL. Make sure OpenSSL is installed and available in PATH.");
}
}
}