#![allow(clippy::all)]
#![allow(warnings)]
#![allow(unused_imports)]
#![allow(unused_variables)]
#![allow(clippy::needless_borrows_for_generic_args)]
#![allow(clippy::assertions_on_constants)]
use rpcnet::{RpcClient, RpcConfig, RpcServer};
use std::time::Duration;
use tokio::time::timeout;
#[tokio::test]
async fn test_invalid_certificate_path() {
let config = RpcConfig::new("/nonexistent/cert.pem", "127.0.0.1:0")
.with_key_path("/nonexistent/key.pem")
.with_server_name("localhost");
let result = timeout(
Duration::from_millis(500),
RpcClient::connect("127.0.0.1:9999".parse().unwrap(), config),
)
.await;
assert!(result.is_err() || result.unwrap().is_err());
}
#[tokio::test]
async fn test_empty_certificate_paths() {
let config = RpcConfig::new("", "127.0.0.1:0")
.with_key_path("")
.with_server_name("localhost");
assert_eq!(config.cert_path, std::path::PathBuf::from(""));
assert_eq!(config.key_path, Some(std::path::PathBuf::from("")));
}
#[tokio::test]
async fn test_mismatched_server_name() {
let config = RpcConfig::new("certs/test_cert.pem", "127.0.0.1:0")
.with_key_path("certs/test_key.pem")
.with_server_name("wrong.hostname.com")
.with_keep_alive_interval(Duration::from_millis(100));
let result = timeout(
Duration::from_millis(500),
RpcClient::connect("127.0.0.1:9999".parse().unwrap(), config),
)
.await;
assert!(result.is_err() || result.unwrap().is_err());
}
#[tokio::test]
async fn test_corrupted_certificate_content() {
use std::fs;
use std::io::Write;
let temp_cert = "/tmp/corrupted_cert.pem";
let mut file = fs::File::create(temp_cert).unwrap();
file.write_all(b"-----BEGIN CERTIFICATE-----\nCORRUPTED_DATA\n-----END CERTIFICATE-----")
.unwrap();
let config = RpcConfig::new(temp_cert, "127.0.0.1:0")
.with_key_path("certs/test_key.pem")
.with_server_name("localhost")
.with_keep_alive_interval(Duration::from_millis(100));
let result = timeout(
Duration::from_millis(500),
RpcClient::connect("127.0.0.1:9999".parse().unwrap(), config),
)
.await;
assert!(result.is_err() || result.unwrap().is_err());
let _ = fs::remove_file(temp_cert);
}
#[tokio::test]
async fn test_very_short_timeout() {
let config = RpcConfig::new("certs/test_cert.pem", "127.0.0.1:0")
.with_key_path("certs/test_key.pem")
.with_server_name("localhost")
.with_keep_alive_interval(Duration::from_nanos(1));
let result = timeout(
Duration::from_millis(100),
RpcClient::connect("127.0.0.1:9999".parse().unwrap(), config),
)
.await;
assert!(result.is_err() || result.unwrap().is_err());
}
#[tokio::test]
async fn test_zero_timeout() {
let config = RpcConfig::new("certs/test_cert.pem", "127.0.0.1:0")
.with_key_path("certs/test_key.pem")
.with_server_name("localhost")
.with_keep_alive_interval(Duration::ZERO);
assert_eq!(config.keep_alive_interval, Some(Duration::ZERO));
}
#[tokio::test]
async fn test_server_with_missing_key_file() {
let config = RpcConfig::new("certs/test_cert.pem", "127.0.0.1:0")
.with_key_path("nonexistent_key.pem")
.with_server_name("localhost");
let _server = RpcServer::new(config);
}
#[tokio::test]
async fn test_invalid_bind_address_formats() {
let invalid_addresses = vec![
"not_an_address",
"256.256.256.256:8080", "127.0.0.1:99999", "127.0.0.1:", ":8080", "127.0.0.1:-1", "", "localhost", ];
for addr in invalid_addresses {
let config =
RpcConfig::new("certs/test_cert.pem", addr).with_key_path("certs/test_key.pem");
assert_eq!(config.bind_address, addr);
}
}
#[tokio::test]
async fn test_unicode_in_server_names() {
let long_name_253 = "a".repeat(253);
let long_name_254 = "a".repeat(254);
let unicode_names = vec![
"тест.example.com", "例え.テスト.jp", "🚀.example.com", "xn--nxasmq6b.com", &long_name_253, &long_name_254, ];
for name in unicode_names {
let config = RpcConfig::new("certs/test_cert.pem", "127.0.0.1:0")
.with_key_path("certs/test_key.pem")
.with_server_name(name);
assert_eq!(config.server_name, name);
}
}
#[tokio::test]
async fn test_path_traversal_in_cert_paths() {
let malicious_paths = vec![
"../../../etc/passwd",
"..\\..\\..\\windows\\system32\\config\\sam",
"/etc/shadow",
"C:\\Windows\\System32\\config\\SAM",
"file:///etc/passwd",
"\\\\server\\share\\file",
"~/.ssh/id_rsa",
"/dev/null",
"/proc/self/environ",
];
for path in malicious_paths {
let config = RpcConfig::new(path, "127.0.0.1:0").with_key_path(path);
assert_eq!(config.cert_path, std::path::PathBuf::from(path));
assert_eq!(config.key_path, Some(std::path::PathBuf::from(path)));
}
}
#[tokio::test]
async fn test_extremely_long_paths() {
let long_path = "a/".repeat(1000) + "cert.pem";
let very_long_path = "b/".repeat(5000) + "key.pem";
let config = RpcConfig::new(&long_path, "127.0.0.1:0").with_key_path(&very_long_path);
assert_eq!(config.cert_path, std::path::PathBuf::from(&long_path));
assert_eq!(
config.key_path,
Some(std::path::PathBuf::from(&very_long_path))
);
}
#[tokio::test]
async fn test_null_bytes_in_paths() {
let paths_with_nulls = vec!["cert\0.pem", "cert.pem\0", "\0cert.pem", "ce\0rt.pem"];
for path in paths_with_nulls {
let config = RpcConfig::new(path, "127.0.0.1:0").with_key_path(path);
assert_eq!(config.cert_path, std::path::PathBuf::from(path));
}
}
#[tokio::test]
async fn test_concurrent_connection_attempts() {
use futures::future::join_all;
let config = RpcConfig::new("certs/test_cert.pem", "127.0.0.1:0")
.with_key_path("certs/test_key.pem")
.with_server_name("localhost")
.with_keep_alive_interval(Duration::from_millis(50));
let mut futures = vec![];
for _ in 0..50 {
let config_clone = config.clone();
futures.push(async move {
timeout(
Duration::from_millis(100),
RpcClient::connect("127.0.0.1:19999".parse().unwrap(), config_clone),
)
.await
});
}
let results = join_all(futures).await;
for result in results {
assert!(result.is_err() || result.unwrap().is_err());
}
}
#[tokio::test]
async fn test_keep_alive_edge_cases() {
let configs = vec![
RpcConfig::new("cert.pem", "127.0.0.1:0").with_keep_alive_interval(Duration::MAX),
RpcConfig::new("cert.pem", "127.0.0.1:0").with_keep_alive_interval(Duration::from_nanos(1)),
RpcConfig::new("cert.pem", "127.0.0.1:0").with_keep_alive_interval(Duration::from_secs(1)),
];
assert_eq!(configs[0].keep_alive_interval, Some(Duration::MAX));
assert_eq!(
configs[1].keep_alive_interval,
Some(Duration::from_nanos(1))
);
assert_eq!(configs[2].keep_alive_interval, Some(Duration::from_secs(1)));
}
#[test]
fn test_config_debug_format_no_secrets() {
let config = RpcConfig::new("/secret/path/cert.pem", "127.0.0.1:8080")
.with_key_path("/secret/path/key.pem")
.with_server_name("secret.internal.com");
let debug_output = format!("{:?}", config);
assert!(!debug_output.is_empty());
}
#[tokio::test]
async fn test_bind_to_privileged_ports() {
let privileged_ports = vec!["0.0.0.0:80", "0.0.0.0:443", "0.0.0.0:22", "0.0.0.0:21"];
for port in privileged_ports {
let config =
RpcConfig::new("certs/test_cert.pem", port).with_key_path("certs/test_key.pem");
assert_eq!(config.bind_address, port);
let _server = RpcServer::new(config);
}
}
#[tokio::test]
async fn test_ipv6_address_handling() {
let ipv6_addresses = vec![
"[::1]:8080", "[::]:8080", "[2001:db8::1]:8080", "[fe80::1%eth0]:8080", "[::ffff:192.168.1.1]:8080", ];
for addr in ipv6_addresses {
let config =
RpcConfig::new("certs/test_cert.pem", addr).with_key_path("certs/test_key.pem");
assert_eq!(config.bind_address, addr);
}
}