1 extern crate tempfile;
2 extern crate xattr;
3 
4 use std::collections::BTreeSet;
5 use std::ffi::OsStr;
6 use xattr::FileExt;
7 
8 use tempfile::{tempfile_in, NamedTempFile};
9 
10 #[test]
11 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
test_fd()12 fn test_fd() {
13     use std::os::unix::ffi::OsStrExt;
14     // Only works on "real" filesystems.
15     let tmp = tempfile_in("/var/tmp").unwrap();
16     assert!(tmp.get_xattr("user.test").unwrap().is_none());
17     assert_eq!(
18         tmp.list_xattr()
19             .unwrap()
20             .filter(|x| x.as_bytes().starts_with(b"user."))
21             .count(),
22         0
23     );
24 
25     tmp.set_xattr("user.test", b"my test").unwrap();
26     assert_eq!(tmp.get_xattr("user.test").unwrap().unwrap(), b"my test");
27     assert_eq!(
28         tmp.list_xattr()
29             .unwrap()
30             .filter(|x| x.as_bytes().starts_with(b"user."))
31             .collect::<Vec<_>>(),
32         vec![OsStr::new("user.test")]
33     );
34 
35     tmp.remove_xattr("user.test").unwrap();
36     assert!(tmp.get_xattr("user.test").unwrap().is_none());
37     assert_eq!(
38         tmp.list_xattr()
39             .unwrap()
40             .filter(|x| x.as_bytes().starts_with(b"user."))
41             .count(),
42         0
43     );
44 }
45 
46 #[test]
47 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
test_path()48 fn test_path() {
49     use std::os::unix::ffi::OsStrExt;
50     // Only works on "real" filesystems.
51     let tmp = NamedTempFile::new_in("/var/tmp").unwrap();
52     assert!(xattr::get(tmp.path(), "user.test").unwrap().is_none());
53     assert_eq!(
54         xattr::list(tmp.path())
55             .unwrap()
56             .filter(|x| x.as_bytes().starts_with(b"user."))
57             .count(),
58         0
59     );
60 
61     xattr::set(tmp.path(), "user.test", b"my test").unwrap();
62     assert_eq!(
63         xattr::get(tmp.path(), "user.test").unwrap().unwrap(),
64         b"my test"
65     );
66     assert_eq!(
67         xattr::list(tmp.path())
68             .unwrap()
69             .filter(|x| x.as_bytes().starts_with(b"user."))
70             .collect::<Vec<_>>(),
71         vec![OsStr::new("user.test")]
72     );
73 
74     xattr::remove(tmp.path(), "user.test").unwrap();
75     assert!(xattr::get(tmp.path(), "user.test").unwrap().is_none());
76     assert_eq!(
77         xattr::list(tmp.path())
78             .unwrap()
79             .filter(|x| x.as_bytes().starts_with(b"user."))
80             .count(),
81         0
82     );
83 }
84 
85 #[test]
86 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
test_missing()87 fn test_missing() {
88     assert!(xattr::get("/var/empty/does-not-exist", "user.test").is_err());
89 }
90 
91 #[test]
92 #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))]
test_multi()93 fn test_multi() {
94     use std::os::unix::ffi::OsStrExt;
95     // Only works on "real" filesystems.
96     let tmp = tempfile_in("/var/tmp").unwrap();
97     let mut items: BTreeSet<_> = [
98         OsStr::new("user.test1"),
99         OsStr::new("user.test2"),
100         OsStr::new("user.test3"),
101     ].iter()
102         .cloned()
103         .collect();
104 
105     for it in &items {
106         tmp.set_xattr(it, b"value").unwrap();
107     }
108     for it in tmp.list_xattr()
109         .unwrap()
110         .filter(|x| x.as_bytes().starts_with(&*b"user."))
111     {
112         assert!(items.remove(&*it));
113     }
114     assert!(items.is_empty());
115 }
116