diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:02:58 +0000 |
commit | 698f8c2f01ea549d77d7dc3338a12e04c11057b9 (patch) | |
tree | 173a775858bd501c378080a10dca74132f05bc50 /vendor/rustix/tests/net/v6.rs | |
parent | Initial commit. (diff) | |
download | rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.tar.xz rustc-698f8c2f01ea549d77d7dc3338a12e04c11057b9.zip |
Adding upstream version 1.64.0+dfsg1.upstream/1.64.0+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/rustix/tests/net/v6.rs')
-rw-r--r-- | vendor/rustix/tests/net/v6.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/vendor/rustix/tests/net/v6.rs b/vendor/rustix/tests/net/v6.rs new file mode 100644 index 000000000..07205be89 --- /dev/null +++ b/vendor/rustix/tests/net/v6.rs @@ -0,0 +1,95 @@ +//! Test a simple IPv6 socket server and client. +//! +//! The client send a message and the server sends one back. + +#![cfg(not(any(target_os = "redox", target_os = "wasi")))] + +use rustix::net::{ + accept, bind_v6, connect_v6, getsockname, listen, recv, send, socket, AddressFamily, Ipv6Addr, + Protocol, RecvFlags, SendFlags, SocketAddrAny, SocketAddrV6, SocketType, +}; +use std::sync::{Arc, Condvar, Mutex}; +use std::thread; + +const BUFFER_SIZE: usize = 20; + +fn server(ready: Arc<(Mutex<u16>, Condvar)>) { + let connection_socket = socket( + AddressFamily::INET6, + SocketType::STREAM, + Protocol::default(), + ) + .unwrap(); + + let name = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), 0, 0, 0); + bind_v6(&connection_socket, &name).unwrap(); + + let who = match getsockname(&connection_socket).unwrap() { + SocketAddrAny::V6(addr) => addr, + _ => panic!(), + }; + + listen(&connection_socket, 1).unwrap(); + + { + let (lock, cvar) = &*ready; + let mut port = lock.lock().unwrap(); + *port = who.port(); + cvar.notify_all(); + } + + let mut buffer = vec![0; BUFFER_SIZE]; + let data_socket = accept(&connection_socket).unwrap(); + let nread = recv(&data_socket, &mut buffer, RecvFlags::empty()).unwrap(); + assert_eq!(String::from_utf8_lossy(&buffer[..nread]), "hello, world"); + + send(&data_socket, b"goodnight, moon", SendFlags::empty()).unwrap(); +} + +fn client(ready: Arc<(Mutex<u16>, Condvar)>) { + let port = { + let (lock, cvar) = &*ready; + let mut port = lock.lock().unwrap(); + while *port == 0 { + port = cvar.wait(port).unwrap(); + } + *port + }; + + let addr = SocketAddrV6::new(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1), port, 0, 0); + let mut buffer = vec![0; BUFFER_SIZE]; + + let data_socket = socket( + AddressFamily::INET6, + SocketType::STREAM, + Protocol::default(), + ) + .unwrap(); + connect_v6(&data_socket, &addr).unwrap(); + + send(&data_socket, b"hello, world", SendFlags::empty()).unwrap(); + + let nread = recv(&data_socket, &mut buffer, RecvFlags::empty()).unwrap(); + assert_eq!(String::from_utf8_lossy(&buffer[..nread]), "goodnight, moon"); +} + +#[test] +fn test_v6() { + let ready = Arc::new((Mutex::new(0_u16), Condvar::new())); + let ready_clone = Arc::clone(&ready); + + let server = thread::Builder::new() + .name("server".to_string()) + .spawn(move || { + server(ready); + }) + .unwrap(); + let client = thread::Builder::new() + .name("client".to_string()) + .spawn(move || { + client(ready_clone); + }) + .unwrap(); + client.join().unwrap(); + server.join().unwrap(); +} |