| 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 |
// |
| 7 |
// spell-checker:ignore utmp runlevel testusr testx boottime |
| 8 |
#![allow(clippy::cast_possible_wrap, clippy::unreadable_literal)] |
| 9 |
|
| 10 |
#[cfg(unix)] |
| 11 |
use uutests::at_and_ucmd; |
| 12 |
use uutests::new_ucmd; |
| 13 |
|
| 14 |
use regex::Regex; |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn test_invalid_arg() { |
| 18 |
new_ucmd!().arg("--definitely-invalid").fails_with_code(1); |
| 19 |
} |
| 20 |
|
| 21 |
#[test] |
| 22 |
#[cfg(not(target_os = "android"))] |
| 23 |
fn test_uptime() { |
| 24 |
let result = new_ucmd!().succeeds(); |
| 25 |
result.stdout_contains(" up "); |
| 26 |
// Don't check for users as it doesn't show in some CI |
| 27 |
#[cfg(unix)] |
| 28 |
result |
| 29 |
.stdout_contains("load average:") |
| 30 |
.stdout_does_not_contain(", ,"); |
| 31 |
|
| 32 |
// Windows has no load average; the line ends after the user count. |
| 33 |
#[cfg(windows)] |
| 34 |
result |
| 35 |
.stdout_does_not_contain("load average") |
| 36 |
.stdout_matches(&Regex::new(r" up .*, \d+ users?\n$").unwrap()); |
| 37 |
} |
| 38 |
|
| 39 |
#[test] |
| 40 |
#[cfg(target_os = "linux")] |
| 41 |
fn test_write_error_handling() { |
| 42 |
use std::fs::File; |
| 43 |
|
| 44 |
let dev_full = |
| 45 |
File::create("/dev/full").expect("Failed to open /dev/full - test must run on Linux"); |
| 46 |
|
| 47 |
new_ucmd!() |
| 48 |
.set_stdout(dev_full) |
| 49 |
.fails() |
| 50 |
.code_is(1) |
| 51 |
.stderr_contains("No space left on device"); |
| 52 |
} |
| 53 |
|
| 54 |
/// Checks for files without utmpx records for which boot time cannot be calculated |
| 55 |
#[test] |
| 56 |
#[cfg(unix)] |
| 57 |
#[cfg(not(any(target_os = "openbsd", target_os = "freebsd", target_os = "android")))] |
| 58 |
// Disabled for freebsd, since it doesn't use the utmpxname() sys call to change the default utmpx |
| 59 |
// file that is accessed using getutxent() |
| 60 |
fn test_uptime_for_file_without_utmpx_records() { |
| 61 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 62 |
at.write("file1", "hello"); |
| 63 |
|
| 64 |
ucmd.arg(at.plus_as_string("file1")) |
| 65 |
.fails() |
| 66 |
.stderr_contains("uptime: couldn't get boot time") |
| 67 |
.stdout_contains("up ???? days ??:??") |
| 68 |
.stdout_contains("load average"); |
| 69 |
} |
| 70 |
|
| 71 |
/// Checks whether uptime displays the correct stderr msg when its called with a fifo |
| 72 |
#[test] |
| 73 |
#[cfg(all(unix, feature = "cp"))] |
| 74 |
#[cfg(not(target_os = "android"))] |
| 75 |
fn test_uptime_with_fifo() { |
| 76 |
use uutests::{util::TestScenario, util_name}; |
| 77 |
|
| 78 |
// This test can go on forever in the CI in some cases, might need aborting |
| 79 |
// Sometimes writing to the pipe is broken |
| 80 |
let ts = TestScenario::new(util_name!()); |
| 81 |
|
| 82 |
let at = &ts.fixtures; |
| 83 |
at.mkfifo("fifo1"); |
| 84 |
|
| 85 |
at.write("a", "hello"); |
| 86 |
// Creating a child process to write to the fifo |
| 87 |
let mut child = ts |
| 88 |
.ccmd("cp") |
| 89 |
.arg(at.plus_as_string("a")) |
| 90 |
.arg(at.plus_as_string("fifo1")) |
| 91 |
.run_no_wait(); |
| 92 |
|
| 93 |
ts.ucmd() |
| 94 |
.arg("fifo1") |
| 95 |
.fails() |
| 96 |
.stderr_contains("uptime: couldn't get boot time") |
| 97 |
.stdout_contains("up ???? days ??:??") |
| 98 |
.stdout_contains("load average"); |
| 99 |
|
| 100 |
child.kill(); |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
#[cfg(unix)] |
| 105 |
#[cfg(not(target_os = "freebsd"))] |
| 106 |
fn test_uptime_with_non_existent_file() { |
| 107 |
// Disabled for freebsd, since it doesn't use the utmpxname() sys call to change the default utmpx |
| 108 |
// file that is accessed using getutxent() |
| 109 |
new_ucmd!() |
| 110 |
.arg("file1") |
| 111 |
.fails() |
| 112 |
.stderr_contains("uptime: couldn't get boot time: No such file or directory") |
| 113 |
.stdout_contains("up ???? days ??:??"); |
| 114 |
} |
| 115 |
|
| 116 |
// TODO create a similar test for macos |
| 117 |
// This will pass |
| 118 |
#[test] |
| 119 |
#[cfg(unix)] |
| 120 |
#[cfg(not(any(target_vendor = "apple", target_os = "openbsd", target_os = "android")))] |
| 121 |
#[cfg(not(target_env = "musl"))] |
| 122 |
#[cfg_attr( |
| 123 |
all(target_arch = "aarch64", target_os = "linux"), |
| 124 |
ignore = "Issue #7159 - Test not supported on ARM64 Linux" |
| 125 |
)] |
| 126 |
#[allow(clippy::too_many_lines, clippy::items_after_statements)] |
| 127 |
fn test_uptime_with_file_containing_valid_boot_time_utmpx_record() { |
| 128 |
use std::fs::File; |
| 129 |
use std::{io::Write, path::PathBuf}; |
| 130 |
|
| 131 |
// This test will pass for freebsd but we currently don't support changing the utmpx file for |
| 132 |
// freebsd. |
| 133 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 134 |
// Regex matches for "up 00::00" ,"up 12 days 00::00", the time can be any valid time and |
| 135 |
// the days can be more than 1 digit or not there. This will match even if the amount of whitespace is |
| 136 |
// wrong between the days and the time. |
| 137 |
|
| 138 |
let re = Regex::new(r"up [(\d){1,} days]*\d{1,2}:\d\d").unwrap(); |
| 139 |
utmp(&at.plus("testx")); |
| 140 |
|
| 141 |
ucmd.arg("testx") |
| 142 |
.succeeds() |
| 143 |
.stdout_matches(&re) |
| 144 |
.stdout_contains("load average"); |
| 145 |
|
| 146 |
// Helper function to create byte sequences |
| 147 |
fn slice_32(slice: &[u8]) -> [i8; 32] { |
| 148 |
let mut arr: [i8; 32] = [0; 32]; |
| 149 |
|
| 150 |
for (i, val) in slice.iter().enumerate() { |
| 151 |
arr[i] = *val as i8; |
| 152 |
} |
| 153 |
arr |
| 154 |
} |
| 155 |
|
| 156 |
// Creates a file utmp records of three different types including a valid BOOT_TIME entry |
| 157 |
fn utmp(path: &PathBuf) { |
| 158 |
// Definitions of our utmpx structs |
| 159 |
const BOOT_TIME: i32 = 2; |
| 160 |
const RUN_LVL: i32 = 1; |
| 161 |
const USER_PROCESS: i32 = 7; |
| 162 |
|
| 163 |
#[repr(C)] |
| 164 |
pub struct TimeVal { |
| 165 |
pub tv_sec: i32, |
| 166 |
pub tv_usec: i32, |
| 167 |
} |
| 168 |
|
| 169 |
#[repr(C)] |
| 170 |
pub struct ExitStatus { |
| 171 |
e_termination: i16, |
| 172 |
e_exit: i16, |
| 173 |
} |
| 174 |
|
| 175 |
#[repr(C, align(4))] |
| 176 |
pub struct Utmp { |
| 177 |
pub ut_type: i32, |
| 178 |
pub ut_pid: i32, |
| 179 |
pub ut_line: [i8; 32], |
| 180 |
pub ut_id: [i8; 4], |
| 181 |
|
| 182 |
pub ut_user: [i8; 32], |
| 183 |
pub ut_host: [i8; 256], |
| 184 |
pub ut_exit: ExitStatus, |
| 185 |
pub ut_session: i32, |
| 186 |
pub ut_tv: TimeVal, |
| 187 |
|
| 188 |
pub ut_addr_v6: [i32; 4], |
| 189 |
glibc_reserved: [i8; 20], |
| 190 |
} |
| 191 |
|
| 192 |
let utmp = Utmp { |
| 193 |
ut_type: BOOT_TIME, |
| 194 |
ut_pid: 0, |
| 195 |
ut_line: slice_32("~".as_bytes()), |
| 196 |
ut_id: [126, 126, 0, 0], |
| 197 |
ut_user: slice_32("reboot".as_bytes()), |
| 198 |
ut_host: [0; 256], |
| 199 |
ut_exit: ExitStatus { |
| 200 |
e_termination: 0, |
| 201 |
e_exit: 0, |
| 202 |
}, |
| 203 |
ut_session: 0, |
| 204 |
ut_tv: TimeVal { |
| 205 |
tv_sec: 1716371201, |
| 206 |
tv_usec: 290913, |
| 207 |
}, |
| 208 |
ut_addr_v6: [127, 0, 0, 1], |
| 209 |
glibc_reserved: [0; 20], |
| 210 |
}; |
| 211 |
let utmp1 = Utmp { |
| 212 |
ut_type: RUN_LVL, |
| 213 |
ut_pid: std::process::id() as i32, |
| 214 |
ut_line: slice_32("~".as_bytes()), |
| 215 |
ut_id: [126, 126, 0, 0], |
| 216 |
ut_user: slice_32("runlevel".as_bytes()), |
| 217 |
ut_host: [0; 256], |
| 218 |
ut_exit: ExitStatus { |
| 219 |
e_termination: 0, |
| 220 |
e_exit: 0, |
| 221 |
}, |
| 222 |
ut_session: 0, |
| 223 |
ut_tv: TimeVal { |
| 224 |
tv_sec: 1716371209, |
| 225 |
tv_usec: 162250, |
| 226 |
}, |
| 227 |
ut_addr_v6: [0, 0, 0, 0], |
| 228 |
glibc_reserved: [0; 20], |
| 229 |
}; |
| 230 |
let utmp2 = Utmp { |
| 231 |
ut_type: USER_PROCESS, |
| 232 |
ut_pid: std::process::id() as i32, |
| 233 |
ut_line: slice_32(":1".as_bytes()), |
| 234 |
ut_id: [126, 126, 0, 0], |
| 235 |
ut_user: slice_32("testusr".as_bytes()), |
| 236 |
ut_host: [0; 256], |
| 237 |
ut_exit: ExitStatus { |
| 238 |
e_termination: 0, |
| 239 |
e_exit: 0, |
| 240 |
}, |
| 241 |
ut_session: 0, |
| 242 |
ut_tv: TimeVal { |
| 243 |
tv_sec: 1716371283, |
| 244 |
tv_usec: 858764, |
| 245 |
}, |
| 246 |
ut_addr_v6: [0, 0, 0, 0], |
| 247 |
glibc_reserved: [0; 20], |
| 248 |
}; |
| 249 |
|
| 250 |
fn serialize_i8_arr(buf: &mut Vec<u8>, arr: &[i8]) { |
| 251 |
for b in arr { |
| 252 |
buf.push(*b as u8); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
fn serialize(utmp: &Utmp) -> Vec<u8> { |
| 257 |
let mut buf = Vec::new(); |
| 258 |
buf.extend_from_slice(&utmp.ut_type.to_ne_bytes()); |
| 259 |
buf.extend_from_slice(&utmp.ut_pid.to_ne_bytes()); |
| 260 |
serialize_i8_arr(&mut buf, &utmp.ut_line); |
| 261 |
serialize_i8_arr(&mut buf, &utmp.ut_id); |
| 262 |
serialize_i8_arr(&mut buf, &utmp.ut_user); |
| 263 |
serialize_i8_arr(&mut buf, &utmp.ut_host); |
| 264 |
buf.extend_from_slice(&utmp.ut_exit.e_termination.to_ne_bytes()); |
| 265 |
buf.extend_from_slice(&utmp.ut_exit.e_exit.to_ne_bytes()); |
| 266 |
buf.extend_from_slice(&utmp.ut_session.to_ne_bytes()); |
| 267 |
buf.extend_from_slice(&utmp.ut_tv.tv_sec.to_ne_bytes()); |
| 268 |
buf.extend_from_slice(&utmp.ut_tv.tv_usec.to_ne_bytes()); |
| 269 |
for v in &utmp.ut_addr_v6 { |
| 270 |
buf.extend_from_slice(&v.to_ne_bytes()); |
| 271 |
} |
| 272 |
serialize_i8_arr(&mut buf, &utmp.glibc_reserved); |
| 273 |
buf |
| 274 |
} |
| 275 |
|
| 276 |
let mut buf = serialize(&utmp); |
| 277 |
buf.append(&mut serialize(&utmp1)); |
| 278 |
buf.append(&mut serialize(&utmp2)); |
| 279 |
let mut f = File::create(path).unwrap(); |
| 280 |
f.write_all(&buf).unwrap(); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
#[test] |
| 285 |
#[cfg(unix)] |
| 286 |
fn test_uptime_with_extra_argument() { |
| 287 |
new_ucmd!() |
| 288 |
.arg("a") |
| 289 |
.arg("b") |
| 290 |
.fails() |
| 291 |
.stderr_contains("unexpected value 'b'"); |
| 292 |
} |
| 293 |
|
| 294 |
/// The utmp file operand is unix-only; any operand is rejected on Windows. |
| 295 |
#[test] |
| 296 |
#[cfg(windows)] |
| 297 |
fn test_uptime_with_file_windows() { |
| 298 |
new_ucmd!() |
| 299 |
.arg("file1") |
| 300 |
.fails_with_code(1) |
| 301 |
.stderr_contains("unexpected argument"); |
| 302 |
} |
| 303 |
|
| 304 |
/// Checks whether uptime displays the correct stderr msg when its called with a directory |
| 305 |
#[test] |
| 306 |
#[cfg(unix)] |
| 307 |
fn test_uptime_with_dir() { |
| 308 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 309 |
|
| 310 |
at.mkdir("dir1"); |
| 311 |
|
| 312 |
ucmd.arg("dir1") |
| 313 |
.fails() |
| 314 |
.stderr_contains("uptime: couldn't get boot time: Is a directory") |
| 315 |
.stdout_contains("up ???? days ??:??"); |
| 316 |
} |
| 317 |
|
| 318 |
#[test] |
| 319 |
#[cfg(target_os = "openbsd")] |
| 320 |
fn test_uptime_check_users_openbsd() { |
| 321 |
new_ucmd!() |
| 322 |
.args(&["openbsd_utmp"]) |
| 323 |
.succeeds() |
| 324 |
.stdout_contains("4 users"); |
| 325 |
} |
| 326 |
|
| 327 |
#[test] |
| 328 |
fn test_uptime_since() { |
| 329 |
let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}").unwrap(); |
| 330 |
|
| 331 |
new_ucmd!().arg("--since").succeeds().stdout_matches(&re); |
| 332 |
} |
| 333 |
|
| 334 |
#[test] |
| 335 |
fn test_uptime_pretty_print() { |
| 336 |
new_ucmd!() |
| 337 |
.arg("-p") |
| 338 |
.succeeds() |
| 339 |
.stdout_contains("up") |
| 340 |
.stdout_matches(&Regex::new(r"hour|minute").unwrap()); |
| 341 |
} |
| 342 |
|
| 343 |
/// Test uptime reliability on macOS with sysctl kern.boottime fallback. |
| 344 |
/// This addresses intermittent failures from issue #3621 by ensuring |
| 345 |
/// the command consistently succeeds when utmpx data is unavailable. |
| 346 |
#[test] |
| 347 |
#[cfg(target_vendor = "apple")] |
| 348 |
fn test_uptime_macos_reliability() { |
| 349 |
// Run uptime multiple times to ensure consistent success |
| 350 |
// (Previously would fail intermittently when utmpx had no BOOT_TIME) |
| 351 |
for i in 0..5 { |
| 352 |
let result = new_ucmd!().succeeds(); |
| 353 |
|
| 354 |
// Verify standard output patterns |
| 355 |
result |
| 356 |
.stdout_contains("up") |
| 357 |
.stdout_contains("load average:"); |
| 358 |
|
| 359 |
// Ensure no error about retrieving system uptime |
| 360 |
let stderr = result.stderr_str(); |
| 361 |
assert!( |
| 362 |
!stderr.contains("could not retrieve system uptime"), |
| 363 |
"Iteration {i}: uptime should not fail on macOS (stderr: {stderr})" |
| 364 |
); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
/// Test uptime --since reliability on macOS. |
| 369 |
/// Verifies the sysctl fallback works for the --since flag. |
| 370 |
#[test] |
| 371 |
#[cfg(target_vendor = "apple")] |
| 372 |
fn test_uptime_since_macos() { |
| 373 |
let re = Regex::new(r"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}").unwrap(); |
| 374 |
|
| 375 |
// Run multiple times to ensure consistency |
| 376 |
for i in 0..3 { |
| 377 |
let result = new_ucmd!().arg("--since").succeeds(); |
| 378 |
|
| 379 |
result.stdout_matches(&re); |
| 380 |
|
| 381 |
// Ensure no error messages |
| 382 |
let stderr = result.stderr_str(); |
| 383 |
assert!( |
| 384 |
stderr.is_empty(), |
| 385 |
"Iteration {i}: uptime --since should not produce stderr on macOS (stderr: {stderr})" |
| 386 |
); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
/// Test that uptime output format is consistent on macOS. |
| 391 |
/// Ensures the sysctl fallback produces properly formatted output. |
| 392 |
#[test] |
| 393 |
#[cfg(target_vendor = "apple")] |
| 394 |
fn test_uptime_macos_output_format() { |
| 395 |
let result = new_ucmd!().succeeds(); |
| 396 |
let stdout = result.stdout_str(); |
| 397 |
|
| 398 |
// Verify time is present (format: HH:MM:SS) |
| 399 |
let time_re = Regex::new(r"\d{2}:\d{2}:\d{2}").unwrap(); |
| 400 |
assert!( |
| 401 |
time_re.is_match(stdout), |
| 402 |
"Output should contain time in HH:MM:SS format: {stdout}" |
| 403 |
); |
| 404 |
|
| 405 |
// Verify uptime format (either "HH:MM" or "X days HH:MM") |
| 406 |
assert!( |
| 407 |
stdout.contains(" up "), |
| 408 |
"Output should contain 'up': {stdout}" |
| 409 |
); |
| 410 |
|
| 411 |
// Verify load average is present |
| 412 |
let load_re = Regex::new(r"load average: \d+\.\d+, \d+\.\d+, \d+\.\d+").unwrap(); |
| 413 |
assert!( |
| 414 |
load_re.is_match(stdout), |
| 415 |
"Output should contain load average: {stdout}" |
| 416 |
); |
| 417 |
} |