From 238fb776ad01146976212ce3e331dd3bbe8a45f4 Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sun, 14 Jan 2024 00:38:17 +0100 Subject: [PATCH] test: add a function to compare the xattr between two files. used by cp & mv (at least) --- tests/common/util.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/common/util.rs b/tests/common/util.rs index 5dac61f7e..9055c238e 100644 --- a/tests/common/util.rs +++ b/tests/common/util.rs @@ -756,6 +756,26 @@ pub fn get_root_path() -> &'static str { } } +/// Compares the extended attributes (xattrs) of two files or directories. +/// +/// # Returns +/// +/// `true` if both paths have the same set of extended attributes, `false` otherwise. +#[cfg(all(unix, not(target_os = "macos")))] +pub fn compare_xattrs>(path1: P, path2: P) -> bool { + let get_sorted_xattrs = |path: P| { + xattr::list(path) + .map(|attrs| { + let mut attrs = attrs.collect::>(); + attrs.sort(); + attrs + }) + .unwrap_or_else(|_| Vec::new()) + }; + + get_sorted_xattrs(path1) == get_sorted_xattrs(path2) +} + /// Object-oriented path struct that represents and operates on /// paths relative to the directory it was constructed for. #[derive(Clone)] @@ -3375,4 +3395,26 @@ mod tests { ); assert!(command.tmpd.is_some()); } + + #[cfg(all(unix, not(target_os = "macos")))] + #[test] + fn test_compare_xattrs() { + use tempfile::tempdir; + + let temp_dir = tempdir().unwrap(); + let file_path1 = temp_dir.path().join("test_file1.txt"); + let file_path2 = temp_dir.path().join("test_file2.txt"); + + File::create(&file_path1).unwrap(); + File::create(&file_path2).unwrap(); + + let test_attr = "user.test_attr"; + let test_value = b"test value"; + xattr::set(&file_path1, test_attr, test_value).unwrap(); + + assert!(!compare_xattrs(&file_path1, &file_path2)); + + xattr::set(&file_path2, test_attr, test_value).unwrap(); + assert!(compare_xattrs(&file_path1, &file_path2)); + } }