From 68fddc2de28773223a5524abd15cf366de9b3257 Mon Sep 17 00:00:00 2001 From: selfhoster1312 Date: Thu, 27 Aug 2026 21:40:03 +0200 Subject: [PATCH] test: Add Dn type tests --- src/dn.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/dn.rs b/src/dn.rs index d3793f3..58cfaeb 100644 --- a/src/dn.rs +++ b/src/dn.rs @@ -24,6 +24,10 @@ impl Dn { pub fn from_dn_str(input: &str) -> Result { let mut keys: IndexMap> = IndexMap::new(); + if input == "" { + return Ok(Self { keys }); + } + for query in input.split(',') { let mut query_parts = query.split('='); // Here we have key=val pairs @@ -98,3 +102,37 @@ impl Dn { self.keys.insert("dc".to_string(), domain_components); } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_basic_dn_str() { + let s = "cn=admin,ou=people,dc=example,dc=com"; + let mut dn = Dn::from_dn_str(s).unwrap(); + + assert_eq!(&dn.to_dn_string(), s); + assert_eq!(dn.get_hostname().as_deref(), Some("example.com")); + dn.set_hostname("a.localhost", false); + assert_eq!(dn.get_hostname().as_deref(), Some("a.localhost")); + assert_eq!(&dn.to_dn_string(), "cn=admin,ou=people,dc=a,dc=localhost"); + } + + #[test] + fn test_empty_dn_str() { + let s = ""; + let mut dn = Dn::from_dn_str(s).unwrap(); + + assert_eq!(&dn.to_dn_string(), s); + assert!(dn.get_hostname().is_none()); + + dn.set_hostname("a.localhost", false); + assert_eq!(dn.get_hostname().as_deref(), None); + assert_eq!(&dn.to_dn_string(), s); + + dn.set_hostname("a.localhost", true); + assert_eq!(dn.get_hostname().as_deref(), Some("a.localhost")); + assert_eq!(&dn.to_dn_string(), "dc=a,dc=localhost"); + } +}