Mercurial > crates > systemd-socket
annotate tests/comm.rs @ 28:cfef4593e207
Run `cargo fmt`.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sat, 19 Apr 2025 01:33:50 -0400 |
parents | 1941e9d9819c |
children |
rev | line source |
---|---|
1
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
1 use std::ffi::OsStr; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
2 use std::io::{self, Read, Write}; |
28 | 3 use std::process::Child; |
1
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
4 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
5 pub trait Test { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
6 const SOCKET_ADDR: &'static str; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
7 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
8 fn spawn_slave(program_name: &OsStr) -> io::Result<Child>; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
9 } |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
10 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
11 const REQUEST: &[u8] = b"orange coin"; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
12 const RESPONSE: &[u8] = b"good"; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
13 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
14 fn main_master(slave: io::Result<Child>) { |
15
08b37039504b
Improved tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
8
diff
changeset
|
15 let mut slave = slave.expect("failed to run a child"); |
1
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
16 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
17 // give slave some time to bind the socket just to be sure |
8
372afb9a700f
Prolong waiting for bind
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
1
diff
changeset
|
18 std::thread::sleep(std::time::Duration::from_secs(5)); |
1
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
19 |
28 | 20 if let Some(exited) = slave |
21 .try_wait() | |
22 .expect("failed to check if the child exited") | |
23 { | |
15
08b37039504b
Improved tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
8
diff
changeset
|
24 panic!("child exited unexpectedly: {}", exited); |
08b37039504b
Improved tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
8
diff
changeset
|
25 } |
08b37039504b
Improved tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
8
diff
changeset
|
26 |
28 | 27 let mut client_socket = std::net::TcpStream::connect("localhost:4242") |
28 .expect("Failed to connect to 127.0.0.1:4242"); | |
29 client_socket | |
30 .write_all(REQUEST) | |
31 .expect("failed to send data"); | |
1
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
32 let mut buf = [0u8; RESPONSE.len()]; |
28 | 33 client_socket |
34 .read_exact(&mut buf) | |
35 .expect("failed to read response"); | |
1
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
36 assert_eq!(buf, RESPONSE); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
37 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
38 let status = slave.wait().expect("faild to wait for slave"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
39 if !status.success() { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
40 panic!("slave did not exit with succcess, status: {}", status); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
41 } |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
42 } |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
43 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
44 fn main_slave(addr: &str) { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
45 use systemd_socket::SocketAddr; |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
46 |
24
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
15
diff
changeset
|
47 // SAFETY: this is the only thread that's going to mess with systemd sockets. |
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
15
diff
changeset
|
48 unsafe { |
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
15
diff
changeset
|
49 systemd_socket::init_unprotected().unwrap(); |
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
15
diff
changeset
|
50 } |
1941e9d9819c
Fix unsound manipulation of env vars
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
15
diff
changeset
|
51 |
1
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
52 let socket = addr |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
53 .parse::<SocketAddr>() |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
54 .expect("failed to parse socket") |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
55 .bind() |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
56 .expect("failed to bind"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
57 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
58 let (mut client_socket, _) = socket.accept().expect("failed to accept"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
59 let mut buf = [0u8; REQUEST.len()]; |
28 | 60 client_socket |
61 .read_exact(&mut buf) | |
62 .expect("failed to read response"); | |
1
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
63 assert_eq!(buf, REQUEST); |
28 | 64 client_socket |
65 .write_all(RESPONSE) | |
66 .expect("failed to send data"); | |
1
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
67 } |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
68 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
69 pub fn main<T: Test>() { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
70 let mut args = std::env::args_os(); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
71 let program_name = args.next().expect("missing program name"); |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
72 |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
73 match std::env::var_os("SYSTEMD_SOCKET_INTEGRATION_TEST") { |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
74 None => main_master(T::spawn_slave(&program_name)), |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
75 Some(arg) if arg == "slave" => main_slave(T::SOCKET_ADDR), |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
76 Some(arg) => panic!("Unknown argument '{:?}'", arg), |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
77 } |
ef8bf41097ac
Added integration tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
diff
changeset
|
78 } |