Files
wordcraft/crates/engine/tests/it/odt_password.rs
T
Dylan M. Taylor 42a5df5f94 chore: cut over image codecs and zip to craft-libs (#6661)
* chore: cut over image codecs and zip to craft-libs, fix main test regressions

* tests: store ODS mimetype and use standard ODF namespaces in mailings test

* Remove unused quick-xml workspace dependency

* fix: use standard ODF namespaces in file_dialogs test and preserve trailing line in markdown import_on_with_lines

* fix(geom): use png_chunks for PNG dimensions and p.fits(budget) in decode_for_drawing
2026-11-14 14:51:35 -05:00

38 lines
2.2 KiB
Rust

//! A password-protected ODT opens with its password (and says when one is needed or wrong), and
//! saving with a password writes it encrypted again (ODF 1.3 Part 2 §3.4).
use wordcraft_engine::io;
#[test]
fn an_odt_is_saved_and_opened_with_a_password() {
let doc = io::open_bytes("plain.txt", b"Private notes.").unwrap();
let enc = io::save_bytes_with("secret.odt", &doc, Some("hunter2"), &Default::default()).unwrap();
assert!(wordcraft_formats::odt::is_encrypted(&enc));
assert!(io::can_encrypt("secret.odt"));
let none = io::open_bytes_with("secret.odt", &enc, None).unwrap_err();
assert!(io::needs_password(&none), "{none}");
let wrong = io::open_bytes_with("secret.odt", &enc, Some("nope")).unwrap_err();
assert!(io::wrong_password(&wrong), "{wrong}");
let (opened, encrypted) = io::open_bytes_with("secret.odt", &enc, Some("hunter2")).unwrap();
assert!(encrypted);
assert_eq!(opened.body.iter().filter_map(|b| b.as_para()).map(|p| p.text.as_str()).collect::<Vec<_>>(), ["Private notes."]);
}
#[test]
fn an_odt_template_opens_and_saves_as_a_template_with_a_password_too() {
let doc = io::open_bytes("plain.txt", b"Letterhead.").unwrap();
let ott = io::save_bytes("letter.ott", &doc).unwrap();
let mut z = craft_zip::Archive::open(&ott, craft_zip::Limits { max_entries: 10_000, max_total_bytes: 64 << 20 }).unwrap();
let mime = String::from_utf8(z.read("mimetype", 16 << 20).unwrap().unwrap()).unwrap();
assert_eq!(mime, wordcraft_formats::odt::TEMPLATE_MIME);
let opened = io::open_bytes("letter.ott", &ott).unwrap();
assert_eq!(opened.body.iter().filter_map(|b| b.as_para()).map(|p| p.text.as_str()).collect::<Vec<_>>(), ["Letterhead."]);
// With a password.
assert!(io::can_encrypt("letter.ott"));
let enc = io::save_bytes_with("letter.ott", &doc, Some("pw"), &Default::default()).unwrap();
assert!(wordcraft_formats::odt::is_encrypted(&enc));
let (back, encrypted) = io::open_bytes_with("letter.ott", &enc, Some("pw")).unwrap();
assert!(encrypted && back.body.iter().filter_map(|b| b.as_para()).any(|p| p.text == "Letterhead."));
assert!(io::OPEN_EXTS.contains(&"ott") && io::SAVE_EXTS.contains(&"ott"));
}