Skip to content

Commit 33bf0c4

Browse files
committed
Make server port allocation less manual
1 parent 32640ee commit 33bf0c4

File tree

1 file changed

+61
-26
lines changed

1 file changed

+61
-26
lines changed

rustls-libssl/tests/runner.rs

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::io::Read;
22
use std::process::{Child, Command, Output, Stdio};
3+
use std::sync::atomic;
34
use std::{fs, net, thread, time};
45

56
/* Note:
@@ -31,6 +32,8 @@ use std::{fs, net, thread, time};
3132
#[test]
3233
#[ignore]
3334
fn client_unauthenticated() {
35+
let (port, port_str) = choose_port();
36+
3437
let _server = KillOnDrop(Some(
3538
Command::new("openssl")
3639
.args([
@@ -44,27 +47,27 @@ fn client_unauthenticated() {
4447
"-alpn",
4548
"hello,world",
4649
"-accept",
47-
"localhost:4443",
50+
&format!("localhost:{port}"),
4851
"-rev",
4952
])
5053
.env("LD_LIBRARY_PATH", "")
5154
.spawn()
5255
.expect("failed to start openssl s_server"),
5356
));
5457

55-
wait_for_port(4443);
58+
wait_for_port(port);
5659

5760
// server is unauthenticated
5861
let openssl_insecure_output = Command::new("tests/maybe-valgrind.sh")
5962
.env("LD_LIBRARY_PATH", "")
60-
.args(["target/client", "localhost", "4443", "insecure"])
63+
.args(["target/client", "localhost", &port_str, "insecure"])
6164
.stdout(Stdio::piped())
6265
.output()
6366
.map(print_output)
6467
.unwrap();
6568

6669
let rustls_insecure_output = Command::new("tests/maybe-valgrind.sh")
67-
.args(["target/client", "localhost", "4443", "insecure"])
70+
.args(["target/client", "localhost", &port_str, "insecure"])
6871
.stdout(Stdio::piped())
6972
.output()
7073
.map(print_output)
@@ -75,14 +78,24 @@ fn client_unauthenticated() {
7578
// server is authenticated, client has no creds
7679
let openssl_secure_output = Command::new("tests/maybe-valgrind.sh")
7780
.env("LD_LIBRARY_PATH", "")
78-
.args(["target/client", "localhost", "4443", "test-ca/rsa/ca.cert"])
81+
.args([
82+
"target/client",
83+
"localhost",
84+
&port_str,
85+
"test-ca/rsa/ca.cert",
86+
])
7987
.stdout(Stdio::piped())
8088
.output()
8189
.map(print_output)
8290
.unwrap();
8391

8492
let rustls_secure_output = Command::new("tests/maybe-valgrind.sh")
85-
.args(["target/client", "localhost", "4443", "test-ca/rsa/ca.cert"])
93+
.args([
94+
"target/client",
95+
"localhost",
96+
&port_str,
97+
"test-ca/rsa/ca.cert",
98+
])
8699
.stdout(Stdio::piped())
87100
.output()
88101
.map(print_output)
@@ -96,7 +109,7 @@ fn client_unauthenticated() {
96109
.args([
97110
"target/client",
98111
"localhost",
99-
"4443",
112+
&port_str,
100113
"test-ca/rsa/ca.cert",
101114
"test-ca/rsa/client.key",
102115
"test-ca/rsa/client.cert",
@@ -110,7 +123,7 @@ fn client_unauthenticated() {
110123
.args([
111124
"target/client",
112125
"localhost",
113-
"4443",
126+
&port_str,
114127
"test-ca/rsa/ca.cert",
115128
"test-ca/rsa/client.key",
116129
"test-ca/rsa/client.cert",
@@ -126,6 +139,8 @@ fn client_unauthenticated() {
126139
#[test]
127140
#[ignore]
128141
fn client_auth() {
142+
let (port, port_str) = choose_port();
143+
129144
let _server = KillOnDrop(Some(
130145
Command::new("openssl")
131146
.args([
@@ -143,23 +158,23 @@ fn client_auth() {
143158
"-CAfile",
144159
"test-ca/rsa/ca.cert",
145160
"-accept",
146-
"localhost:4444",
161+
&format!("localhost:{port}"),
147162
"-rev",
148163
])
149164
.env("LD_LIBRARY_PATH", "")
150165
.spawn()
151166
.expect("failed to start openssl s_server"),
152167
));
153168

154-
wait_for_port(4444);
169+
wait_for_port(port);
155170

156171
// mutual auth
157172
let openssl_authed_output = Command::new("tests/maybe-valgrind.sh")
158173
.env("LD_LIBRARY_PATH", "")
159174
.args([
160175
"target/client",
161176
"localhost",
162-
"4444",
177+
&port_str,
163178
"test-ca/rsa/ca.cert",
164179
"test-ca/rsa/client.key",
165180
"test-ca/rsa/client.cert",
@@ -173,7 +188,7 @@ fn client_auth() {
173188
.args([
174189
"target/client",
175190
"localhost",
176-
"4444",
191+
&port_str,
177192
"test-ca/rsa/ca.cert",
178193
"test-ca/rsa/client.key",
179194
"test-ca/rsa/client.cert",
@@ -188,14 +203,24 @@ fn client_auth() {
188203
// failed auth
189204
let openssl_failed_output = Command::new("tests/maybe-valgrind.sh")
190205
.env("LD_LIBRARY_PATH", "")
191-
.args(["target/client", "localhost", "4444", "test-ca/rsa/ca.cert"])
206+
.args([
207+
"target/client",
208+
"localhost",
209+
&port_str,
210+
"test-ca/rsa/ca.cert",
211+
])
192212
.stdout(Stdio::piped())
193213
.output()
194214
.map(print_output)
195215
.unwrap();
196216

197217
let rustls_failed_output = Command::new("tests/maybe-valgrind.sh")
198-
.args(["target/client", "localhost", "4444", "test-ca/rsa/ca.cert"])
218+
.args([
219+
"target/client",
220+
"localhost",
221+
&port_str,
222+
"test-ca/rsa/ca.cert",
223+
])
199224
.stdout(Stdio::piped())
200225
.output()
201226
.map(print_output)
@@ -273,14 +298,16 @@ fn ciphers() {
273298
#[test]
274299
#[ignore]
275300
fn server() {
276-
fn curl() {
301+
let (port, port_str) = choose_port();
302+
303+
fn curl(port: u16) {
277304
Command::new("curl")
278305
.env("LD_LIBRARY_PATH", "")
279306
.args([
280307
"-v",
281308
"--cacert",
282309
"test-ca/rsa/ca.cert",
283-
"https://localhost:5555/",
310+
&format!("https://localhost:{port}/"),
284311
])
285312
.stdout(Stdio::piped())
286313
.output()
@@ -293,7 +320,7 @@ fn server() {
293320
.env("LD_LIBRARY_PATH", "")
294321
.args([
295322
"target/server",
296-
"5555",
323+
&port_str,
297324
"test-ca/rsa/server.key",
298325
"test-ca/rsa/server.cert",
299326
"unauth",
@@ -304,15 +331,15 @@ fn server() {
304331
.unwrap(),
305332
));
306333
wait_for_stdout(openssl_server.0.as_mut().unwrap(), b"listening\n");
307-
curl();
334+
curl(port);
308335

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

311338
let mut rustls_server = KillOnDrop(Some(
312339
Command::new("tests/maybe-valgrind.sh")
313340
.args([
314341
"target/server",
315-
"5555",
342+
&port_str,
316343
"test-ca/rsa/server.key",
317344
"test-ca/rsa/server.cert",
318345
"unauth",
@@ -323,20 +350,20 @@ fn server() {
323350
.unwrap(),
324351
));
325352
wait_for_stdout(rustls_server.0.as_mut().unwrap(), b"listening\n");
326-
curl();
353+
curl(port);
327354

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

332359
fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str) {
333-
fn connect(key_type: &str, sig_algs: &str, version_flag: &str) {
360+
fn connect(port: u16, key_type: &str, sig_algs: &str, version_flag: &str) {
334361
Command::new("openssl")
335362
.env("LD_LIBRARY_PATH", "")
336363
.args([
337364
"s_client",
338365
"-connect",
339-
"localhost:5556",
366+
&format!("localhost:{port}"),
340367
"-sigalgs",
341368
sig_algs,
342369
"-CAfile",
@@ -351,12 +378,14 @@ fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str)
351378
.unwrap();
352379
}
353380

381+
let (port, port_str) = choose_port();
382+
354383
let mut openssl_server = KillOnDrop(Some(
355384
Command::new("tests/maybe-valgrind.sh")
356385
.env("LD_LIBRARY_PATH", "")
357386
.args([
358387
"target/server",
359-
"5556",
388+
&port_str,
360389
&format!("test-ca/{key_type}/server.key"),
361390
&format!("test-ca/{key_type}/server.cert"),
362391
"unauth",
@@ -367,15 +396,15 @@ fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str)
367396
.unwrap(),
368397
));
369398
wait_for_stdout(openssl_server.0.as_mut().unwrap(), b"listening\n");
370-
connect(key_type, sig_algs, version_flag);
399+
connect(port, key_type, sig_algs, version_flag);
371400

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

374403
let mut rustls_server = KillOnDrop(Some(
375404
Command::new("tests/maybe-valgrind.sh")
376405
.args([
377406
"target/server",
378-
"5556",
407+
&port_str,
379408
&format!("test-ca/{key_type}/server.key"),
380409
&format!("test-ca/{key_type}/server.cert"),
381410
"unauth",
@@ -386,7 +415,7 @@ fn server_with_key_algorithm(key_type: &str, sig_algs: &str, version_flag: &str)
386415
.unwrap(),
387416
));
388417
wait_for_stdout(rustls_server.0.as_mut().unwrap(), b"listening\n");
389-
connect(key_type, sig_algs, version_flag);
418+
connect(port, key_type, sig_algs, version_flag);
390419

391420
let rustls_output = print_output(rustls_server.take_inner().wait_with_output().unwrap());
392421
assert_eq!(openssl_output, rustls_output);
@@ -581,3 +610,9 @@ fn wait_for_stdout(stream: &mut Child, expected: &[u8]) {
581610
};
582611
}
583612
}
613+
614+
fn choose_port() -> (u16, String) {
615+
static NEXT_PORT: atomic::AtomicU16 = atomic::AtomicU16::new(5555);
616+
let port = NEXT_PORT.fetch_add(1, atomic::Ordering::SeqCst);
617+
(port, port.to_string())
618+
}

0 commit comments

Comments
 (0)