From 7449705a98a241bbbbbc691df5f4d29ced405f28 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Tue, 19 May 2026 03:12:41 +0100 Subject: [PATCH] fix(tests): refine feature checks for SELinux --- tests/by-util/test_chcon.rs | 5 +++- tests/by-util/test_cp.rs | 50 ++++++++++++++++++++++++++++------- tests/by-util/test_id.rs | 5 +++- tests/by-util/test_install.rs | 20 +++++++++++--- tests/by-util/test_ls.rs | 35 +++++++++++++++++++----- tests/by-util/test_mkdir.rs | 15 ++++++++--- tests/by-util/test_mkfifo.rs | 15 ++++++++--- tests/by-util/test_mknod.rs | 20 +++++++++++--- tests/by-util/test_mv.rs | 10 +++++-- tests/by-util/test_runcon.rs | 15 ++++++++--- tests/by-util/test_stat.rs | 5 +++- 11 files changed, 156 insertions(+), 39 deletions(-) diff --git a/tests/by-util/test_chcon.rs b/tests/by-util/test_chcon.rs index 12c8c6e85..f90bbdb0c 100644 --- a/tests/by-util/test_chcon.rs +++ b/tests/by-util/test_chcon.rs @@ -4,7 +4,10 @@ // file that was distributed with this source code. // spell-checker:ignore (jargon) xattributes #![allow(clippy::missing_errors_doc, clippy::similar_names)] -#![cfg(feature = "feat_selinux")] +#![cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] use std::ffi::CString; use std::path::Path; diff --git a/tests/by-util/test_cp.rs b/tests/by-util/test_cp.rs index f0ec1d68c..8a178d172 100644 --- a/tests/by-util/test_cp.rs +++ b/tests/by-util/test_cp.rs @@ -8,7 +8,10 @@ #[cfg(unix)] use rstest::rstest; use uucore::display::Quotable; -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] use uucore::selinux::get_getfattr_output; use uutests::util::TestScenario; use uutests::{at_and_ucmd, new_ucmd, path_concat, util_name}; @@ -6771,7 +6774,10 @@ fn test_cp_from_stream_permission() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_cp_selinux() { let ts = TestScenario::new(util_name!()); let at = &ts.fixtures; @@ -6820,7 +6826,10 @@ fn test_cp_selinux_invalid() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_cp_preserve_selinux() { let ts = TestScenario::new(util_name!()); let at = &ts.fixtures; @@ -6858,7 +6867,10 @@ fn test_cp_preserve_selinux() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_cp_preserve_selinux_admin_context() { let ts = TestScenario::new(util_name!()); let at = &ts.fixtures; @@ -6917,7 +6929,10 @@ fn test_cp_preserve_selinux_admin_context() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_cp_selinux_context_priority() { // This test verifies that -Z takes priority over --context @@ -6997,7 +7012,10 @@ fn test_cp_selinux_context_priority() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_cp_selinux_empty_context() { // This test verifies that --context without a value works like -Z @@ -7043,7 +7061,10 @@ fn test_cp_selinux_empty_context() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_cp_selinux_recursive() { // Test SELinux context preservation in recursive directory copies @@ -7097,7 +7118,10 @@ fn test_cp_selinux_recursive() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_cp_preserve_context_root() { use uutests::util::run_ucmd_as_root; let scene = TestScenario::new(util_name!()); @@ -7800,7 +7824,10 @@ fn test_cp_gnu_preserve_mode() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_cp_a_z_overrides_context() { // Verifies -aZ succeeds (-Z overrides implicit --preserve=context from -a) use std::path::Path; @@ -7818,7 +7845,10 @@ fn test_cp_a_z_overrides_context() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_cp_a_preserves_context() { use std::path::Path; use uucore::selinux::{get_selinux_security_context, set_selinux_security_context}; diff --git a/tests/by-util/test_id.rs b/tests/by-util/test_id.rs index dd2b97799..1a30b75c9 100644 --- a/tests/by-util/test_id.rs +++ b/tests/by-util/test_id.rs @@ -383,7 +383,10 @@ fn test_id_zero() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_id_context() { if !uucore::selinux::is_selinux_enabled() { println!("test skipped: Kernel has no support for SElinux context"); diff --git a/tests/by-util/test_install.rs b/tests/by-util/test_install.rs index afb10eaa2..ec1c0e07a 100644 --- a/tests/by-util/test_install.rs +++ b/tests/by-util/test_install.rs @@ -15,7 +15,10 @@ use std::process; #[cfg(any(target_os = "linux", target_os = "android"))] use std::thread::sleep; use uucore::process::{getegid, geteuid}; -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] use uucore::selinux::get_getfattr_output; use uutests::at_and_ucmd; use uutests::new_ucmd; @@ -2334,7 +2337,10 @@ fn test_install_no_target_basic() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_selinux() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; @@ -2383,7 +2389,10 @@ fn test_selinux() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_selinux_invalid_args() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; @@ -2416,7 +2425,10 @@ fn test_selinux_invalid_args() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_selinux_default_context() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; diff --git a/tests/by-util/test_ls.rs b/tests/by-util/test_ls.rs index 7fcad9d0f..7afedc35a 100644 --- a/tests/by-util/test_ls.rs +++ b/tests/by-util/test_ls.rs @@ -4654,7 +4654,10 @@ fn test_ls_dangling_symlinks() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_ls_context1() { if !uucore::selinux::is_selinux_enabled() { println!("test skipped: Kernel has no support for SElinux context"); @@ -4669,7 +4672,10 @@ fn test_ls_context1() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_ls_context2() { if !uucore::selinux::is_selinux_enabled() { println!("test skipped: Kernel has no support for SElinux context"); @@ -4685,7 +4691,10 @@ fn test_ls_context2() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_ls_context_long() { if !uucore::selinux::is_selinux_enabled() { return; @@ -4704,7 +4713,10 @@ fn test_ls_context_long() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_ls_context_format() { if !uucore::selinux::is_selinux_enabled() { println!("test skipped: Kernel has no support for SElinux context"); @@ -4734,7 +4746,10 @@ fn test_ls_context_format() { } /// Helper function to validate `SELinux` context format -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn validate_selinux_context(context: &str) { assert!( context.contains(':'), @@ -4749,7 +4764,10 @@ fn validate_selinux_context(context: &str) { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_ls_selinux_context_format() { if !uucore::selinux::is_selinux_enabled() { println!("test skipped: Kernel has no support for SElinux context"); @@ -4782,7 +4800,10 @@ fn test_ls_selinux_context_format() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_ls_selinux_context_indicator() { if !uucore::selinux::is_selinux_enabled() { println!("test skipped: Kernel has no support for SElinux context"); diff --git a/tests/by-util/test_mkdir.rs b/tests/by-util/test_mkdir.rs index fb919ac0a..17c124225 100644 --- a/tests/by-util/test_mkdir.rs +++ b/tests/by-util/test_mkdir.rs @@ -11,7 +11,10 @@ use libc::mode_t; #[cfg(not(windows))] use std::os::unix::fs::PermissionsExt; -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] use uucore::selinux::get_getfattr_output; #[cfg(not(windows))] use uutests::at_and_ucmd; @@ -449,7 +452,10 @@ fn test_empty_argument() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_selinux() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; @@ -473,7 +479,10 @@ fn test_selinux() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_selinux_invalid() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; diff --git a/tests/by-util/test_mkfifo.rs b/tests/by-util/test_mkfifo.rs index 4d19da636..02fa91b2a 100644 --- a/tests/by-util/test_mkfifo.rs +++ b/tests/by-util/test_mkfifo.rs @@ -5,7 +5,10 @@ // spell-checker:ignore nconfined -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] use uucore::selinux::get_getfattr_output; use uutests::new_ucmd; use uutests::util::TestScenario; @@ -167,7 +170,10 @@ fn test_create_fifo_permission_denied() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_mkfifo_selinux() { let ts = TestScenario::new(util_name!()); let at = &ts.fixtures; @@ -191,7 +197,10 @@ fn test_mkfifo_selinux() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_mkfifo_selinux_invalid() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; diff --git a/tests/by-util/test_mknod.rs b/tests/by-util/test_mknod.rs index 1bcae7ea3..a19a4e4d7 100644 --- a/tests/by-util/test_mknod.rs +++ b/tests/by-util/test_mknod.rs @@ -7,7 +7,10 @@ use std::os::unix::fs::PermissionsExt; -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] use uucore::selinux::get_getfattr_output; use uutests::new_ucmd; use uutests::util::TestScenario; @@ -184,7 +187,10 @@ fn test_mknod_mode_comma_separated() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_mknod_selinux() { let ts = TestScenario::new(util_name!()); let at = &ts.fixtures; @@ -215,7 +221,10 @@ fn test_mknod_selinux() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_mknod_selinux_invalid() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; @@ -242,7 +251,10 @@ fn test_mknod_selinux_invalid() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_mknod_selinux_invalid_cleanup() { let scene = TestScenario::new(util_name!()); let at = &scene.fixtures; diff --git a/tests/by-util/test_mv.rs b/tests/by-util/test_mv.rs index 9fbc7c574..a8cb184af 100644 --- a/tests/by-util/test_mv.rs +++ b/tests/by-util/test_mv.rs @@ -10,7 +10,10 @@ use rstest::rstest; use std::io::Write; #[cfg(not(windows))] use std::path::Path; -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] use uucore::selinux::get_getfattr_output; use uutests::new_ucmd; #[cfg(unix)] @@ -2617,7 +2620,10 @@ fn test_mv_cross_device_permission_denied() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_mv_selinux_context() { let test_cases = [ ("-Z", None), diff --git a/tests/by-util/test_runcon.rs b/tests/by-util/test_runcon.rs index 39a73d883..3d85b8f7b 100644 --- a/tests/by-util/test_runcon.rs +++ b/tests/by-util/test_runcon.rs @@ -4,7 +4,10 @@ // file that was distributed with this source code. // spell-checker:ignore (jargon) xattributes -#![cfg(feature = "feat_selinux")] +#![cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] use uutests::new_ucmd; @@ -84,7 +87,10 @@ fn invalid() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn plain_context() { let ctx = "unconfined_u:unconfined_r:unconfined_t:s0-s0"; new_ucmd!().args(&[ctx, "/bin/true"]).succeeds(); @@ -103,7 +109,10 @@ fn plain_context() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn custom_context() { let t_ud = "unconfined_t"; let u_ud = "unconfined_u"; diff --git a/tests/by-util/test_stat.rs b/tests/by-util/test_stat.rs index c536a7744..d73dcf9b8 100644 --- a/tests/by-util/test_stat.rs +++ b/tests/by-util/test_stat.rs @@ -550,7 +550,10 @@ fn test_printf_invalid_directive() { } #[test] -#[cfg(feature = "feat_selinux")] +#[cfg(all( + feature = "feat_selinux", + any(target_os = "linux", target_os = "android") +))] fn test_stat_selinux() { let ts = TestScenario::new(util_name!()); let at = &ts.fixtures;