• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #![cfg(unix)]
6 
7 use std::path::Path;
8 
9 use base::safe_descriptor_from_path;
10 use base::Error;
11 use base::FromRawDescriptor;
12 use base::SafeDescriptor;
13 use libc::EBADF;
14 use libc::EINVAL;
15 
16 /// Runs all unix specific integration tests in a single binary.
17 mod net;
18 mod scoped_signal_handler;
19 mod syslog;
20 mod tube;
21 
22 #[test]
safe_descriptor_from_path_valid()23 fn safe_descriptor_from_path_valid() {
24     assert!(safe_descriptor_from_path(Path::new("/proc/self/fd/2"))
25         .unwrap()
26         .is_some());
27 }
28 
29 #[test]
safe_descriptor_from_path_invalid_integer()30 fn safe_descriptor_from_path_invalid_integer() {
31     assert_eq!(
32         safe_descriptor_from_path(Path::new("/proc/self/fd/blah")),
33         Err(Error::new(EINVAL))
34     );
35 }
36 
37 #[test]
safe_descriptor_from_path_invalid_fd()38 fn safe_descriptor_from_path_invalid_fd() {
39     assert_eq!(
40         safe_descriptor_from_path(Path::new("/proc/self/fd/42")),
41         Err(Error::new(EBADF))
42     );
43 }
44 
45 #[test]
safe_descriptor_from_path_none()46 fn safe_descriptor_from_path_none() {
47     assert_eq!(
48         safe_descriptor_from_path(Path::new("/something/else")).unwrap(),
49         None
50     );
51 }
52 
53 #[test]
54 #[allow(clippy::eq_op)]
clone_equality()55 fn clone_equality() {
56     let ret = unsafe { libc::eventfd(0, 0) };
57     if ret < 0 {
58         panic!("failed to create eventfd");
59     }
60     let descriptor = unsafe { SafeDescriptor::from_raw_descriptor(ret) };
61 
62     assert_eq!(descriptor, descriptor);
63 
64     assert_eq!(
65         descriptor,
66         descriptor.try_clone().expect("failed to clone eventfd")
67     );
68 
69     let ret = unsafe { libc::eventfd(0, 0) };
70     if ret < 0 {
71         panic!("failed to create eventfd");
72     }
73     let another = unsafe { SafeDescriptor::from_raw_descriptor(ret) };
74 
75     assert_ne!(descriptor, another);
76 }
77