Skip to content

Commit

Permalink
Remove use of auv crate from tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bjorn3 authored and Amanieu committed Oct 10, 2023
1 parent 33ec0e3 commit 88a9c46
Showing 1 changed file with 11 additions and 48 deletions.
59 changes: 11 additions & 48 deletions crates/std_detect/src/detect/os/linux/auxvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub(crate) const AT_HWCAP2: usize = 26;
/// If an entry cannot be read all the bits in the bitfield are set to zero.
/// This should be interpreted as all the features being disabled.
#[derive(Debug, Copy, Clone)]
#[cfg_attr(test, derive(PartialEq))]
pub(crate) struct AuxVec {
pub hwcap: usize,
#[cfg(any(
Expand Down Expand Up @@ -174,9 +175,12 @@ pub(crate) fn auxv() -> Result<AuxVec, ()> {
/// Tries to read the `key` from the auxiliary vector by calling the
/// dynamically-linked `getauxval` function. If the function is not linked,
/// this function return `Err`.
#[cfg(all(
feature = "std_detect_dlsym_getauxval",
not(all(target_os = "linux", target_env = "gnu"))
#[cfg(any(
test,
all(
feature = "std_detect_dlsym_getauxval",
not(all(target_os = "linux", target_env = "gnu"))
)
))]
fn getauxval(key: usize) -> Result<usize, ()> {
use libc;
Expand Down Expand Up @@ -262,35 +266,8 @@ fn auxv_from_buf(buf: &[usize]) -> Result<AuxVec, ()> {

#[cfg(test)]
mod tests {
extern crate auxv as auxv_crate;
use super::*;

// Reads the Auxiliary Vector key from /proc/self/auxv
// using the auxv crate.
#[cfg(feature = "std_detect_file_io")]
fn auxv_crate_getprocfs(key: usize) -> Option<usize> {
use self::auxv_crate::procfs::search_procfs_auxv;
use self::auxv_crate::AuxvType;
let k = key as AuxvType;
match search_procfs_auxv(&[k]) {
Ok(v) => Some(v[&k] as usize),
Err(_) => None,
}
}

// Reads the Auxiliary Vector key from getauxval()
// using the auxv crate.
#[cfg(not(any(target_arch = "mips", target_arch = "mips64")))]
fn auxv_crate_getauxval(key: usize) -> Option<usize> {
use self::auxv_crate::getauxval::Getauxval;
use self::auxv_crate::AuxvType;
let q = auxv_crate::getauxval::NativeGetauxval {};
match q.getauxval(key as AuxvType) {
Ok(v) => Some(v as usize),
Err(_) => None,
}
}

// FIXME: on mips/mips64 getauxval returns 0, and /proc/self/auxv
// does not always contain the AT_HWCAP key under qemu.
#[cfg(any(
Expand All @@ -301,7 +278,7 @@ mod tests {
#[test]
fn auxv_crate() {
let v = auxv();
if let Some(hwcap) = auxv_crate_getauxval(AT_HWCAP) {
if let Ok(hwcap) = getauxval(AT_HWCAP) {
let rt_hwcap = v.expect("failed to find hwcap key").hwcap;
assert_eq!(rt_hwcap, hwcap);
}
Expand All @@ -314,7 +291,7 @@ mod tests {
target_arch = "powerpc64"
))]
{
if let Some(hwcap2) = auxv_crate_getauxval(AT_HWCAP2) {
if let Ok(hwcap2) = getauxval(AT_HWCAP2) {
let rt_hwcap2 = v.expect("failed to find hwcap2 key").hwcap2;
assert_eq!(rt_hwcap2, hwcap2);
}
Expand Down Expand Up @@ -391,22 +368,8 @@ mod tests {
#[test]
#[cfg(feature = "std_detect_file_io")]
fn auxv_crate_procfs() {
let v = auxv();
if let Some(hwcap) = auxv_crate_getprocfs(AT_HWCAP) {
assert_eq!(v.unwrap().hwcap, hwcap);
}

// Targets with AT_HWCAP and AT_HWCAP2:
#[cfg(any(
target_arch = "aarch64",
target_arch = "arm",
target_arch = "powerpc",
target_arch = "powerpc64"
))]
{
if let Some(hwcap2) = auxv_crate_getprocfs(AT_HWCAP2) {
assert_eq!(v.unwrap().hwcap2, hwcap2);
}
if let Ok(procfs_auxv) = auxv_from_file("/proc/self/auxv") {
assert_eq!(auxv().unwrap(), procfs_auxv);
}
}
}

0 comments on commit 88a9c46

Please sign in to comment.