From 4dc91cdaf8ce907eedbe72686b1b3b9ca80ff973 Mon Sep 17 00:00:00 2001 From: ".CooCoo" Date: Mon, 16 Nov 2026 18:36:07 -0800 Subject: [PATCH] craft-xls: the testing module is built only for tests and the test-support feature (#1377) --- Cargo.lock | 1 + crates/xls/Cargo.toml | 5 +++++ crates/xls/README.md | 3 ++- crates/xls/src/crypt.rs | 4 ++++ crates/xls/src/lib.rs | 1 + 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 362c82ac7..bfb97a7f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3242,6 +3242,7 @@ version = "0.1.0" dependencies = [ "craft-cfb", "craft-sheet", + "craft-xls", "md-5 0.10.6", "miniz_oxide 0.9.1", "sha1 0.10.7", diff --git a/crates/xls/Cargo.toml b/crates/xls/Cargo.toml index a97354581..b3df21cfc 100644 --- a/crates/xls/Cargo.toml +++ b/crates/xls/Cargo.toml @@ -10,6 +10,10 @@ repository.workspace = true [lints] workspace = true +[features] +# `testing`: a writer of small BIFF8 workbooks, plain or encrypted, for the tests of crates that read them. +test-support = [] + [dependencies] craft-cfb = { workspace = true } craft-sheet = { workspace = true } @@ -21,4 +25,5 @@ miniz_oxide = "0.9" thiserror = { workspace = true } [dev-dependencies] +craft-xls = { path = ".", features = ["test-support"] } miniz_oxide = "0.9" diff --git a/crates/xls/README.md b/crates/xls/README.md index d656e0798..13984d5fe 100644 --- a/crates/xls/README.md +++ b/crates/xls/README.md @@ -63,7 +63,8 @@ with `read_with_password` ([MS-OFFCRYPTO] 2.3.5, 2.3.6). Without a password, Exc is `Error::WrongPassword`; XOR obfuscation and CryptoAPI with AES, another cipher or hash, or an external provider are `Error::UnsupportedEncryption`. Every size in the FilePass record is checked before it is used. There is no writer -(`craft_xls::testing` builds small fixtures for tests). +(`craft_xls::testing` builds small fixtures for tests; another crate's tests get it through the +`test-support` feature). A formula that cannot be put back into text (a link to another workbook, a macro command, an unknown token) leaves its cell with the saved result, and `Warning::FormulaKept` says why. diff --git a/crates/xls/src/crypt.rs b/crates/xls/src/crypt.rs index c723341d1..1dc0e59d8 100644 --- a/crates/xls/src/crypt.rs +++ b/crates/xls/src/crypt.rs @@ -337,6 +337,7 @@ impl Rc4 { /// Encrypts a Workbook stream the way [`decrypt`] reads it, for tests: the FilePass record /// (RC4 1.1 with a fixed salt and verifier) goes right after the globals' BOF. `None` when the /// stream does not start with a record. +#[cfg(any(test, feature = "test-support"))] pub(crate) fn encrypt(stream: &[u8], password: &str) -> Option> { let salt: [u8; 16] = std::array::from_fn(|n| n as u8 + 0x40); let verifier: [u8; 16] = std::array::from_fn(|n| n as u8 + 0x60); @@ -359,6 +360,7 @@ pub(crate) fn encrypt(stream: &[u8], password: &str) -> Option> { /// The FilePass data [`encrypt_cryptoapi`] writes: RC4 CryptoAPI version 4.2, as current Excel /// writes it, with a `bits`-bit key (40 to 128) and a fixed salt and verifier. +#[cfg(any(test, feature = "test-support"))] pub(crate) fn cryptoapi_filepass(password: &str, bits: u32) -> Vec { let salt: [u8; SALT_LEN] = std::array::from_fn(|n| (n as u8).wrapping_mul(7).wrapping_add(0x11)); let verifier: [u8; SALT_LEN] = std::array::from_fn(|n| n as u8 + 0x80); @@ -381,12 +383,14 @@ pub(crate) fn cryptoapi_filepass(password: &str, bits: u32) -> Vec { } /// [`encrypt`] with RC4 CryptoAPI ([`cryptoapi_filepass`]). +#[cfg(any(test, feature = "test-support"))] pub(crate) fn encrypt_cryptoapi(stream: &[u8], password: &str, bits: u32) -> Option> { with_filepass(stream, &cryptoapi_filepass(password, bits), password) } /// `stream` with a FilePass record holding `filepass` right after its first BOF, encrypted with /// `password`. +#[cfg(any(test, feature = "test-support"))] fn with_filepass(stream: &[u8], filepass: &[u8], password: &str) -> Option> { let (_, bof) = header(stream, 0)?; let bof_len = 4 + bof; diff --git a/crates/xls/src/lib.rs b/crates/xls/src/lib.rs index 9ddfa00aa..adec09780 100644 --- a/crates/xls/src/lib.rs +++ b/crates/xls/src/lib.rs @@ -46,6 +46,7 @@ mod notes; mod read; mod records; mod styles; +#[cfg(any(test, feature = "test-support"))] #[doc(hidden)] pub mod testing;