Skip to main content

keepass/db/
mod.rs

1//! Types for representing data contained in a KeePass database
2pub mod fields;
3
4mod open;
5mod types;
6
7#[cfg(feature = "_merge")]
8mod merge;
9
10#[cfg(feature = "totp")]
11mod otp;
12
13#[cfg(feature = "save_kdbx4")]
14mod save;
15
16#[cfg(feature = "save_kdbx4")]
17pub use crate::db::save::DatabaseSaveError;
18
19pub use crate::db::{
20    open::{DatabaseFormatError, DatabaseOpenError},
21    types::*,
22};
23
24#[cfg(feature = "totp")]
25pub use crate::db::otp::{TOTPAlgorithm, TOTPError, TOTP};
26
27#[cfg(test)]
28mod database_tests {
29    use std::fs::File;
30
31    use crate::{db::DatabaseOpenError, Database, DatabaseKey};
32
33    #[test]
34    fn test_xml() -> Result<(), DatabaseOpenError> {
35        let xml = Database::get_xml(
36            &mut File::open("tests/resources/test_db_with_password.kdbx")?,
37            DatabaseKey::new().with_password("demopass"),
38        )?;
39
40        assert!(xml.len() > 100);
41
42        Ok(())
43    }
44
45    #[test]
46    fn test_open_invalid_version_header_size() {
47        assert!(Database::parse(&[], DatabaseKey::new().with_password("testing")).is_err());
48        assert!(Database::parse(
49            &[0, 0, 0, 0, 0, 0, 0, 0],
50            DatabaseKey::new().with_password("testing")
51        )
52        .is_err());
53        assert!(Database::parse(
54            &[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
55            DatabaseKey::new().with_password("testing")
56        )
57        .is_err());
58    }
59
60    #[cfg(feature = "save_kdbx4")]
61    #[test]
62    fn test_save() {
63        use crate::{db::Entry, format::variant_dictionary::VariantDictionary};
64        let mut db = Database::new(Default::default());
65
66        let mut public_custom_data = VariantDictionary::new();
67        public_custom_data.set("example", 42);
68
69        db.config.public_custom_data = Some(public_custom_data);
70
71        db.root.entries.push(Entry::new());
72        db.root.entries.push(Entry::new());
73        db.root.entries.push(Entry::new());
74
75        let mut buffer = Vec::new();
76
77        db.save(&mut buffer, DatabaseKey::new().with_password("testing"))
78            .unwrap();
79
80        let db_loaded = Database::open(
81            &mut buffer.as_slice(),
82            DatabaseKey::new().with_password("testing"),
83        )
84        .unwrap();
85
86        assert_eq!(db, db_loaded);
87    }
88}