| 1 |
use lopdf::{dictionary, Document, Object, ObjectStream}; |
| 2 |
|
| 3 |
#[test] |
| 4 |
fn test_multiple_trailer_references_all_compressible() { |
| 5 |
let mut doc = Document::with_version("1.5"); |
| 6 |
|
| 7 |
// Create multiple objects that will be referenced in trailer |
| 8 |
let catalog_id = doc.add_object(dictionary! { |
| 9 |
"Type" => "Catalog", |
| 10 |
"Pages" => Object::Reference((10, 0)) |
| 11 |
}); |
| 12 |
|
| 13 |
let info_id = doc.add_object(dictionary! { |
| 14 |
"Title" => "Test", |
| 15 |
"Author" => "Test Author" |
| 16 |
}); |
| 17 |
|
| 18 |
let metadata_id = doc.add_object(dictionary! { |
| 19 |
"Type" => "Metadata", |
| 20 |
"Subtype" => "XML" |
| 21 |
}); |
| 22 |
|
| 23 |
let outlines_id = doc.add_object(dictionary! { |
| 24 |
"Type" => "Outlines", |
| 25 |
"Count" => 0 |
| 26 |
}); |
| 27 |
|
| 28 |
// Add all to trailer |
| 29 |
doc.trailer.set("Root", catalog_id); |
| 30 |
doc.trailer.set("Info", info_id); |
| 31 |
doc.trailer.set("Metadata", metadata_id); |
| 32 |
doc.trailer.set("Outlines", outlines_id); |
| 33 |
|
| 34 |
// All should be compressible |
| 35 |
assert!(ObjectStream::can_be_compressed(catalog_id, doc.objects.get(&catalog_id).unwrap(), &doc), |
| 36 |
"Catalog should be compressible"); |
| 37 |
assert!(ObjectStream::can_be_compressed(info_id, doc.objects.get(&info_id).unwrap(), &doc), |
| 38 |
"Info should be compressible"); |
| 39 |
assert!(ObjectStream::can_be_compressed(metadata_id, doc.objects.get(&metadata_id).unwrap(), &doc), |
| 40 |
"Metadata should be compressible"); |
| 41 |
assert!(ObjectStream::can_be_compressed(outlines_id, doc.objects.get(&outlines_id).unwrap(), &doc), |
| 42 |
"Outlines should be compressible"); |
| 43 |
} |
| 44 |
|
| 45 |
#[test] |
| 46 |
fn test_encryption_dict_with_other_trailer_refs() { |
| 47 |
let mut doc = Document::with_version("1.5"); |
| 48 |
|
| 49 |
// Create catalog and encryption dictionary |
| 50 |
let catalog_id = doc.add_object(dictionary! { |
| 51 |
"Type" => "Catalog", |
| 52 |
"Pages" => Object::Reference((10, 0)) |
| 53 |
}); |
| 54 |
|
| 55 |
let encrypt_id = doc.add_object(dictionary! { |
| 56 |
"Filter" => "Standard", |
| 57 |
"V" => 2, |
| 58 |
"R" => 3, |
| 59 |
"Length" => 128 |
| 60 |
}); |
| 61 |
|
| 62 |
let info_id = doc.add_object(dictionary! { |
| 63 |
"Title" => "Encrypted PDF" |
| 64 |
}); |
| 65 |
|
| 66 |
// Add to trailer |
| 67 |
doc.trailer.set("Root", catalog_id); |
| 68 |
doc.trailer.set("Encrypt", encrypt_id); |
| 69 |
doc.trailer.set("Info", info_id); |
| 70 |
|
| 71 |
// Catalog and Info should be compressible, Encrypt should not |
| 72 |
assert!(ObjectStream::can_be_compressed(catalog_id, doc.objects.get(&catalog_id).unwrap(), &doc), |
| 73 |
"Catalog should be compressible even with encryption"); |
| 74 |
assert!(!ObjectStream::can_be_compressed(encrypt_id, doc.objects.get(&encrypt_id).unwrap(), &doc), |
| 75 |
"Encryption dictionary should NOT be compressible"); |
| 76 |
assert!(ObjectStream::can_be_compressed(info_id, doc.objects.get(&info_id).unwrap(), &doc), |
| 77 |
"Info should be compressible even with encryption"); |
| 78 |
} |
| 79 |
|
| 80 |
#[test] |
| 81 |
fn test_trailer_with_non_reference_values() { |
| 82 |
let mut doc = Document::with_version("1.5"); |
| 83 |
|
| 84 |
let catalog_id = doc.add_object(dictionary! { |
| 85 |
"Type" => "Catalog" |
| 86 |
}); |
| 87 |
|
| 88 |
// Add various types to trailer |
| 89 |
doc.trailer.set("Root", catalog_id); |
| 90 |
doc.trailer.set("Size", Object::Integer(100)); |
| 91 |
doc.trailer.set("Prev", Object::Integer(1234)); |
| 92 |
doc.trailer.set("ID", Object::Array(vec![ |
| 93 |
Object::String(vec![1, 2, 3, 4], lopdf::StringFormat::Hexadecimal), |
| 94 |
Object::String(vec![5, 6, 7, 8], lopdf::StringFormat::Hexadecimal) |
| 95 |
])); |
| 96 |
|
| 97 |
// Catalog should still be compressible |
| 98 |
assert!(ObjectStream::can_be_compressed(catalog_id, doc.objects.get(&catalog_id).unwrap(), &doc), |
| 99 |
"Catalog should be compressible with non-reference trailer entries"); |
| 100 |
} |
| 101 |
|
| 102 |
#[test] |
| 103 |
fn test_same_object_referenced_multiple_times() { |
| 104 |
let mut doc = Document::with_version("1.5"); |
| 105 |
|
| 106 |
let shared_dict_id = doc.add_object(dictionary! { |
| 107 |
"Shared" => "Dictionary" |
| 108 |
}); |
| 109 |
|
| 110 |
// Reference the same object from multiple trailer keys |
| 111 |
doc.trailer.set("Custom1", shared_dict_id); |
| 112 |
doc.trailer.set("Custom2", shared_dict_id); |
| 113 |
doc.trailer.set("Custom3", shared_dict_id); |
| 114 |
|
| 115 |
// Should still be compressible (not encryption dict) |
| 116 |
assert!(ObjectStream::can_be_compressed(shared_dict_id, doc.objects.get(&shared_dict_id).unwrap(), &doc), |
| 117 |
"Object referenced multiple times in trailer should be compressible"); |
| 118 |
} |
| 119 |
|
| 120 |
#[test] |
| 121 |
fn test_encrypt_key_with_non_reference_value() { |
| 122 |
let mut doc = Document::with_version("1.5"); |
| 123 |
|
| 124 |
let catalog_id = doc.add_object(dictionary! { |
| 125 |
"Type" => "Catalog" |
| 126 |
}); |
| 127 |
|
| 128 |
// Set Encrypt to non-reference value (invalid but should not crash) |
| 129 |
doc.trailer.set("Root", catalog_id); |
| 130 |
doc.trailer.set("Encrypt", Object::Null); |
| 131 |
|
| 132 |
// Catalog should be compressible |
| 133 |
assert!(ObjectStream::can_be_compressed(catalog_id, doc.objects.get(&catalog_id).unwrap(), &doc), |
| 134 |
"Catalog should be compressible when Encrypt is not a reference"); |
| 135 |
} |
| 136 |
|
| 137 |
#[test] |
| 138 |
fn test_missing_encrypt_key() { |
| 139 |
let mut doc = Document::with_version("1.5"); |
| 140 |
|
| 141 |
let catalog_id = doc.add_object(dictionary! { |
| 142 |
"Type" => "Catalog" |
| 143 |
}); |
| 144 |
|
| 145 |
let some_dict_id = doc.add_object(dictionary! { |
| 146 |
"Filter" => "Standard" // Looks like encryption dict but not referenced as one |
| 147 |
}); |
| 148 |
|
| 149 |
doc.trailer.set("Root", catalog_id); |
| 150 |
// No Encrypt key in trailer |
| 151 |
|
| 152 |
// Both should be compressible |
| 153 |
assert!(ObjectStream::can_be_compressed(catalog_id, doc.objects.get(&catalog_id).unwrap(), &doc), |
| 154 |
"Catalog should be compressible without Encrypt in trailer"); |
| 155 |
assert!(ObjectStream::can_be_compressed(some_dict_id, doc.objects.get(&some_dict_id).unwrap(), &doc), |
| 156 |
"Dictionary that looks like encryption should be compressible if not referenced as Encrypt"); |
| 157 |
} |
| 158 |
|
| 159 |
#[test] |
| 160 |
fn test_linearized_with_trailer_references() { |
| 161 |
let mut doc = Document::with_version("1.5"); |
| 162 |
|
| 163 |
// Add linearization dictionary |
| 164 |
let _lin_id = doc.add_object(dictionary! { |
| 165 |
"Linearized" => 1 |
| 166 |
}); |
| 167 |
|
| 168 |
let catalog_id = doc.add_object(dictionary! { |
| 169 |
"Type" => "Catalog" |
| 170 |
}); |
| 171 |
|
| 172 |
let info_id = doc.add_object(dictionary! { |
| 173 |
"Title" => "Linearized PDF" |
| 174 |
}); |
| 175 |
|
| 176 |
doc.trailer.set("Root", catalog_id); |
| 177 |
doc.trailer.set("Info", info_id); |
| 178 |
|
| 179 |
// In linearized PDF, catalog cannot be compressed but info can |
| 180 |
assert!(!ObjectStream::can_be_compressed(catalog_id, doc.objects.get(&catalog_id).unwrap(), &doc), |
| 181 |
"Catalog should NOT be compressible in linearized PDF"); |
| 182 |
assert!(ObjectStream::can_be_compressed(info_id, doc.objects.get(&info_id).unwrap(), &doc), |
| 183 |
"Info should be compressible even in linearized PDF"); |
| 184 |
} |
| 185 |
|
| 186 |
#[test] |
| 187 |
fn test_indirect_object_chain() { |
| 188 |
let mut doc = Document::with_version("1.5"); |
| 189 |
|
| 190 |
// Create a chain of objects |
| 191 |
let obj3_id = doc.add_object(dictionary! { |
| 192 |
"Level" => 3 |
| 193 |
}); |
| 194 |
|
| 195 |
let obj2_id = doc.add_object(dictionary! { |
| 196 |
"Level" => 2, |
| 197 |
"Next" => obj3_id |
| 198 |
}); |
| 199 |
|
| 200 |
let obj1_id = doc.add_object(dictionary! { |
| 201 |
"Level" => 1, |
| 202 |
"Next" => obj2_id |
| 203 |
}); |
| 204 |
|
| 205 |
// Only reference the first in trailer |
| 206 |
doc.trailer.set("Chain", obj1_id); |
| 207 |
|
| 208 |
// All should be compressible (trailer only directly references obj1) |
| 209 |
assert!(ObjectStream::can_be_compressed(obj1_id, doc.objects.get(&obj1_id).unwrap(), &doc), |
| 210 |
"Object directly referenced in trailer should be compressible"); |
| 211 |
assert!(ObjectStream::can_be_compressed(obj2_id, doc.objects.get(&obj2_id).unwrap(), &doc), |
| 212 |
"Object indirectly referenced should be compressible"); |
| 213 |
assert!(ObjectStream::can_be_compressed(obj3_id, doc.objects.get(&obj3_id).unwrap(), &doc), |
| 214 |
"Object indirectly referenced should be compressible"); |
| 215 |
} |
| 216 |
|
| 217 |
#[test] |
| 218 |
fn test_real_world_trailer_structure() { |
| 219 |
let mut doc = Document::with_version("1.5"); |
| 220 |
|
| 221 |
// Simulate a real PDF trailer structure |
| 222 |
let pages_id = doc.new_object_id(); |
| 223 |
|
| 224 |
let catalog_id = doc.add_object(dictionary! { |
| 225 |
"Type" => "Catalog", |
| 226 |
"Pages" => pages_id, |
| 227 |
"Version" => "/1.5" |
| 228 |
}); |
| 229 |
|
| 230 |
let info_id = doc.add_object(dictionary! { |
| 231 |
"Title" => "Real World PDF", |
| 232 |
"Author" => "Test Suite", |
| 233 |
"Subject" => "Testing trailer compression", |
| 234 |
"Creator" => "lopdf", |
| 235 |
"Producer" => "lopdf test", |
| 236 |
"CreationDate" => "D:20250101120000Z", |
| 237 |
"ModDate" => "D:20250807120000Z" |
| 238 |
}); |
| 239 |
|
| 240 |
// Typical trailer entries |
| 241 |
doc.trailer.set("Root", catalog_id); |
| 242 |
doc.trailer.set("Info", info_id); |
| 243 |
doc.trailer.set("Size", Object::Integer(50)); |
| 244 |
doc.trailer.set("ID", Object::Array(vec![ |
| 245 |
Object::String(b"<4E4F204944454153>".to_vec(), lopdf::StringFormat::Hexadecimal), |
| 246 |
Object::String(b"<4E4F204944454153>".to_vec(), lopdf::StringFormat::Hexadecimal) |
| 247 |
])); |
| 248 |
|
| 249 |
// Both catalog and info should be compressible |
| 250 |
assert!(ObjectStream::can_be_compressed(catalog_id, doc.objects.get(&catalog_id).unwrap(), &doc), |
| 251 |
"Real-world catalog should be compressible"); |
| 252 |
assert!(ObjectStream::can_be_compressed(info_id, doc.objects.get(&info_id).unwrap(), &doc), |
| 253 |
"Real-world info dictionary should be compressible"); |
| 254 |
} |
| 255 |
|
| 256 |
#[test] |
| 257 |
fn test_compression_with_save_integration() { |
| 258 |
use lopdf::SaveOptions; |
| 259 |
|
| 260 |
let mut doc = Document::with_version("1.5"); |
| 261 |
let pages_id = doc.new_object_id(); |
| 262 |
|
| 263 |
let page_id = doc.add_object(dictionary! { |
| 264 |
"Type" => "Page", |
| 265 |
"Parent" => pages_id, |
| 266 |
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()] |
| 267 |
}); |
| 268 |
|
| 269 |
doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 270 |
"Type" => "Pages", |
| 271 |
"Kids" => vec![page_id.into()], |
| 272 |
"Count" => 1 |
| 273 |
})); |
| 274 |
|
| 275 |
let catalog_id = doc.add_object(dictionary! { |
| 276 |
"Type" => "Catalog", |
| 277 |
"Pages" => pages_id |
| 278 |
}); |
| 279 |
|
| 280 |
let info_id = doc.add_object(dictionary! { |
| 281 |
"Title" => "Compression Test", |
| 282 |
"Keywords" => "test, compression, object streams" |
| 283 |
}); |
| 284 |
|
| 285 |
doc.trailer.set("Root", catalog_id); |
| 286 |
doc.trailer.set("Info", info_id); |
| 287 |
|
| 288 |
// Save with object streams |
| 289 |
let options = SaveOptions::builder() |
| 290 |
.use_object_streams(true) |
| 291 |
.build(); |
| 292 |
|
| 293 |
let mut output = Vec::new(); |
| 294 |
doc.save_with_options(&mut output, options).unwrap(); |
| 295 |
|
| 296 |
let content = String::from_utf8_lossy(&output); |
| 297 |
|
| 298 |
// Verify object streams exist |
| 299 |
assert!(content.contains("/ObjStm"), "Object streams should be created"); |
| 300 |
|
| 301 |
// Verify trailer-referenced objects are compressed |
| 302 |
assert!(!content.contains(&format!("{} 0 obj\n<</Type/Catalog", catalog_id.0)), |
| 303 |
"Catalog should be in object stream"); |
| 304 |
assert!(!content.contains(&format!("{} 0 obj\n<</Title", info_id.0)), |
| 305 |
"Info should be in object stream"); |
| 306 |
|
| 307 |
// Load and verify the PDF is valid |
| 308 |
let loaded = Document::load_mem(&output).unwrap(); |
| 309 |
assert!(loaded.trailer.get(b"Root").is_ok(), "Loaded PDF should have valid Root"); |
| 310 |
assert!(loaded.trailer.get(b"Info").is_ok(), "Loaded PDF should have valid Info"); |
| 311 |
} |