| 1 |
use lopdf::{dictionary, Document, Object, ObjectStream, Stream}; |
| 2 |
|
| 3 |
#[test] |
| 4 |
fn test_can_be_compressed_stream_objects() { |
| 5 |
let mut doc = Document::new(); |
| 6 |
let stream_id = doc.add_object(Stream::new( |
| 7 |
dictionary! { "Type" => "XObject" }, |
| 8 |
vec![1, 2, 3] |
| 9 |
)); |
| 10 |
|
| 11 |
let stream_obj = doc.objects.get(&stream_id).unwrap(); |
| 12 |
assert!(!ObjectStream::can_be_compressed(stream_id, stream_obj, &doc), |
| 13 |
"Stream objects should not be compressible"); |
| 14 |
} |
| 15 |
|
| 16 |
#[test] |
| 17 |
fn test_can_be_compressed_non_zero_generation() { |
| 18 |
let mut doc = Document::new(); |
| 19 |
let obj_id = (1, 5); // Non-zero generation |
| 20 |
doc.objects.insert(obj_id, Object::Integer(42)); |
| 21 |
|
| 22 |
let obj = doc.objects.get(&obj_id).unwrap(); |
| 23 |
assert!(!ObjectStream::can_be_compressed(obj_id, obj, &doc), |
| 24 |
"Objects with non-zero generation should not be compressible"); |
| 25 |
} |
| 26 |
|
| 27 |
#[test] |
| 28 |
fn test_can_be_compressed_trailer_referenced() { |
| 29 |
let mut doc = Document::new(); |
| 30 |
let obj_id = doc.add_object(Object::Dictionary(dictionary! { |
| 31 |
"Test" => "Value" |
| 32 |
})); |
| 33 |
|
| 34 |
// Add to trailer (non-encryption reference) |
| 35 |
doc.trailer.set("TestRef", obj_id); |
| 36 |
|
| 37 |
let obj = doc.objects.get(&obj_id).unwrap(); |
| 38 |
assert!(ObjectStream::can_be_compressed(obj_id, obj, &doc), |
| 39 |
"Non-encryption objects referenced in trailer should be compressible"); |
| 40 |
} |
| 41 |
|
| 42 |
#[test] |
| 43 |
fn test_can_be_compressed_xref_stream() { |
| 44 |
let mut doc = Document::new(); |
| 45 |
let xref_id = doc.add_object(Object::Dictionary(dictionary! { |
| 46 |
"Type" => "XRef" |
| 47 |
})); |
| 48 |
|
| 49 |
let obj = doc.objects.get(&xref_id).unwrap(); |
| 50 |
assert!(!ObjectStream::can_be_compressed(xref_id, obj, &doc), |
| 51 |
"XRef streams should not be compressible"); |
| 52 |
} |
| 53 |
|
| 54 |
#[test] |
| 55 |
fn test_can_be_compressed_objstm() { |
| 56 |
let mut doc = Document::new(); |
| 57 |
let objstm_id = doc.add_object(Object::Dictionary(dictionary! { |
| 58 |
"Type" => "ObjStm" |
| 59 |
})); |
| 60 |
|
| 61 |
let obj = doc.objects.get(&objstm_id).unwrap(); |
| 62 |
assert!(!ObjectStream::can_be_compressed(objstm_id, obj, &doc), |
| 63 |
"Object streams should not be compressible"); |
| 64 |
} |
| 65 |
|
| 66 |
#[test] |
| 67 |
fn test_can_be_compressed_catalog_non_linearized() { |
| 68 |
let mut doc = Document::new(); |
| 69 |
let catalog_id = doc.add_object(Object::Dictionary(dictionary! { |
| 70 |
"Type" => "Catalog", |
| 71 |
"Pages" => Object::Reference((2, 0)) |
| 72 |
})); |
| 73 |
|
| 74 |
let obj = doc.objects.get(&catalog_id).unwrap(); |
| 75 |
assert!(ObjectStream::can_be_compressed(catalog_id, obj, &doc), |
| 76 |
"Catalog should be compressible in non-linearized PDFs"); |
| 77 |
} |
| 78 |
|
| 79 |
#[test] |
| 80 |
fn test_can_be_compressed_catalog_linearized() { |
| 81 |
let mut doc = Document::new(); |
| 82 |
|
| 83 |
// Add linearization dictionary |
| 84 |
let _lin_id = doc.add_object(Object::Dictionary(dictionary! { |
| 85 |
"Linearized" => 1 |
| 86 |
})); |
| 87 |
|
| 88 |
let catalog_id = doc.add_object(Object::Dictionary(dictionary! { |
| 89 |
"Type" => "Catalog", |
| 90 |
"Pages" => Object::Reference((2, 0)) |
| 91 |
})); |
| 92 |
|
| 93 |
let obj = doc.objects.get(&catalog_id).unwrap(); |
| 94 |
assert!(!ObjectStream::can_be_compressed(catalog_id, obj, &doc), |
| 95 |
"Catalog should not be compressible in linearized PDFs"); |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn test_can_be_compressed_pages() { |
| 100 |
let mut doc = Document::new(); |
| 101 |
let pages_id = doc.add_object(Object::Dictionary(dictionary! { |
| 102 |
"Type" => "Pages", |
| 103 |
"Kids" => vec![Object::Reference((3, 0))], |
| 104 |
"Count" => 1 |
| 105 |
})); |
| 106 |
|
| 107 |
let obj = doc.objects.get(&pages_id).unwrap(); |
| 108 |
assert!(ObjectStream::can_be_compressed(pages_id, obj, &doc), |
| 109 |
"Pages objects should be compressible"); |
| 110 |
} |
| 111 |
|
| 112 |
#[test] |
| 113 |
fn test_can_be_compressed_page() { |
| 114 |
let mut doc = Document::new(); |
| 115 |
let page_id = doc.add_object(Object::Dictionary(dictionary! { |
| 116 |
"Type" => "Page", |
| 117 |
"Parent" => Object::Reference((2, 0)), |
| 118 |
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()] |
| 119 |
})); |
| 120 |
|
| 121 |
let obj = doc.objects.get(&page_id).unwrap(); |
| 122 |
assert!(ObjectStream::can_be_compressed(page_id, obj, &doc), |
| 123 |
"Page objects should be compressible"); |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn test_can_be_compressed_encryption_dict() { |
| 128 |
let mut doc = Document::new(); |
| 129 |
let encrypt_id = doc.add_object(Object::Dictionary(dictionary! { |
| 130 |
"Filter" => "Standard", |
| 131 |
"V" => 1, |
| 132 |
"R" => 2 |
| 133 |
})); |
| 134 |
|
| 135 |
// Set as encryption dictionary |
| 136 |
doc.trailer.set("Encrypt", encrypt_id); |
| 137 |
|
| 138 |
let obj = doc.objects.get(&encrypt_id).unwrap(); |
| 139 |
assert!(!ObjectStream::can_be_compressed(encrypt_id, obj, &doc), |
| 140 |
"Encryption dictionary should not be compressible"); |
| 141 |
} |
| 142 |
|
| 143 |
#[test] |
| 144 |
fn test_catalog_can_be_compressed_with_trailer_reference() { |
| 145 |
let mut doc = Document::with_version("1.5"); |
| 146 |
let catalog_id = doc.add_object(dictionary! { |
| 147 |
"Type" => "Catalog", |
| 148 |
"Pages" => Object::Reference((2, 0)) |
| 149 |
}); |
| 150 |
|
| 151 |
// Set as root in trailer (standard PDF structure) |
| 152 |
doc.trailer.set("Root", catalog_id); |
| 153 |
|
| 154 |
let catalog_obj = doc.objects.get(&catalog_id).unwrap(); |
| 155 |
assert!(ObjectStream::can_be_compressed(catalog_id, catalog_obj, &doc), |
| 156 |
"Catalog should be compressible even when referenced in trailer"); |
| 157 |
} |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn test_info_dict_can_be_compressed_with_trailer_reference() { |
| 161 |
let mut doc = Document::with_version("1.5"); |
| 162 |
let info_id = doc.add_object(dictionary! { |
| 163 |
"Title" => "Test PDF", |
| 164 |
"Author" => "Test Author", |
| 165 |
"CreationDate" => "D:20250807120000Z" |
| 166 |
}); |
| 167 |
|
| 168 |
// Set as info in trailer |
| 169 |
doc.trailer.set("Info", info_id); |
| 170 |
|
| 171 |
let info_obj = doc.objects.get(&info_id).unwrap(); |
| 172 |
assert!(ObjectStream::can_be_compressed(info_id, info_obj, &doc), |
| 173 |
"Info dictionary should be compressible even when referenced in trailer"); |
| 174 |
} |
| 175 |
|
| 176 |
#[test] |
| 177 |
fn test_linearized_detection_via_catalog_compression() { |
| 178 |
let mut doc = Document::new(); |
| 179 |
|
| 180 |
// Create catalog in non-linearized document |
| 181 |
let catalog_id = doc.add_object(Object::Dictionary(dictionary! { |
| 182 |
"Type" => "Catalog", |
| 183 |
"Pages" => Object::Reference((2, 0)) |
| 184 |
})); |
| 185 |
|
| 186 |
// Initially should be compressible (not linearized) |
| 187 |
{ |
| 188 |
let obj = doc.objects.get(&catalog_id).unwrap(); |
| 189 |
assert!(ObjectStream::can_be_compressed(catalog_id, obj, &doc), |
| 190 |
"Catalog should be compressible in non-linearized PDF"); |
| 191 |
} |
| 192 |
|
| 193 |
// Add linearization dictionary |
| 194 |
let _lin_id = doc.add_object(Object::Dictionary(dictionary! { |
| 195 |
"Linearized" => 1, |
| 196 |
"L" => 12345, |
| 197 |
"H" => vec![Object::Integer(100), Object::Integer(200)] |
| 198 |
})); |
| 199 |
|
| 200 |
// Now catalog should NOT be compressible |
| 201 |
let obj = doc.objects.get(&catalog_id).unwrap(); |
| 202 |
assert!(!ObjectStream::can_be_compressed(catalog_id, obj, &doc), |
| 203 |
"Catalog should not be compressible after linearization"); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn test_can_be_compressed_regular_objects() { |
| 208 |
let mut doc = Document::new(); |
| 209 |
|
| 210 |
// Test various regular object types that should be compressible |
| 211 |
let int_id = doc.add_object(Object::Integer(42)); |
| 212 |
let bool_id = doc.add_object(Object::Boolean(true)); |
| 213 |
let string_id = doc.add_object(Object::String(b"Hello".to_vec(), lopdf::StringFormat::Literal)); |
| 214 |
let name_id = doc.add_object(Object::Name(b"Test".to_vec())); |
| 215 |
let array_id = doc.add_object(Object::Array(vec![Object::Integer(1), Object::Integer(2)])); |
| 216 |
let dict_id = doc.add_object(Object::Dictionary(dictionary! { |
| 217 |
"Key" => "Value" |
| 218 |
})); |
| 219 |
|
| 220 |
// All should be compressible |
| 221 |
assert!(ObjectStream::can_be_compressed(int_id, doc.objects.get(&int_id).unwrap(), &doc)); |
| 222 |
assert!(ObjectStream::can_be_compressed(bool_id, doc.objects.get(&bool_id).unwrap(), &doc)); |
| 223 |
assert!(ObjectStream::can_be_compressed(string_id, doc.objects.get(&string_id).unwrap(), &doc)); |
| 224 |
assert!(ObjectStream::can_be_compressed(name_id, doc.objects.get(&name_id).unwrap(), &doc)); |
| 225 |
assert!(ObjectStream::can_be_compressed(array_id, doc.objects.get(&array_id).unwrap(), &doc)); |
| 226 |
assert!(ObjectStream::can_be_compressed(dict_id, doc.objects.get(&dict_id).unwrap(), &doc)); |
| 227 |
} |
| 228 |
|
| 229 |
#[test] |
| 230 |
fn test_save_with_object_streams_empty_document() { |
| 231 |
let mut doc = Document::with_version("1.5"); |
| 232 |
doc.trailer.set("Root", Object::Reference((1, 0))); |
| 233 |
|
| 234 |
let mut buffer = Vec::new(); |
| 235 |
let result = doc.save_modern(&mut buffer); |
| 236 |
assert!(result.is_ok(), "Empty document should save successfully"); |
| 237 |
} |
| 238 |
|
| 239 |
#[test] |
| 240 |
fn test_save_with_object_streams_single_page() { |
| 241 |
let mut doc = Document::with_version("1.5"); |
| 242 |
let pages_id = doc.new_object_id(); |
| 243 |
|
| 244 |
let page_id = doc.add_object(dictionary! { |
| 245 |
"Type" => "Page", |
| 246 |
"Parent" => pages_id, |
| 247 |
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 248 |
}); |
| 249 |
|
| 250 |
doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 251 |
"Type" => "Pages", |
| 252 |
"Kids" => vec![page_id.into()], |
| 253 |
"Count" => 1, |
| 254 |
})); |
| 255 |
|
| 256 |
let catalog_id = doc.add_object(dictionary! { |
| 257 |
"Type" => "Catalog", |
| 258 |
"Pages" => pages_id, |
| 259 |
}); |
| 260 |
|
| 261 |
doc.trailer.set("Root", catalog_id); |
| 262 |
|
| 263 |
let mut buffer = Vec::new(); |
| 264 |
let result = doc.save_modern(&mut buffer); |
| 265 |
assert!(result.is_ok()); |
| 266 |
|
| 267 |
let content = String::from_utf8_lossy(&buffer); |
| 268 |
assert!(content.contains("/ObjStm"), "Object streams should be created for single page PDF"); |
| 269 |
} |
| 270 |
|
| 271 |
#[test] |
| 272 |
fn test_object_stream_builder_custom_config() { |
| 273 |
let builder = ObjectStream::builder() |
| 274 |
.max_objects(50) |
| 275 |
.compression_level(9); |
| 276 |
|
| 277 |
assert_eq!(builder.get_max_objects(), 50); |
| 278 |
assert_eq!(builder.get_compression_level(), 9); |
| 279 |
|
| 280 |
let _obj_stream = builder.build(); |
| 281 |
// Note: max_objects and compression_level are private fields |
| 282 |
// We've already tested the builder getter methods above |
| 283 |
} |
| 284 |
|
| 285 |
#[test] |
| 286 |
fn test_object_stream_add_max_objects() { |
| 287 |
let mut obj_stream = ObjectStream::builder() |
| 288 |
.max_objects(3) |
| 289 |
.build(); |
| 290 |
|
| 291 |
// Add objects up to max |
| 292 |
assert!(obj_stream.add_object((1, 0), Object::Integer(1)).is_ok()); |
| 293 |
assert!(obj_stream.add_object((2, 0), Object::Integer(2)).is_ok()); |
| 294 |
assert!(obj_stream.add_object((3, 0), Object::Integer(3)).is_ok()); |
| 295 |
|
| 296 |
// Should fail when exceeding max |
| 297 |
assert!(obj_stream.add_object((4, 0), Object::Integer(4)).is_err()); |
| 298 |
} |
| 299 |
|
| 300 |
#[test] |
| 301 |
fn test_object_stream_invalid_generation() { |
| 302 |
let mut obj_stream = ObjectStream::builder().build(); |
| 303 |
|
| 304 |
// Note: The current implementation doesn't validate generation in add_object |
| 305 |
// It only checks during can_be_compressed. This test documents current behavior. |
| 306 |
let result = obj_stream.add_object((1, 1), Object::Integer(42)); |
| 307 |
assert!(result.is_ok(), "add_object currently doesn't validate generation"); |
| 308 |
} |
| 309 |
|
| 310 |
#[test] |
| 311 |
fn test_save_modern_with_mixed_objects() { |
| 312 |
let mut doc = Document::with_version("1.5"); |
| 313 |
let pages_id = doc.new_object_id(); |
| 314 |
|
| 315 |
// Add various object types |
| 316 |
let font_id = doc.add_object(dictionary! { |
| 317 |
"Type" => "Font", |
| 318 |
"Subtype" => "Type1", |
| 319 |
"BaseFont" => "Helvetica" |
| 320 |
}); |
| 321 |
|
| 322 |
let xobject_stream_id = doc.add_object(Stream::new( |
| 323 |
dictionary! { "Type" => "XObject", "Subtype" => "Image" }, |
| 324 |
vec![0; 100] |
| 325 |
)); |
| 326 |
|
| 327 |
let annotation_id = doc.add_object(dictionary! { |
| 328 |
"Type" => "Annot", |
| 329 |
"Subtype" => "Text", |
| 330 |
"Rect" => vec![100.into(), 100.into(), 200.into(), 200.into()] |
| 331 |
}); |
| 332 |
|
| 333 |
let page_id = doc.add_object(dictionary! { |
| 334 |
"Type" => "Page", |
| 335 |
"Parent" => pages_id, |
| 336 |
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 337 |
"Annots" => vec![annotation_id.into()], |
| 338 |
}); |
| 339 |
|
| 340 |
doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 341 |
"Type" => "Pages", |
| 342 |
"Kids" => vec![page_id.into()], |
| 343 |
"Count" => 1, |
| 344 |
})); |
| 345 |
|
| 346 |
let catalog_id = doc.add_object(dictionary! { |
| 347 |
"Type" => "Catalog", |
| 348 |
"Pages" => pages_id, |
| 349 |
}); |
| 350 |
|
| 351 |
doc.trailer.set("Root", catalog_id); |
| 352 |
|
| 353 |
let mut buffer = Vec::new(); |
| 354 |
let result = doc.save_modern(&mut buffer); |
| 355 |
assert!(result.is_ok()); |
| 356 |
|
| 357 |
let content = String::from_utf8_lossy(&buffer); |
| 358 |
|
| 359 |
// Verify object streams were created |
| 360 |
assert!(content.contains("/ObjStm")); |
| 361 |
|
| 362 |
// Verify stream objects are NOT in object streams |
| 363 |
assert!(content.contains(&format!("{} 0 obj", xobject_stream_id.0)), |
| 364 |
"Stream objects should remain as top-level objects"); |
| 365 |
|
| 366 |
// Verify compressible objects are NOT present as individual objects |
| 367 |
assert!(!content.contains(&format!("{} 0 obj\n<</Type/Font", font_id.0)), |
| 368 |
"Font object should be compressed"); |
| 369 |
assert!(!content.contains(&format!("{} 0 obj\n<</Type/Annot", annotation_id.0)), |
| 370 |
"Annotation object should be compressed"); |
| 371 |
} |
| 372 |
|
| 373 |
#[test] |
| 374 |
fn test_parse_existing_object_stream() { |
| 375 |
// Create a simple object stream content |
| 376 |
let content = b"1 0 2 50 3 100\n\ |
| 377 |
<</Type/Font/Subtype/Type1/BaseFont/Helvetica>>\n\ |
| 378 |
<</Type/Annot/Subtype/Text/Rect[100 100 200 200]>>\n\ |
| 379 |
42"; |
| 380 |
|
| 381 |
let mut stream = Stream::new( |
| 382 |
dictionary! { |
| 383 |
"Type" => "ObjStm", |
| 384 |
"N" => 3, |
| 385 |
"First" => 15 |
| 386 |
}, |
| 387 |
content.to_vec() |
| 388 |
); |
| 389 |
|
| 390 |
let obj_stream = ObjectStream::new(&mut stream); |
| 391 |
assert!(obj_stream.is_ok()); |
| 392 |
|
| 393 |
let obj_stream = obj_stream.unwrap(); |
| 394 |
assert_eq!(obj_stream.objects.len(), 3); |
| 395 |
assert!(obj_stream.objects.contains_key(&(1, 0))); |
| 396 |
assert!(obj_stream.objects.contains_key(&(2, 0))); |
| 397 |
assert!(obj_stream.objects.contains_key(&(3, 0))); |
| 398 |
} |
| 399 |
|
| 400 |
#[test] |
| 401 |
fn test_regression_structural_objects_compression() { |
| 402 |
// This test ensures the fix for structural object compression doesn't regress |
| 403 |
let mut doc = Document::with_version("1.5"); |
| 404 |
let pages_id = doc.new_object_id(); |
| 405 |
|
| 406 |
let page_id = doc.add_object(dictionary! { |
| 407 |
"Type" => "Page", |
| 408 |
"Parent" => pages_id, |
| 409 |
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 410 |
}); |
| 411 |
|
| 412 |
doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 413 |
"Type" => "Pages", |
| 414 |
"Kids" => vec![page_id.into()], |
| 415 |
"Count" => 1, |
| 416 |
})); |
| 417 |
|
| 418 |
let catalog_id = doc.add_object(dictionary! { |
| 419 |
"Type" => "Catalog", |
| 420 |
"Pages" => pages_id, |
| 421 |
}); |
| 422 |
|
| 423 |
// Set trailer reference |
| 424 |
doc.trailer.set("Root", catalog_id); |
| 425 |
|
| 426 |
// Verify objects can be compressed even after trailer reference |
| 427 |
let catalog_obj = doc.objects.get(&catalog_id).unwrap(); |
| 428 |
let pages_obj = doc.objects.get(&pages_id).unwrap(); |
| 429 |
let page_obj = doc.objects.get(&page_id).unwrap(); |
| 430 |
|
| 431 |
assert!(ObjectStream::can_be_compressed(catalog_id, catalog_obj, &doc), |
| 432 |
"Catalog must be compressible in non-linearized PDFs even with trailer reference"); |
| 433 |
assert!(ObjectStream::can_be_compressed(pages_id, pages_obj, &doc), |
| 434 |
"Pages object must be compressible"); |
| 435 |
assert!(ObjectStream::can_be_compressed(page_id, page_obj, &doc), |
| 436 |
"Page object must be compressible"); |
| 437 |
|
| 438 |
// Save and verify compression |
| 439 |
let mut buffer = Vec::new(); |
| 440 |
doc.save_modern(&mut buffer).unwrap(); |
| 441 |
|
| 442 |
let content = String::from_utf8_lossy(&buffer); |
| 443 |
|
| 444 |
// Must have object streams |
| 445 |
assert!(content.contains("/ObjStm"), |
| 446 |
"Object streams must be created when saving with modern format"); |
| 447 |
|
| 448 |
// All structural objects should be compressed now |
| 449 |
assert!(!content.contains(&format!("{} 0 obj\n<</Type/Catalog", catalog_id.0)), |
| 450 |
"Catalog should be in object stream, not as individual object"); |
| 451 |
assert!(!content.contains(&format!("{} 0 obj\n<</Type/Pages", pages_id.0)), |
| 452 |
"Pages should be in object stream, not as individual object"); |
| 453 |
assert!(!content.contains(&format!("{} 0 obj\n<</Type/Page", page_id.0)), |
| 454 |
"Page should be in object stream, not as individual object"); |
| 455 |
} |