Completed first draft of create_subnet() and added additional tests. NOTE: One test is still failing in order to outline the fact that there is still a slight bug in the code

This commit is contained in:
Luke Else 2023-05-02 07:25:05 +01:00
parent aad67836df
commit b8e4ff21b0
2 changed files with 12 additions and 7 deletions

View File

@ -5,7 +5,6 @@ use crate::networking::Network;
mod networking; mod networking;
#[allow(unused_variables, unused_mut, unused_assignments)]
fn main() { fn main() {
println!("Enter the IP and cidr for your given network"); println!("Enter the IP and cidr for your given network");

View File

@ -58,17 +58,12 @@ impl Network {
//let subnet_mask = Network::gen_subnet_mask(cidr)?.to_arr()?; //let subnet_mask = Network::gen_subnet_mask(cidr)?.to_arr()?;
// If the CIDR < class network enum associated value, then there is an invalid subnet. // If the CIDR < class network enum associated value, then there is an invalid subnet.
if cidr <= network_class as u8 { if cidr < network_class as u8 {
return Err(NetworkingErr::InvalidCIDRErr) return Err(NetworkingErr::InvalidCIDRErr)
} }
let num_borrowed_bits = cidr - network_class as u8; let num_borrowed_bits = cidr - network_class as u8;
//If no subnetting is required, just return the base address
if num_borrowed_bits == 0 {
unimplemented!();
}
// Determine the starting network // Determine the starting network
let mut base_network = network_address.clone(); let mut base_network = network_address.clone();
let most_sig_octet: u8 = match network_class { let most_sig_octet: u8 = match network_class {
@ -239,5 +234,16 @@ mod tests {
use super::*; use super::*;
assert_eq!(Network::create_subnet(&IpAddr::V4(192, 168, 0, 1), 23).unwrap_err(), NetworkingErr::InvalidCIDRErr); assert_eq!(Network::create_subnet(&IpAddr::V4(192, 168, 0, 1), 23).unwrap_err(), NetworkingErr::InvalidCIDRErr);
let networks = Network::create_subnet(&IpAddr::V4(127, 0, 0, 1), 10).unwrap();
assert_eq!(networks.len(), 4);
let networks = Network::create_subnet(&IpAddr::V4(192, 168, 200, 1), 31).unwrap();
assert_eq!(networks.len(), 128);
assert_eq!(networks.last().unwrap(), &IpAddr::V4(192, 168, 200, 254));
let networks = Network::create_subnet(&IpAddr::V4(127, 0, 0, 0), 8).unwrap();
assert_eq!(networks.len(), 1);
assert_eq!(networks.last().unwrap(), &IpAddr::V4(127, 0, 0, 0));
} }
} }