| 1 |
use lopdf::{dictionary, Document, Object, ObjectStream}; |
| 2 |
|
| 3 |
#[test] |
| 4 |
fn test_empty_trailer() { |
| 5 |
let doc = Document::with_version("1.5"); |
| 6 |
let obj_id = (1, 0); |
| 7 |
let obj = Object::Dictionary(dictionary! { "Test" => "Value" }); |
| 8 |
|
| 9 |
// With empty trailer, all objects should be compressible |
| 10 |
assert!(ObjectStream::can_be_compressed(obj_id, &obj, &doc), |
| 11 |
"Object should be compressible with empty trailer"); |
| 12 |
} |
| 13 |
|
| 14 |
#[test] |
| 15 |
fn test_malformed_encrypt_reference() { |
| 16 |
let mut doc = Document::with_version("1.5"); |
| 17 |
|
| 18 |
let obj_id = doc.add_object(dictionary! { |
| 19 |
"Type" => "Catalog" |
| 20 |
}); |
| 21 |
|
| 22 |
// Set Encrypt to a malformed reference (wrong generation) |
| 23 |
doc.trailer.set("Encrypt", Object::Reference((999, 99))); |
| 24 |
|
| 25 |
// Object should still be compressible (not the encryption dict) |
| 26 |
assert!(ObjectStream::can_be_compressed(obj_id, doc.objects.get(&obj_id).unwrap(), &doc), |
| 27 |
"Object should be compressible even with malformed Encrypt reference"); |
| 28 |
} |
| 29 |
|
| 30 |
#[test] |
| 31 |
fn test_self_referencing_object() { |
| 32 |
let mut doc = Document::with_version("1.5"); |
| 33 |
|
| 34 |
let obj_id = (5, 0); |
| 35 |
doc.objects.insert(obj_id, Object::Dictionary(dictionary! { |
| 36 |
"Type" => "Test", |
| 37 |
"Self" => Object::Reference(obj_id) // Self reference |
| 38 |
})); |
| 39 |
|
| 40 |
doc.trailer.set("Test", obj_id); |
| 41 |
|
| 42 |
// Should be compressible (not encryption dict) |
| 43 |
assert!(ObjectStream::can_be_compressed(obj_id, doc.objects.get(&obj_id).unwrap(), &doc), |
| 44 |
"Self-referencing object should be compressible"); |
| 45 |
} |
| 46 |
|
| 47 |
#[test] |
| 48 |
fn test_circular_references() { |
| 49 |
let mut doc = Document::with_version("1.5"); |
| 50 |
|
| 51 |
let obj1_id = (1, 0); |
| 52 |
let obj2_id = (2, 0); |
| 53 |
|
| 54 |
doc.objects.insert(obj1_id, Object::Dictionary(dictionary! { |
| 55 |
"Next" => Object::Reference(obj2_id) |
| 56 |
})); |
| 57 |
|
| 58 |
doc.objects.insert(obj2_id, Object::Dictionary(dictionary! { |
| 59 |
"Next" => Object::Reference(obj1_id) // Circular reference |
| 60 |
})); |
| 61 |
|
| 62 |
doc.trailer.set("Start", obj1_id); |
| 63 |
|
| 64 |
// Both should be compressible |
| 65 |
assert!(ObjectStream::can_be_compressed(obj1_id, doc.objects.get(&obj1_id).unwrap(), &doc), |
| 66 |
"First object in circular reference should be compressible"); |
| 67 |
assert!(ObjectStream::can_be_compressed(obj2_id, doc.objects.get(&obj2_id).unwrap(), &doc), |
| 68 |
"Second object in circular reference should be compressible"); |
| 69 |
} |
| 70 |
|
| 71 |
#[test] |
| 72 |
fn test_trailer_with_all_pdf_types() { |
| 73 |
let mut doc = Document::with_version("1.5"); |
| 74 |
|
| 75 |
let obj_id = doc.add_object(dictionary! { "Type" => "Test" }); |
| 76 |
|
| 77 |
// Add all possible PDF types to trailer |
| 78 |
doc.trailer.set("Root", obj_id); |
| 79 |
doc.trailer.set("Null", Object::Null); |
| 80 |
doc.trailer.set("Bool", Object::Boolean(true)); |
| 81 |
doc.trailer.set("Int", Object::Integer(42)); |
| 82 |
doc.trailer.set("Real", Object::Real(3.14159)); |
| 83 |
doc.trailer.set("String", Object::String(b"test".to_vec(), lopdf::StringFormat::Literal)); |
| 84 |
doc.trailer.set("Name", Object::Name(b"Test".to_vec())); |
| 85 |
doc.trailer.set("Array", Object::Array(vec![Object::Integer(1), Object::Integer(2)])); |
| 86 |
doc.trailer.set("Dict", Object::Dictionary(dictionary! { "Key" => "Value" })); |
| 87 |
|
| 88 |
// Object should still be compressible |
| 89 |
assert!(ObjectStream::can_be_compressed(obj_id, doc.objects.get(&obj_id).unwrap(), &doc), |
| 90 |
"Object should be compressible with diverse trailer entries"); |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn test_unicode_trailer_keys() { |
| 95 |
let mut doc = Document::with_version("1.5"); |
| 96 |
|
| 97 |
let obj_id = doc.add_object(dictionary! { "Test" => "Value" }); |
| 98 |
|
| 99 |
// Use Unicode/non-ASCII keys in trailer |
| 100 |
doc.trailer.set("Root", obj_id); |
| 101 |
doc.trailer.set("Ünïcödé", Object::String(b"test".to_vec(), lopdf::StringFormat::Literal)); |
| 102 |
doc.trailer.set("日本語", Object::Integer(42)); |
| 103 |
doc.trailer.set("🎯", Object::Boolean(true)); |
| 104 |
|
| 105 |
// Should still work correctly |
| 106 |
assert!(ObjectStream::can_be_compressed(obj_id, doc.objects.get(&obj_id).unwrap(), &doc), |
| 107 |
"Object should be compressible with Unicode trailer keys"); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn test_very_large_trailer() { |
| 112 |
let mut doc = Document::with_version("1.5"); |
| 113 |
|
| 114 |
let obj_id = doc.add_object(dictionary! { "Type" => "Catalog" }); |
| 115 |
|
| 116 |
// Add many entries to trailer |
| 117 |
doc.trailer.set("Root", obj_id); |
| 118 |
for i in 0..1000 { |
| 119 |
doc.trailer.set(format!("Custom{:04}", i).as_bytes(), Object::Integer(i)); |
| 120 |
} |
| 121 |
|
| 122 |
// Should still be efficient |
| 123 |
let start = std::time::Instant::now(); |
| 124 |
let compressible = ObjectStream::can_be_compressed(obj_id, doc.objects.get(&obj_id).unwrap(), &doc); |
| 125 |
let duration = start.elapsed(); |
| 126 |
|
| 127 |
assert!(compressible, "Object should be compressible even with large trailer"); |
| 128 |
assert!(duration.as_micros() < 100, "Check should be fast even with large trailer"); |
| 129 |
} |
| 130 |
|
| 131 |
#[test] |
| 132 |
fn test_concurrent_modification_safety() { |
| 133 |
// This test verifies the function doesn't panic with concurrent-like access patterns |
| 134 |
let mut doc = Document::with_version("1.5"); |
| 135 |
|
| 136 |
let obj1_id = doc.add_object(dictionary! { "Type" => "Test1" }); |
| 137 |
let obj2_id = doc.add_object(dictionary! { "Type" => "Test2" }); |
| 138 |
|
| 139 |
doc.trailer.set("Ref1", obj1_id); |
| 140 |
|
| 141 |
// Check first object |
| 142 |
let result1 = ObjectStream::can_be_compressed(obj1_id, doc.objects.get(&obj1_id).unwrap(), &doc); |
| 143 |
|
| 144 |
// Modify trailer between checks |
| 145 |
doc.trailer.set("Ref2", obj2_id); |
| 146 |
doc.trailer.set("Encrypt", obj1_id); // Now obj1 is encryption dict |
| 147 |
|
| 148 |
// Check again - result should change |
| 149 |
let result2 = ObjectStream::can_be_compressed(obj1_id, doc.objects.get(&obj1_id).unwrap(), &doc); |
| 150 |
|
| 151 |
assert!(result1, "Initially should be compressible"); |
| 152 |
assert!(!result2, "Should not be compressible after becoming encryption dict"); |
| 153 |
} |