Skip to content

Commit

Permalink
Add overflow tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Jun 24, 2024
1 parent 807a2ec commit 0616555
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
17 changes: 17 additions & 0 deletions crates/claims/crates/status/src/impl/bitstream_status_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ impl StatusMap for StatusList {
mod tests {
use rand::{rngs::StdRng, RngCore, SeedableRng};

use crate::Overflow;

use super::{BitString, StatusSize};

fn random_bit_string(
Expand Down Expand Up @@ -580,4 +582,19 @@ mod tests {
randomized_write(i, 7u8.try_into().unwrap(), 1000);
}
}

#[test]
fn overflows() {
let mut rng = StdRng::seed_from_u64(0);
let (_, mut bitstring) = random_bit_string(&mut rng, 1u8.try_into().unwrap(), 15);

// Out of bounds.
assert!(bitstring.get(15).is_none());

// Out of bounds (even if there are enough bytes in the list).
assert_eq!(bitstring.set(15, 0), Err(Overflow::Index(15)));

// Too many bits.
assert_eq!(bitstring.set(14, 2), Err(Overflow::Value(2)));
}
}
2 changes: 1 addition & 1 deletion crates/claims/crates/status/src/impl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod token_status_list;
pub use flate2::Compression;

/// Status list overflow.
#[derive(Debug, thiserror::Error)]
#[derive(Debug, PartialEq, thiserror::Error)]
pub enum Overflow {
/// Value is too large.
#[error("value `{0}` is too large")]
Expand Down
17 changes: 17 additions & 0 deletions crates/claims/crates/status/src/impl/token_status_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,8 @@ mod tests {
use flate2::Compression;
use rand::{rngs::StdRng, RngCore, SeedableRng};

use crate::Overflow;

use super::{json::JsonStatusList, BitString, StatusSize};

fn random_bit_string(
Expand Down Expand Up @@ -710,4 +712,19 @@ mod tests {
randomized_write(i, 4u8.try_into().unwrap(), 1000);
}
}

#[test]
fn overflows() {
let mut rng = StdRng::seed_from_u64(0);
let (_, mut bitstring) = random_bit_string(&mut rng, 1u8.try_into().unwrap(), 15);

// Out of bounds.
assert!(bitstring.get(15).is_none());

// Out of bounds (even if there are enough bytes in the list).
assert_eq!(bitstring.set(15, 0), Err(Overflow::Index(15)));

// Too many bits.
assert_eq!(bitstring.set(14, 2), Err(Overflow::Value(2)));
}
}

0 comments on commit 0616555

Please sign in to comment.