Skip to content

Commit

Permalink
test: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mlegner committed Sep 25, 2023
1 parent 034a1d1 commit 0d617f3
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 6 deletions.
33 changes: 33 additions & 0 deletions crates/scion/src/address/asn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ impl Asn {
}

/// Return true for the special 'wildcard' AS number, 0.
///
/// # Examples
///
/// ```
/// # use scion::address::Asn;
/// assert!(Asn::WILDCARD.is_wildcard());
/// assert!(!Asn::new(1).is_wildcard());
/// ```
pub fn is_wildcard(&self) -> bool {
self == &Self::WILDCARD
}
Expand Down Expand Up @@ -120,6 +128,31 @@ impl FromStr for Asn {
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);
}
};
}

test_success!(wildcard, 0, Asn::WILDCARD);
test_success!(max_value, Asn::MAX_VALUE, Asn(0xffff_ffff_ffff));

#[test]
fn out_of_range() {
assert_eq!(
Asn::try_from(Asn::MAX_VALUE + 1).unwrap_err(),
AddressParseError::AsnOutOfRange
);
}
}

mod parse {
use super::*;

Expand Down
52 changes: 47 additions & 5 deletions crates/scion/src/address/ia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ impl IA {
}

/// Return true if either the ISD or AS numbers are wildcards
///
/// # Examples
///
/// ```
/// # use scion::address::{Asn,IA,Isd};
/// assert!(IA::new(Isd::WILDCARD,Asn::new(1)).is_wildcard());
/// assert!(IA::new(Isd::new(1),Asn::WILDCARD).is_wildcard());
/// assert!(!IA::new(Isd::new(1),Asn::new(1)).is_wildcard());
/// ```
pub fn is_wildcard(&self) -> bool {
self.isd().is_wildcard() || self.asn().is_wildcard()
}
Expand Down Expand Up @@ -66,11 +75,10 @@ impl FromStr for IA {
return Err(Self::Err::InvalidIaString(string.into()));
}

if let Some((isd_str, asn_str)) = string.split_once('-') {
Ok(IA::new(Isd::from_str(isd_str)?, Asn::from_str(asn_str)?))
} else {
Err(Self::Err::InvalidIaString(string.into()))
}
let (isd_str, asn_str) = string
.split_once('-')
.expect("already checked that the string contains exactly one '-'");
Ok(IA::new(Isd::from_str(isd_str)?, Asn::from_str(asn_str)?))
}
}

Expand Down Expand Up @@ -142,9 +150,42 @@ mod tests {
Asn::new(0xffff_ffff_ffff)
);

mod conversion {
use super::*;

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

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

test_success!(wildcard, 0, IA::new(Isd::WILDCARD, Asn::WILDCARD));
test_success!(max_value, -1_i64 as u64, IA(0xffff_ffff_ffff_ffff));
}

mod display {
use super::*;

#[test]
fn debug() {
assert_eq!(
format!("{:?}", IA(0x0001_ff00_0000_00ab)),
"IA(0x0001ff00000000ab)"
);
}

#[test]
fn simple() {
assert_eq!(IA(0x0001_ff00_0000_00ab).to_string(), "1-ff00:0:ab");
Expand Down Expand Up @@ -172,6 +213,7 @@ mod tests {
#[test]
fn $name() {
assert_eq!(IA::from_str($input).unwrap(), $expected);
assert_eq!(IA::try_from($input.to_string()).unwrap(), $expected);
}
};
}
Expand Down
24 changes: 23 additions & 1 deletion crates/scion/src/address/isd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ impl Isd {
self.0
}

// Return true for the special 'wildcard' AS number
/// Return true for the special 'wildcard' AS number
///
/// # Examples
///
/// ```
/// # use scion::address::Isd;
/// assert!(Isd::WILDCARD.is_wildcard());
/// assert!(!Isd::new(1).is_wildcard());
/// ```
pub fn is_wildcard(&self) -> bool {
self == &Self::WILDCARD
}
Expand All @@ -54,3 +62,17 @@ impl FromStr for Isd {
}
}
}

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

mod display {
use super::*;

#[test]
fn wildcard() {
assert_eq!(Isd::WILDCARD.to_string(), "0");
}
}
}

0 comments on commit 0d617f3

Please sign in to comment.