| 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 |
use std::os::unix::fs::PermissionsExt; |
| 9 |
|
| 10 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 11 |
use uucore::selinux::get_getfattr_output; |
| 12 |
use uutests::new_ucmd; |
| 13 |
use uutests::util::TestScenario; |
| 14 |
use uutests::util::run_ucmd_as_root; |
| 15 |
use uutests::util_name; |
| 16 |
|
| 17 |
//Reject 2^32+1 major/minor device number |
| 18 |
#[test] |
| 19 |
fn test_mknod_overflow_major_minor() { |
| 20 |
new_ucmd!() |
| 21 |
.arg("lg32") |
| 22 |
.arg("c") |
| 23 |
.arg("4294967296") |
| 24 |
.arg("1") |
| 25 |
.fails_with_code(1) |
| 26 |
.no_stdout() |
| 27 |
.stderr_contains("invalid value '4294967296'"); //clap generated message, thats fine. |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn test_mknod_invalid_arg() { |
| 32 |
new_ucmd!() |
| 33 |
.arg("--foo") |
| 34 |
.fails_with_code(1) |
| 35 |
.no_stdout() |
| 36 |
.stderr_contains("unexpected argument '--foo' found"); |
| 37 |
} |
| 38 |
|
| 39 |
#[test] |
| 40 |
fn test_mknod_help() { |
| 41 |
new_ucmd!() |
| 42 |
.arg("--help") |
| 43 |
.succeeds() |
| 44 |
.no_stderr() |
| 45 |
.stdout_contains("Usage:"); |
| 46 |
} |
| 47 |
|
| 48 |
#[test] |
| 49 |
fn test_mknod_version() { |
| 50 |
assert!( |
| 51 |
new_ucmd!() |
| 52 |
.arg("--version") |
| 53 |
.succeeds() |
| 54 |
.no_stderr() |
| 55 |
.stdout_str() |
| 56 |
.starts_with("mknod") |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
#[test] |
| 61 |
fn test_mknod_fifo_default_writable() { |
| 62 |
let ts = TestScenario::new(util_name!()); |
| 63 |
ts.ucmd().arg("test_file").arg("p").succeeds(); |
| 64 |
assert!(ts.fixtures.is_fifo("test_file")); |
| 65 |
assert!(!ts.fixtures.metadata("test_file").permissions().readonly()); |
| 66 |
} |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn test_mknod_fifo_mnemonic_usage() { |
| 70 |
let ts = TestScenario::new(util_name!()); |
| 71 |
ts.ucmd().arg("test_file").arg("pipe").succeeds(); |
| 72 |
assert!(ts.fixtures.is_fifo("test_file")); |
| 73 |
} |
| 74 |
|
| 75 |
#[test] |
| 76 |
fn test_mknod_fifo_read_only() { |
| 77 |
let ts = TestScenario::new(util_name!()); |
| 78 |
ts.ucmd() |
| 79 |
.arg("-m") |
| 80 |
.arg("a=r") |
| 81 |
.arg("test_file") |
| 82 |
.arg("p") |
| 83 |
.succeeds(); |
| 84 |
assert!(ts.fixtures.is_fifo("test_file")); |
| 85 |
assert!(ts.fixtures.metadata("test_file").permissions().readonly()); |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
fn test_mknod_fifo_invalid_extra_operand() { |
| 90 |
new_ucmd!() |
| 91 |
.arg("test_file") |
| 92 |
.arg("p") |
| 93 |
.arg("1") |
| 94 |
.arg("2") |
| 95 |
.fails() |
| 96 |
.stderr_contains("Fifos do not have major and minor device numbers"); |
| 97 |
} |
| 98 |
|
| 99 |
#[test] |
| 100 |
fn test_mknod_character_device_requires_major_and_minor() { |
| 101 |
new_ucmd!() |
| 102 |
.arg("test_file") |
| 103 |
.arg("c") |
| 104 |
.fails_with_code(1) |
| 105 |
.stderr_contains("Special files require major and minor device numbers."); |
| 106 |
new_ucmd!() |
| 107 |
.arg("test_file") |
| 108 |
.arg("c") |
| 109 |
.arg("1") |
| 110 |
.fails_with_code(1) |
| 111 |
.stderr_contains("Special files require major and minor device numbers."); |
| 112 |
new_ucmd!() |
| 113 |
.arg("test_file") |
| 114 |
.arg("c") |
| 115 |
.arg("1") |
| 116 |
.arg("c") |
| 117 |
.fails() |
| 118 |
.stderr_contains("invalid value 'c'"); |
| 119 |
new_ucmd!() |
| 120 |
.arg("test_file") |
| 121 |
.arg("c") |
| 122 |
.arg("c") |
| 123 |
.arg("1") |
| 124 |
.fails() |
| 125 |
.stderr_contains("invalid value 'c'"); |
| 126 |
} |
| 127 |
|
| 128 |
#[test] |
| 129 |
fn test_mknod_invalid_mode() { |
| 130 |
new_ucmd!() |
| 131 |
.arg("--mode") |
| 132 |
.arg("rw") |
| 133 |
.arg("test_file") |
| 134 |
.arg("p") |
| 135 |
.fails() |
| 136 |
.no_stdout() |
| 137 |
.code_is(1) |
| 138 |
.stderr_contains("invalid mode"); |
| 139 |
} |
| 140 |
|
| 141 |
#[test] |
| 142 |
fn test_mknod_mode_permissions() { |
| 143 |
for test_mode in [0o0666, 0o0000, 0o0444, 0o0004, 0o0040, 0o0400, 0o0644] { |
| 144 |
let ts = TestScenario::new(util_name!()); |
| 145 |
let filename = format!("null_file-{test_mode:04o}"); |
| 146 |
|
| 147 |
if let Ok(result) = run_ucmd_as_root( |
| 148 |
&ts, |
| 149 |
&[ |
| 150 |
"--mode", |
| 151 |
&format!("{test_mode:04o}"), |
| 152 |
&filename, |
| 153 |
"c", |
| 154 |
"1", |
| 155 |
"3", |
| 156 |
], |
| 157 |
) { |
| 158 |
result.success().no_stdout(); |
| 159 |
} else { |
| 160 |
print!("Test skipped; `mknod c 1 3` for null char dev requires root user"); |
| 161 |
break; |
| 162 |
} |
| 163 |
|
| 164 |
assert!(ts.fixtures.is_char_device(&filename)); |
| 165 |
let permissions = ts.fixtures.metadata(&filename).permissions(); |
| 166 |
assert_eq!(test_mode, PermissionsExt::mode(&permissions) & 0o777); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
#[test] |
| 171 |
fn test_mknod_mode_comma_separated() { |
| 172 |
let ts = TestScenario::new(util_name!()); |
| 173 |
ts.ucmd() |
| 174 |
.arg("-m") |
| 175 |
.arg("u=rwx,g=rx,o=") |
| 176 |
.arg("test_file") |
| 177 |
.arg("p") |
| 178 |
.succeeds(); |
| 179 |
assert!(ts.fixtures.is_fifo("test_file")); |
| 180 |
assert_eq!( |
| 181 |
ts.fixtures.metadata("test_file").permissions().mode() & 0o777, |
| 182 |
0o750 |
| 183 |
); |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 188 |
fn test_mknod_selinux() { |
| 189 |
let ts = TestScenario::new(util_name!()); |
| 190 |
let at = &ts.fixtures; |
| 191 |
let dest = "test_file"; |
| 192 |
let args = [ |
| 193 |
"-Z", |
| 194 |
"--context", |
| 195 |
"--context=unconfined_u:object_r:user_tmp_t:s0", |
| 196 |
]; |
| 197 |
for arg in args { |
| 198 |
ts.ucmd() |
| 199 |
.arg(arg) |
| 200 |
.arg("-m") |
| 201 |
.arg("a=r") |
| 202 |
.arg(dest) |
| 203 |
.arg("p") |
| 204 |
.succeeds(); |
| 205 |
assert!(ts.fixtures.is_fifo("test_file")); |
| 206 |
assert!(ts.fixtures.metadata("test_file").permissions().readonly()); |
| 207 |
|
| 208 |
let context_value = get_getfattr_output(&at.plus_as_string(dest)); |
| 209 |
assert!( |
| 210 |
context_value.contains("unconfined_u"), |
| 211 |
"Expected 'unconfined_u' not found in getfattr output:\n{context_value}" |
| 212 |
); |
| 213 |
at.remove(&at.plus_as_string(dest)); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 219 |
fn test_mknod_selinux_invalid() { |
| 220 |
let scene = TestScenario::new(util_name!()); |
| 221 |
let at = &scene.fixtures; |
| 222 |
let dest = "orig"; |
| 223 |
|
| 224 |
let args = [ |
| 225 |
"--context=a", |
| 226 |
"--context=unconfined_u:object_r:user_tmp_t:s0:a", |
| 227 |
"--context=nconfined_u:object_r:user_tmp_t:s0", |
| 228 |
]; |
| 229 |
for arg in args { |
| 230 |
new_ucmd!() |
| 231 |
.arg(arg) |
| 232 |
.arg("-m") |
| 233 |
.arg("a=r") |
| 234 |
.arg(dest) |
| 235 |
.arg("p") |
| 236 |
.fails() |
| 237 |
.stderr_contains("failed to"); |
| 238 |
if at.file_exists(dest) { |
| 239 |
at.remove(dest); |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
#[test] |
| 245 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 246 |
fn test_mknod_selinux_invalid_cleanup() { |
| 247 |
let scene = TestScenario::new(util_name!()); |
| 248 |
let at = &scene.fixtures; |
| 249 |
let dest = "test_fifo"; |
| 250 |
|
| 251 |
new_ucmd!() |
| 252 |
.arg("--context=invalid_context_t") |
| 253 |
.arg(at.plus_as_string(dest)) |
| 254 |
.arg("p") |
| 255 |
.fails() |
| 256 |
.no_stdout(); |
| 257 |
|
| 258 |
// invalid context → node must not exist |
| 259 |
assert!(!at.file_exists(dest)); |
| 260 |
} |
| 261 |
|
| 262 |
// The mode is only parsed where a mode means something. |
| 263 |
#[cfg(unix)] |
| 264 |
#[cfg(all(feature = "feat_diagnostics", not(wasi_runner)))] |
| 265 |
mod diagnostics { |
| 266 |
use super::*; |
| 267 |
#[test] |
| 268 |
fn test_snippet_points_at_the_bad_operator() { |
| 269 |
let result = new_ucmd!() |
| 270 |
.terminal_sim_stderr() |
| 271 |
.args(&["-m", "u+rw?", "some_node", "p"]) |
| 272 |
.fails_with_code(1); |
| 273 |
let stderr = result.stderr_str(); |
| 274 |
|
| 275 |
assert!(stderr.contains("invalid mode"), "{stderr}"); |
| 276 |
// The caret lands on `?`: three columns of `-m ` and four of mode. |
| 277 |
assert_eq!(result.caret_column(), Some(8), "{stderr}"); |
| 278 |
} |
| 279 |
|
| 280 |
#[test] |
| 281 |
fn test_plain_message_when_stderr_is_a_pipe() { |
| 282 |
// The test harness pipes stderr, so the report must not appear. |
| 283 |
let result = new_ucmd!() |
| 284 |
.args(&["-m", "u+rw?", "some_node", "p"]) |
| 285 |
.fails_with_code(1); |
| 286 |
let stderr = result.stderr_str(); |
| 287 |
|
| 288 |
assert!(stderr.starts_with("mknod: "), "{stderr}"); |
| 289 |
assert!(!stderr.contains(":1:"), "{stderr}"); |
| 290 |
} |
| 291 |
} |