Skip to content

PdfCraft: An open-source PDF reader and editor written by scratch in pure Rust

Merge PR #2724: fix(print): disable Print on documents whose security forbids printing

aiongg38fdeb3f22ac
diff --cc crates/ui-egui/tests/print.rs
index 37514bcec,618480525..45adcb066
--- a/crates/ui-egui/tests/print.rs
+++ b/crates/ui-egui/tests/print.rs
@@@ -478,211 -427,52 +478,261 @@@ fn an_empty_printer_list_says_none_were
assert!(!h.get_by_role_and_label(Role::Button, "Save as PDF").accesskit_node().is_disabled());
}
+/// `PAPERS` index of the named sheet.
+fn paper(name: &str) -> usize {
+ pdfcraft_engine::print::PAPERS.iter().position(|p| p.0 == name).unwrap()
+}
+
+/// Label loads label stock; leaving it gives the paper the person had back (#3564).
+#[test]
+fn leaving_label_gives_back_the_paper_it_replaced() {
+ use pdfcraft_engine::print::LABEL_PAPER;
+ use pdfcraft_ui_egui::PrintHandling;
+ let a4 = paper("A4");
+ let mut h = harness();
+ assert!(h.state_mut().execute("print.dialog"));
+ h.run_steps(3);
+ for (mode, tab) in
+ [(PrintHandling::Size, "Size"), (PrintHandling::Poster, "Poster"), (PrintHandling::Multiple, "Multiple"), (PrintHandling::Booklet, "Booklet")]
+ {
+ h.state_mut().print_draft.paper = a4;
+ h.state_mut().print_draft.handling = PrintHandling::Size;
+ h.run_steps(2);
+ h.get_by_role_and_label(Role::Button, "Label").click();
+ h.run_steps(2);
+ assert_eq!(h.state().print_draft.handling, PrintHandling::Label);
+ assert_eq!(h.state().print_draft.paper, LABEL_PAPER, "Label puts label stock in");
+ h.get_by_role_and_label(Role::Button, tab).click();
+ h.run_steps(2);
+ assert_eq!(h.state().print_draft.handling, mode);
+ assert_eq!(h.state().print_draft.paper, a4, "{tab} gives A4 back after Label");
+ h.get_by_value("A4");
+ }
+}
+
+/// A paper picked while in Label is the person's choice, and leaving Label keeps it.
+#[test]
+fn a_paper_picked_while_in_label_is_kept_when_leaving_it() {
+ use pdfcraft_engine::print::LABEL_PAPER;
+ use pdfcraft_ui_egui::{PrintDraft, PrintHandling};
+ let (a4, letter) = (paper("A4"), paper("US Letter"));
+ let mut d = PrintDraft { paper: a4, ..Default::default() };
+ d.set_handling(PrintHandling::Label);
+ assert_eq!(d.paper, LABEL_PAPER);
+ d.paper = letter;
+ d.set_handling(PrintHandling::Size);
+ assert_eq!(d.paper, letter);
+ // A second round trip from the kept paper gives that paper back.
+ d.set_handling(PrintHandling::Label);
+ d.set_handling(PrintHandling::Poster);
+ assert_eq!(d.paper, letter);
+ // Clicking Label again while in Label doesn't forget the paper it replaced.
+ d.set_handling(PrintHandling::Label);
+ d.set_handling(PrintHandling::Label);
+ d.set_handling(PrintHandling::Multiple);
+ assert_eq!(d.paper, letter);
+}
+
+/// Picking the label stock from the Paper menu switches to Label (the existing behaviour); leaving
+/// Label afterwards gives back the paper picked before it.
+#[test]
+fn label_stock_picked_from_the_paper_menu_is_undone_by_leaving_label() {
+ use pdfcraft_engine::print::{LABEL_PAPER, PAPERS};
+ use pdfcraft_ui_egui::PrintHandling;
+ let a4 = paper("A4");
+ let mut h = harness();
+ assert!(h.state_mut().execute("print.dialog"));
+ h.run_steps(3);
+ h.state_mut().print_draft.paper = a4;
+ h.run_steps(2);
+ h.get_by_value("A4").click();
+ h.run_steps(2);
+ // The label stock is last in a menu that scrolls: bring it into view (it settles over a few
+ // frames) before clicking it.
+ h.get_by_label(PAPERS[LABEL_PAPER].0).scroll_to_me();
+ h.run_steps(4);
+ h.get_by_label(PAPERS[LABEL_PAPER].0).click();
+ h.run_steps(2);
+ assert_eq!(h.state().print_draft.paper, LABEL_PAPER);
+ assert_eq!(h.state().print_draft.handling, PrintHandling::Label);
+ h.get_by_role_and_label(Role::Button, "Size").click();
+ h.run_steps(2);
+ assert_eq!(h.state().print_draft.handling, PrintHandling::Size);
+ assert_eq!(h.state().print_draft.paper, a4);
+}
+
+/// The same through the Paper menu: US Letter picked while in Label stays when Size is chosen,
+/// rather than the A4 Label replaced coming back.
+#[test]
+fn a_paper_picked_from_the_menu_while_in_label_is_kept() {
+ use pdfcraft_engine::print::{LABEL_PAPER, PAPERS};
+ use pdfcraft_ui_egui::PrintHandling;
+ let (a4, letter) = (paper("A4"), paper("US Letter"));
+ let mut h = harness();
+ assert!(h.state_mut().execute("print.dialog"));
+ h.run_steps(3);
+ h.state_mut().print_draft.paper = a4;
+ h.run_steps(2);
+ h.get_by_role_and_label(Role::Button, "Label").click();
+ h.run_steps(2);
+ assert_eq!(h.state().print_draft.paper, LABEL_PAPER);
+ h.get_by_value(PAPERS[LABEL_PAPER].0).click();
+ h.run_steps(2);
+ h.get_by_label("US Letter").scroll_to_me();
+ h.run_steps(4);
+ h.get_by_label("US Letter").click();
+ h.run_steps(2);
+ assert_eq!(h.state().print_draft.paper, letter);
+ h.get_by_role_and_label(Role::Button, "Size").click();
+ h.run_steps(2);
+ assert_eq!(h.state().print_draft.handling, PrintHandling::Size);
+ assert_eq!(h.state().print_draft.paper, letter, "the paper picked in Label is kept");
+ h.get_by_value("US Letter");
+}
+
+/// Properties… reads the printer's driver options off the GUI thread: the dialog keeps drawing
+/// while CUPS answers, and a read that dies leaves the panel with nothing rather than waiting
+/// (#1818). The click's request is made directly, so the test runs where Properties… opens the
+/// driver's own window instead (Windows).
+#[cfg(not(target_arch = "wasm32"))]
+#[test]
+fn printer_options_are_read_off_the_gui_thread_and_a_dead_read_offers_nothing() {
+ use pdfcraft_engine::print::spool::PrinterOption;
+ use std::sync::{Arc, Mutex};
+ let mut h = harness();
+ // The source waits until the test lets it answer, so the dialog is seen while it reads.
+ let gate = Arc::new(Mutex::new(()));
+ let hold = gate.lock().unwrap();
+ let wait = gate.clone();
+ h.state_mut().options_source = Some(Arc::new(move |printer: &str| {
+ assert_ne!(printer, "Dead", "the read died");
+ let _open = wait.lock();
+ vec![PrinterOption {
+ key: "InputSlot".into(),
+ label: "Paper tray".into(),
+ group: String::new(),
+ choices: vec![("Auto".into(), "Automatic".into()), ("Tray2".into(), "Tray 2".into())],
+ default: "Auto".into(),
+ }]
+ }));
+ assert!(h.state_mut().execute("print.dialog"));
+ h.run_steps(2);
+ let ask = |h: &mut Harness<'static, PdfCraftApp>, printer: &str| {
+ let d = &mut h.state_mut().print_draft;
+ d.driver_options = None;
+ d.driver_options_loading = Some(printer.into());
+ d.read_options_for = Some(printer.into());
+ d.show_driver_options = true;
+ };
+ let settle = |h: &mut Harness<'static, PdfCraftApp>| {
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
+ while h.state().print_draft.driver_options_loading.is_some() && std::time::Instant::now() < deadline {
+ h.run_steps(1);
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ };
+ ask(&mut h, "Office");
+ h.run_steps(3);
+ assert_eq!(h.state().print_draft.driver_options_loading.as_deref(), Some("Office"), "frames keep coming while the read waits");
+ assert!(h.state().print_draft.driver_options.is_none());
+ h.get_by_label("Reading the printer's settings…");
+ drop(hold);
+ settle(&mut h);
+ let (read_for, options) = h.state().print_draft.driver_options.clone().expect("the options arrived");
+ assert_eq!(read_for, "Office");
+ assert_eq!(options.len(), 1);
+ assert_eq!(options[0].key, "InputSlot");
+ h.get_by_label("Paper tray");
+
+ ask(&mut h, "Dead");
+ settle(&mut h);
+ assert_eq!(h.state().print_draft.driver_options, Some(("Dead".into(), Vec::new())), "a dead read offers nothing, and the panel says so");
+ h.get_by_label("This printer offers no settings of its own.");
+}
+
+/// A read of the printer's options that dies still asks for a frame, so an idle dialog stops
+/// saying it is reading without waiting for the mouse to move (#2108 review).
+#[cfg(not(target_arch = "wasm32"))]
+#[test]
+fn a_dead_options_read_wakes_an_idle_dialog() {
+ use std::sync::atomic::{AtomicBool, Ordering};
+ use std::sync::{Arc, Mutex};
+ let mut h = harness();
+ let gate = Arc::new(Mutex::new(()));
+ let hold = gate.lock().unwrap();
+ let wait = gate.clone();
+ let dying = Arc::new(AtomicBool::new(false));
+ let flag = dying.clone();
+ h.state_mut().options_source = Some(Arc::new(move |_: &str| {
+ let _open = wait.lock();
+ flag.store(true, Ordering::SeqCst);
+ panic!("the read died");
+ }));
+ assert!(h.state_mut().execute("print.dialog"));
+ h.run_steps(2);
+ let d = &mut h.state_mut().print_draft;
+ d.driver_options = None;
+ d.driver_options_loading = Some("Office".into());
+ d.read_options_for = Some("Office".into());
+ d.show_driver_options = true;
+ h.run();
+ assert!(!h.ctx.has_requested_repaint(), "the dialog is idle while the read waits");
+ drop(hold);
+ let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
+ while !(dying.load(Ordering::SeqCst) && h.ctx.has_requested_repaint()) && std::time::Instant::now() < deadline {
+ std::thread::sleep(std::time::Duration::from_millis(5));
+ }
+ assert!(h.ctx.has_requested_repaint(), "the dead read asked for a frame");
+ h.run();
+ assert_eq!(h.state().print_draft.driver_options, Some(("Office".into(), Vec::new())));
+}
++
+ /// A document whose security forbids printing (#2669) says so before the dialog opens, and the
+ /// top bar's Print button is disabled, instead of the dialog refusing on confirm.
+ #[test]
+ fn print_restricted_documents_say_so_before_the_dialog() {
+ let mut doc = pdfcraft_cos::Document::open(std::sync::Arc::new(include_bytes!("data/form.pdf").to_vec())).unwrap();
+ doc.set_encryption(&pdfcraft_cos::NewEncryption {
+ algorithm: pdfcraft_cos::Algorithm::Aes256,
+ user_password: "",
+ owner_password: "owner",
+ permissions: -1 ^ 0b100,
+ encrypt_metadata: true,
+ seed: [3; 32],
+ })
+ .unwrap();
+ let bytes = pdfcraft_cos::write_full(&doc, &Default::default()).unwrap();
+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(move |_| {
+ let mut app = PdfCraftApp::new();
+ app.set_option("language", "en").unwrap();
+ app.open_bytes("restricted.pdf", None, bytes).unwrap();
+ app
+ });
+ h.ctx.set_os(egui::os::OperatingSystem::Windows);
+ h.run_steps(4);
+ assert!(h.get_by_label("Print (Ctrl+P)").accesskit_node().is_disabled());
+ h.get_by_label("Print (Ctrl+P)").hover();
+ h.run_steps(30);
+ h.get_by_label_contains("don't allow printing");
+ assert!(!h.state_mut().execute("print.dialog"));
+ h.run_steps(2);
+ assert_eq!(h.state().dialog, None);
+ assert_eq!(h.state().toast.as_ref().map(|t| t.0.as_str()), Some("The document's security settings don't allow printing"));
+ }
+
+ /// With no document open the top bar's Print button is disabled too, but not for security
+ /// reasons: its tooltip must not blame the document's settings (#2669).
+ #[test]
+ fn print_without_a_document_does_not_blame_security() {
+ let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).build_eframe(|_| {
+ let mut app = PdfCraftApp::new();
+ app.set_option("language", "en").unwrap();
+ app
+ });
+ h.ctx.set_os(egui::os::OperatingSystem::Windows);
+ h.run_steps(4);
+ assert!(h.get_by_label("Print (Ctrl+P)").accesskit_node().is_disabled());
+ h.get_by_label("Print (Ctrl+P)").hover();
+ h.run_steps(30);
+ assert!(h.query_by_label_contains("don't allow printing").is_none());
+ }