Skip to content

Commit

Permalink
Make server port allocation less manual
Browse files Browse the repository at this point in the history
  • Loading branch information
ctz committed May 5, 2024
1 parent 4a7ccb1 commit ce6858f
Showing 1 changed file with 62 additions and 27 deletions.
89 changes: 62 additions & 27 deletions rustls-libssl/tests/runner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::Read;
use std::process::{Child, Command, Output, Stdio};
use std::{fs, net, thread, time};
use std::{fs, net, sync::atomic, thread, time};

/* Note:
*
Expand Down Expand Up @@ -31,6 +31,9 @@ use std::{fs, net, thread, time};
#[test]
#[ignore]
fn client_unauthenticated() {
let port = choose_port();
let port_str = &format!("{port}");

let _server = KillOnDrop(Some(
Command::new("openssl")
.args([
Expand All @@ -44,27 +47,27 @@ fn client_unauthenticated() {
"-alpn",
"hello,world",
"-accept",
"localhost:4443",
&format!("localhost:{port}"),
"-rev",
])
.env("LD_LIBRARY_PATH", "")
.spawn()
.expect("failed to start openssl s_server"),
));

wait_for_port(4443);
wait_for_port(port);

// server is unauthenticated
let openssl_insecure_output = Command::new("tests/maybe-valgrind.sh")
.env("LD_LIBRARY_PATH", "")
.args(["target/client", "localhost", "4443", "insecure"])
.args(["target/client", "localhost", port_str, "insecure"])
.stdout(Stdio::piped())
.output()
.map(print_output)
.unwrap();

let rustls_insecure_output = Command::new("tests/maybe-valgrind.sh")
.args(["target/client", "localhost", "4443", "insecure"])
.args(["target/client", "localhost", port_str, "insecure"])
.stdout(Stdio::piped())
.output()
.map(print_output)
Expand All @@ -75,14 +78,24 @@ fn client_unauthenticated() {
// server is authenticated, client has no creds
let openssl_secure_output = Command::new("tests/maybe-valgrind.sh")
.env("LD_LIBRARY_PATH", "")
.args(["target/client", "localhost", "4443", "test-ca/rsa/ca.cert"])
.args([
"target/client",
"localhost",
port_str,
"test-ca/rsa/ca.cert",
])
.stdout(Stdio::piped())
.output()
.map(print_output)
.unwrap();

let rustls_secure_output = Command::new("tests/maybe-valgrind.sh")
.args(["target/client", "localhost", "4443", "test-ca/rsa/ca.cert"])
.args([
"target/client",
"localhost",
port_str,
"test-ca/rsa/ca.cert",
])
.stdout(Stdio::piped())
.output()
.map(print_output)
Expand All @@ -96,7 +109,7 @@ fn client_unauthenticated() {
.args([
"target/client",
"localhost",
"4443",
port_str,
"test-ca/rsa/ca.cert",
"test-ca/rsa/client.key",
"test-ca/rsa/client.cert",
Expand All @@ -110,7 +123,7 @@ fn client_unauthenticated() {
.args([
"target/client",
"localhost",
"4443",
port_str,
"test-ca/rsa/ca.cert",
"test-ca/rsa/client.key",
"test-ca/rsa/client.cert",
Expand All @@ -126,6 +139,9 @@ fn client_unauthenticated() {
#[test]
#[ignore]
fn client_auth() {
let port = choose_port();
let port_str = &format!("{port}");

let _server = KillOnDrop(Some(
Command::new("openssl")
.args([
Expand All @@ -143,23 +159,23 @@ fn client_auth() {
"-CAfile",
"test-ca/rsa/ca.cert",
"-accept",
"localhost:4444",
&format!("localhost:{port}"),
"-rev",
])
.env("LD_LIBRARY_PATH", "")
.spawn()
.expect("failed to start openssl s_server"),
));

wait_for_port(4444);
wait_for_port(port);

// mutual auth
let openssl_authed_output = Command::new("tests/maybe-valgrind.sh")
.env("LD_LIBRARY_PATH", "")
.args([
"target/client",
"localhost",
"4444",
port_str,
"test-ca/rsa/ca.cert",
"test-ca/rsa/client.key",
"test-ca/rsa/client.cert",
Expand All @@ -173,7 +189,7 @@ fn client_auth() {
.args([
"target/client",
"localhost",
"4444",
port_str,
"test-ca/rsa/ca.cert",
"test-ca/rsa/client.key",
"test-ca/rsa/client.cert",
Expand All @@ -188,14 +204,24 @@ fn client_auth() {
// failed auth
let openssl_failed_output = Command::new("tests/maybe-valgrind.sh")
.env("LD_LIBRARY_PATH", "")
.args(["target/client", "localhost", "4444", "test-ca/rsa/ca.cert"])
.args([
"target/client",
"localhost",
port_str,
"test-ca/rsa/ca.cert",
])
.stdout(Stdio::piped())
.output()
.map(print_output)
.unwrap();

let rustls_failed_output = Command::new("tests/maybe-valgrind.sh")
.args(["target/client", "localhost", "4444", "test-ca/rsa/ca.cert"])
.args([
"target/client",
"localhost",
port_str,
"test-ca/rsa/ca.cert",
])
.stdout(Stdio::piped())
.output()
.map(print_output)
Expand Down Expand Up @@ -273,14 +299,16 @@ fn ciphers() {
#[test]
#[ignore]
fn server() {
fn curl() {
let port = choose_port();

fn curl(port: u16) {
Command::new("curl")
.env("LD_LIBRARY_PATH", "")
.args([
"-v",
"--cacert",
"test-ca/rsa/ca.cert",
"https://localhost:5555/",
&format!("https://localhost:{port}/"),
])
.stdout(Stdio::piped())
.output()
Expand All @@ -293,7 +321,7 @@ fn server() {
.env("LD_LIBRARY_PATH", "")
.args([
"target/server",
"5555",
&format!("{port}"),
"test-ca/rsa/server.key",
"test-ca/rsa/server.cert",
"unauth",
Expand All @@ -304,15 +332,15 @@ fn server() {
.unwrap(),
));
wait_for_stdout(openssl_server.0.as_mut().unwrap(), b"listening\n");
curl();
curl(port);

let openssl_output = print_output(openssl_server.take_inner().wait_with_output().unwrap());

let mut rustls_server = KillOnDrop(Some(
Command::new("tests/maybe-valgrind.sh")
.args([
"target/server",
"5555",
&format!("{port}"),
"test-ca/rsa/server.key",
"test-ca/rsa/server.cert",
"unauth",
Expand All @@ -323,20 +351,20 @@ fn server() {
.unwrap(),
));
wait_for_stdout(rustls_server.0.as_mut().unwrap(), b"listening\n");
curl();
curl(port);

let rustls_output = print_output(rustls_server.take_inner().wait_with_output().unwrap());
assert_eq!(openssl_output, rustls_output);
}

fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str) {
fn connect(key_type: &str, sig_algs: &str, version_flag: &str) {
fn connect(port: u16, key_type: &str, sig_algs: &str, version_flag: &str) {
Command::new("openssl")
.env("LD_LIBRARY_PATH", "")
.args([
"s_client",
"-connect",
"localhost:5556",
&format!("localhost:{port}"),
"-sigalgs",
sig_algs,
"-CAfile",
Expand All @@ -351,12 +379,14 @@ fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str)
.unwrap();
}

let port = choose_port();

let mut openssl_server = KillOnDrop(Some(
Command::new("tests/maybe-valgrind.sh")
.env("LD_LIBRARY_PATH", "")
.args([
"target/server",
"5556",
&format!("{port}"),
&format!("test-ca/{key_type}/server.key"),
&format!("test-ca/{key_type}/server.cert"),
"unauth",
Expand All @@ -367,15 +397,15 @@ fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str)
.unwrap(),
));
wait_for_stdout(openssl_server.0.as_mut().unwrap(), b"listening\n");
connect(key_type, sig_algs, version_flag);
connect(port, key_type, sig_algs, version_flag);

let openssl_output = print_output(openssl_server.take_inner().wait_with_output().unwrap());

let mut rustls_server = KillOnDrop(Some(
Command::new("tests/maybe-valgrind.sh")
.args([
"target/server",
"5556",
&format!("{port}"),
&format!("test-ca/{key_type}/server.key"),
&format!("test-ca/{key_type}/server.cert"),
"unauth",
Expand All @@ -386,7 +416,7 @@ fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str)
.unwrap(),
));
wait_for_stdout(rustls_server.0.as_mut().unwrap(), b"listening\n");
connect(key_type, sig_algs, version_flag);
connect(port, key_type, sig_algs, version_flag);

let rustls_output = print_output(rustls_server.take_inner().wait_with_output().unwrap());
assert_eq!(openssl_output, rustls_output);
Expand Down Expand Up @@ -581,3 +611,8 @@ fn wait_for_stdout(stream: &mut Child, expected: &[u8]) {
};
}
}

fn choose_port() -> u16 {
static NEXT_PORT: atomic::AtomicU16 = atomic::AtomicU16::new(5555);
NEXT_PORT.fetch_add(1, atomic::Ordering::SeqCst)
}

0 comments on commit ce6858f

Please sign in to comment.