Files
xop/tests/integration.rs
T
2026-10-02 18:25:29 +08:00

362 lines
10 KiB
Rust

//! Integration tests for the xop CLI (v0.2).
//!
//! Run with: `cargo test --test integration -- --test-threads=1`
//! (the --test-threads=1 is because some tests share HOME or env state)
//!
//! Tests that touch the OS auth gate (Touch ID / PAM) set
//! `XOP_AUTH_BYPASS=1` to skip it. Tests that need a real vault set
//! `XOP_VAULT_DIR` to a tempdir.
use assert_cmd::Command;
use predicates::prelude::*;
fn xop() -> Command {
Command::cargo_bin("xop").expect("binary built")
}
fn xop_bypass_auth() -> Command {
let mut c = xop();
c.env("XOP_AUTH_BYPASS", "1");
c
}
fn make_vault() -> tempfile::TempDir {
let tmp = tempfile::tempdir().unwrap();
// Set HOME so xop finds ~/.xop/key.db under our tempdir.
std::env::set_var("HOME", tmp.path());
// Pre-create the vault file by piping a non-interactive init via env var.
tmp
}
#[test]
fn help_shows_subcommands() {
xop()
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("init"))
.stdout(predicate::str::contains("set"))
.stdout(predicate::str::contains("list"))
.stdout(predicate::str::contains("get"))
.stdout(predicate::str::contains("rm"))
.stdout(predicate::str::contains("Touch ID"));
}
#[test]
fn no_args_shows_banner() {
xop()
.assert()
.success()
.stdout(predicate::str::contains("xop"))
.stdout(predicate::str::contains("Quick start"));
}
#[test]
fn dry_run_prints_effective_policy() {
xop()
.args([
"--dry-run",
"--secret", "DB_PASS=db_pass",
"--allow-net=db.local:5432",
"--deny-net=*",
"--",
"echo", "hello",
])
.assert()
.success()
.stdout(predicate::str::contains("# xop dry-run"))
.stdout(predicate::str::contains("db_pass"))
.stdout(predicate::str::contains("db.local:5432"))
.stdout(predicate::str::contains("(dry-run: not executing)"));
}
#[test]
fn missing_command_exits_64() {
xop()
.args(["--dry-run"])
.assert()
.failure()
.code(64)
.stderr(predicate::str::contains("no command given"));
}
#[test]
fn bad_ref_format_exits_64() {
xop()
.args([
"--dry-run",
"--secret", "not_a_ref",
"--",
"true",
])
.assert()
.failure()
.code(64)
.stderr(predicate::str::contains("invalid ref"));
}
#[test]
fn unknown_scheme_rejected() {
// Old v0.1 op:// scheme must be rejected (we migrated to bare keys).
xop()
.args([
"--dry-run",
"--secret", "X=op://v/i/f",
"--",
"true",
])
.assert()
.failure()
.code(64)
.stderr(predicate::str::contains("invalid ref"));
}
#[test]
fn secrets_file_parses() {
let tmp = tempfile::NamedTempFile::new().unwrap();
std::fs::write(
tmp.path(),
r#"
# comment
DB_PASS="db_pass"
API_KEY='api_key'
"#,
)
.unwrap();
xop()
.args([
"--dry-run",
"--secrets-file", tmp.path().to_str().unwrap(),
"--",
"true",
])
.assert()
.success()
.stdout(predicate::str::contains("db_pass"))
.stdout(predicate::str::contains("api_key"));
}
#[test]
fn env_var_picks_up_xop_log_level() {
xop()
.env("RUST_LOG", "xop=debug")
.args(["--dry-run", "--", "true"])
.assert()
.success();
}
#[test]
fn init_creates_vault() {
// With `--features test-bypass`, `XOP_VAULT_PASSWORD` feeds the passphrase
// prompt via stdin (non-tty fallback), so init runs end-to-end.
let _vault = make_vault();
xop_bypass_auth()
.env("XOP_INIT_NONINTERACTIVE", "1")
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["init"])
.assert()
.success();
}
#[test]
fn run_masks_secret_in_child_stdout() {
// Full flow: init → set → `xop run` whose child ECHOES the injected
// secret. The stdout guard must replace the value with the partial mask
// `va****【xop禁止将密钥泄漏】` and never let the raw value through.
let vault_dir = make_vault();
// init
xop_bypass_auth()
.env("XOP_INIT_NONINTERACTIVE", "1")
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["init"])
.assert()
.success();
// set a known secret (value piped on stdin, non-TTY)
xop_bypass_auth()
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["set", "LEAKY"])
.write_stdin("value12345\n")
.assert()
.success();
// run: child prints $LEAKY — must be masked
xop_bypass_auth()
.env("XOP_VAULT_PASSWORD", "testpass")
.args([
"run",
"--secret", "LEAKY=LEAKY",
"--",
"sh", "-c", "echo leaked: $LEAKY",
])
.assert()
.success()
.stdout(predicate::str::contains("va****【xop禁止将密钥泄漏】"))
.stdout(predicate::str::contains("value12345").not());
let _ = vault_dir;
}
#[test]
fn run_masks_argv_placeholder_value() {
// `xop://NAME` argv substitution puts the value into the child's argv;
// when the child echoes it, the guard must still catch it.
make_vault();
xop_bypass_auth()
.env("XOP_INIT_NONINTERACTIVE", "1")
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["init"])
.assert()
.success();
xop_bypass_auth()
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["set", "-f", "PH"])
.write_stdin("secretvalue\n")
.assert()
.success();
xop_bypass_auth()
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["run", "--", "echo", "token=xop://PH"])
.assert()
.success()
.stdout(predicate::str::contains("se****【xop禁止将密钥泄漏】"))
.stdout(predicate::str::contains("secretvalue").not());
}
#[test]
fn keyring_enables_passphrase_free_unlock() {
// Issue #3: after `xop init` writes the 0600 keyring, later ops unlock
// with NO passphrase — even with XOP_VAULT_PASSWORD absent and stdin
// empty. (OS gate bypassed via XOP_AUTH_BYPASS as in all vault tests.)
let _home = make_vault();
xop_bypass_auth()
.env("XOP_INIT_NONINTERACTIVE", "1")
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["init"])
.assert()
.success();
xop_bypass_auth()
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["set", "K"])
.write_stdin("val123\n")
.assert()
.success();
// Crucially: env_remove — no passphrase channel exists at all now.
xop_bypass_auth()
.env_remove("XOP_VAULT_PASSWORD")
.args(["run", "--secret", "V=K", "--", "sh", "-c", "echo got $V"])
.assert()
.success()
.stdout(predicate::str::contains("va****【xop禁止将密钥泄漏】"))
.stdout(predicate::str::contains("val123").not());
// And `xop key` status reports it active.
xop_bypass_auth()
.args(["key"])
.assert()
.success()
.stdout(predicate::str::contains("auto-unlock ACTIVE"));
}
#[test]
fn ssh_env_headless_still_unlocks_via_keyring() {
// Regression guard for the SSH redesign: SSH_* env vars set + NO pty
// (scp/ansible/agent-style `ssh host cmd`) → headless automation path →
// keyring silent unlock, no passphrase, no osascript call.
// XOP_AUTH_BYPASS deliberately NOT set — this exercises the real gate.
let _home = make_vault();
xop_bypass_auth()
.env("XOP_INIT_NONINTERACTIVE", "1")
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["init"])
.assert()
.success();
xop_bypass_auth()
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["set", "K"])
.write_stdin("val123\n")
.assert()
.success();
xop()
.env_remove("XOP_AUTH_BYPASS")
.env("SSH_CONNECTION", "10.0.0.5 51234 10.0.0.1 22")
.env("SSH_TTY", "/dev/ttys003")
.env_remove("XOP_VAULT_PASSWORD")
.args(["list"])
.assert()
.success()
.stdout(predicate::str::contains("K"));
}
#[test]
fn get_unknown_key_exits_77() {
let _vault = make_vault();
// Vault doesn't exist → init first via direct file write below.
xop_bypass_auth()
.args(["get", "MISSING"])
.assert()
.failure()
.code(78); // VaultNotInitialized → 78 EX_CONFIG
}
#[test]
fn get_masks_value_by_default() {
// `xop get` is routinely driven by AI agents — the value must NEVER
// reach stdout in the clear. Default output is the partial mask;
// plaintext only via explicit -v (see get_dash_v_prints_plaintext).
let _home = make_vault();
xop_bypass_auth()
.env("XOP_INIT_NONINTERACTIVE", "1")
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["init"])
.assert()
.success();
xop_bypass_auth()
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["set", "SHOW"])
.write_stdin("sup3rsecret\n")
.assert()
.success();
xop_bypass_auth()
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["get", "SHOW"])
.assert()
.success()
.stdout(predicate::str::contains("su****【xop禁止将密钥泄漏】"))
.stdout(predicate::str::contains("sup3rsecret").not())
.stderr(predicate::str::contains("-v"));
}
#[test]
fn get_dash_v_prints_plaintext() {
// -v is the human visual-inspection escape hatch: plaintext on stdout,
// loud warning on stderr. Agents must not use this path.
let _home = make_vault();
xop_bypass_auth()
.env("XOP_INIT_NONINTERACTIVE", "1")
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["init"])
.assert()
.success();
xop_bypass_auth()
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["set", "SHOW"])
.write_stdin("sup3rsecret\n")
.assert()
.success();
xop_bypass_auth()
.env("XOP_VAULT_PASSWORD", "testpass")
.args(["get", "SHOW", "-v"])
.assert()
.success()
.stdout(predicate::str::contains("sup3rsecret"))
.stdout(predicate::str::contains("【xop禁止将密钥泄漏】").not())
.stderr(predicate::str::contains("AI agent"));
}
#[test]
fn list_on_empty_vault() {
let _vault = make_vault();
xop_bypass_auth()
.args(["list"])
.assert()
.failure()
.code(78); // vault not initialized
}