Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve test coverage #9

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ jobs:
permissions:
contents: read
pull-requests: write
env:
RUSTC_BOOTSTRAP: 1
steps:
- uses: actions/checkout@v4
- run: rustup update stable
Expand All @@ -81,7 +83,7 @@ jobs:
- run: cargo install [email protected]

- name: Run tests and record coverage
run: cargo tarpaulin --workspace --skip-clean --out html --out xml
run: cargo tarpaulin --workspace --skip-clean --all-targets --doc --out html --out xml

- name: Upload coverage report
uses: actions/upload-artifact@v3
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ To analyze test coverage, we use [Tarpaulin](https://crates.io/crates/cargo-tarp

```sh
cargo install cargo-tarpaulin
cargo tarpaulin --workspace --out html
cargo tarpaulin --workspace --all-targets --doc --out html
```

This creates a file `tarpaulin-report.html`, which shows you coverage statistics as well as which individual lines are or aren't covered by tests.
Expand Down
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");
}
}
}