Skip to content

Commit

Permalink
test: added test to nameserver
Browse files Browse the repository at this point in the history
  • Loading branch information
elbaxd committed Jan 12, 2025
1 parent e9c8a5d commit e775f65
Showing 1 changed file with 164 additions and 1 deletion.
165 changes: 164 additions & 1 deletion src/nameserver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
pub mod DnsZone;
use std::collections::HashMap;
use std::io;
use crate::zones::DnsZone;
use crate::message::domain_name::DomainName;


/// Structure to represent a Name Server
#[derive (PartialEq, Debug)]
Expand Down Expand Up @@ -46,3 +50,162 @@ impl NameServer {
self.zones.len()
}
}

#[cfg(test)]
mod test_name_server {
use crate::zones::DnsZone;
use crate::message::domain_name::DomainName;
use std::path::Path;
use crate::message::resource_record::ResourceRecord;

#[test]
fn test_new() {
// Masterfile path to initialize the NameServer
let masterfile_path = "1034-scenario-6.1-edu.txt";

// Verify that the file exists before continuing
assert!(Path::new(masterfile_path).exists(), "Masterfile not found.");

// Create a NameServer from the masterfile
let name_server = NameServer::new(masterfile_path).unwrap();

// Validate that the zone was added correctly
let domain_name = DomainName::new_from_str("EDU.".to_string());
assert!(name_server.zones.contains_key(&domain_name));
}

#[test]
fn test_add_zone() {
let mut name_server = NameServer {
zones: HashMap::new(),
};

// Create a new zone to add
let zone = DnsZone::new(
"example.com.",
3600,
SoaRdata {
mname: DomainName::new_from_str("ns1.example.com.".to_string()),
rname: DomainName::new_from_str("admin.example.com.".to_string()),
serial: 20240101,
refresh: 3600,
retry: 1800,
expire: 1209600,
minimum: 3600,
},
);

// Add the zone to the server
name_server.add_zone(zone.clone());

// Validate that the zone was added correctly
assert!(name_server.zones.contains_key(&DomainName::new_from_str("example.com.".to_string())));
}

#[test]
fn test_remove_zone() {
let mut name_server = NameServer {
zones: HashMap::new(),
};

// Create and add a zone
let zone = DnsZone::new(
"example.com.",
3600,
SoaRdata {
mname: DomainName::new_from_str("ns1.example.com.".to_string()),
rname: DomainName::new_from_str("admin.example.com.".to_string()),
serial: 20240101,
refresh: 3600,
retry: 1800,
expire: 1209600,
minimum: 3600,
},
);

name_server.add_zone(zone);

// Remove the zone by its domain name
let removed = name_server.remove_zone("example.com.");
assert!(removed);

// Verify that the zone was removed
assert!(!name_server.zones.contains_key(&DomainName::new_from_str("example.com.".to_string())));
}

#[test]
fn test_get_list_zones() {
let mut name_server = NameServer {
zones: HashMap::new(),
};

// Create and add two zones
let zone1 = DnsZone::new(
"example.com.",
3600,
SoaRdata {
mname: DomainName::new_from_str("ns1.example.com.".to_string()),
rname: DomainName::new_from_str("admin.example.com.".to_string()),
serial: 20240101,
refresh: 3600,
retry: 1800,
expire: 1209600,
minimum: 3600,
},
);

let zone2 = DnsZone::new(
"example.org.",
3600,
SoaRdata {
mname: DomainName::new_from_str("ns1.example.org.".to_string()),
rname: DomainName::new_from_str("admin.example.org.".to_string()),
serial: 20240102,
refresh: 3600,
retry: 1800,
expire: 1209600,
minimum: 3600,
},
);

name_server.add_zone(zone1);
name_server.add_zone(zone2);

// Get the list of zones
let zone_list = name_server.get_list_zones();

// Validate that it contains both zones
assert!(zone_list.contains(&"example.com.".to_string()));
assert!(zone_list.contains(&"example.org.".to_string()));
assert_eq!(zone_list.len(), 2);
}

#[test]
fn test_get_zone_count() {
let mut name_server = NameServer {
zones: HashMap::new(),
};

// Validate that initially there are no zones
assert_eq!(name_server.get_zone_count(), 0);

// Add a zone and validate the count
let zone = DnsZone::new(
"example.com.",
3600,
SoaRdata {
mname: DomainName::new_from_str("ns1.example.com.".to_string()),
rname: DomainName::new_from_str("admin.example.com.".to_string()),
serial: 20240101,
refresh: 3600,
retry: 1800,
expire: 1209600,
minimum: 3600,
},
);

name_server.add_zone(zone);
assert_eq!(name_server.get_zone_count(), 1);
}
}

0 comments on commit e775f65

Please sign in to comment.