Debian Codegraph @

debian/missing-sources/lopdf/tests/object_stream_methods_test.rs

raw
1 use lopdf::{dictionary, Document, Object, ObjectStream, Stream, SaveOptions};
2
3 #[test]
4 fn test_object_stream_parse_empty() {
5 let mut stream = Stream::new(
6 dictionary! {
7 "Type" => "ObjStm",
8 "N" => 0,
9 "First" => 0
10 },
11 vec![]
12 );
13
14 let obj_stream = ObjectStream::new(&mut stream).unwrap();
15 assert_eq!(obj_stream.objects.len(), 0);
16 }
17
18 #[test]
19 fn test_object_stream_config_default() {
20 let config = lopdf::ObjectStreamConfig::default();
21 assert_eq!(config.max_objects_per_stream, 100);
22 assert_eq!(config.compression_level, 6);
23 }
24
25 #[test]
26 fn test_object_stream_to_stream_object() {
27 let mut obj_stream = ObjectStream::builder().build();
28
29 // Add some objects
30 obj_stream.add_object((1, 0), Object::Integer(42)).unwrap();
31 obj_stream.add_object((2, 0), Object::Boolean(true)).unwrap();
32 obj_stream.add_object((3, 0), Object::Name(b"Test".to_vec())).unwrap();
33
34 // Convert to stream object
35 let stream_obj = obj_stream.to_stream_object().unwrap();
36
37 // Verify stream properties
38 assert_eq!(stream_obj.dict.get(b"Type").unwrap(), &Object::Name(b"ObjStm".to_vec()));
39 assert_eq!(stream_obj.dict.get(b"N").unwrap(), &Object::Integer(3));
40 assert!(stream_obj.dict.has(b"First"));
41 assert!(stream_obj.dict.has(b"Length"));
42
43 // Verify content is not empty
44 assert!(!stream_obj.content.is_empty());
45 }
46
47 #[test]
48 fn test_save_options_with_object_streams() {
49 let mut doc = Document::with_version("1.5");
50 let pages_id = doc.new_object_id();
51
52 let page_id = doc.add_object(dictionary! {
53 "Type" => "Page",
54 "Parent" => pages_id,
55 "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
56 });
57
58 doc.objects.insert(pages_id, Object::Dictionary(dictionary! {
59 "Type" => "Pages",
60 "Kids" => vec![page_id.into()],
61 "Count" => 1,
62 }));
63
64 let catalog_id = doc.add_object(dictionary! {
65 "Type" => "Catalog",
66 "Pages" => pages_id,
67 });
68
69 doc.trailer.set("Root", catalog_id);
70
71 // Save with object streams enabled
72 let save_options = SaveOptions {
73 use_object_streams: true,
74 use_xref_streams: false,
75 linearize: false,
76 object_stream_config: Default::default(),
77 };
78
79 let mut buffer = Vec::new();
80 doc.save_with_options(&mut buffer, save_options).unwrap();
81
82 let content = String::from_utf8_lossy(&buffer);
83
84 // Verify object streams were created
85 assert!(content.contains("/ObjStm"), "Object streams should be created");
86
87 // Verify PDF version is 1.5 or higher
88 assert!(content.starts_with("%PDF-1.5") || content.starts_with("%PDF-1.6") || content.starts_with("%PDF-1.7"),
89 "PDF version should be 1.5 or higher for object streams");
90 }
91
92 #[test]
93 fn test_save_options_without_object_streams() {
94 let mut doc = Document::with_version("1.4");
95 let pages_id = doc.new_object_id();
96
97 let page_id = doc.add_object(dictionary! {
98 "Type" => "Page",
99 "Parent" => pages_id,
100 "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
101 });
102
103 doc.objects.insert(pages_id, Object::Dictionary(dictionary! {
104 "Type" => "Pages",
105 "Kids" => vec![page_id.into()],
106 "Count" => 1,
107 }));
108
109 let catalog_id = doc.add_object(dictionary! {
110 "Type" => "Catalog",
111 "Pages" => pages_id,
112 });
113
114 doc.trailer.set("Root", catalog_id);
115
116 // Save without object streams
117 let save_options = SaveOptions {
118 use_object_streams: false,
119 use_xref_streams: false,
120 linearize: false,
121 object_stream_config: Default::default(),
122 };
123
124 let mut buffer = Vec::new();
125 doc.save_with_options(&mut buffer, save_options).unwrap();
126
127 let content = String::from_utf8_lossy(&buffer);
128
129 // Verify no object streams were created
130 assert!(!content.contains("/ObjStm"), "Object streams should not be created");
131
132 // Verify objects exist as individual objects
133 assert!(content.contains(&format!("{} 0 obj", catalog_id.0)));
134 assert!(content.contains(&format!("{} 0 obj", pages_id.0)));
135 assert!(content.contains(&format!("{} 0 obj", page_id.0)));
136 }
137
138 #[test]
139 fn test_encrypted_document_object_streams() {
140 let mut doc = Document::with_version("1.5");
141 let pages_id = doc.new_object_id();
142
143 // Create encryption dictionary
144 let encrypt_id = doc.add_object(dictionary! {
145 "Filter" => "Standard",
146 "V" => 2,
147 "R" => 3,
148 "Length" => 128,
149 "P" => -1,
150 "O" => Object::String(vec![0; 32], lopdf::StringFormat::Hexadecimal),
151 "U" => Object::String(vec![0; 32], lopdf::StringFormat::Hexadecimal),
152 });
153
154 doc.trailer.set("Encrypt", encrypt_id);
155
156 let page_id = doc.add_object(dictionary! {
157 "Type" => "Page",
158 "Parent" => pages_id,
159 "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
160 });
161
162 doc.objects.insert(pages_id, Object::Dictionary(dictionary! {
163 "Type" => "Pages",
164 "Kids" => vec![page_id.into()],
165 "Count" => 1,
166 }));
167
168 let catalog_id = doc.add_object(dictionary! {
169 "Type" => "Catalog",
170 "Pages" => pages_id,
171 });
172
173 doc.trailer.set("Root", catalog_id);
174
175 // Verify encryption dictionary cannot be compressed
176 let encrypt_obj = doc.objects.get(&encrypt_id).unwrap();
177 assert!(!ObjectStream::can_be_compressed(encrypt_id, encrypt_obj, &doc),
178 "Encryption dictionary must not be compressible");
179
180 let mut buffer = Vec::new();
181 let result = doc.save_modern(&mut buffer);
182 assert!(result.is_ok());
183
184 let content = String::from_utf8_lossy(&buffer);
185
186 // Encryption dictionary should remain as top-level object
187 assert!(content.contains(&format!("{} 0 obj", encrypt_id.0)),
188 "Encryption dictionary must remain as individual object");
189 }
190
191 #[test]
192 fn test_linearized_pdf_catalog_handling() {
193 let mut doc = Document::with_version("1.5");
194
195 // Add linearization parameters
196 let _lin_id = doc.add_object(dictionary! {
197 "Linearized" => 1,
198 "L" => 50000, // File length
199 "H" => vec![Object::Integer(1000), Object::Integer(2000)], // Hint stream location
200 "O" => 10, // First page object number
201 "E" => 5000, // End of first page
202 "N" => 1, // Number of pages
203 "T" => 45000 // First entry in main cross-reference table
204 });
205
206 let pages_id = doc.new_object_id();
207
208 let page_id = doc.add_object(dictionary! {
209 "Type" => "Page",
210 "Parent" => pages_id,
211 "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
212 });
213
214 doc.objects.insert(pages_id, Object::Dictionary(dictionary! {
215 "Type" => "Pages",
216 "Kids" => vec![page_id.into()],
217 "Count" => 1,
218 }));
219
220 let catalog_id = doc.add_object(dictionary! {
221 "Type" => "Catalog",
222 "Pages" => pages_id,
223 });
224
225 doc.trailer.set("Root", catalog_id);
226
227 // In linearized PDF, catalog should not be compressible
228 let catalog_obj = doc.objects.get(&catalog_id).unwrap();
229 assert!(!ObjectStream::can_be_compressed(catalog_id, catalog_obj, &doc),
230 "Catalog must not be compressible in linearized PDFs");
231
232 // But Pages and Page should still be compressible
233 let pages_obj = doc.objects.get(&pages_id).unwrap();
234 let page_obj = doc.objects.get(&page_id).unwrap();
235 assert!(ObjectStream::can_be_compressed(pages_id, pages_obj, &doc),
236 "Pages should still be compressible in linearized PDFs");
237 assert!(ObjectStream::can_be_compressed(page_id, page_obj, &doc),
238 "Page should still be compressible in linearized PDFs");
239 }
240
241 #[test]
242 fn test_object_stream_with_references() {
243 let mut obj_stream = ObjectStream::builder().build();
244
245 // Add objects with references
246 obj_stream.add_object((1, 0), Object::Dictionary(dictionary! {
247 "Type" => "Font",
248 "BaseFont" => "Helvetica",
249 "Encoding" => Object::Reference((5, 0)) // Reference to another object
250 })).unwrap();
251
252 obj_stream.add_object((2, 0), Object::Array(vec![
253 Object::Integer(100),
254 Object::Reference((10, 0)), // Reference
255 Object::String(b"Test".to_vec(), lopdf::StringFormat::Literal)
256 ])).unwrap();
257
258 let stream_obj = obj_stream.to_stream_object().unwrap();
259 assert_eq!(stream_obj.dict.get(b"N").unwrap(), &Object::Integer(2));
260
261 // Verify content contains the references
262 let content = String::from_utf8_lossy(&stream_obj.content);
263 assert!(content.contains("5 0 R")); // Reference syntax
264 assert!(content.contains("10 0 R"));
265 }
266
267 #[test]
268 fn test_object_stream_with_nested_dictionaries() {
269 let mut obj_stream = ObjectStream::builder().build();
270
271 // Add complex nested dictionary
272 obj_stream.add_object((1, 0), Object::Dictionary(dictionary! {
273 "Type" => "ExtGState",
274 "CA" => 0.5,
275 "ca" => 0.5,
276 "BM" => Object::Array(vec![
277 Object::Name(b"Normal".to_vec()),
278 Object::Name(b"Multiply".to_vec())
279 ]),
280 "SMask" => dictionary! {
281 "Type" => "Mask",
282 "S" => "Alpha",
283 "G" => Object::Reference((10, 0))
284 }
285 })).unwrap();
286
287 let stream_obj = obj_stream.to_stream_object().unwrap();
288 assert!(!stream_obj.content.is_empty());
289 }
290
291 #[test]
292 fn test_multiple_object_streams_in_document() {
293 let mut doc = Document::with_version("1.5");
294 let pages_id = doc.new_object_id();
295
296 // Add many objects to force multiple object streams
297 let mut page_ids = vec![];
298 for i in 0..250 { // More than default max_objects_per_stream
299 let page_id = doc.add_object(dictionary! {
300 "Type" => "Page",
301 "Parent" => pages_id,
302 "MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()],
303 "Contents" => Object::Reference((1000 + i, 0))
304 });
305 page_ids.push(page_id);
306 }
307
308 doc.objects.insert(pages_id, Object::Dictionary(dictionary! {
309 "Type" => "Pages",
310 "Kids" => page_ids.into_iter().map(Object::Reference).collect::<Vec<_>>(),
311 "Count" => 250,
312 }));
313
314 let catalog_id = doc.add_object(dictionary! {
315 "Type" => "Catalog",
316 "Pages" => pages_id,
317 });
318
319 doc.trailer.set("Root", catalog_id);
320
321 let mut buffer = Vec::new();
322 doc.save_modern(&mut buffer).unwrap();
323
324 let content = String::from_utf8_lossy(&buffer);
325
326 // Count object streams
327 let objstm_count = content.matches("/ObjStm").count();
328 assert!(objstm_count >= 3, "Should have multiple object streams for 250+ objects");
329 }