| 1 |
use std::collections::BTreeSet; |
| 2 |
use std::ffi::OsStr; |
| 3 |
use xattr::FileExt; |
| 4 |
|
| 5 |
use tempfile::{tempfile, tempfile_in, NamedTempFile}; |
| 6 |
|
| 7 |
#[test] |
| 8 |
#[cfg(any( |
| 9 |
target_os = "linux", |
| 10 |
target_os = "freebsd", |
| 11 |
target_os = "macos", |
| 12 |
target_os = "android" |
| 13 |
))] |
| 14 |
fn test_fd() { |
| 15 |
use std::os::unix::ffi::OsStrExt; |
| 16 |
let tmp = tempfile().unwrap(); |
| 17 |
assert!(tmp.get_xattr("user.test").unwrap().is_none()); |
| 18 |
assert_eq!( |
| 19 |
tmp.list_xattr() |
| 20 |
.unwrap() |
| 21 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 22 |
.count(), |
| 23 |
0 |
| 24 |
); |
| 25 |
|
| 26 |
tmp.set_xattr("user.test", b"my test").unwrap(); |
| 27 |
assert_eq!(tmp.get_xattr("user.test").unwrap().unwrap(), b"my test"); |
| 28 |
assert_eq!( |
| 29 |
tmp.list_xattr() |
| 30 |
.unwrap() |
| 31 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 32 |
.collect::<Vec<_>>(), |
| 33 |
vec![OsStr::new("user.test")] |
| 34 |
); |
| 35 |
|
| 36 |
tmp.remove_xattr("user.test").unwrap(); |
| 37 |
assert!(tmp.get_xattr("user.test").unwrap().is_none()); |
| 38 |
assert_eq!( |
| 39 |
tmp.list_xattr() |
| 40 |
.unwrap() |
| 41 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 42 |
.count(), |
| 43 |
0 |
| 44 |
); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
#[cfg(any( |
| 49 |
target_os = "linux", |
| 50 |
target_os = "freebsd", |
| 51 |
target_os = "macos", |
| 52 |
target_os = "android" |
| 53 |
))] |
| 54 |
fn test_path() { |
| 55 |
use std::os::unix::ffi::OsStrExt; |
| 56 |
let tmp = NamedTempFile::new().unwrap(); |
| 57 |
assert!(xattr::get(tmp.path(), "user.test").unwrap().is_none()); |
| 58 |
assert_eq!( |
| 59 |
xattr::list(tmp.path()) |
| 60 |
.unwrap() |
| 61 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 62 |
.count(), |
| 63 |
0 |
| 64 |
); |
| 65 |
|
| 66 |
xattr::set(tmp.path(), "user.test", b"my test").unwrap(); |
| 67 |
assert_eq!( |
| 68 |
xattr::get(tmp.path(), "user.test").unwrap().unwrap(), |
| 69 |
b"my test" |
| 70 |
); |
| 71 |
assert_eq!( |
| 72 |
xattr::list(tmp.path()) |
| 73 |
.unwrap() |
| 74 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 75 |
.collect::<Vec<_>>(), |
| 76 |
vec![OsStr::new("user.test")] |
| 77 |
); |
| 78 |
|
| 79 |
xattr::remove(tmp.path(), "user.test").unwrap(); |
| 80 |
assert!(xattr::get(tmp.path(), "user.test").unwrap().is_none()); |
| 81 |
assert_eq!( |
| 82 |
xattr::list(tmp.path()) |
| 83 |
.unwrap() |
| 84 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 85 |
.count(), |
| 86 |
0 |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
#[cfg(any( |
| 92 |
target_os = "linux", |
| 93 |
target_os = "freebsd", |
| 94 |
target_os = "macos", |
| 95 |
target_os = "android" |
| 96 |
))] |
| 97 |
fn test_missing() { |
| 98 |
assert!(xattr::get("/var/empty/does-not-exist", "user.test").is_err()); |
| 99 |
} |
| 100 |
|
| 101 |
#[test] |
| 102 |
#[cfg(any( |
| 103 |
target_os = "linux", |
| 104 |
target_os = "freebsd", |
| 105 |
target_os = "macos", |
| 106 |
target_os = "android" |
| 107 |
))] |
| 108 |
fn test_debug() { |
| 109 |
use std::os::unix::ffi::OsStrExt; |
| 110 |
|
| 111 |
// Only works on "real" filesystems. |
| 112 |
let tmp = tempfile().unwrap(); |
| 113 |
|
| 114 |
tmp.set_xattr("user.myattr", b"value").unwrap(); |
| 115 |
let mut attrs = tmp.list_xattr().unwrap(); |
| 116 |
|
| 117 |
let debugstr = format!("{:?}", attrs); |
| 118 |
|
| 119 |
// Debug is idempotent |
| 120 |
assert_eq!(debugstr, format!("{:?}", attrs)); |
| 121 |
|
| 122 |
// It produces the right value. We can't just assert that it's equal to the expected value as |
| 123 |
// the system may have added additional attributes (selinux, etc.). See #68. |
| 124 |
assert!(debugstr.starts_with("XAttrs([")); |
| 125 |
assert!(debugstr.ends_with("])")); |
| 126 |
assert!(debugstr.contains(r#""user.myattr""#)); |
| 127 |
|
| 128 |
// It doesn't affect the underlying iterator. |
| 129 |
assert_eq!( |
| 130 |
"user.myattr", |
| 131 |
attrs.find(|x| x.as_bytes().starts_with(b"user.")).unwrap() |
| 132 |
); |
| 133 |
|
| 134 |
// drain it. |
| 135 |
let _ = attrs.by_ref().count(); |
| 136 |
|
| 137 |
// An empty iterator produces the right value. |
| 138 |
assert_eq!(r#"XAttrs([])"#, format!("{:?}", attrs)); |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
#[cfg(any( |
| 143 |
target_os = "linux", |
| 144 |
target_os = "freebsd", |
| 145 |
target_os = "macos", |
| 146 |
target_os = "android" |
| 147 |
))] |
| 148 |
fn test_multi() { |
| 149 |
use std::os::unix::ffi::OsStrExt; |
| 150 |
// Only works on "real" filesystems. |
| 151 |
let tmp = tempfile().unwrap(); |
| 152 |
let mut items: BTreeSet<_> = [ |
| 153 |
OsStr::new("user.test1"), |
| 154 |
OsStr::new("user.test2"), |
| 155 |
OsStr::new("user.test3"), |
| 156 |
] |
| 157 |
.iter() |
| 158 |
.cloned() |
| 159 |
.collect(); |
| 160 |
|
| 161 |
for it in &items { |
| 162 |
tmp.set_xattr(it, b"value").unwrap(); |
| 163 |
} |
| 164 |
for it in tmp |
| 165 |
.list_xattr() |
| 166 |
.unwrap() |
| 167 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 168 |
{ |
| 169 |
assert!(items.remove(&*it)); |
| 170 |
} |
| 171 |
assert!(items.is_empty()); |
| 172 |
} |
| 173 |
|
| 174 |
// This test is skipped on android because the /data/tmp filesystem doesn't support >4kib of |
| 175 |
// extended attributes. |
| 176 |
#[test] |
| 177 |
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "macos"))] |
| 178 |
fn test_large() { |
| 179 |
use std::ffi::OsString; |
| 180 |
use std::os::unix::ffi::OsStrExt; |
| 181 |
// On Linux, this only works in tmpfs! |
| 182 |
let tmp = if cfg!(target_os = "linux") { |
| 183 |
tempfile_in("/dev/shm") |
| 184 |
} else { |
| 185 |
tempfile() |
| 186 |
} |
| 187 |
.unwrap(); |
| 188 |
let mut items: BTreeSet<OsString> = (0..100) |
| 189 |
.map(|i| format!("user.test{i:0100}").into()) |
| 190 |
.collect(); |
| 191 |
|
| 192 |
for it in &items { |
| 193 |
tmp.set_xattr(it, b"value").unwrap(); |
| 194 |
} |
| 195 |
for it in tmp |
| 196 |
.list_xattr() |
| 197 |
.unwrap() |
| 198 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 199 |
{ |
| 200 |
assert!(items.remove(&*it)); |
| 201 |
} |
| 202 |
assert!(items.is_empty()); |
| 203 |
} |
| 204 |
|
| 205 |
// Tests the deref API variants - regression test for |
| 206 |
// https://github.com/Stebalien/xattr/issues/57 |
| 207 |
#[test] |
| 208 |
#[cfg(any( |
| 209 |
target_os = "linux", |
| 210 |
target_os = "freebsd", |
| 211 |
target_os = "macos", |
| 212 |
target_os = "android" |
| 213 |
))] |
| 214 |
fn test_path_deref() { |
| 215 |
use std::os::unix::ffi::OsStrExt; |
| 216 |
// Only works on "real" filesystems. |
| 217 |
let tmp = NamedTempFile::new().unwrap(); |
| 218 |
assert!(xattr::get_deref(tmp.path(), "user.test").unwrap().is_none()); |
| 219 |
assert_eq!( |
| 220 |
xattr::list_deref(tmp.path()) |
| 221 |
.unwrap() |
| 222 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 223 |
.count(), |
| 224 |
0 |
| 225 |
); |
| 226 |
|
| 227 |
xattr::set_deref(tmp.path(), "user.test", b"my test").unwrap(); |
| 228 |
assert_eq!( |
| 229 |
xattr::get_deref(tmp.path(), "user.test").unwrap().unwrap(), |
| 230 |
b"my test" |
| 231 |
); |
| 232 |
assert_eq!( |
| 233 |
xattr::list_deref(tmp.path()) |
| 234 |
.unwrap() |
| 235 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 236 |
.collect::<Vec<_>>(), |
| 237 |
vec![OsStr::new("user.test")] |
| 238 |
); |
| 239 |
|
| 240 |
xattr::remove_deref(tmp.path(), "user.test").unwrap(); |
| 241 |
assert!(xattr::get_deref(tmp.path(), "user.test").unwrap().is_none()); |
| 242 |
assert_eq!( |
| 243 |
xattr::list_deref(tmp.path()) |
| 244 |
.unwrap() |
| 245 |
.filter(|x| x.as_bytes().starts_with(b"user.")) |
| 246 |
.count(), |
| 247 |
0 |
| 248 |
); |
| 249 |
} |