Skip to content

Commit

Permalink
Reorganize unit tests (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
zarvd authored Aug 22, 2024
1 parent 1437301 commit 4cf9262
Show file tree
Hide file tree
Showing 12 changed files with 632 additions and 519 deletions.
14 changes: 13 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,17 @@ name = "routing_table"
required-features = ["routing-table"]

[[test]]
name = "routing_table"
name = "routing_table_insert"
required-features = ["routing-table"]

[[test]]
name = "routing_table_len"
required-features = ["routing-table"]

[[test]]
name = "routing_table_list_matched"
required-features = ["routing-table"]

[[test]]
name = "routing_table_match_longest"
required-features = ["routing-table"]
140 changes: 6 additions & 134 deletions src/cidr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ impl Ipv4Cidr {
if bits == 0 {
return 0;
}
if bits > 32 {
panic!("bits must be <= 32");
}
u32::MAX << (32 - bits)
}

Expand Down Expand Up @@ -432,6 +435,9 @@ impl Ipv6Cidr {
if bits == 0 {
return 0;
}
if bits > 128 {
panic!("bits must be <= 128");
}
u128::MAX << (128 - bits)
}

Expand Down Expand Up @@ -1140,137 +1146,3 @@ impl Iterator for Hosts {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_ipv4_mask() {
let tests = [
(0, [0, 0, 0, 0]),
(1, [0x80, 0, 0, 0]),
(2, [0xc0, 0, 0, 0]),
(3, [0xe0, 0, 0, 0]),
(4, [0xf0, 0, 0, 0]),
(5, [0xf8, 0, 0, 0]),
(6, [0xfc, 0, 0, 0]),
(7, [0xfe, 0, 0, 0]),
(8, [0xff, 0, 0, 0]),
(16, [0xff, 0xff, 0, 0]),
(20, [0xff, 0xff, 0xf0, 0]),
(24, [0xff, 0xff, 0xff, 0]),
(32, [0xff, 0xff, 0xff, 0xff]),
];

for (bit, expected) in tests {
let expected = u32::from_be_bytes(expected);
let actual = Ipv4Cidr::mask_of(bit);
assert_eq!(actual, expected);
}
}

#[test]
#[should_panic]
fn test_ipv4_mask_overflow() {
Ipv4Cidr::mask_of(33);
}

#[test]
fn test_ipv6_mask() {
let tests = [
(0, [0x00; 16]),
(
1,
[
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
],
),
(
2,
[
0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
],
),
(
3,
[
0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00,
],
),
(128, [0xff; 16]),
];

for (bit, expected) in tests {
let expected = u128::from_be_bytes(expected);
let actual = Ipv6Cidr::mask_of(bit);
assert_eq!(actual, expected);
}
}

#[test]
#[should_panic]
fn test_ipv6_mask_overflow() {
Ipv6Cidr::mask_of(129);
}

#[test]
fn test_ipv4_cidr_contains() {
{
let ret = Ipv4Cidr::new([192, 168, 0, 1], 0);
assert!(ret.is_ok());
let cidr = ret.unwrap();
assert_eq!(cidr.bits(), 0);
assert!(cidr.contains(Ipv4Addr::new(192, 168, 0, 0)));
assert!(cidr.contains(Ipv4Addr::new(10, 10, 0, 0)));
assert!(cidr.contains(Ipv4Addr::new(172, 0, 0, 0)));
assert!(cidr.contains(Ipv4Addr::new(0, 0, 0, 0)));
}
{
let ret = Ipv4Cidr::new([192, 168, 0, 1], 24);
assert!(ret.is_ok());
let cidr = ret.unwrap();
assert_eq!(cidr.bits(), 24);
for i in 0..=255 {
assert!(cidr.contains(Ipv4Addr::new(192, 168, 0, i)));
}
assert!(!cidr.contains(Ipv4Addr::new(192, 168, 1, 0)));
assert!(!cidr.contains(Ipv4Addr::new(192, 168, 2, 0)));
assert!(!cidr.contains(Ipv4Addr::new(192, 168, 1, 255)));
}

{
let ret = Ipv4Cidr::new([192, 168, 24, 1], 24);
assert!(ret.is_ok());
let cidr = ret.unwrap();
assert_eq!(cidr.bits(), 24);
for i in 0..=255 {
assert!(cidr.contains(Ipv4Addr::new(192, 168, 24, i)));
}
assert!(!cidr.contains(Ipv4Addr::new(192, 168, 23, 0)));
assert!(!cidr.contains(Ipv4Addr::new(192, 168, 25, 255)));
assert!(!cidr.contains(Ipv4Addr::new(192, 0, 0, 0)));
assert!(!cidr.contains(Ipv4Addr::new(192, 167, 255, 255)));
}

{
let ret = Ipv4Cidr::new([192, 168, 24, 1], 16);
assert!(ret.is_ok());
let cidr = ret.unwrap();
assert_eq!(cidr.bits(), 16);
}
}

#[test]
fn test_cidr_v6() {
{
let ret = Ipv6Cidr::from_str("::/0");
assert!(ret.is_ok());
let cidr = ret.unwrap();
assert_eq!(cidr.bits(), 0);
}
}
}
132 changes: 0 additions & 132 deletions tests/cidr.rs

This file was deleted.

39 changes: 39 additions & 0 deletions tests/cidr_contains.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use core::net::{Ipv4Addr, Ipv6Addr};

use cidrs::{Ipv4Cidr, Ipv6Cidr};
#[test]
fn ipv4_basic() {
let cidr = Ipv4Cidr::new([192, 168, 0, 0], 24).unwrap();

// Test addresses within the CIDR block
assert!(cidr.contains(Ipv4Addr::new(192, 168, 0, 1)));
assert!(cidr.contains(Ipv4Addr::new(192, 168, 0, 254)));

// Test the network address and broadcast address
assert!(cidr.contains(Ipv4Addr::new(192, 168, 0, 0)));
assert!(cidr.contains(Ipv4Addr::new(192, 168, 0, 255)));

// Test addresses outside the CIDR block
assert!(!cidr.contains(Ipv4Addr::new(192, 168, 1, 0)));
assert!(!cidr.contains(Ipv4Addr::new(192, 167, 255, 255)));
}

#[test]
fn ipv6_basic() {
let cidr = Ipv6Cidr::new([0x2001, 0xdb8, 0, 0, 0, 0, 0, 0], 48).unwrap();

// Test addresses within the CIDR block
assert!(cidr.contains(Ipv6Addr::new(0x2001, 0xdb8, 0, 1, 0, 0, 0, 1)));
assert!(cidr.contains(Ipv6Addr::new(
0x2001, 0xdb8, 0, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
)));

// Test the network address
assert!(cidr.contains(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)));

// Test addresses outside the CIDR block
assert!(!cidr.contains(Ipv6Addr::new(0x2001, 0xdb9, 0, 0, 0, 0, 0, 0)));
assert!(!cidr.contains(Ipv6Addr::new(
0x2001, 0xdb7, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
)));
}
Loading

0 comments on commit 4cf9262

Please sign in to comment.