Skip to content

Commit 44131e7

Browse files
elbaxdjoalopez1206
authored andcommitted
test(nameserver.rs): added test to search_zone and added some comments
1 parent cf7701e commit 44131e7

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/nameserver.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,59 @@ impl NameServer {
3434

3535
Ok(NameServer { zones })
3636
}
37+
38+
/// Searches for a zone by its domain name
39+
///
40+
/// # Examples
41+
///
42+
/// ```
43+
/// let name_server = NameServer::new("masterfile.txt").unwrap();
44+
/// let domain = DomainName::new_from_str("example.com.".to_string());
45+
/// let zone = name_server.search_zone(&domain);
46+
///
47+
/// assert!(zone.is_some());
48+
/// ```
49+
pub fn search_zone(&self, domain: &DomainName) -> Option<&DnsZone> {
50+
self.zones.get(&domain.to_string())
51+
}
52+
3753
/// Adds a new zone to the NameServer
54+
///
55+
/// # Examples
56+
///
57+
/// ```
58+
/// let mut name_server = NameServer::new("masterfile.txt").unwrap();
59+
/// let domain = DomainName::new_from_str("example.com.".to_string());
60+
/// let zone = DnsZone::new("example.com.", 3600, SoaRdata::new());
61+
/// name_server.add_zone(zone);
62+
///
63+
/// assert!(name_server.zones.contains_key(&domain));
64+
/// ```
3865
pub fn add_zone(&mut self, zone: Zone) {
3966
self.zones.insert(zone.domain.clone(), zone);
4067
}
4168
/// Removes a zone by its domain name
69+
///
70+
/// # Examples
71+
///
72+
/// ```
73+
/// let mut name_server = NameServer::new("masterfile.txt").unwrap();
74+
/// let domain = DomainName::new_from_str("example.com.".to_string());
75+
/// let zone = DnsZone::new("example.com.", 3600, SoaRdata::new());
76+
/// name_server.add_zone(zone);
77+
///
78+
/// let removed = name_server.remove_zone("example.com.");
79+
/// assert!(removed);
80+
///
81+
/// assert!(!name_server.get_zones().contains_key(&domain));
82+
/// ```
4283
pub fn remove_zone(&mut self, domain: &str) -> bool {
4384
self.zones.remove(domain).is_some()
4485
}
86+
}
87+
88+
/// Getters
89+
impl NameServer {
4590
/// Lists the domains managed by this server
4691
pub fn get_list_zones(&self) -> Vec<String> {
4792
self.zones.keys().cloned().collect()
@@ -75,6 +120,39 @@ mod test_name_server {
75120
assert!(name_server.zones.contains_key(&domain_name));
76121
}
77122

123+
#[test]
124+
fn test_search_zone(){
125+
let mut name_server = NameServer {
126+
zones: HashMap::new(),
127+
};
128+
129+
// Create the SOA RData for the zone
130+
let mut soa_data = SoaRdata::new();
131+
soa_data.set_name_server(DomainName::new_from_str("ns1.example.com.".to_string()));
132+
soa_data.set_responsible_person(DomainName::new_from_str("admin.example.com.".to_string()));
133+
soa_data.set_serial(20240101);
134+
soa_data.set_refresh(3600);
135+
soa_data.set_retry(1800);
136+
soa_data.set_expire(1209600);
137+
soa_data.set_minimum(3600);
138+
139+
// Create and add a zone
140+
let zone = DnsZone::new(
141+
"example.com.",
142+
3600,
143+
soa_data,
144+
);
145+
146+
name_server.add_zone(zone);
147+
148+
// Search for the zone by its domain name
149+
let domain = DomainName::new_from_str("example.com.".to_string());
150+
let found_zone = name_server.search_zone(&domain);
151+
152+
// Validate that the zone was found
153+
assert!(found_zone.is_some());
154+
}
155+
78156
#[test]
79157
fn test_add_zone() {
80158
let mut name_server = NameServer {

0 commit comments

Comments
 (0)