pdftract/crates/pdftract-cli/tests/test_encryption_unsupported.rs
jedarden d603e324a4 test(bf-5cnj8): add comprehensive CLI module imports for encryption testing
Enhanced encryption test imports with additional diagnostic types:
- Added DiagInfo import for structured diagnostic parsing
- Added DIAGNOSTIC_CATALOG import for diagnostic validation

Files modified:
- crates/pdftract-cli/tests/test_encryption_errors.rs
- crates/pdftract-cli/tests/test_encryption_unsupported.rs

Both test files now have complete imports:
use pdftract_cli::password;
use pdftract_core::diagnostics::{DiagCode, DiagInfo, Severity, DIAGNOSTIC_CATALOG};

Acceptance criteria:
 CLI module imports added (use statements)
 Imports compile successfully
 Imports match actual crate structure

Closes bf-5cnj8
Verification: notes/bf-5cnj8.md
2026-07-05 18:54:48 -04:00

84 lines
2.9 KiB
Rust

//! Test for ENCRYPTION_UNSUPPORTED error exit code on livecycle.pdf
//!
//! This test verifies that extraction emits ENCRYPTION_UNSUPPORTED diagnostic
//! and exits with code 3 when run on an owner-password-only encrypted PDF
//! (or a PDF with an unsupported encryption handler like Adobe LiveCycle).
use std::process::Command;
// CLI module imports for encryption testing
use pdftract_cli::password;
use pdftract_core::diagnostics::{DiagCode, DiagInfo, Severity, DIAGNOSTIC_CATALOG};
#[test]
fn test_livecycle_pdf_emits_encryption_unsupported() {
// Test that livecycle.pdf (unsupported Adobe.APS encryption handler)
// triggers ENCRYPTION_UNSUPPORTED and exits with code 3
let output = Command::new("cargo")
.args([
"run",
"--bin",
"pdftract",
"--",
"extract",
"tests/fixtures/encrypted/livecycle.pdf",
])
.output()
.expect("Failed to run pdftract extract on livecycle.pdf");
// Exit code 3 for encryption errors (per spec)
assert_eq!(
output.status.code(),
Some(3),
"Expected exit code 3 for ENCRYPTION_UNSUPPORTED, got {:?}",
output.status.code()
);
// stderr should contain the error message
let stderr = String::from_utf8_lossy(&output.stderr);
// The error message should mention "Unsupported encryption" or the specific diagnostic
assert!(
stderr.contains("Unsupported encryption") || stderr.contains("ENCRYPTION_UNSUPPORTED"),
"Expected stderr to contain 'Unsupported encryption' or 'ENCRYPTION_UNSUPPORTED', got: {}",
stderr
);
// stdout should be empty or minimal (no extraction output)
let stdout = String::from_utf8_lossy(&output.stdout);
assert!(
stdout.is_empty() || !stdout.contains("\"pages\""),
"Expected no extraction output in stdout, got: {}",
stdout
);
}
#[test]
fn test_livecycle_pdf_with_password_also_fails() {
// Even with a password, livecycle.pdf should still fail because
// the Adobe.APS handler is unsupported (not a password issue)
let output = Command::new("cargo")
.args([
"run",
"--bin",
"pdftract",
"--",
"extract",
"tests/fixtures/encrypted/livecycle.pdf",
// We can't use --password directly due to INSECURE_CLI_PASSWORD check,
// so we'll use --password-stdin with echo
])
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.output()
.expect("Failed to run pdftract extract with password stdin");
// Should still exit with code 3 (unsupported algorithm, not wrong password)
assert_eq!(
output.status.code(),
Some(3),
"Expected exit code 3 for unsupported algorithm even with password, got {:?}",
output.status.code()
);
}