1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use std::{ffi::OsString, net::TcpListener};
use pretty_assertions::assert_eq;
// Covers: a re-execution passes on the arguments the server started with
// and drops the socket an earlier one handed it, so an HTTP server upgraded
// again and again hands on exactly one.
// Owner: shared HTTP in-place upgrade.
#[test]
fn a_relaunch_keeps_the_arguments_but_an_earlier_socket() {
let args = |args: &[&str]| args.iter().map(OsString::from).collect::<Vec<_>>();
let cases = [
(
args(&[
"mcp",
"serve",
"--http",
"127.0.0.1:8765",
"--name",
"lab-box",
]),
args(&[
"mcp",
"serve",
"--http",
"127.0.0.1:8765",
"--name",
"lab-box",
]),
),
(
args(&[
"mcp",
"serve",
"--http",
"127.0.0.1:8765",
"--listener-fd",
"7",
]),
args(&["mcp", "serve", "--http", "127.0.0.1:8765"]),
),
(
args(&[
"daemon",
"serve",
"--http",
"127.0.0.1:8766",
"--allow-host",
"rho.example.com",
"--listener-fd",
"9",
]),
args(&[
"daemon",
"serve",
"--http",
"127.0.0.1:8766",
"--allow-host",
"rho.example.com",
]),
),
];
for (given, kept) in cases {
assert_eq!(super::without_listener_fd(given.into_iter()), kept);
}
}
// Covers: adopting an inherited listener rejects a socket bound to a different
// address/port than `--http` requested, and accepts a matching address or port 0.
// Owner: shared HTTP in-place upgrade.
#[tokio::test]
async fn adopted_listener_checks_bound_address() {
let std_listener = TcpListener::bind("127.0.0.1:0").unwrap();
std_listener.set_nonblocking(true).unwrap();
let local = std_listener.local_addr().unwrap();
let wrong = std::net::SocketAddr::from(([127, 0, 0, 1], local.port().wrapping_add(1).max(1)));
let dup = std_listener.try_clone().unwrap();
assert!(super::adopted_listener(dup, wrong).is_err());
let wildcard_port = std::net::SocketAddr::from(([127, 0, 0, 1], 0));
let dup = std_listener.try_clone().unwrap();
assert_eq!(
super::adopted_listener(dup, wildcard_port)
.unwrap()
.local_addr()
.unwrap(),
local
);
assert_eq!(
super::adopted_listener(std_listener, local)
.unwrap()
.local_addr()
.unwrap(),
local
);
}