From b8e4ff21b0a23c09e8d400b73db4c474cbeb216f Mon Sep 17 00:00:00 2001 From: Luke Else Date: Tue, 2 May 2023 07:25:05 +0100 Subject: [PATCH] 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 --- src/main.rs | 1 - src/networking/mod.rs | 18 ++++++++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index d34b067..c131c3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,6 @@ use crate::networking::Network; mod networking; -#[allow(unused_variables, unused_mut, unused_assignments)] fn main() { println!("Enter the IP and cidr for your given network"); diff --git a/src/networking/mod.rs b/src/networking/mod.rs index c01c42c..d6f5ccb 100644 --- a/src/networking/mod.rs +++ b/src/networking/mod.rs @@ -58,17 +58,12 @@ impl Network { //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 cidr <= network_class as u8 { + if cidr < network_class as u8 { return Err(NetworkingErr::InvalidCIDRErr) } 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 let mut base_network = network_address.clone(); let most_sig_octet: u8 = match network_class { @@ -239,5 +234,16 @@ mod tests { use super::*; 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)); } } \ No newline at end of file