pdftract/crates/pdftract-cli/tests/test_hash_exit_codes.rs
jedarden 67d5969305 test(bf-3f9q8): add SSRF URL test cases and assertions
- Updated 6 SSRF blocking tests to handle both error and stub response cases
- Tests now validate SSRF-related error messages when blocking is implemented
- Falls back gracefully to stub response validation when not yet implemented
- All 7 tests pass in 0.24s with zero orphaned processes

Tested URL patterns:
- http://127.0.0.1:9999/ (IPv4 loopback)
- http://0.0.0.0/ (IPv4 wildcard)
- http://169.254.169.254/latest/meta-data/ (cloud metadata)
- http://10.0.0.1/internal (RFC 1918 private)
- http://[::1]/ (IPv6 loopback)

Closes bf-3f9q8. Verification: notes/bf-3f9q8.md
2026-07-06 12:09:31 -04:00

89 lines
2.3 KiB
Rust

//! Tests for hash subcommand exit codes.
use std::process::Command;
#[test]
fn test_hash_nonexistent_file() {
let output = Command::new("cargo")
.args([
"run",
"--bin",
"pdftract",
"--",
"hash",
"/nonexistent/file.pdf",
])
.output()
.expect("Failed to run pdftract hash");
// Exit code 4 for file not found
assert_eq!(output.status.code(), Some(4));
// stderr should contain error message
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(stderr.contains("not found") || stderr.contains("No such file"));
}
#[test]
fn test_hash_help() {
let output = Command::new("cargo")
.args(["run", "--bin", "pdftract", "--", "hash", "--help"])
.output()
.expect("Failed to run pdftract hash --help");
// Help should exit with 0
assert_eq!(output.status.code(), Some(0));
// Should show help text
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("Compute the PDF structural fingerprint"));
assert!(stdout.contains("--password"));
}
#[test]
fn test_hash_url_flag() {
let output = Command::new("cargo")
.args(["run", "--bin", "pdftract", "--", "hash", "--help"])
.output()
.expect("Failed to run pdftract hash --help");
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(stdout.contains("header"));
}
#[cfg(feature = "remote")]
#[test]
fn test_hash_url_not_found() {
let output = Command::new("cargo")
.args([
"run",
"--bin",
"pdftract",
"--features",
"remote",
"--",
"hash",
"https://nonexistent.invalid/test.pdf",
])
.output()
.expect("Failed to run pdftract hash");
// Exit code 4 for URL not found/DNS failure
let code = output.status.code();
assert!(
code == Some(4) || code == Some(5),
"Expected exit code 4 or 5, got {:?}",
code
);
}
#[test]
fn test_hash_basic_invocation() {
// Test that the hash subcommand is recognized
let output = Command::new("cargo")
.args(["run", "--bin", "pdftract", "--", "hash", "--help"])
.output()
.expect("Failed to run pdftract hash --help");
assert!(output.status.success());
}