Skip to content

Commit

Permalink
Simplify ChecksummedBlock tests
Browse files Browse the repository at this point in the history
Signed-off-by: Alessandro Passaro <[email protected]>
  • Loading branch information
passaro committed Oct 24, 2023
1 parent 016cd7b commit 99d590f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
2 changes: 1 addition & 1 deletion mountpoint-s3/src/checksums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use block::ChecksummedBlock;
pub use bytes::ChecksummedBytes;

/// Calculates the combined checksum for `AB` where `prefix_crc` is the checksum for `A`,
/// `suffix_crc` is the checksum for `B`, and `suffic_len` is the length of `B`.
/// `suffix_crc` is the checksum for `B`, and `suffix_len` is the length of `B`.
pub fn combine_checksums(prefix_crc: Crc32c, suffix_crc: Crc32c, suffix_len: usize) -> Crc32c {
let combined = ::crc32c::crc32c_combine(prefix_crc.value(), suffix_crc.value(), suffix_len);
Crc32c::new(combined)
Expand Down
36 changes: 13 additions & 23 deletions mountpoint-s3/src/checksums/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,31 +158,25 @@ mod tests {
#[test]
fn test_extend() {
let bytes = Bytes::from_static(b"some bytes");
let expected = Bytes::from_static(b"some bytes extended");
let checksum = crc32c::checksum(&bytes);
let mut checksummed_block = ChecksummedBlock::new(bytes, checksum);

let extend = Bytes::from_static(b" extended");
let extend_checksum = crc32c::checksum(&extend);
let extend = ChecksummedBlock::new(extend, extend_checksum);
checksummed_block.extend(extend);
let expected = Bytes::from_static(b"some bytes extended");

let mut checksummed_block = ChecksummedBlock::from_bytes(bytes);
let extend_block = ChecksummedBlock::from_bytes(extend);
checksummed_block.extend(extend_block);
let actual = checksummed_block.bytes;
assert_eq!(expected, actual);
}

#[test]
fn test_extend_self_corrupted() {
let bytes = Bytes::from_static(b"some bytes");
let checksum = crc32c::checksum(&bytes);
let mut checksummed_block = ChecksummedBlock::new(bytes, checksum);

let checksum = crc32c::checksum(b"some bytes");
let currupted_bytes = Bytes::from_static(b"corrupted data");
checksummed_block.bytes = currupted_bytes.clone();
let mut checksummed_block = ChecksummedBlock::new(currupted_bytes, checksum);

let extend = Bytes::from_static(b" extended");
let extend_checksum = crc32c::checksum(&extend);
let extend = ChecksummedBlock::new(extend, extend_checksum);
checksummed_block.extend(extend);
let extend_block = ChecksummedBlock::from_bytes(extend);
checksummed_block.extend(extend_block);
assert!(matches!(
checksummed_block.validate(),
Err(IntegrityError::ChecksumMismatch(_, _))
Expand All @@ -192,17 +186,13 @@ mod tests {
#[test]
fn test_extend_other_corrupted() {
let bytes = Bytes::from_static(b"some bytes");
let checksum = crc32c::checksum(&bytes);
let mut checksummed_block = ChecksummedBlock::new(bytes, checksum);

let extend = Bytes::from_static(b" extended");
let extend_checksum = crc32c::checksum(&extend);
let mut extend = ChecksummedBlock::new(extend, extend_checksum);
let mut checksummed_block = ChecksummedBlock::from_bytes(bytes);

let currupted_bytes = Bytes::from_static(b"corrupted data");
extend.bytes = currupted_bytes.clone();
let extend_checksum = crc32c::checksum(b" extended");
let extend_block = ChecksummedBlock::new(currupted_bytes, extend_checksum);

checksummed_block.extend(extend);
checksummed_block.extend(extend_block);
assert!(matches!(
checksummed_block.validate(),
Err(IntegrityError::ChecksumMismatch(_, _))
Expand Down

0 comments on commit 99d590f

Please sign in to comment.