Files
Pierre Warnier 96f9c40ca3 tests: deduplicate skip_unless_root() into common module
Replace 13 local copies across all test files with a shared import
from tests/common/mod.rs. Fix getuid/geteuid inconsistency in
test_useradd.rs and test_userdel.rs (effective UID is correct for
setuid-root tools).

Fixes #133
2026-04-03 17:45:10 +02:00

69 lines
2.0 KiB
Rust

// This file is part of the shadow-rs package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//! Integration tests for the `chsh` utility.
//!
//! Root-only tests exercise real operations via synthetic files.
//! Non-root tests exercise clap parsing and error paths.
use std::ffi::OsString;
#[path = "../common/mod.rs"]
mod common;
/// Run `uumain` with the given args, returning the exit code.
fn run(args: &[&str]) -> i32 {
let os_args: Vec<OsString> = args.iter().map(|s| (*s).into()).collect();
chsh::uumain(os_args.into_iter())
}
// ---------------------------------------------------------------------------
// Non-root tests
// ---------------------------------------------------------------------------
#[test]
fn test_help_exits_zero() {
let code = run(&["chsh", "--help"]);
assert_eq!(code, 0, "--help should exit 0");
}
#[test]
fn test_unknown_flag_exits_one() {
let code = run(&["chsh", "--bogus"]);
assert_eq!(code, 1, "unknown flag should exit 1");
}
#[test]
fn test_no_shell_flag_exits_error() {
// Without -s flag, chsh should error
let code = run(&["chsh"]);
assert_eq!(code, 1, "no -s flag should exit 1");
}
// ---------------------------------------------------------------------------
// Root-only tests
// ---------------------------------------------------------------------------
#[test]
fn test_list_shells() {
if common::skip_unless_root() {
return;
}
// -l should list shells and exit 0 (assuming /etc/shells exists on the system)
let code = run(&["chsh", "-l"]);
// Exit 0 even if /etc/shells is empty — the tool prints a warning but succeeds
assert_eq!(code, 0, "--list-shells should exit 0");
}
#[test]
fn test_invalid_shell_path() {
if common::skip_unless_root() {
return;
}
// Relative path should be rejected
let code = run(&["chsh", "-s", "bin/bash"]);
assert_eq!(code, 1, "relative shell path should exit 1");
}