mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2024-11-24 20:48:55 +00:00
2768f223ee
BUG=b:316174930 TEST=none Change-Id: I5c7811b2c548155aa003e4b71a54bbc16e2f2588 Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/5120567 Commit-Queue: Vikram Auradkar <auradkar@google.com> Reviewed-by: Dennis Kempin <denniskempin@google.com>
80 lines
2 KiB
Rust
80 lines
2 KiB
Rust
// Copyright 2022 The ChromiumOS Authors
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
#![cfg(any(target_os = "android", target_os = "linux"))]
|
|
|
|
use std::path::Path;
|
|
|
|
use base::safe_descriptor_from_path;
|
|
use base::Error;
|
|
use base::FromRawDescriptor;
|
|
use base::SafeDescriptor;
|
|
use libc::EBADF;
|
|
use libc::EINVAL;
|
|
|
|
/// Runs all unix specific integration tests in a single binary.
|
|
mod net;
|
|
mod syslog;
|
|
mod tube;
|
|
|
|
#[test]
|
|
fn safe_descriptor_from_path_valid() {
|
|
assert!(safe_descriptor_from_path(Path::new("/proc/self/fd/2"))
|
|
.unwrap()
|
|
.is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn safe_descriptor_from_path_invalid_integer() {
|
|
assert_eq!(
|
|
safe_descriptor_from_path(Path::new("/proc/self/fd/blah")),
|
|
Err(Error::new(EINVAL))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn safe_descriptor_from_path_invalid_fd() {
|
|
assert_eq!(
|
|
safe_descriptor_from_path(Path::new("/proc/self/fd/42")),
|
|
Err(Error::new(EBADF))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn safe_descriptor_from_path_none() {
|
|
assert_eq!(
|
|
safe_descriptor_from_path(Path::new("/something/else")).unwrap(),
|
|
None
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
#[allow(clippy::eq_op)]
|
|
fn clone_equality() {
|
|
// SAFETY: Safe because return value is checked.
|
|
let ret = unsafe { libc::eventfd(0, 0) };
|
|
if ret < 0 {
|
|
panic!("failed to create eventfd");
|
|
}
|
|
// SAFETY: Safe because ret is valid and return value is checked.
|
|
let descriptor = unsafe { SafeDescriptor::from_raw_descriptor(ret) };
|
|
|
|
assert_eq!(descriptor, descriptor);
|
|
|
|
assert_eq!(
|
|
descriptor,
|
|
descriptor.try_clone().expect("failed to clone eventfd")
|
|
);
|
|
|
|
// SAFETY: Safe because return value is checked.
|
|
let ret = unsafe { libc::eventfd(0, 0) };
|
|
if ret < 0 {
|
|
panic!("failed to create eventfd");
|
|
}
|
|
|
|
// SAFETY: Safe because ret is valid and return value is checked.
|
|
let another = unsafe { SafeDescriptor::from_raw_descriptor(ret) };
|
|
|
|
assert_ne!(descriptor, another);
|
|
}
|