mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-10 13:01:29 +00:00
M10: test the print entry points against a Windows without the DLLs (#2630)
Review follow-up on #2701: the test no longer requires the DLLs in the host's System32 (the trimmed installations this supports lack them). Instead a test-only override points the lookup at an empty folder and submit, submit_with_progress and printers are called through: printing is refused naming the DLL and no printers are listed, before any call into the delay-loaded DLLs. The job names a printer that doesn't exist and the bytes aren't a PDF, so a missing guard can't reach a real one. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
03a147eced
commit
f1753c1431
2 files changed
+37
-3
No files matched your search
@@ -19,11 +19,22 @@ pub(crate) fn missing_print_dll_in(system32: &std::path::Path) -> Option<&'stati
|
||||
PRINT_DLLS.into_iter().find(|dll| !system32.join(dll).is_file())
|
||||
}
|
||||
|
||||
#[cfg(all(windows, test))]
|
||||
thread_local! {
|
||||
/// Tests: the folder the print DLLs are looked for in, instead of the system's, so the
|
||||
/// public entry points can be checked against a Windows that lacks them.
|
||||
pub(crate) static SYSTEM32_FOR_TEST: std::cell::RefCell<Option<std::path::PathBuf>> = const { std::cell::RefCell::new(None) };
|
||||
}
|
||||
|
||||
/// The first print DLL this Windows installation lacks. `None` when all are there, and when the
|
||||
/// system folder is unknown (the loader then decides, as before).
|
||||
fn missing_print_dll() -> Option<&'static str> {
|
||||
#[cfg(windows)]
|
||||
{
|
||||
#[cfg(test)]
|
||||
if let Some(dir) = SYSTEM32_FOR_TEST.with(|d| d.borrow().clone()) {
|
||||
return missing_print_dll_in(&dir);
|
||||
}
|
||||
let root = std::env::var_os("SystemRoot").or_else(|| std::env::var_os("windir"))?;
|
||||
missing_print_dll_in(&std::path::Path::new(&root).join("System32"))
|
||||
}
|
||||
|
||||
@@ -1160,7 +1160,30 @@ fn missing_print_dlls_are_named() {
|
||||
std::fs::write(dir.join(PRINT_DLLS[1]), b"").unwrap();
|
||||
assert_eq!(missing_print_dll_in(&dir), None);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
// This machine's own Windows has them.
|
||||
let system32 = std::path::Path::new(&std::env::var_os("SystemRoot").unwrap()).join("System32");
|
||||
assert_eq!(missing_print_dll_in(&system32), None);
|
||||
}
|
||||
|
||||
/// #2630: on a Windows without the print DLLs, the public entry points answer before any call
|
||||
/// into the delay-loaded DLLs: no printers, and printing refused naming the DLL. The job names a
|
||||
/// printer that doesn't exist and the bytes aren't a PDF, so a missing guard fails here with
|
||||
/// another error rather than reaching a real printer.
|
||||
#[cfg(windows)]
|
||||
#[test]
|
||||
fn printing_without_the_print_dlls_is_refused_before_the_spooler() {
|
||||
use crate::spool::{PRINT_DLLS, SYSTEM32_FOR_TEST, printers, submit, submit_with_progress};
|
||||
let dir = std::env::temp_dir().join(format!("pdfcraft-print-no-dlls-{}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
SYSTEM32_FOR_TEST.with(|d| *d.borrow_mut() = Some(dir.clone()));
|
||||
let job = crate::spool::Job { printer: Some("PdfCraft test: no such printer".into()), ..Default::default() };
|
||||
let named = |r: Result<String, crate::PrintError>| match r {
|
||||
Err(crate::PrintError::Spool(m)) => m.contains(PRINT_DLLS[0]),
|
||||
_ => false,
|
||||
};
|
||||
assert!(named(submit(b"not a PDF", &job)), "submit names the missing DLL");
|
||||
let mut called = false;
|
||||
assert!(named(submit_with_progress(b"not a PDF", &job, &mut |_, _| called = true)), "submit_with_progress names it");
|
||||
assert!(!called, "no sheet reached a spooler");
|
||||
assert!(printers().is_empty(), "no printers are listed");
|
||||
SYSTEM32_FOR_TEST.with(|d| *d.borrow_mut() = None);
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
}
|
||||
Reference in new issue
Block a user