mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-03 04:25:52 -05:00
Cover oversized PDF editing attachments end to end
This commit is contained in:
1 parent
1dbfb84d06
commit
b76ac867f7
1 file changed
+30
-8
@@ -45,6 +45,35 @@ fn attachment_compression_stays_within_the_reader_budget() {
|
||||
assert!(matches!(requested(true, i32::MAX as usize + 1), Err(PdfError::Write(_))));
|
||||
}
|
||||
|
||||
fn editing_attachment_is_filtered(bytes: &[u8], password: Option<&str>) -> bool {
|
||||
crate::pages::open(bytes, password)
|
||||
.unwrap()
|
||||
.objects()
|
||||
.into_iter()
|
||||
.find_map(|object| match object {
|
||||
Object::Stream(stream) if stream.dict().get::<Name<'_>>(b"Type").is_some_and(|name| name.as_ref() == b"EmbeddedFile") => {
|
||||
Some(stream.dict().contains_key(b"Filter"))
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.expect("editing attachment")
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "allocates several copies of a 256 MiB attachment"]
|
||||
fn oversized_editing_attachment_falls_back_to_raw_and_reopens() {
|
||||
let limit = crate::streams::MAX_DECODED.min(craft::pdf::syntax::DecodeLimits::default().max_decoded_stream());
|
||||
let native = vec![b'x'; limit + 1];
|
||||
let settings = serde_json::from_value(json!({"preserveEditing": true, "compression": {"compressText": true}})).unwrap();
|
||||
let opts = PdfOptions { settings, native: Some(native), ..Default::default() };
|
||||
let report = export_with_report(&doc(), &opts).unwrap();
|
||||
assert!(report.setting_notes.iter().any(|note| note.contains("the editing attachment was stored without compression")));
|
||||
assert!(!editing_attachment_is_filtered(&report.bytes, None));
|
||||
let restored = editing_with(&report.bytes, None).unwrap();
|
||||
assert_eq!(restored.data.as_slice(), opts.native.as_deref().unwrap(), "attachment bytes changed");
|
||||
assert!(restored.intact);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn linearized_and_encrypted_raw_editing_attachments_remain_unfiltered() {
|
||||
let native = b"{\"native\":true}";
|
||||
@@ -60,14 +89,7 @@ fn linearized_and_encrypted_raw_editing_attachments_remain_unfiltered() {
|
||||
let bytes = export(&doc(), &PdfOptions { settings, native: Some(native.to_vec()), ..Default::default() }).unwrap();
|
||||
assert!(bytes.windows(b"/Linearized".len()).any(|part| part == b"/Linearized"));
|
||||
let pw = (!pw.is_empty()).then_some(pw);
|
||||
let opened = crate::pages::open(&bytes, pw).unwrap();
|
||||
let raw = opened.objects().into_iter().find_map(|object| match object {
|
||||
Object::Stream(stream) if stream.dict().get::<Name<'_>>(b"Type").is_some_and(|name| name.as_ref() == b"EmbeddedFile") => {
|
||||
Some(!stream.dict().contains_key(b"Filter"))
|
||||
}
|
||||
_ => None,
|
||||
});
|
||||
assert_eq!(raw, Some(true));
|
||||
assert!(!editing_attachment_is_filtered(&bytes, pw));
|
||||
let restored = editing_with(&bytes, pw).unwrap();
|
||||
assert_eq!(restored.data, native);
|
||||
assert!(restored.intact);
|
||||
|
||||
Reference in new issue
Block a user