Further itterations on create_subnet function and relevant tests

This commit is contained in:
Luke Else 2023-04-26 21:44:13 +01:00
parent 6fafa97d61
commit cf35765cb4

View File

@ -1,8 +1,6 @@
use std::{str::FromStr, f32::consts::E}; use std::str::FromStr;
use ip::IpAddr; use ip::IpAddr;
use self::ip::InvalidIPErr;
pub mod ip; pub mod ip;
#[allow(dead_code)] #[allow(dead_code)]
@ -15,7 +13,7 @@ pub enum NetworkingErr {
} }
/// enum to allow the identification of the class of the network /// enum to allow the identification of the class of the network
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum NetworkClass { enum NetworkClass {
A = 8, A = 8,
B = 16, B = 16,
@ -23,6 +21,7 @@ enum NetworkClass {
} }
#[allow(unused)] #[allow(unused)]
#[derive(Debug)]
pub struct Network { pub struct Network {
network_address: IpAddr, network_address: IpAddr,
broadcast_addr: IpAddr, broadcast_addr: IpAddr,
@ -54,10 +53,20 @@ impl Network {
/// ``` /// ```
pub fn create_subnet(network_address: &IpAddr, cidr: u8) -> Result<Vec<Network>, NetworkingErr> { pub fn create_subnet(network_address: &IpAddr, cidr: u8) -> Result<Vec<Network>, NetworkingErr> {
//Get number of host and network bits. //Get number of host and network bits.
let subnet_mask = Network::gen_subnet_mask(cidr)?.to_arr()?; let network_class = Network::get_network_class(network_address)?;
let network_address = network_address.to_arr()?; let network_address = network_address.to_arr()?;
let subnet_mask = Network::gen_subnet_mask(cidr)?.to_arr()?;
// Determine the network class that the given address is in. // If the CIDR < class network enum associated value, then there is an invalid subnet.
if cidr <= network_class as u8 {
return Err(NetworkingErr::InvalidCIDRErr)
}
let num_borrowed_bits = cidr - network_class as u8;
// Determine the starting network
// Determine the networking gaps
// Determine how many networks available
let networks = vec![]; let networks = vec![];
Ok(networks) Ok(networks)
@ -184,8 +193,9 @@ mod tests {
} }
#[test] #[test]
fn test_network_class() { fn network_class_test() {
use super::*; use super::*;
assert_eq!(Network::get_network_class(&IpAddr::V4(127, 0, 0, 1)).unwrap(), NetworkClass::A); assert_eq!(Network::get_network_class(&IpAddr::V4(127, 0, 0, 1)).unwrap(), NetworkClass::A);
assert_eq!(Network::get_network_class(&IpAddr::V4(172, 6, 8, 10)).unwrap(), NetworkClass::B); assert_eq!(Network::get_network_class(&IpAddr::V4(172, 6, 8, 10)).unwrap(), NetworkClass::B);
assert_eq!(Network::get_network_class(&IpAddr::V4(192, 168, 0, 1)).unwrap(), NetworkClass::C); assert_eq!(Network::get_network_class(&IpAddr::V4(192, 168, 0, 1)).unwrap(), NetworkClass::C);
@ -195,4 +205,11 @@ mod tests {
assert_eq!(Network::get_network_class(&IpAddr::V4(0, 0, 0, 0)).unwrap_err(), NetworkingErr::InvalidIPErr); assert_eq!(Network::get_network_class(&IpAddr::V4(0, 0, 0, 0)).unwrap_err(), NetworkingErr::InvalidIPErr);
assert_eq!(Network::get_network_class(&IpAddr::V4(0, 0, 0, 1)).unwrap_err(), NetworkingErr::InvalidIPErr); assert_eq!(Network::get_network_class(&IpAddr::V4(0, 0, 0, 1)).unwrap_err(), NetworkingErr::InvalidIPErr);
} }
#[test]
fn subnet_creation_test() {
use super::*;
assert_eq!(Network::create_subnet(&IpAddr::V4(192, 168, 0, 1), 23).unwrap_err(), NetworkingErr::InvalidCIDRErr);
}
} }