From 817f1fc96d0c58ed50dab063feab6cb2f8bb1955 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Thu, 28 Dec 2023 01:08:56 +0100 Subject: [PATCH] install: Add a test to cover recent changes --- tests/by-util/test_install.rs | 57 +++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/tests/by-util/test_install.rs b/tests/by-util/test_install.rs index fb360533f11..90a8177b62a 100644 --- a/tests/by-util/test_install.rs +++ b/tests/by-util/test_install.rs @@ -4,9 +4,10 @@ // file that was distributed with this source code. // spell-checker:ignore (words) helloworld nodir objdump n'source -use crate::common::util::{is_ci, TestScenario}; +use crate::common::util::{is_ci, run_ucmd_as_root, TestScenario}; use filetime::FileTime; -use std::os::unix::fs::PermissionsExt; +use std::fs; +use std::os::unix::fs::{MetadataExt, PermissionsExt}; #[cfg(not(any(windows, target_os = "freebsd")))] use std::process::Command; #[cfg(any(target_os = "linux", target_os = "android"))] @@ -1613,3 +1614,55 @@ fn test_target_file_ends_with_slash() { .fails() .stderr_contains("failed to access 'dir/target_file/': Not a directory"); } + +#[test] +// Matches part of tests/install/install-C-root.sh +fn test_install_root() { + let ts = TestScenario::new(util_name!()); + let at = ts.fixtures.clone(); + at.touch("a"); + + let run_and_check = |args: &[&str], expected_uid: u32, expected_gid: u32| { + if let Ok(result) = run_ucmd_as_root(&ts, args) { + result.success(); + assert!(at.file_exists("b")); + assert!(result.stdout_str().contains("'a' -> 'b'\n")); + + // Check the UID and GID of the file + let metadata = fs::metadata(at.plus("b")).unwrap(); + assert_eq!(metadata.uid(), expected_uid); + assert_eq!(metadata.gid(), expected_gid); + } else { + print!("Test skipped; requires root user"); + } + }; + + run_and_check(&["-Cv", "-o1", "-g1", "a", "b"], 1, 1); + run_and_check(&["-Cv", "-o2", "-g1", "a", "b"], 2, 1); + run_and_check(&["-Cv", "-o2", "-g2", "a", "b"], 2, 2); +} + +#[test] +fn test_install_as_root() { + // Matches part of tests/install/install-C-root.sh + let ts = TestScenario::new(util_name!()); + let at = ts.fixtures.clone(); + at.touch("a"); + + let run_and_check_uid_gid = |args: &[&str], expected_uid: u32, expected_gid: u32| { + if let Ok(result) = run_ucmd_as_root(&ts, args) { + result.success(); + assert!(at.file_exists("b")); + + let metadata = fs::metadata(at.plus("b")).unwrap(); + assert_eq!(metadata.uid(), expected_uid); + assert_eq!(metadata.gid(), expected_gid); + } else { + print!("Test skipped; requires root user"); + } + }; + + run_and_check_uid_gid(&["-Cv", "-o2", "a", "b"], 2, 0); + run_and_check_uid_gid(&["-Cv", "a", "b"], 0, 0); + run_and_check_uid_gid(&["-Cv", "a", "b"], 0, 0); +}