Skip to content

Commit

Permalink
adding unit tests for bgp::Prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteJ committed Nov 23, 2024
1 parent 2cb2373 commit 1f4375d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
4 changes: 1 addition & 3 deletions src/bgp.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod bgp;
36 changes: 36 additions & 0 deletions tests/bgp_tests.rs
Original file line number Diff line number Diff line change
@@ -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<Prefix, _> = "invalid_prefix".parse();
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "Invalid prefix format. Expected <IP>/<prefix_length>");
}

#[test]
fn test_invalid_prefix_length_ipv4() {
let result: Result<Prefix, _> = "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<Prefix, _> = "2001:db8::/129".parse();
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "IPv6 prefix length cannot exceed 128");
}

0 comments on commit 1f4375d

Please sign in to comment.