Debian Codegraph @

third_party/rust/tokio/tests/net_panic.rs

raw
1 #![warn(rust_2018_idioms)]
2 #![cfg(all(feature = "full", not(target_os = "wasi")))]
3 #![cfg(panic = "unwind")]
4
5 use std::error::Error;
6 use tokio::net::{TcpListener, TcpStream};
7 use tokio::runtime::{Builder, Runtime};
8
9 mod support {
10 pub mod panic;
11 }
12 use support::panic::test_panic;
13
14 #[test]
15 fn udp_socket_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
16 use std::net::SocketAddr;
17 use tokio::net::UdpSocket;
18
19 let addr = "127.0.0.1:0".parse::<SocketAddr>().unwrap();
20 let std_sock = std::net::UdpSocket::bind(addr).unwrap();
21 std_sock.set_nonblocking(true).unwrap();
22
23 let panic_location_file = test_panic(|| {
24 let rt = runtime_without_io();
25 rt.block_on(async {
26 let _sock = UdpSocket::from_std(std_sock);
27 });
28 });
29
30 // The panic location should be in this file
31 assert_eq!(&panic_location_file.unwrap(), file!());
32
33 Ok(())
34 }
35
36 #[test]
37 fn tcp_listener_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
38 let std_listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
39 std_listener.set_nonblocking(true).unwrap();
40
41 let panic_location_file = test_panic(|| {
42 let rt = runtime_without_io();
43 rt.block_on(async {
44 let _ = TcpListener::from_std(std_listener);
45 });
46 });
47
48 // The panic location should be in this file
49 assert_eq!(&panic_location_file.unwrap(), file!());
50
51 Ok(())
52 }
53
54 #[test]
55 fn tcp_stream_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
56 let std_listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
57
58 let std_stream = std::net::TcpStream::connect(std_listener.local_addr().unwrap()).unwrap();
59 std_stream.set_nonblocking(true).unwrap();
60
61 let panic_location_file = test_panic(|| {
62 let rt = runtime_without_io();
63 rt.block_on(async {
64 let _ = TcpStream::from_std(std_stream);
65 });
66 });
67
68 // The panic location should be in this file
69 assert_eq!(&panic_location_file.unwrap(), file!());
70
71 Ok(())
72 }
73
74 #[test]
75 #[cfg(unix)]
76 fn unix_listener_bind_panic_caller() -> Result<(), Box<dyn Error>> {
77 use tokio::net::UnixListener;
78
79 let dir = tempfile::tempdir().unwrap();
80 let sock_path = dir.path().join("socket");
81
82 let panic_location_file = test_panic(|| {
83 let rt = runtime_without_io();
84 rt.block_on(async {
85 let _ = UnixListener::bind(&sock_path);
86 });
87 });
88
89 // The panic location should be in this file
90 assert_eq!(&panic_location_file.unwrap(), file!());
91
92 Ok(())
93 }
94
95 #[test]
96 #[cfg(unix)]
97 fn unix_listener_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
98 use tokio::net::UnixListener;
99
100 let dir = tempfile::tempdir().unwrap();
101 let sock_path = dir.path().join("socket");
102 let std_listener = std::os::unix::net::UnixListener::bind(sock_path).unwrap();
103
104 let panic_location_file = test_panic(|| {
105 let rt = runtime_without_io();
106 rt.block_on(async {
107 let _ = UnixListener::from_std(std_listener);
108 });
109 });
110
111 // The panic location should be in this file
112 assert_eq!(&panic_location_file.unwrap(), file!());
113
114 Ok(())
115 }
116
117 #[test]
118 #[cfg(unix)]
119 fn unix_stream_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
120 use tokio::net::UnixStream;
121
122 let dir = tempfile::tempdir().unwrap();
123 let sock_path = dir.path().join("socket");
124 let _std_listener = std::os::unix::net::UnixListener::bind(&sock_path).unwrap();
125 let std_stream = std::os::unix::net::UnixStream::connect(&sock_path).unwrap();
126
127 let panic_location_file = test_panic(|| {
128 let rt = runtime_without_io();
129 rt.block_on(async {
130 let _ = UnixStream::from_std(std_stream);
131 });
132 });
133
134 // The panic location should be in this file
135 assert_eq!(&panic_location_file.unwrap(), file!());
136
137 Ok(())
138 }
139
140 #[test]
141 #[cfg(unix)]
142 fn unix_datagram_from_std_panic_caller() -> Result<(), Box<dyn Error>> {
143 use std::os::unix::net::UnixDatagram as StdUDS;
144 use tokio::net::UnixDatagram;
145
146 let dir = tempfile::tempdir().unwrap();
147 let sock_path = dir.path().join("socket");
148
149 // Bind the socket to a filesystem path
150 // /let socket_path = tmp.path().join("socket");
151 let std_socket = StdUDS::bind(sock_path).unwrap();
152 std_socket.set_nonblocking(true).unwrap();
153
154 let panic_location_file = test_panic(move || {
155 let rt = runtime_without_io();
156 rt.block_on(async {
157 let _ = UnixDatagram::from_std(std_socket);
158 });
159 });
160
161 // The panic location should be in this file
162 assert_eq!(&panic_location_file.unwrap(), file!());
163
164 Ok(())
165 }
166
167 #[test]
168 #[cfg(windows)]
169 fn server_options_max_instances_panic_caller() -> Result<(), Box<dyn Error>> {
170 use tokio::net::windows::named_pipe::ServerOptions;
171
172 let panic_location_file = test_panic(move || {
173 let rt = runtime_without_io();
174 rt.block_on(async {
175 let mut options = ServerOptions::new();
176 options.max_instances(255);
177 });
178 });
179
180 // The panic location should be in this file
181 assert_eq!(&panic_location_file.unwrap(), file!());
182
183 Ok(())
184 }
185
186 // Runtime without `enable_io` so it has no IO driver set.
187 fn runtime_without_io() -> Runtime {
188 Builder::new_current_thread().build().unwrap()
189 }