use oxidize_pdf::parser::{ParseError, PdfReader};
fn get_test_pdf_path() -> std::path::PathBuf {
std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/Cold_Email_Hacks.pdf")
}
#[test]
fn test_unencrypted_pdf_not_encrypted() {
let path = get_test_pdf_path();
if !path.exists() {
eprintln!("Skipping test: test PDF not found at {:?}", path);
return;
}
let reader = PdfReader::open(path).expect("Failed to open PDF");
assert!(
!reader.is_encrypted(),
"Unencrypted PDF should report is_encrypted() = false"
);
}
#[test]
fn test_unencrypted_pdf_is_unlocked() {
let path = get_test_pdf_path();
if !path.exists() {
eprintln!("Skipping test: test PDF not found at {:?}", path);
return;
}
let reader = PdfReader::open(path).expect("Failed to open PDF");
assert!(
reader.is_unlocked(),
"Unencrypted PDF should report is_unlocked() = true"
);
}
#[test]
fn test_unlock_on_unencrypted_pdf_is_noop() {
let path = get_test_pdf_path();
if !path.exists() {
eprintln!("Skipping test: test PDF not found at {:?}", path);
return;
}
let mut reader = PdfReader::open(path).expect("Failed to open PDF");
let result = reader.unlock("any_password");
assert!(
result.is_ok(),
"unlock() on unencrypted PDF should succeed: {:?}",
result
);
}
#[test]
fn test_unencrypted_pdf_can_read_objects() {
let path = get_test_pdf_path();
if !path.exists() {
eprintln!("Skipping test: test PDF not found at {:?}", path);
return;
}
let mut reader = PdfReader::open(path).expect("Failed to open PDF");
let catalog = reader.catalog();
assert!(
catalog.is_ok(),
"Should read catalog from unencrypted PDF: {:?}",
catalog
);
let catalog = catalog.unwrap();
let type_name = catalog
.get("Type")
.and_then(|o| o.as_name())
.map(|n| n.0.as_str());
assert_eq!(
type_name,
Some("Catalog"),
"Catalog should have /Type /Catalog"
);
}
#[test]
fn test_parse_error_wrong_password_exists() {
let error = ParseError::WrongPassword;
let error_string = error.to_string();
assert!(
error_string.contains("password"),
"WrongPassword error should mention 'password': {}",
error_string
);
}
#[test]
fn test_parse_error_pdf_locked_exists() {
let error = ParseError::PdfLocked;
let error_string = error.to_string();
assert!(
error_string.contains("locked") || error_string.contains("unlock"),
"PdfLocked error should mention 'locked' or 'unlock': {}",
error_string
);
}
#[test]
fn test_unlock_idempotent_on_unencrypted() {
let path = get_test_pdf_path();
if !path.exists() {
eprintln!("Skipping test: test PDF not found at {:?}", path);
return;
}
let mut reader = PdfReader::open(path).expect("Failed to open PDF");
assert!(
reader.unlock("password1").is_ok(),
"First unlock should succeed"
);
assert!(
reader.unlock("password2").is_ok(),
"Second unlock should succeed"
);
assert!(reader.unlock("").is_ok(), "Third unlock should succeed");
assert!(reader.is_unlocked(), "Should remain unlocked");
assert!(reader.catalog().is_ok(), "Should still read catalog");
}
#[test]
fn test_get_object_on_unencrypted_pdf() {
let path = get_test_pdf_path();
if !path.exists() {
eprintln!("Skipping test: test PDF not found at {:?}", path);
return;
}
let mut reader = PdfReader::open(path).expect("Failed to open PDF");
let obj = reader.get_object(1, 0);
assert!(
obj.is_ok(),
"Should get object from unencrypted PDF: {:?}",
obj
);
let obj = obj.unwrap();
assert!(
obj.as_dict().is_some() || obj.is_null(),
"Object 1 should be a dictionary or null"
);
}
#[test]
fn test_multiple_object_reads() {
let path = get_test_pdf_path();
if !path.exists() {
eprintln!("Skipping test: test PDF not found at {:?}", path);
return;
}
let mut reader = PdfReader::open(path).expect("Failed to open PDF");
{
let obj1 = reader.get_object(1, 0);
assert!(obj1.is_ok(), "Should get object 1: {:?}", obj1);
}
{
let obj2 = reader.get_object(2, 0);
assert!(obj2.is_ok(), "Should get object 2: {:?}", obj2);
}
{
let obj3 = reader.get_object(3, 0);
assert!(obj3.is_ok(), "Should get object 3: {:?}", obj3);
}
}
#[test]
fn test_unlock_api_example() {
let path = get_test_pdf_path();
if !path.exists() {
eprintln!("Skipping test: test PDF not found at {:?}", path);
return;
}
let mut reader = PdfReader::open(path).expect("Failed to open PDF");
if reader.is_encrypted() {
reader.unlock("password").expect("Failed to unlock");
}
let catalog = reader.catalog().expect("Failed to read catalog");
assert!(catalog.get("Type").is_some());
}