Skip to content

Commit

Permalink
Add tests, fix off-by-one errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
partim committed Jan 10, 2025
1 parent 0b6d046 commit e91f397
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
25 changes: 24 additions & 1 deletion src/repository/aspa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ impl ProviderAsSet {
while let Some(asn) = Asn::take_opt_from(
cons
)? {
if len > Self::MAX_LEN {
if len >= Self::MAX_LEN {
return Err(cons.content_err(
"too many provider ASNs"
));
Expand Down Expand Up @@ -555,6 +555,29 @@ mod signer_test {
make_aspa(customer_as, providers);
}

#[test]
fn provider_asn_size() {
fn make_aspa(len: usize) -> Captured {
let mut builder = AspaBuilder::empty(0.into());
for i in 0..(len as u32) {
builder.add_provider(Asn::from(i + 1)).unwrap();
}
builder.into_attestation().encode_ref().to_captured(Mode::Der)
}

make_aspa(
ProviderAsSet::MAX_LEN - 1
).decode(AsProviderAttestation::take_from).unwrap();
make_aspa(
ProviderAsSet::MAX_LEN
).decode(AsProviderAttestation::take_from).unwrap();
assert!(
make_aspa(
ProviderAsSet::MAX_LEN + 1
).decode(AsProviderAttestation::take_from).is_err()
);
}

#[test]
#[cfg(feature = "serde")]
fn serde_aspa() {
Expand Down
14 changes: 7 additions & 7 deletions src/rtr/pdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ pub struct ProviderAsns(Bytes);

impl ProviderAsns {
/// The maximum number of provider ASNs.
const MAX_COUNT: usize = 16380;
pub const MAX_COUNT: usize = 16380;

/// Returns an empty value.
pub fn empty() -> Self {
Expand All @@ -967,7 +967,7 @@ impl ProviderAsns {
let iter = iter.into_iter();
let mut providers = Vec::with_capacity(iter.size_hint().0);
iter.enumerate().try_for_each(|(idx, item)| {
if idx > Self::MAX_COUNT {
if idx >= Self::MAX_COUNT {
return Err(ProviderAsnsError(()))
}
providers.extend_from_slice(&item.into_u32().to_be_bytes());
Expand Down Expand Up @@ -1840,19 +1840,19 @@ mod test {
fn provider_count() {
assert_eq!(
ProviderAsns::try_from_iter(
iter::repeat(Asn::from(0)).take(usize::from(u16::MAX - 1))
iter::repeat(Asn::from(0)).take(ProviderAsns::MAX_COUNT - 1)
).unwrap().asn_count(),
u16::MAX - 1
(ProviderAsns::MAX_COUNT - 1) as u16,
);
assert_eq!(
ProviderAsns::try_from_iter(
iter::repeat(Asn::from(0)).take(usize::from(u16::MAX))
iter::repeat(Asn::from(0)).take(ProviderAsns::MAX_COUNT)
).unwrap().asn_count(),
u16::MAX
ProviderAsns::MAX_COUNT as u16,
);
assert!(
ProviderAsns::try_from_iter(
iter::repeat(Asn::from(0)).take(usize::from(u16::MAX) + 1)
iter::repeat(Asn::from(0)).take(ProviderAsns::MAX_COUNT + 1)
).is_err()
);
}
Expand Down

0 comments on commit e91f397

Please sign in to comment.