Skip to content

Commit

Permalink
refactor: move param_test to own crate and refactor some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcsmith committed Jan 30, 2024
1 parent 62cfa68 commit 898680e
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 198 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"crates/scion",
"crates/scion-grpc",
"crates/scion-proto",
"crates/test-utils",
]

[workspace.dependencies]
Expand Down
1 change: 1 addition & 0 deletions crates/scion-proto/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ criterion = "0.5.1"
prost-types = { workspace = true }
rand = "0.8.5"
rand_xorshift = "0.3.0"
test-utils = { version = "0.1.0", path = "../test-utils" }

[[bench]]
name = "checksum"
Expand Down
134 changes: 54 additions & 80 deletions crates/scion-proto/src/address/asn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,92 +148,66 @@ impl FromStr for Asn {

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

mod conversion {
use super::*;

macro_rules! test_success {
($name:ident, $number:expr, $asn:expr) => {
#[test]
fn $name() {
assert_eq!(Asn::try_from($number).unwrap(), $asn);
assert_eq!(u64::from($asn), $number);
}
};
}
use test_utils::param_test;

test_success!(wildcard, 0, Asn::WILDCARD);
test_success!(max_value, 0xffff_ffff_ffff, Asn::MAX);
use super::*;

#[test]
fn out_of_range() {
assert_eq!(
Asn::try_from(Asn::MAX.to_u64() + 1).unwrap_err(),
AddressParseError(AddressKind::Asn)
);
}
param_test! {
converts_from_number: [
wildcard: (0, Ok(Asn::WILDCARD)),
max_value: (0xffff_ffff_ffff, Ok(Asn::MAX)),
out_of_range: (0xffff_ffff_ffff + 1, Err(AddressParseError(AddressKind::Asn)))
]
}

mod parse {
use super::*;

macro_rules! test_success {
($name:ident, $input:expr, $expected:expr) => {
#[test]
fn $name() {
assert_eq!(Asn::from_str($input).unwrap(), $expected);
}
};
}

test_success!(zero, "0", Asn::WILDCARD);
test_success!(zero_with_colon, "0:0:0", Asn::WILDCARD);
test_success!(low_bit, "0:0:1", Asn(1));
test_success!(high_bit, "1:0:0", Asn(0x000100000000));
test_success!(max, "ffff:ffff:ffff", Asn::MAX);
test_success!(bgp_asn, "65535", Asn(65535));

macro_rules! test_error {
($name:ident, $input:expr) => {
#[test]
fn $name() {
assert_eq!(
Asn::from_str($input).unwrap_err(),
AddressParseError(AddressKind::Asn)
);
}
};
}

test_error!(errs_large_decimal_format, "65536");
test_error!(errs_on_only_colon, ":");
test_error!(errs_extra_colon, "0:0:0:");
test_error!(errs_too_few, "0:0");
test_error!(errs_invalid_part, ":0:0");
test_error!(errs_out_of_range, "10000:0:0");
test_error!(errs_out_of_range2, "0:0:10000");
test_error!(errs_invalid_format, "0:0x0:0");
fn converts_from_number(numeric_value: u64, expected: Result<Asn, AddressParseError>) {
assert_eq!(Asn::try_from(numeric_value), expected);
}

mod display {
use super::*;
param_test! {
successfully_parses_valid_strings: [
zero: ("0", Asn::WILDCARD),
zero_with_colon: ("0:0:0", Asn::WILDCARD),
low_bit: ("0:0:1", Asn(1)),
high_bit: ("1:0:0", Asn(0x000100000000)),
max: ("ffff:ffff:ffff", Asn::MAX),
bgp_asn: ("65535", Asn(65535))
]
}
fn successfully_parses_valid_strings(asn_str: &str, expected: Asn) {
assert_eq!(Ok(expected), asn_str.parse());
}

macro_rules! test_display {
($name:ident, $asn:expr, $expected:expr) => {
#[test]
fn $name() {
assert_eq!($asn.to_string(), $expected);
}
};
}
param_test! {
parse_rejects_invalid_strings: [
large_decimal_format: ("65536"),
only_colon: (":"),
extra_colon: ("0:0:0:"),
too_few: ("0:0"),
invalid_part: (":0:0"),
out_of_range: ("10000:0:0"),
out_of_range2: ("0:0:10000"),
invalid_format: ("0:0x0:0"),
]
}
fn parse_rejects_invalid_strings(asn_str: &str) {
assert_eq!(
Asn::from_str(asn_str),
Err(AddressParseError(AddressKind::Asn))
);
}

test_display!(large, Asn(0xff00000000ab), "ff00:0:ab");
test_display!(large_symmetric, Asn(0x0001fcd10001), "1:fcd1:1");
test_display!(max, Asn::MAX, "ffff:ffff:ffff");
test_display!(wildcard, Asn(0), "0");
test_display!(bgp_asn, Asn(1), "1");
test_display!(bgp_asn_max, Asn(65535), "65535");
test_display!(outside_bgp_asn, Asn(65536), "0:1:0");
param_test! {
correctly_displays_asn: [
large: (Asn(0xff00000000ab), "ff00:0:ab"),
large_symmetric: (Asn(0x0001fcd10001), "1:fcd1:1"),
max: (Asn::MAX, "ffff:ffff:ffff"),
wildcard: (Asn(0), "0"),
bgp_asn: (Asn(1), "1"),
bgp_asn_max: (Asn(65535), "65535"),
outside_bgp_asn: (Asn(65536), "0:1:0"),
]
}
fn correctly_displays_asn(asn: Asn, expected: &str) {
assert_eq!(asn.to_string(), expected);
}
}
191 changes: 73 additions & 118 deletions crates/scion-proto/src/address/ia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,138 +162,93 @@ impl From<u64> for IsdAsn {

#[cfg(test)]
mod tests {
use test_utils::param_test;

use super::*;
use crate::address::{Asn, Isd};

macro_rules! test_new_and_get {
($name:ident, $ia:expr, $isd:expr, $asn:expr) => {
mod $name {
use super::*;

#[test]
fn construct() {
assert_eq!($ia, IsdAsn::new($isd, $asn));
}

#[test]
fn get_isd() {
assert_eq!($isd, $ia.isd());
}

#[test]
fn get_asn() {
assert_eq!($asn, $ia.asn());
}
}
};
param_test! {
constructs_from_parts: [
arbitrary: (Isd::new(1), Asn::new(0xff00_0000_00ab), IsdAsn(0x1_ff00_0000_00ab)),
wildcard: (Isd::WILDCARD, Asn::WILDCARD, IsdAsn::WILDCARD),
]
}

test_new_and_get!(wildcard, IsdAsn(0), Isd::new(0), Asn::new(0));
test_new_and_get!(
long,
IsdAsn(0x0001_ff00_0000_00ab),
Isd::new(1),
Asn::new(0xff00_0000_00ab)
);
test_new_and_get!(
max_and_min,
IsdAsn(0xffff_0000_0000_0000),
Isd::new(0xffff),
Asn::new(0)
);
test_new_and_get!(
min_and_max,
IsdAsn(0x0000_ffff_ffff_ffff),
Isd::new(0),
Asn::new(0xffff_ffff_ffff)
);

mod conversion {
use super::*;

#[test]
fn as_u64() {
assert_eq!(
IsdAsn::new(Isd::new(0x0123), Asn::new(0x4567_89ab_cdef)).to_u64(),
0x0123_4567_89ab_cdef
)
}

macro_rules! test_success {
($name:ident, $number:expr, $ia:expr) => {
#[test]
fn $name() {
assert_eq!(IsdAsn::from($number), $ia);
assert_eq!(u64::from($ia), $number);
}
};
}

test_success!(wildcard, 0, IsdAsn::new(Isd::WILDCARD, Asn::WILDCARD));
test_success!(max_value, -1_i64 as u64, IsdAsn(0xffff_ffff_ffff_ffff));
fn constructs_from_parts(isd: Isd, asn: Asn, expected: IsdAsn) {
assert_eq!(IsdAsn::new(isd, asn), expected);
}

mod display {
use super::*;
#[test]
fn gets_isd() {
assert_eq!(IsdAsn(0x2_ff00_0000_1101).isd(), Isd::new(2));
}

#[test]
fn debug() {
assert_eq!(
format!("{:?}", IsdAsn(0x0001_ff00_0000_00ab)),
"IA(0x0001ff00000000ab)"
);
}
#[test]
fn gets_asn() {
assert_eq!(IsdAsn(0x2_ff00_0000_1101).asn(), Asn::new(0xff00_0000_1101));
}

#[test]
fn simple() {
assert_eq!(IsdAsn(0x0001_ff00_0000_00ab).to_string(), "1-ff00:0:ab");
}
#[test]
fn to_u64() {
assert_eq!(
IsdAsn::new(Isd::new(0x0123), Asn::new(0x4567_89ab_cdef)).to_u64(),
0x0123_4567_89ab_cdef
)
}

#[test]
fn wildcard() {
assert_eq!(IsdAsn(0).to_string(), "0-0");
}
#[test]
fn correctly_formats_debug_repr() {
assert_eq!(
format!("{:?}", IsdAsn(0x0001_ff00_0000_00ab)),
"IA(0x0001ff00000000ab)"
);
}

#[test]
fn max_ia() {
assert_eq!(
IsdAsn(0xffff_ffff_ffff_ffff).to_string(),
"65535-ffff:ffff:ffff"
);
}
param_test! {
correctly_displays_ia: [
simple: (IsdAsn(0x0001_ff00_0000_00ab), "1-ff00:0:ab"),
wildcard: (IsdAsn::WILDCARD, "0-0"),
max_ia: (IsdAsn::MAX, "65535-ffff:ffff:ffff"),
]
}
fn correctly_displays_ia(ia: IsdAsn, expected: &str) {
assert_eq!(ia.to_string(), expected);
}

mod parse {
use super::*;

macro_rules! test_success {
($name:ident, $input:expr, $expected:expr) => {
#[test]
fn $name() {
assert_eq!(IsdAsn::from_str($input).unwrap(), $expected);
assert_eq!(IsdAsn::try_from($input.to_string()).unwrap(), $expected);
}
};
}
param_test! {
from_str_parses_valid_strings: [
wildcard: ("0-0", IsdAsn::WILDCARD),
max_ia: ("65535-ffff:ffff:ffff", IsdAsn::MAX),
min_non_wildcard: ("1-0:0:1", IsdAsn(0x0001_0000_0000_0001)),
]
}
fn from_str_parses_valid_strings(ia_str: &str, expected: IsdAsn) {
assert_eq!(IsdAsn::from_str(ia_str), Ok(expected));
}

test_success!(max, "65535-ffff:ffff:ffff", IsdAsn(0xffff_ffff_ffff_ffff));
test_success!(wildcard, "0-0", IsdAsn::WILDCARD);
test_success!(min_non_wildcard, "1-0:0:1", IsdAsn(0x0001_0000_0000_0001));
param_test! {
try_from_str_parses_valid_strings: [
wildcard: ("0-0", IsdAsn::WILDCARD),
max_ia: ("65535-ffff:ffff:ffff", IsdAsn::MAX),
min_non_wildcard: ("1-0:0:1", IsdAsn(0x0001_0000_0000_0001)),
]
}
fn try_from_str_parses_valid_strings(ia_str: &str, expected: IsdAsn) {
assert_eq!(IsdAsn::try_from(ia_str.to_string()), Ok(expected));
}

#[test]
fn invalid() {
assert_eq!(
IsdAsn::from_str("a-0:0:1").unwrap_err(),
AddressParseError(AddressKind::IsdAsn)
);
}
#[test]
fn invalid() {
assert_eq!(
IsdAsn::from_str("a-0:0:1").unwrap_err(),
AddressParseError(AddressKind::IsdAsn)
);
}

#[test]
fn invalid_parts() {
assert_eq!(
IsdAsn::from_str("1-1-0:0:1").unwrap_err(),
AddressParseError(AddressKind::IsdAsn)
);
}
#[test]
fn invalid_parts() {
assert_eq!(
IsdAsn::from_str("1-1-0:0:1").unwrap_err(),
AddressParseError(AddressKind::IsdAsn)
);
}
}
14 changes: 14 additions & 0 deletions crates/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "test-utils"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
publish = false

[dependencies]

[lints]
workspace = true

[dev-dependencies]
tokio = { version = "1.35.1", features = ["test-util", "tokio-macros", "macros"] }
Loading

0 comments on commit 898680e

Please sign in to comment.