| 1 |
use lopdf::{Document, Object}; |
| 2 |
|
| 3 |
#[cfg(not(feature = "async"))] |
| 4 |
#[test] |
| 5 |
fn test_load_encrypted_pdf_from_assets() { |
| 6 |
// Test loading the existing encrypted.pdf file |
| 7 |
let doc = Document::load("assets/encrypted.pdf").unwrap(); |
| 8 |
|
| 9 |
// Verify the document was loaded |
| 10 |
assert!(doc.is_encrypted()); |
| 11 |
|
| 12 |
// Check that we can access the decrypted content |
| 13 |
let pages = doc.get_pages(); |
| 14 |
assert_eq!(pages.len(), 1, "Should have exactly one page"); |
| 15 |
|
| 16 |
// Try to extract text from the document |
| 17 |
let page_numbers: Vec<u32> = pages.keys().cloned().collect(); |
| 18 |
// The document should be readable even if encrypted with empty password |
| 19 |
let text = doc.extract_text(&page_numbers).unwrap(); |
| 20 |
|
| 21 |
// Verify we can extract meaningful text |
| 22 |
assert!(text.contains("USCIS"), "Should contain USCIS text from the form"); |
| 23 |
assert!(text.contains("Form G-1145"), "Should contain form number"); |
| 24 |
|
| 25 |
// Verify we can access objects |
| 26 |
for i in 1..=10 { |
| 27 |
// Should be able to access at least the first 10 objects |
| 28 |
assert!(doc.get_object((i, 0)).is_ok(), "Should be able to access object ({}, 0)", i); |
| 29 |
} |
| 30 |
|
| 31 |
// Verify trailer has required entries for encrypted PDF |
| 32 |
assert!(doc.trailer.get(b"Root").is_ok(), "Trailer should have Root entry"); |
| 33 |
assert!(doc.trailer.get(b"Encrypt").is_ok(), "Trailer should have Encrypt entry"); |
| 34 |
assert!(doc.trailer.get(b"Info").is_ok(), "Trailer should have Info entry"); |
| 35 |
|
| 36 |
// Verify encryption state is properly set |
| 37 |
assert!(doc.encryption_state.is_some(), "Encryption state should be set"); |
| 38 |
} |
| 39 |
|
| 40 |
#[cfg(not(feature = "async"))] |
| 41 |
#[test] |
| 42 |
fn test_decrypt_pdf_with_empty_password() { |
| 43 |
// Create a simple PDF document |
| 44 |
let mut doc = Document::with_version("1.5"); |
| 45 |
|
| 46 |
// Add an ID to the trailer (required for encryption) |
| 47 |
let id1 = vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; |
| 48 |
let id2 = vec![16u8, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; |
| 49 |
doc.trailer.set( |
| 50 |
"ID", |
| 51 |
Object::Array(vec![ |
| 52 |
Object::String(id1, lopdf::StringFormat::Literal), |
| 53 |
Object::String(id2, lopdf::StringFormat::Literal), |
| 54 |
]), |
| 55 |
); |
| 56 |
|
| 57 |
// Create a simple page structure |
| 58 |
let pages_id = doc.new_object_id(); |
| 59 |
let page_id = doc.new_object_id(); |
| 60 |
let content_id = doc.new_object_id(); |
| 61 |
let font_id = doc.new_object_id(); |
| 62 |
let resources_id = doc.new_object_id(); |
| 63 |
|
| 64 |
// Create catalog |
| 65 |
let catalog_dict = lopdf::dictionary! { |
| 66 |
"Type" => "Catalog", |
| 67 |
"Pages" => Object::Reference(pages_id) |
| 68 |
}; |
| 69 |
let catalog_id = doc.add_object(catalog_dict); |
| 70 |
doc.trailer.set("Root", Object::Reference(catalog_id)); |
| 71 |
|
| 72 |
// Create pages |
| 73 |
let pages_dict = lopdf::dictionary! { |
| 74 |
"Type" => "Pages", |
| 75 |
"Kids" => vec![Object::Reference(page_id)], |
| 76 |
"Count" => 1 |
| 77 |
}; |
| 78 |
doc.objects.insert(pages_id, Object::Dictionary(pages_dict)); |
| 79 |
|
| 80 |
// Create resources |
| 81 |
let resources_dict = lopdf::dictionary! { |
| 82 |
"Font" => lopdf::dictionary! { |
| 83 |
"F1" => Object::Reference(font_id) |
| 84 |
} |
| 85 |
}; |
| 86 |
doc.objects.insert(resources_id, Object::Dictionary(resources_dict)); |
| 87 |
|
| 88 |
// Create page |
| 89 |
let page_dict = lopdf::dictionary! { |
| 90 |
"Type" => "Page", |
| 91 |
"Parent" => Object::Reference(pages_id), |
| 92 |
"MediaBox" => vec![Object::Integer(0), Object::Integer(0), Object::Integer(612), Object::Integer(792)], |
| 93 |
"Resources" => Object::Reference(resources_id), |
| 94 |
"Contents" => Object::Reference(content_id) |
| 95 |
}; |
| 96 |
doc.objects.insert(page_id, Object::Dictionary(page_dict)); |
| 97 |
|
| 98 |
// Create font |
| 99 |
let font_dict = lopdf::dictionary! { |
| 100 |
"Type" => "Font", |
| 101 |
"Subtype" => "Type1", |
| 102 |
"BaseFont" => "Helvetica" |
| 103 |
}; |
| 104 |
doc.objects.insert(font_id, Object::Dictionary(font_dict)); |
| 105 |
|
| 106 |
// Create content stream |
| 107 |
let content = b"BT\n/F1 12 Tf\n100 700 Td\n(Hello, Encrypted World!) Tj\nET\n"; |
| 108 |
let content_stream = lopdf::Stream::new(lopdf::dictionary! {}, content.to_vec()); |
| 109 |
doc.objects.insert(content_id, Object::Stream(content_stream)); |
| 110 |
|
| 111 |
// Save to a temporary file |
| 112 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 113 |
let unencrypted_path = temp_dir.path().join("test_unencrypted.pdf"); |
| 114 |
doc.save(&unencrypted_path).unwrap(); |
| 115 |
|
| 116 |
// Encrypt the document with empty password |
| 117 |
let permissions = lopdf::Permissions::all(); |
| 118 |
let encryption_version = lopdf::EncryptionVersion::V2 { |
| 119 |
document: &doc, |
| 120 |
owner_password: "", |
| 121 |
user_password: "", |
| 122 |
key_length: 128, |
| 123 |
permissions, |
| 124 |
}; |
| 125 |
|
| 126 |
let encryption_state = lopdf::EncryptionState::try_from(encryption_version).unwrap(); |
| 127 |
doc.encrypt(&encryption_state).unwrap(); |
| 128 |
|
| 129 |
// Save encrypted document |
| 130 |
let encrypted_path = temp_dir.path().join("test_encrypted.pdf"); |
| 131 |
doc.save(&encrypted_path).unwrap(); |
| 132 |
|
| 133 |
// Now test loading the encrypted document |
| 134 |
let loaded_doc = Document::load(&encrypted_path).unwrap(); |
| 135 |
|
| 136 |
// Verify the document was loaded and decrypted |
| 137 |
assert!(loaded_doc.is_encrypted()); |
| 138 |
|
| 139 |
// Check that we can access the decrypted content |
| 140 |
let pages = loaded_doc.get_pages(); |
| 141 |
assert_eq!(pages.len(), 1); |
| 142 |
|
| 143 |
// Extract text to verify decryption worked |
| 144 |
let page_numbers: Vec<u32> = pages.keys().cloned().collect(); |
| 145 |
let text = loaded_doc.extract_text(&page_numbers).unwrap(); |
| 146 |
assert!(text.contains("Hello, Encrypted World!")); |
| 147 |
} |
| 148 |
|
| 149 |
#[cfg(not(feature = "async"))] |
| 150 |
#[test] |
| 151 |
#[ignore] // Object streams with encryption need more work |
| 152 |
fn test_decrypt_pdf_with_object_streams() { |
| 153 |
// Create a document with object streams |
| 154 |
let mut doc = Document::with_version("1.5"); |
| 155 |
|
| 156 |
// Add an ID to the trailer |
| 157 |
let id1 = vec![10u8, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160]; |
| 158 |
let id2 = vec![160u8, 150, 140, 130, 120, 110, 100, 90, 80, 70, 60, 50, 40, 30, 20, 10]; |
| 159 |
doc.trailer.set( |
| 160 |
"ID", |
| 161 |
Object::Array(vec![ |
| 162 |
Object::String(id1, lopdf::StringFormat::Literal), |
| 163 |
Object::String(id2, lopdf::StringFormat::Literal), |
| 164 |
]), |
| 165 |
); |
| 166 |
|
| 167 |
// Create catalog |
| 168 |
let catalog_dict = lopdf::dictionary! { |
| 169 |
"Type" => "Catalog", |
| 170 |
"Pages" => Object::Reference((2, 0)) |
| 171 |
}; |
| 172 |
let catalog_id = doc.add_object(catalog_dict); |
| 173 |
doc.trailer.set("Root", Object::Reference(catalog_id)); |
| 174 |
|
| 175 |
// Create pages tree |
| 176 |
let pages_dict = lopdf::dictionary! { |
| 177 |
"Type" => "Pages", |
| 178 |
"Kids" => vec![Object::Reference((3, 0))], |
| 179 |
"Count" => 1 |
| 180 |
}; |
| 181 |
doc.objects.insert((2, 0), Object::Dictionary(pages_dict)); |
| 182 |
|
| 183 |
// Create page |
| 184 |
let page_dict = lopdf::dictionary! { |
| 185 |
"Type" => "Page", |
| 186 |
"Parent" => Object::Reference((2, 0)), |
| 187 |
"MediaBox" => vec![Object::Integer(0), Object::Integer(0), Object::Integer(612), Object::Integer(792)], |
| 188 |
"Resources" => lopdf::dictionary! { |
| 189 |
"Font" => lopdf::dictionary! { |
| 190 |
"F1" => Object::Reference((4, 0)) |
| 191 |
} |
| 192 |
}, |
| 193 |
"Contents" => Object::Reference((5, 0)) |
| 194 |
}; |
| 195 |
doc.objects.insert((3, 0), Object::Dictionary(page_dict)); |
| 196 |
|
| 197 |
// Create font |
| 198 |
let font_dict = lopdf::dictionary! { |
| 199 |
"Type" => "Font", |
| 200 |
"Subtype" => "Type1", |
| 201 |
"BaseFont" => "Helvetica" |
| 202 |
}; |
| 203 |
doc.objects.insert((4, 0), Object::Dictionary(font_dict)); |
| 204 |
|
| 205 |
// Create content stream |
| 206 |
let content = b"BT\n/F1 12 Tf\n100 700 Td\n(Test with Object Streams!) Tj\nET\n"; |
| 207 |
let content_stream = lopdf::Stream::new(lopdf::dictionary! {}, content.to_vec()); |
| 208 |
doc.objects.insert((5, 0), Object::Stream(content_stream)); |
| 209 |
|
| 210 |
// Compress document using object streams |
| 211 |
doc.compress(); |
| 212 |
|
| 213 |
// Encrypt the document |
| 214 |
let permissions = lopdf::Permissions::all(); |
| 215 |
let encryption_version = lopdf::EncryptionVersion::V2 { |
| 216 |
document: &doc, |
| 217 |
owner_password: "owner", |
| 218 |
user_password: "", |
| 219 |
key_length: 128, |
| 220 |
permissions, |
| 221 |
}; |
| 222 |
|
| 223 |
let encryption_state = lopdf::EncryptionState::try_from(encryption_version).unwrap(); |
| 224 |
doc.encrypt(&encryption_state).unwrap(); |
| 225 |
|
| 226 |
// Save encrypted document |
| 227 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 228 |
let encrypted_path = temp_dir.path().join("test_encrypted_objstream.pdf"); |
| 229 |
doc.save(&encrypted_path).unwrap(); |
| 230 |
|
| 231 |
// Load and verify |
| 232 |
let loaded_doc = Document::load(&encrypted_path).unwrap(); |
| 233 |
assert!(loaded_doc.is_encrypted()); |
| 234 |
|
| 235 |
// Verify we can access the content |
| 236 |
let pages = loaded_doc.get_pages(); |
| 237 |
assert_eq!(pages.len(), 1); |
| 238 |
|
| 239 |
// Extract text to verify decryption worked |
| 240 |
let page_numbers: Vec<u32> = pages.keys().cloned().collect(); |
| 241 |
let text = loaded_doc.extract_text(&page_numbers).unwrap(); |
| 242 |
assert!(text.contains("Test with Object Streams!")); |
| 243 |
} |
| 244 |
|
| 245 |
#[cfg(not(feature = "async"))] |
| 246 |
#[test] |
| 247 |
#[ignore] // Raw object extraction needs adjustments for new decryption approach |
| 248 |
fn test_encrypted_pdf_raw_object_extraction() { |
| 249 |
// This test verifies that the raw object extraction works correctly |
| 250 |
// for encrypted PDFs, which is crucial for the pdftk-style decryption |
| 251 |
|
| 252 |
let mut doc = Document::with_version("1.5"); |
| 253 |
|
| 254 |
// Add ID |
| 255 |
let id1 = vec![99u8; 16]; |
| 256 |
let id2 = vec![88u8; 16]; |
| 257 |
doc.trailer.set( |
| 258 |
"ID", |
| 259 |
Object::Array(vec![ |
| 260 |
Object::String(id1, lopdf::StringFormat::Literal), |
| 261 |
Object::String(id2, lopdf::StringFormat::Literal), |
| 262 |
]), |
| 263 |
); |
| 264 |
|
| 265 |
// Create a minimal document structure |
| 266 |
let catalog_dict = lopdf::dictionary! { |
| 267 |
"Type" => "Catalog", |
| 268 |
"Pages" => Object::Reference((2, 0)) |
| 269 |
}; |
| 270 |
let catalog_id = doc.add_object(catalog_dict); |
| 271 |
doc.trailer.set("Root", Object::Reference(catalog_id)); |
| 272 |
|
| 273 |
// Add pages tree |
| 274 |
let pages_dict = lopdf::dictionary! { |
| 275 |
"Type" => "Pages", |
| 276 |
"Kids" => vec![], |
| 277 |
"Count" => 0 |
| 278 |
}; |
| 279 |
doc.objects.insert((2, 0), Object::Dictionary(pages_dict)); |
| 280 |
|
| 281 |
// Add some test objects with different types |
| 282 |
doc.objects.insert((10, 0), Object::Integer(42)); |
| 283 |
doc.objects.insert((11, 0), Object::String(b"test string".to_vec(), lopdf::StringFormat::Literal)); |
| 284 |
doc.objects.insert((12, 0), Object::Array(vec![Object::Integer(1), Object::Integer(2), Object::Integer(3)])); |
| 285 |
|
| 286 |
// Encrypt |
| 287 |
let permissions = lopdf::Permissions::all(); |
| 288 |
let encryption_version = lopdf::EncryptionVersion::V2 { |
| 289 |
document: &doc, |
| 290 |
owner_password: "test", |
| 291 |
user_password: "", |
| 292 |
key_length: 128, |
| 293 |
permissions, |
| 294 |
}; |
| 295 |
|
| 296 |
let encryption_state = lopdf::EncryptionState::try_from(encryption_version).unwrap(); |
| 297 |
doc.encrypt(&encryption_state).unwrap(); |
| 298 |
|
| 299 |
// Save and reload |
| 300 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 301 |
let path = temp_dir.path().join("test_raw_extraction.pdf"); |
| 302 |
doc.save(&path).unwrap(); |
| 303 |
|
| 304 |
let loaded_doc = Document::load(&path).unwrap(); |
| 305 |
assert!(loaded_doc.is_encrypted()); |
| 306 |
|
| 307 |
// Verify that all objects were properly decrypted |
| 308 |
assert_eq!(loaded_doc.get_object((10, 0)).unwrap().as_i64().unwrap(), 42); |
| 309 |
|
| 310 |
let string_obj = loaded_doc.get_object((11, 0)).unwrap(); |
| 311 |
if let Object::String(bytes, _) = string_obj { |
| 312 |
assert_eq!(bytes, b"test string"); |
| 313 |
} else { |
| 314 |
panic!("Expected string object"); |
| 315 |
} |
| 316 |
|
| 317 |
let array_obj = loaded_doc.get_object((12, 0)).unwrap(); |
| 318 |
if let Object::Array(arr) = array_obj { |
| 319 |
assert_eq!(arr.len(), 3); |
| 320 |
assert_eq!(arr[0].as_i64().unwrap(), 1); |
| 321 |
assert_eq!(arr[1].as_i64().unwrap(), 2); |
| 322 |
assert_eq!(arr[2].as_i64().unwrap(), 3); |
| 323 |
} else { |
| 324 |
panic!("Expected array object"); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
#[cfg(not(feature = "async"))] |
| 329 |
#[test] |
| 330 |
#[ignore] // Structure preservation test needs adjustments |
| 331 |
fn test_encrypted_pdf_preserves_structure() { |
| 332 |
// Test that the document structure is preserved after encryption/decryption |
| 333 |
let mut doc = Document::with_version("1.5"); |
| 334 |
|
| 335 |
// Add ID |
| 336 |
doc.trailer.set( |
| 337 |
"ID", |
| 338 |
Object::Array(vec![ |
| 339 |
Object::String(vec![77u8; 16], lopdf::StringFormat::Literal), |
| 340 |
Object::String(vec![66u8; 16], lopdf::StringFormat::Literal), |
| 341 |
]), |
| 342 |
); |
| 343 |
|
| 344 |
// Create a complex structure |
| 345 |
let catalog_dict = lopdf::dictionary! { |
| 346 |
"Type" => "Catalog", |
| 347 |
"Pages" => Object::Reference((2, 0)), |
| 348 |
"Metadata" => Object::Reference((3, 0)) |
| 349 |
}; |
| 350 |
let catalog_id = doc.add_object(catalog_dict); |
| 351 |
doc.trailer.set("Root", Object::Reference(catalog_id)); |
| 352 |
|
| 353 |
// Pages tree |
| 354 |
let pages_dict = lopdf::dictionary! { |
| 355 |
"Type" => "Pages", |
| 356 |
"Kids" => vec![Object::Reference((4, 0))], |
| 357 |
"Count" => 1 |
| 358 |
}; |
| 359 |
doc.objects.insert((2, 0), Object::Dictionary(pages_dict)); |
| 360 |
|
| 361 |
// Metadata stream |
| 362 |
let metadata = b"<rdf:RDF>test metadata</rdf:RDF>"; |
| 363 |
let metadata_stream = lopdf::Stream::new( |
| 364 |
lopdf::dictionary! { |
| 365 |
"Type" => "Metadata", |
| 366 |
"Subtype" => "XML" |
| 367 |
}, |
| 368 |
metadata.to_vec() |
| 369 |
); |
| 370 |
doc.objects.insert((3, 0), Object::Stream(metadata_stream)); |
| 371 |
|
| 372 |
// Page |
| 373 |
let page_dict = lopdf::dictionary! { |
| 374 |
"Type" => "Page", |
| 375 |
"Parent" => Object::Reference((2, 0)), |
| 376 |
"MediaBox" => vec![Object::Integer(0), Object::Integer(0), Object::Integer(612), Object::Integer(792)], |
| 377 |
"Resources" => lopdf::dictionary! {} |
| 378 |
}; |
| 379 |
doc.objects.insert((4, 0), Object::Dictionary(page_dict)); |
| 380 |
|
| 381 |
// Encrypt |
| 382 |
let encryption_version = lopdf::EncryptionVersion::V2 { |
| 383 |
document: &doc, |
| 384 |
owner_password: "complex", |
| 385 |
user_password: "", |
| 386 |
key_length: 128, |
| 387 |
permissions: lopdf::Permissions::all(), |
| 388 |
}; |
| 389 |
|
| 390 |
let encryption_state = lopdf::EncryptionState::try_from(encryption_version).unwrap(); |
| 391 |
doc.encrypt(&encryption_state).unwrap(); |
| 392 |
|
| 393 |
// Save and reload |
| 394 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 395 |
let path = temp_dir.path().join("test_structure.pdf"); |
| 396 |
doc.save(&path).unwrap(); |
| 397 |
|
| 398 |
let loaded_doc = Document::load(&path).unwrap(); |
| 399 |
assert!(loaded_doc.is_encrypted()); |
| 400 |
|
| 401 |
// Verify structure is preserved |
| 402 |
let root = loaded_doc.trailer.get(b"Root").unwrap().as_reference().unwrap(); |
| 403 |
let catalog = loaded_doc.get_object(root).unwrap(); |
| 404 |
|
| 405 |
if let Object::Dictionary(dict) = catalog { |
| 406 |
assert_eq!(dict.get(b"Type").unwrap(), &Object::Name(b"Catalog".to_vec())); |
| 407 |
assert!(dict.has(b"Pages")); |
| 408 |
assert!(dict.has(b"Metadata")); |
| 409 |
} else { |
| 410 |
panic!("Expected catalog to be a dictionary"); |
| 411 |
} |
| 412 |
|
| 413 |
// Check metadata stream was decrypted correctly |
| 414 |
let metadata_obj = loaded_doc.get_object((3, 0)).unwrap(); |
| 415 |
if let Object::Stream(stream) = metadata_obj { |
| 416 |
assert_eq!(stream.dict.get(b"Type").unwrap(), &Object::Name(b"Metadata".to_vec())); |
| 417 |
// Note: Content might be compressed, so we just check it exists |
| 418 |
assert!(!stream.content.is_empty()); |
| 419 |
} else { |
| 420 |
panic!("Expected metadata to be a stream"); |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
#[cfg(feature = "async")] |
| 425 |
#[tokio::test] |
| 426 |
async fn test_decrypt_pdf_with_empty_password_async() { |
| 427 |
// Create a simple PDF document |
| 428 |
let mut doc = Document::with_version("1.5"); |
| 429 |
|
| 430 |
// Add an ID to the trailer (required for encryption) |
| 431 |
let id1 = vec![1u8, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; |
| 432 |
let id2 = vec![16u8, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1]; |
| 433 |
doc.trailer.set( |
| 434 |
"ID", |
| 435 |
Object::Array(vec![ |
| 436 |
Object::String(id1, lopdf::StringFormat::Literal), |
| 437 |
Object::String(id2, lopdf::StringFormat::Literal), |
| 438 |
]), |
| 439 |
); |
| 440 |
|
| 441 |
// Create a simple page structure (similar to sync version) |
| 442 |
let pages_id = doc.new_object_id(); |
| 443 |
let page_id = doc.new_object_id(); |
| 444 |
let content_id = doc.new_object_id(); |
| 445 |
let font_id = doc.new_object_id(); |
| 446 |
let resources_id = doc.new_object_id(); |
| 447 |
|
| 448 |
// Create catalog |
| 449 |
let catalog_dict = lopdf::dictionary! { |
| 450 |
"Type" => "Catalog", |
| 451 |
"Pages" => Object::Reference(pages_id) |
| 452 |
}; |
| 453 |
let catalog_id = doc.add_object(catalog_dict); |
| 454 |
doc.trailer.set("Root", Object::Reference(catalog_id)); |
| 455 |
|
| 456 |
// Create pages |
| 457 |
let pages_dict = lopdf::dictionary! { |
| 458 |
"Type" => "Pages", |
| 459 |
"Kids" => vec![Object::Reference(page_id)], |
| 460 |
"Count" => 1 |
| 461 |
}; |
| 462 |
doc.objects.insert(pages_id, Object::Dictionary(pages_dict)); |
| 463 |
|
| 464 |
// Create resources |
| 465 |
let resources_dict = lopdf::dictionary! { |
| 466 |
"Font" => lopdf::dictionary! { |
| 467 |
"F1" => Object::Reference(font_id) |
| 468 |
} |
| 469 |
}; |
| 470 |
doc.objects.insert(resources_id, Object::Dictionary(resources_dict)); |
| 471 |
|
| 472 |
// Create page |
| 473 |
let page_dict = lopdf::dictionary! { |
| 474 |
"Type" => "Page", |
| 475 |
"Parent" => Object::Reference(pages_id), |
| 476 |
"MediaBox" => vec![Object::Integer(0), Object::Integer(0), Object::Integer(612), Object::Integer(792)], |
| 477 |
"Resources" => Object::Reference(resources_id), |
| 478 |
"Contents" => Object::Reference(content_id) |
| 479 |
}; |
| 480 |
doc.objects.insert(page_id, Object::Dictionary(page_dict)); |
| 481 |
|
| 482 |
// Create font |
| 483 |
let font_dict = lopdf::dictionary! { |
| 484 |
"Type" => "Font", |
| 485 |
"Subtype" => "Type1", |
| 486 |
"BaseFont" => "Helvetica" |
| 487 |
}; |
| 488 |
doc.objects.insert(font_id, Object::Dictionary(font_dict)); |
| 489 |
|
| 490 |
// Create content stream |
| 491 |
let content = b"BT\n/F1 12 Tf\n100 700 Td\n(Hello, Async Encrypted World!) Tj\nET\n"; |
| 492 |
let content_stream = lopdf::Stream::new(lopdf::dictionary! {}, content.to_vec()); |
| 493 |
doc.objects.insert(content_id, Object::Stream(content_stream)); |
| 494 |
|
| 495 |
// Save to a temporary file |
| 496 |
let temp_dir = tempfile::tempdir().unwrap(); |
| 497 |
let encrypted_path = temp_dir.path().join("test_encrypted_async.pdf"); |
| 498 |
|
| 499 |
// Encrypt the document with empty password |
| 500 |
let permissions = lopdf::Permissions::all(); |
| 501 |
let encryption_version = lopdf::EncryptionVersion::V2 { |
| 502 |
document: &doc, |
| 503 |
owner_password: "", |
| 504 |
user_password: "", |
| 505 |
key_length: 128, |
| 506 |
permissions, |
| 507 |
}; |
| 508 |
|
| 509 |
let encryption_state = lopdf::EncryptionState::try_from(encryption_version).unwrap(); |
| 510 |
doc.encrypt(&encryption_state).unwrap(); |
| 511 |
doc.save(&encrypted_path).unwrap(); |
| 512 |
|
| 513 |
// Now test loading the encrypted document asynchronously |
| 514 |
let loaded_doc = Document::load(&encrypted_path).await.unwrap(); |
| 515 |
|
| 516 |
// Verify the document was loaded and decrypted |
| 517 |
assert!(loaded_doc.is_encrypted()); |
| 518 |
|
| 519 |
// Check that we can access the decrypted content |
| 520 |
let pages = loaded_doc.get_pages(); |
| 521 |
assert_eq!(pages.len(), 1); |
| 522 |
|
| 523 |
// Extract text to verify decryption worked |
| 524 |
let page_numbers: Vec<u32> = pages.keys().cloned().collect(); |
| 525 |
let text = loaded_doc.extract_text(&page_numbers).unwrap(); |
| 526 |
assert!(text.contains("Hello, Async Encrypted World!")); |
| 527 |
} |