| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore nconfined |
| 7 |
|
| 8 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 9 |
use uucore::selinux::get_getfattr_output; |
| 10 |
use uutests::new_ucmd; |
| 11 |
use uutests::util::TestScenario; |
| 12 |
use uutests::util_name; |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn test_invalid_arg() { |
| 16 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 17 |
} |
| 18 |
|
| 19 |
#[test] |
| 20 |
fn test_create_fifo_missing_operand() { |
| 21 |
new_ucmd!().fails(); // clap provided error message |
| 22 |
} |
| 23 |
|
| 24 |
#[test] |
| 25 |
fn test_create_one_fifo() { |
| 26 |
new_ucmd!().arg("abc").succeeds(); |
| 27 |
} |
| 28 |
|
| 29 |
#[test] |
| 30 |
fn test_create_one_fifo_with_invalid_mode() { |
| 31 |
new_ucmd!() |
| 32 |
.arg("abcd") |
| 33 |
.arg("-m") |
| 34 |
.arg("invalid") |
| 35 |
.fails() |
| 36 |
.stderr_contains("invalid mode"); |
| 37 |
|
| 38 |
new_ucmd!() |
| 39 |
.arg("abcd") |
| 40 |
.arg("-m") |
| 41 |
.arg("0999") |
| 42 |
.fails() |
| 43 |
.stderr_contains("invalid mode"); |
| 44 |
} |
| 45 |
|
| 46 |
#[test] |
| 47 |
fn test_create_one_fifo_with_non_file_permission_mode() { |
| 48 |
new_ucmd!() |
| 49 |
.arg("abcd") |
| 50 |
.arg("-m") |
| 51 |
.arg("1777") |
| 52 |
.fails() |
| 53 |
.stderr_is("mkfifo: mode must specify only file permission bits\n"); |
| 54 |
new_ucmd!() |
| 55 |
.arg("abcd") |
| 56 |
.arg("-m") |
| 57 |
.arg("1999") |
| 58 |
.fails() |
| 59 |
.stderr_contains("invalid mode"); |
| 60 |
} |
| 61 |
|
| 62 |
#[test] |
| 63 |
fn test_create_multiple_fifos() { |
| 64 |
new_ucmd!() |
| 65 |
.arg("abcde") |
| 66 |
.arg("def") |
| 67 |
.arg("sed") |
| 68 |
.arg("dum") |
| 69 |
.succeeds(); |
| 70 |
} |
| 71 |
|
| 72 |
#[test] |
| 73 |
fn test_create_one_fifo_with_mode() { |
| 74 |
new_ucmd!().arg("abcde").arg("-m600").succeeds(); |
| 75 |
} |
| 76 |
|
| 77 |
#[test] |
| 78 |
fn test_create_one_fifo_already_exists() { |
| 79 |
new_ucmd!() |
| 80 |
.arg("abcdef") |
| 81 |
.arg("abcdef") |
| 82 |
.fails() |
| 83 |
.stderr_is("mkfifo: cannot create fifo 'abcdef': File exists\n"); |
| 84 |
} |
| 85 |
|
| 86 |
#[test] |
| 87 |
fn test_create_fifo_with_mode_and_umask() { |
| 88 |
use uucore::fs::display_permissions; |
| 89 |
let scene = TestScenario::new(util_name!()); |
| 90 |
let at = &scene.fixtures; |
| 91 |
|
| 92 |
let test_fifo_creation = |mode: &str, umask: u16, expected: &str| { |
| 93 |
scene |
| 94 |
.ucmd() |
| 95 |
.arg("-m") |
| 96 |
.arg(mode) |
| 97 |
.arg(format!("fifo_test_{mode}")) |
| 98 |
.umask(libc::mode_t::from(umask)) |
| 99 |
.succeeds(); |
| 100 |
|
| 101 |
let metadata = std::fs::metadata(at.subdir.join(format!("fifo_test_{mode}"))).unwrap(); |
| 102 |
let permissions = display_permissions(&metadata, true); |
| 103 |
assert_eq!(permissions, expected.to_string()); |
| 104 |
}; |
| 105 |
|
| 106 |
test_fifo_creation("734", 0o077, "prwx-wxr--"); // spell-checker:disable-line |
| 107 |
test_fifo_creation("706", 0o777, "prwx---rw-"); // spell-checker:disable-line |
| 108 |
test_fifo_creation("a=rwx", 0o022, "prwxrwxrwx"); // spell-checker:disable-line |
| 109 |
test_fifo_creation("a=rx", 0o022, "pr-xr-xr-x"); // spell-checker:disable-line |
| 110 |
test_fifo_creation("a=r", 0o022, "pr--r--r--"); // spell-checker:disable-line |
| 111 |
test_fifo_creation("=rwx", 0o022, "prwxr-xr-x"); // spell-checker:disable-line |
| 112 |
test_fifo_creation("u+w", 0o022, "prw-rw-rw-"); // spell-checker:disable-line |
| 113 |
test_fifo_creation("u-w", 0o022, "pr--rw-rw-"); // spell-checker:disable-line |
| 114 |
test_fifo_creation("u+x", 0o022, "prwxrw-rw-"); // spell-checker:disable-line |
| 115 |
test_fifo_creation("u-r,g-w,o+x", 0o022, "p-w-r--rwx"); // spell-checker:disable-line |
| 116 |
test_fifo_creation("a=rwx,o-w", 0o022, "prwxrwxr-x"); // spell-checker:disable-line |
| 117 |
test_fifo_creation("=rwx,o-w", 0o022, "prwxr-xr-x"); // spell-checker:disable-line |
| 118 |
test_fifo_creation("ug+rw,o+r", 0o022, "prw-rw-rw-"); // spell-checker:disable-line |
| 119 |
test_fifo_creation("u=rwx,g=rx,o=", 0o022, "prwxr-x---"); // spell-checker:disable-line |
| 120 |
} |
| 121 |
|
| 122 |
#[test] |
| 123 |
fn test_create_fifo_with_umask() { |
| 124 |
use uucore::fs::display_permissions; |
| 125 |
let scene = TestScenario::new(util_name!()); |
| 126 |
let at = &scene.fixtures; |
| 127 |
|
| 128 |
let test_fifo_creation = |umask: u16, expected: &str| { |
| 129 |
scene |
| 130 |
.ucmd() |
| 131 |
.arg("fifo_test") |
| 132 |
.umask(libc::mode_t::from(umask)) |
| 133 |
.succeeds(); |
| 134 |
|
| 135 |
let metadata = std::fs::metadata(at.subdir.join("fifo_test")).unwrap(); |
| 136 |
let permissions = display_permissions(&metadata, true); |
| 137 |
assert_eq!(permissions, expected.to_string()); |
| 138 |
at.remove("fifo_test"); |
| 139 |
}; |
| 140 |
|
| 141 |
test_fifo_creation(0o022, "prw-r--r--"); // spell-checker:disable-line |
| 142 |
test_fifo_creation(0o777, "p---------"); // spell-checker:disable-line |
| 143 |
} |
| 144 |
|
| 145 |
#[test] |
| 146 |
fn test_create_fifo_permission_denied() { |
| 147 |
let scene = TestScenario::new(util_name!()); |
| 148 |
let at = &scene.fixtures; |
| 149 |
|
| 150 |
let no_exec_dir = "owner_no_exec_dir"; |
| 151 |
let named_pipe = "owner_no_exec_dir/mkfifo_err"; |
| 152 |
|
| 153 |
at.mkdir(no_exec_dir); |
| 154 |
at.set_mode(no_exec_dir, 0o644); |
| 155 |
|
| 156 |
// The parent directory has no execute bit, so the kernel refuses to add |
| 157 |
// an entry to it (EACCES). mkfifo should surface that reason instead of |
| 158 |
// the old hardcoded "File exists". |
| 159 |
let err_msg = format!("mkfifo: cannot create fifo '{named_pipe}': Permission denied\n"); |
| 160 |
|
| 161 |
scene |
| 162 |
.ucmd() |
| 163 |
.arg(named_pipe) |
| 164 |
.arg("-m") |
| 165 |
.arg("666") |
| 166 |
.fails() |
| 167 |
.stderr_is(err_msg.as_str()); |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 172 |
fn test_mkfifo_selinux() { |
| 173 |
let ts = TestScenario::new(util_name!()); |
| 174 |
let at = &ts.fixtures; |
| 175 |
let dest = "test_file"; |
| 176 |
let args = [ |
| 177 |
"-Z", |
| 178 |
"--context", |
| 179 |
"--context=unconfined_u:object_r:user_tmp_t:s0", |
| 180 |
]; |
| 181 |
for arg in args { |
| 182 |
ts.ucmd().arg(arg).arg(dest).succeeds(); |
| 183 |
assert!(at.is_fifo("test_file")); |
| 184 |
|
| 185 |
let context_value = get_getfattr_output(&at.plus_as_string(dest)); |
| 186 |
assert!( |
| 187 |
context_value.contains("unconfined_u"), |
| 188 |
"Expected 'unconfined_u' not found in getfattr output:\n{context_value}" |
| 189 |
); |
| 190 |
at.remove(&at.plus_as_string(dest)); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
#[test] |
| 195 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 196 |
fn test_mkfifo_selinux_invalid() { |
| 197 |
let scene = TestScenario::new(util_name!()); |
| 198 |
let at = &scene.fixtures; |
| 199 |
let dest = "orig"; |
| 200 |
|
| 201 |
let args = [ |
| 202 |
"--context=a", |
| 203 |
"--context=unconfined_u:object_r:user_tmp_t:s0:a", |
| 204 |
"--context=nconfined_u:object_r:user_tmp_t:s0", |
| 205 |
]; |
| 206 |
for arg in args { |
| 207 |
new_ucmd!() |
| 208 |
.arg(arg) |
| 209 |
.arg(dest) |
| 210 |
.fails() |
| 211 |
.stderr_contains("failed to"); |
| 212 |
if at.file_exists(dest) { |
| 213 |
at.remove(dest); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
#[test] |
| 219 |
fn test_mkfifo_permission_unchanged_when_failed() { |
| 220 |
use uucore::fs::display_permissions; |
| 221 |
|
| 222 |
let scene = TestScenario::new(util_name!()); |
| 223 |
let at = &scene.fixtures; |
| 224 |
|
| 225 |
let file_name = "test_file"; |
| 226 |
at.write(file_name, "content"); |
| 227 |
at.set_mode(file_name, 0o600); |
| 228 |
|
| 229 |
let err_msg = format!("mkfifo: cannot create fifo '{file_name}': File exists\n"); |
| 230 |
|
| 231 |
scene |
| 232 |
.ucmd() |
| 233 |
.arg(file_name) |
| 234 |
.arg("-m") |
| 235 |
.arg("666") |
| 236 |
.fails() |
| 237 |
.stderr_is(err_msg.as_str()); |
| 238 |
let metadata = std::fs::metadata(at.subdir.join(file_name)).unwrap(); |
| 239 |
let permissions = display_permissions(&metadata, true); |
| 240 |
let expected = "-rw-------"; |
| 241 |
assert_eq!(permissions, expected.to_string()); |
| 242 |
} |
| 243 |
|
| 244 |
// The mode is only parsed where a mode means something. |
| 245 |
#[cfg(unix)] |
| 246 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 247 |
mod diagnostics { |
| 248 |
use super::*; |
| 249 |
#[test] |
| 250 |
fn test_snippet_points_at_the_bad_operator() { |
| 251 |
let result = new_ucmd!() |
| 252 |
.terminal_sim_stderr() |
| 253 |
.args(&["-m", "+rw?", "some_pipe"]) |
| 254 |
.fails_with_code(1); |
| 255 |
let stderr = result.stderr_str(); |
| 256 |
|
| 257 |
assert!(stderr.contains("invalid mode"), "{stderr}"); |
| 258 |
// The caret lands on `?`: three columns of `-m ` and four of mode. |
| 259 |
assert_eq!(result.caret_column(), Some(7), "{stderr}"); |
| 260 |
} |
| 261 |
|
| 262 |
#[test] |
| 263 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 264 |
// The test harness pipes stderr, so the report must not appear. |
| 265 |
let result = new_ucmd!() |
| 266 |
.args(&["-m", "+rw?", "some_pipe"]) |
| 267 |
.fails_with_code(1); |
| 268 |
let stderr = result.stderr_str(); |
| 269 |
|
| 270 |
assert!(stderr.starts_with("mkfifo: "), "{stderr}"); |
| 271 |
assert!(!stderr.contains(":1:"), "{stderr}"); |
| 272 |
} |
| 273 |
} |