xref: /aosp_15_r20/tools/netsim/rust/libslirp-rs/tests/integration_test.rs (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1 // Copyright 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use bytes::Bytes;
16 use libslirp_rs::libslirp::LibSlirp;
17 use libslirp_rs::libslirp_config::SlirpConfig;
18 
19 use std::io;
20 use std::sync::mpsc;
21 use std::time::Duration;
22 
23 /// Test whether shutdown closes the rx channel
24 #[test]
it_shutdown()25 fn it_shutdown() {
26     let config = SlirpConfig { ..Default::default() };
27 
28     let before_fd_count = count_open_fds().unwrap();
29 
30     let (tx, rx) = mpsc::channel::<Bytes>();
31     let slirp = LibSlirp::new(config, tx, None);
32     slirp.shutdown();
33     assert_eq!(
34         rx.recv_timeout(Duration::from_millis(5)),
35         Err(mpsc::RecvTimeoutError::Disconnected)
36     );
37 
38     let after_fd_count = count_open_fds().unwrap();
39     assert_eq!(before_fd_count, after_fd_count);
40 }
41 
42 #[cfg(target_os = "linux")]
count_open_fds() -> io::Result<usize>43 fn count_open_fds() -> io::Result<usize> {
44     use std::fs;
45     let entries = fs::read_dir("/proc/self/fd")?;
46     Ok(entries.count())
47 }
48 
49 #[cfg(not(target_os = "linux"))]
count_open_fds() -> io::Result<usize>50 fn count_open_fds() -> io::Result<usize> {
51     Ok(0)
52 }
53