154 lines
6.4 KiB
Rust
154 lines
6.4 KiB
Rust
//! Mandatory test 1 (blocker): true redaction. After redacting the box on page 1, neither pdfium's text
|
|
//! extraction, a lopdf object scan (strings, names, raw and decompressed streams) nor a raw byte grep finds any
|
|
//! redacted string, the document's metadata and hidden parts are gone, and page 2 is byte-equal in content.
|
|
mod common;
|
|
use common::*;
|
|
use lopdf::Document;
|
|
use pictor_pdf::{Redaction, ViewRect};
|
|
|
|
fn redact(input: &[u8], rects: Vec<ViewRect>) -> pictor_pdf::Redacted {
|
|
pictor_pdf::redact(engine(), input, None, &[Redaction { page: 0, rects }], &Default::default())
|
|
.expect("redaction succeeds")
|
|
}
|
|
|
|
fn assert_no_secrets(output: &[u8]) {
|
|
// 1. pdfium text extraction, every page.
|
|
let text = pictor_pdf::extract_text(engine(), output, None).expect("pdfium opens the output");
|
|
let all: String = text.concat();
|
|
for secret in SECRETS {
|
|
assert!(!all.contains(secret), "pdfium text extraction still finds {secret:?}");
|
|
}
|
|
assert!(all.contains("PAGE-TWO-KEEP"), "page 2 lost its text: {all:?}");
|
|
// 2. lopdf object scan.
|
|
for blob in scan_objects(output) {
|
|
for secret in SECRETS {
|
|
assert!(!contains(&blob, secret), "lopdf object scan still finds {secret:?}");
|
|
}
|
|
}
|
|
// 3. Raw byte grep of the whole file.
|
|
for secret in SECRETS {
|
|
assert!(!contains(output, secret), "a raw byte grep still finds {secret:?}");
|
|
}
|
|
}
|
|
|
|
fn assert_page_two_unchanged(input: &[u8], output: &[u8]) {
|
|
let a = Document::load_mem(input).unwrap();
|
|
let b = Document::load_mem(output).unwrap();
|
|
let (pa, pb) = (page_ids(&a)[1], page_ids(&b)[1]);
|
|
assert_eq!(page_content(&a, pa), page_content(&b, pb), "page 2's content changed");
|
|
let font = |d: &Document, p| {
|
|
let fonts = d.get_page_fonts(p).unwrap();
|
|
fonts.iter().map(|(k, v)| (k.clone(), format!("{:?}", v))).collect::<Vec<_>>()
|
|
};
|
|
assert_eq!(font(&a, pa), font(&b, pb), "page 2's fonts changed");
|
|
// The raw stream bytes too (lopdf writes untouched streams as they were).
|
|
let raw = |d: &Document, p| -> Vec<Vec<u8>> {
|
|
d.get_page_contents(p).iter().map(|id| d.get_object(*id).unwrap().as_stream().unwrap().content.clone()).collect()
|
|
};
|
|
assert_eq!(raw(&a, pa), raw(&b, pb), "page 2's raw content stream bytes changed");
|
|
}
|
|
|
|
fn assert_hidden_parts_gone(output: &[u8]) {
|
|
let doc = Document::load_mem(output).unwrap();
|
|
assert!(doc.trailer.get(b"Info").is_err(), "/Info survived");
|
|
let catalog = doc.catalog().unwrap();
|
|
for key in ["Metadata", "OCProperties", "StructTreeRoot", "Outlines", "AcroForm"] {
|
|
assert!(catalog.get(key.as_bytes()).is_err(), "catalog /{key} survived");
|
|
}
|
|
for o in doc.objects.values() {
|
|
let dict = match o {
|
|
lopdf::Object::Dictionary(d) => d,
|
|
lopdf::Object::Stream(s) => &s.dict,
|
|
_ => continue,
|
|
};
|
|
assert!(dict.get(b"Metadata").is_err(), "a /Metadata entry survived");
|
|
assert!(!dict.has_type(b"OCG"), "an optional content group survived");
|
|
assert!(!dict.has_type(b"EmbeddedFile"), "an attachment survived");
|
|
}
|
|
}
|
|
|
|
fn check(opts: Options, page1_size: (f32, f32)) {
|
|
let input = secret_pdf(&opts);
|
|
// The fixture really contains every secret before redaction (else the test proves nothing).
|
|
let before: Vec<Vec<u8>> = scan_objects(&input);
|
|
for secret in SECRETS {
|
|
assert!(before.iter().any(|b| contains(b, secret)), "fixture lacks {secret:?}");
|
|
}
|
|
let rect = match opts.rotate {
|
|
0 => secret_box(),
|
|
// For a page shown rotated, the same content sits elsewhere in view space; redact the whole page then.
|
|
_ => ViewRect { x: 0.0, y: 0.0, w: page1_size.0, h: page1_size.1 },
|
|
};
|
|
let out = redact(&input, vec![rect]);
|
|
assert_no_secrets(&out.bytes);
|
|
assert_page_two_unchanged(&input, &out.bytes);
|
|
assert_hidden_parts_gone(&out.bytes);
|
|
let sizes = pictor_pdf::page_sizes(engine(), &out.bytes, None).unwrap();
|
|
assert_eq!(sizes.len(), 2);
|
|
assert!((sizes[0].0 - page1_size.0).abs() < 0.5 && (sizes[0].1 - page1_size.1).abs() < 0.5, "{sizes:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn text_image_vector_hidden_layer_and_metadata_are_gone() {
|
|
check(Options::default(), (612.0, 792.0));
|
|
}
|
|
|
|
#[test]
|
|
fn compressed_streams_are_redacted_too() {
|
|
check(Options { compress: true, ..Default::default() }, (612.0, 792.0));
|
|
}
|
|
|
|
#[test]
|
|
fn a_rotated_page_keeps_its_displayed_size() {
|
|
check(Options { rotate: 90, ..Default::default() }, (792.0, 612.0));
|
|
}
|
|
|
|
#[test]
|
|
fn inherited_resources_and_rotation_are_handled() {
|
|
check(Options { inherited: true, rotate: 90, ..Default::default() }, (792.0, 612.0));
|
|
}
|
|
|
|
#[test]
|
|
fn the_box_is_black_and_the_rest_of_the_page_still_shows() {
|
|
let input = secret_pdf(&Options::default());
|
|
let out = redact(&input, vec![secret_box()]);
|
|
let img = pictor_pdf::render_page(engine(), &out.bytes, None, 0, 612).unwrap().to_rgb8();
|
|
// Inside the box (view 300, 100): black.
|
|
let p = img.get_pixel(300, 100);
|
|
assert!(p.0.iter().all(|&c| c < 16), "box not black: {p:?}");
|
|
// "PUBLIC-LINE" at PDF (72, 300) → view y ≈ 492: some dark pixels remain in that band.
|
|
let dark = (480..500).flat_map(|y| (72..160).map(move |x| (x, y))).filter(|&(x, y)| img.get_pixel(x, y).0[0] < 100).count();
|
|
assert!(dark > 20, "the text outside the box disappeared ({dark} dark pixels)");
|
|
}
|
|
|
|
#[test]
|
|
fn a_redacted_encrypted_document_needs_its_password_and_comes_out_clean() {
|
|
let input = secret_pdf(&Options::default());
|
|
let locked = pictor_pdf::protect(&input, None, "open-sesame", None).unwrap();
|
|
assert!(
|
|
pictor_pdf::redact(engine(), &locked, None, &[Redaction { page: 0, rects: vec![secret_box()] }], &Default::default())
|
|
.is_err(),
|
|
"redacted without the password"
|
|
);
|
|
let out = pictor_pdf::redact(
|
|
engine(),
|
|
&locked,
|
|
Some("open-sesame"),
|
|
&[Redaction { page: 0, rects: vec![secret_box()] }],
|
|
&Default::default(),
|
|
)
|
|
.unwrap();
|
|
assert_no_secrets(&out.bytes);
|
|
}
|
|
|
|
#[test]
|
|
fn no_redaction_box_is_refused() {
|
|
let input = secret_pdf(&Options::default());
|
|
assert!(pictor_pdf::redact(engine(), &input, None, &[Redaction { page: 0, rects: vec![] }], &Default::default()).is_err());
|
|
assert!(
|
|
pictor_pdf::redact(engine(), &input, None, &[Redaction { page: 9, rects: vec![secret_box()] }], &Default::default())
|
|
.is_err(),
|
|
"a page that does not exist was accepted"
|
|
);
|
|
}
|