| 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 readelf |
| 7 |
|
| 8 |
use uutests::util::TestScenario; |
| 9 |
|
| 10 |
#[cfg(unix)] |
| 11 |
use std::os::unix::fs::symlink as symlink_file; |
| 12 |
|
| 13 |
use std::env; |
| 14 |
pub const TESTS_BINARY: &str = env!("CARGO_BIN_EXE_coreutils"); |
| 15 |
|
| 16 |
// Set the environment variable for any tests |
| 17 |
|
| 18 |
// Use the ctor attribute to run this function before any tests |
| 19 |
#[ctor::ctor(unsafe)] |
| 20 |
fn init() { |
| 21 |
unsafe { |
| 22 |
env::set_var("UUTESTS_BINARY_PATH", TESTS_BINARY); |
| 23 |
} |
| 24 |
// Print for debugging |
| 25 |
eprintln!("Setting UUTESTS_BINARY_PATH={TESTS_BINARY}"); |
| 26 |
} |
| 27 |
|
| 28 |
#[test] |
| 29 |
#[cfg(all( |
| 30 |
feature = "env", |
| 31 |
any(target_os = "linux", target_os = "android"), |
| 32 |
not(target_env = "musl") |
| 33 |
))] |
| 34 |
fn binary_name_protection() { |
| 35 |
let ts = TestScenario::new("env"); |
| 36 |
let bin = ts.bin_path.clone(); |
| 37 |
ts.ucmd() |
| 38 |
.arg("-a") |
| 39 |
.arg("hijacked") |
| 40 |
.arg(&bin) |
| 41 |
.arg("--version") |
| 42 |
.succeeds() |
| 43 |
.stdout_contains("coreutils"); |
| 44 |
} |
| 45 |
|
| 46 |
// missing /proc/self/fd on Android? |
| 47 |
#[test] |
| 48 |
#[cfg(all(feature = "true", target_os = "linux", target_env = "gnu"))] |
| 49 |
fn binary_name_execfn_fallback() { |
| 50 |
use std::os::{fd::AsRawFd, unix::process::CommandExt}; |
| 51 |
use std::process::Command; |
| 52 |
let ts = TestScenario::new("true"); |
| 53 |
let fd = rustix::fs::memfd_create("a", rustix::fs::MemfdFlags::empty()).unwrap(); |
| 54 |
let mut file = std::fs::File::from(fd); |
| 55 |
let mut source = std::fs::File::open(&ts.bin_path).unwrap(); |
| 56 |
std::io::copy(&mut source, &mut file).unwrap(); |
| 57 |
let raw_fd = file.as_raw_fd(); |
| 58 |
|
| 59 |
// memfd has AT_EXECFN as below. Fallback to argv[0] |
| 60 |
let res1 = Command::new(format!("/proc/self/fd/{raw_fd}")) |
| 61 |
.arg0("true") |
| 62 |
.output() |
| 63 |
.unwrap() |
| 64 |
.status |
| 65 |
.success(); |
| 66 |
// fexecve uses /dev/fd which is symlink to /proc/self/fd/N |
| 67 |
let res2 = Command::new(format!("/dev/fd/{raw_fd}")) |
| 68 |
.arg0("true") |
| 69 |
.output() |
| 70 |
.unwrap() |
| 71 |
.status |
| 72 |
.success(); |
| 73 |
assert!(res1 && res2); |
| 74 |
} |
| 75 |
|
| 76 |
#[test] |
| 77 |
fn test_coreutils_help_ignore_args() { |
| 78 |
let scenario = TestScenario::new("help_ignoring_args"); |
| 79 |
let output = std::process::Command::new(&scenario.bin_path) |
| 80 |
.arg("--help") |
| 81 |
.arg("---") |
| 82 |
.output() |
| 83 |
.unwrap(); |
| 84 |
|
| 85 |
assert_eq!(output.status.code(), Some(0)); |
| 86 |
} |
| 87 |
|
| 88 |
#[test] |
| 89 |
#[cfg(feature = "ls")] |
| 90 |
fn execution_phrase_double() { |
| 91 |
use std::process::Command; |
| 92 |
|
| 93 |
let scenario = TestScenario::new("ls"); |
| 94 |
if !scenario.bin_path.exists() { |
| 95 |
println!("Skipping test: Binary not found at {:?}", scenario.bin_path); |
| 96 |
return; |
| 97 |
} |
| 98 |
let output = Command::new(&scenario.bin_path) |
| 99 |
.arg("ls") |
| 100 |
.arg("--some-invalid-arg") |
| 101 |
.env("LANG", "en_US.UTF-8") |
| 102 |
.output() |
| 103 |
.unwrap(); |
| 104 |
assert!( |
| 105 |
String::from_utf8(output.stderr) |
| 106 |
.unwrap() |
| 107 |
.contains(&"Usage: ls".to_string()) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
#[test] |
| 112 |
#[ignore = "Test assumes error upon non-UTF8"] |
| 113 |
#[cfg(feature = "sort")] |
| 114 |
fn util_name_double() { |
| 115 |
use std::{ |
| 116 |
io::Write, |
| 117 |
process::{Command, Stdio}, |
| 118 |
}; |
| 119 |
|
| 120 |
let scenario = TestScenario::new("sort"); |
| 121 |
if !scenario.bin_path.exists() { |
| 122 |
println!("Skipping test: Binary not found at {:?}", scenario.bin_path); |
| 123 |
return; |
| 124 |
} |
| 125 |
let mut child = Command::new(&scenario.bin_path) |
| 126 |
.arg("sort") |
| 127 |
.stdin(Stdio::piped()) |
| 128 |
.stderr(Stdio::piped()) |
| 129 |
.spawn() |
| 130 |
.unwrap(); |
| 131 |
// input invalid utf8 to cause an error |
| 132 |
child.stdin.take().unwrap().write_all(&[255]).unwrap(); |
| 133 |
let output = child.wait_with_output().unwrap(); |
| 134 |
assert!(String::from_utf8(output.stderr).unwrap().contains("sort: ")); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
#[ignore = "Test assumes error upon non-UTF8"] |
| 139 |
#[cfg(feature = "sort")] |
| 140 |
#[cfg(unix)] |
| 141 |
fn util_name_single() { |
| 142 |
use std::{ |
| 143 |
io::Write, |
| 144 |
process::{Command, Stdio}, |
| 145 |
}; |
| 146 |
|
| 147 |
let scenario = TestScenario::new("sort"); |
| 148 |
if !scenario.bin_path.exists() { |
| 149 |
println!("Skipping test: Binary not found at {:?}", scenario.bin_path); |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
symlink_file(&scenario.bin_path, scenario.fixtures.plus("uu-sort")).unwrap(); |
| 154 |
let mut child = Command::new(scenario.fixtures.plus("uu-sort")) |
| 155 |
.stdin(Stdio::piped()) |
| 156 |
.stderr(Stdio::piped()) |
| 157 |
.spawn() |
| 158 |
.unwrap(); |
| 159 |
// input invalid utf8 to cause an error |
| 160 |
child.stdin.take().unwrap().write_all(&[255]).unwrap(); |
| 161 |
let output = child.wait_with_output().unwrap(); |
| 162 |
assert!(String::from_utf8(output.stderr).unwrap().contains(&format!( |
| 163 |
"{}: ", |
| 164 |
scenario.fixtures.plus("uu-sort").display() |
| 165 |
))); |
| 166 |
} |
| 167 |
|
| 168 |
#[test] |
| 169 |
#[cfg(unix)] |
| 170 |
fn util_invalid_name_help() { |
| 171 |
use std::process::Command; |
| 172 |
|
| 173 |
let scenario = TestScenario::new("invalid_name"); |
| 174 |
if !scenario.bin_path.exists() { |
| 175 |
println!("Skipping test: Binary not found at {:?}", scenario.bin_path); |
| 176 |
return; |
| 177 |
} |
| 178 |
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap(); |
| 179 |
let code = Command::new(scenario.fixtures.plus("invalid_name")) |
| 180 |
.arg("--help") |
| 181 |
.status() |
| 182 |
.unwrap() |
| 183 |
.code(); |
| 184 |
assert_eq!(code, Some(1)); |
| 185 |
} |
| 186 |
|
| 187 |
#[test] |
| 188 |
// The exact set of permitted filenames depends on many factors. Non-UTF-8 strings |
| 189 |
// work on very few platforms, but linux works, especially because it also increases |
| 190 |
// the likelihood that a filesystem is being used that supports non-UTF-8 filenames. |
| 191 |
#[cfg(target_os = "linux")] |
| 192 |
fn util_non_utf8_name_help() { |
| 193 |
// Make sure we don't crash even if the util name is invalid UTF-8. |
| 194 |
use std::{ |
| 195 |
ffi::OsStr, |
| 196 |
os::unix::ffi::OsStrExt, |
| 197 |
process::{Command, Stdio}, |
| 198 |
}; |
| 199 |
|
| 200 |
let scenario = TestScenario::new("invalid_name"); |
| 201 |
let non_utf8_path = scenario.fixtures.plus(OsStr::from_bytes(b"\xff")); |
| 202 |
if !scenario.bin_path.exists() { |
| 203 |
println!("Skipping test: Binary not found at {:?}", scenario.bin_path); |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
symlink_file(&scenario.bin_path, &non_utf8_path).unwrap(); |
| 208 |
let child = Command::new(&non_utf8_path) |
| 209 |
.arg("--help") |
| 210 |
.stdin(Stdio::piped()) |
| 211 |
.stdout(Stdio::piped()) |
| 212 |
.stderr(Stdio::piped()) |
| 213 |
.spawn() |
| 214 |
.unwrap(); |
| 215 |
let output = child.wait_with_output().unwrap(); |
| 216 |
assert_eq!(output.status.code(), Some(0)); |
| 217 |
assert_eq!(output.stderr, b""); |
| 218 |
let output_str = String::from_utf8(output.stdout).unwrap(); |
| 219 |
assert!(output_str.contains("(multi-call binary)"), "{output_str:?}"); |
| 220 |
assert!( |
| 221 |
output_str.contains("Usage: <unknown binary name> [function "), |
| 222 |
"{output_str:?}" |
| 223 |
); |
| 224 |
} |
| 225 |
|
| 226 |
#[test] |
| 227 |
#[cfg(unix)] |
| 228 |
fn util_invalid_name_invalid_command() { |
| 229 |
use std::process::Command; |
| 230 |
|
| 231 |
let scenario = TestScenario::new("invalid_name"); |
| 232 |
symlink_file(&scenario.bin_path, scenario.fixtures.plus("invalid_name")).unwrap(); |
| 233 |
if !scenario.bin_path.exists() { |
| 234 |
println!("Skipping test: Binary not found at {:?}", scenario.bin_path); |
| 235 |
return; |
| 236 |
} |
| 237 |
|
| 238 |
let code = Command::new(scenario.fixtures.plus("invalid_name")) |
| 239 |
.status() |
| 240 |
.unwrap() |
| 241 |
.code(); |
| 242 |
assert_eq!(code, Some(1)); |
| 243 |
} |
| 244 |
|
| 245 |
#[test] |
| 246 |
fn util_version() { |
| 247 |
use std::process::{Command, Stdio}; |
| 248 |
|
| 249 |
let scenario = TestScenario::new("--version"); |
| 250 |
if !scenario.bin_path.exists() { |
| 251 |
println!("Skipping test: Binary not found at {:?}", scenario.bin_path); |
| 252 |
return; |
| 253 |
} |
| 254 |
for arg in ["-V", "--version"] { |
| 255 |
let child = Command::new(&scenario.bin_path) |
| 256 |
.arg(arg) |
| 257 |
.stdin(Stdio::piped()) |
| 258 |
.stdout(Stdio::piped()) |
| 259 |
.stderr(Stdio::piped()) |
| 260 |
.spawn() |
| 261 |
.unwrap(); |
| 262 |
let output = child.wait_with_output().unwrap(); |
| 263 |
assert_eq!(output.status.code(), Some(0)); |
| 264 |
assert_eq!(output.stderr, b""); |
| 265 |
let output_str = String::from_utf8(output.stdout).unwrap(); |
| 266 |
let ver = env::var("CARGO_PKG_VERSION").unwrap(); |
| 267 |
assert_eq!(format!("coreutils {ver} (multi-call binary)\n"), output_str); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
#[test] |
| 272 |
#[cfg(target_env = "musl")] |
| 273 |
fn test_musl_no_dynamic_deps() { |
| 274 |
use std::process::Command; |
| 275 |
|
| 276 |
let scenario = TestScenario::new("test_musl_no_dynamic_deps"); |
| 277 |
if !scenario.bin_path.exists() { |
| 278 |
println!("Skipping test: Binary not found at {:?}", scenario.bin_path); |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
let output = Command::new("readelf") |
| 283 |
.arg("-d") |
| 284 |
.arg(&scenario.bin_path) |
| 285 |
.output() |
| 286 |
.expect("Failed to run readelf"); |
| 287 |
|
| 288 |
let stdout = String::from_utf8_lossy(&output.stdout); |
| 289 |
|
| 290 |
// Static binaries should have no NEEDED entries (dynamic library dependencies) |
| 291 |
assert!( |
| 292 |
!stdout.contains("NEEDED"), |
| 293 |
"Found dynamic dependencies in musl binary:\n{}", |
| 294 |
stdout |
| 295 |
); |
| 296 |
} |
| 297 |
|
| 298 |
#[test] |
| 299 |
fn test_sorted_utils() { |
| 300 |
let s = TestScenario::new("list_sorted"); |
| 301 |
let out = String::from_utf8( |
| 302 |
std::process::Command::new(&s.bin_path) |
| 303 |
.arg("--list") |
| 304 |
.output() |
| 305 |
.unwrap() |
| 306 |
.stdout, |
| 307 |
) |
| 308 |
.unwrap(); |
| 309 |
assert!(out.lines().filter(|s| !s.is_empty()).is_sorted()); |
| 310 |
} |