crosvm/base/tests/unix/main.rs
Dennis Kempin 9a5dcbc68d base: Spawn separate process for executing process tests
These tests will use fork, which does not work in a multi-threaded
process. This is currently enforced by passing --test-threads 1 to
this test, but that won't work with cargo nextest.

To ensure that the test process is single-threaded, re-execute the
test binary as a child process.

BUG=b:261600801
TEST=presubmit

Change-Id: I1762ebbae66331895f84df9daa484468f1fe086d
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4086902
Commit-Queue: Dennis Kempin <denniskempin@google.com>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Auto-Submit: Dennis Kempin <denniskempin@google.com>
2022-12-07 19:19:46 +00:00

76 lines
1.7 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(unix)]
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 scoped_signal_handler;
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() {
let ret = unsafe { libc::eventfd(0, 0) };
if ret < 0 {
panic!("failed to create eventfd");
}
let descriptor = unsafe { SafeDescriptor::from_raw_descriptor(ret) };
assert_eq!(descriptor, descriptor);
assert_eq!(
descriptor,
descriptor.try_clone().expect("failed to clone eventfd")
);
let ret = unsafe { libc::eventfd(0, 0) };
if ret < 0 {
panic!("failed to create eventfd");
}
let another = unsafe { SafeDescriptor::from_raw_descriptor(ret) };
assert_ne!(descriptor, another);
}