Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / passwords / src / main / rust / tests / vault_edge_cases.rs

vault_edge_cases.rs71 lines · 2.9 KiBRaw
1//! Vault edge cases for the KDBX read/write core.
2//!
3//! Extends `src/tests.rs` without duplicating it: that suite already covers one wrong password,
4//! one two-level nesting shape, one malformed blob and the keepassjava2 vector. These tests add
5//! a trailing-space password variant, header corruption plus truncation, an empty vault, unicode
6//! fields, and sibling-group flattening.
7
8use keepass::db::{fields, Database};
9use keepass::DatabaseKey;
10use passwords_kdbx::{export_kdbx, import_kdbx};
11
12const PW: &str = "correct horse battery staple";
13
14fn single_entry_json(title: &str, password: &str) -> String {
15 format!(r#"[{{"Title":"{title}","Password":"{password}"}}]"#)
16}
17
18#[test]
19fn wrong_password_with_trailing_space_fails() {
20 let bytes = export_kdbx(PW, &single_entry_json("x", "p")).expect("export");
21 assert!(import_kdbx(&format!("{PW} "), &bytes).is_none());
22 assert!(import_kdbx("", &bytes).is_none());
23}
24
25#[test]
26fn corrupt_header_and_truncation_are_rejected() {
27 let bytes = export_kdbx(PW, &single_entry_json("x", "p")).expect("export");
28 let mut bad_magic = bytes.clone();
29 bad_magic[0] ^= 0xFF;
30 assert!(import_kdbx(PW, &bad_magic).is_none());
31 let mut bad_tail = bytes.clone();
32 let last = bad_tail.len() - 1;
33 bad_tail[last] ^= 0xFF;
34 assert!(import_kdbx(PW, &bad_tail).is_none());
35 let half = bytes.len() / 2;
36 assert!(import_kdbx(PW, &bytes[..half]).is_none());
37 assert!(import_kdbx(PW, &[]).is_none());
38}
39
40#[test]
41fn empty_vault_round_trips_to_no_entries() {
42 let bytes = export_kdbx(PW, "[]").expect("export empty");
43 let out = import_kdbx(PW, &bytes).expect("import empty");
44 assert_eq!(out, "[]");
45}
46
47#[test]
48fn unicode_fields_survive_round_trip() {
49 let json = r#"[{"Title":"café ☕ 日本語 🎉","Password":"pässwörd🔑","UserName":"用户"}]"#;
50 let bytes = export_kdbx(PW, json).expect("export");
51 let out = import_kdbx(PW, &bytes).expect("import");
52 assert!(out.contains("café ☕ 日本語 🎉"), "title survived: {out}");
53 assert!(out.contains("pässwörd🔑"), "password survived: {out}");
54 assert!(out.contains("用户"), "username survived: {out}");
55}
56
57#[test]
58fn entries_in_sibling_groups_are_flattened() {
59 let mut db = Database::new();
60 db.root_mut().add_entry().set_unprotected(fields::TITLE, "Root");
61 let a_id = db.root_mut().add_group().edit(|g| g.name = "Alpha".into()).id();
62 let b_id = db.root_mut().add_group().edit(|g| g.name = "Beta".into()).id();
63 db.group_mut(a_id).unwrap().add_entry().set_unprotected(fields::TITLE, "InAlpha");
64 db.group_mut(b_id).unwrap().add_entry().set_unprotected(fields::TITLE, "InBeta");
65 let mut bytes = Vec::new();
66 db.save(&mut bytes, DatabaseKey::new().with_password(PW)).unwrap();
67 let out = import_kdbx(PW, &bytes).expect("import");
68 for title in ["Root", "InAlpha", "InBeta"] {
69 assert!(out.contains(title), "missing {title}: {out}");
70 }
71}