diff --git a/src/bgp.rs b/src/bgp.rs index 09313be..d6ad347 100644 --- a/src/bgp.rs +++ b/src/bgp.rs @@ -1,17 +1,15 @@ use std::fmt; use std::net::{Ipv4Addr, Ipv6Addr, TcpStream}; -use std::rc::Rc; use std::sync::RwLock; use log::*; use zettabgp::prelude::*; use std::str::FromStr; -use thiserror::Error; use tokio::net::TcpSocket; use std::io::{Read, Write}; const BGP_PORT: u16 = 179; -#[derive(Debug)] +#[derive(Debug, PartialEq)] pub enum Prefix { V4(Ipv4Addr, u8), // Holds an IPv4 address and prefix length V6(Ipv6Addr, u8), // Holds an IPv6 address and prefix length diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ace076b --- /dev/null +++ b/src/lib.rs @@ -0,0 +1 @@ +pub mod bgp; \ No newline at end of file diff --git a/tests/bgp_tests.rs b/tests/bgp_tests.rs new file mode 100644 index 0000000..4cf4cd0 --- /dev/null +++ b/tests/bgp_tests.rs @@ -0,0 +1,36 @@ +use febgp::bgp::*; + +#[test] +fn test_ipv4_prefix() { + let prefix: Prefix = "192.168.1.0/24".parse().unwrap(); + assert_eq!(prefix, Prefix::V4("192.168.1.0".parse().unwrap(), 24)); + assert_eq!(prefix.to_string(), "192.168.1.0/24"); +} + +#[test] +fn test_ipv6_prefix() { + let prefix: Prefix = "2001:db8::/32".parse().unwrap(); + assert_eq!(prefix, Prefix::V6("2001:db8::".parse().unwrap(), 32)); + assert_eq!(prefix.to_string(), "2001:db8::/32"); +} + +#[test] +fn test_invalid_prefix_format() { + let result: Result = "invalid_prefix".parse(); + assert!(result.is_err()); + assert_eq!(result.unwrap_err(), "Invalid prefix format. Expected /"); +} + +#[test] +fn test_invalid_prefix_length_ipv4() { + let result: Result = "192.168.1.0/33".parse(); + assert!(result.is_err()); + assert_eq!(result.unwrap_err(), "IPv4 prefix length cannot exceed 32"); +} + +#[test] +fn test_invalid_prefix_length_ipv6() { + let result: Result = "2001:db8::/129".parse(); + assert!(result.is_err()); + assert_eq!(result.unwrap_err(), "IPv6 prefix length cannot exceed 128"); +}