From b7e74d943712100790929720fe87a8d985f6abb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cyryl=20P=C5=82otnicki?= Date: Sat, 15 May 2021 21:20:30 +0100 Subject: [PATCH] fixed corruption test itself --- Cargo.lock | 37 ++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/index/io.rs | 2 -- src/io/error_correcting_encoder.rs | 13 +++++++---- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dd8dd98..88a3068 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,14 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi", +] + [[package]] name = "anyhow" version = "1.0.38" @@ -38,6 +47,7 @@ dependencies = [ "hex", "log", "nix 0.19.1", + "pretty_assertions", "proptest", "rand 0.8.3", "rayon", @@ -195,6 +205,12 @@ dependencies = [ "syn", ] +[[package]] +name = "diff" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499" + [[package]] name = "digest" version = "0.9.0" @@ -420,12 +436,33 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" +[[package]] +name = "output_vt100" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53cdc5b785b7a58c5aad8216b3dfa114df64b0b06ae6e1501cef91df2fbdf8f9" +dependencies = [ + "winapi", +] + [[package]] name = "ppv-lite86" version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" +[[package]] +name = "pretty_assertions" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cab0e7c02cf376875e9335e0ba1da535775beb5450d21e1dffca068818ed98b" +dependencies = [ + "ansi_term", + "ctor", + "diff", + "output_vt100", +] + [[package]] name = "proc-macro2" version = "1.0.24" diff --git a/Cargo.toml b/Cargo.toml index 4a32b2d..6f30472 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ walkdir = "2.3" [dev-dependencies] proptest = "0.10" two-rusty-forks = "0.4.0" +pretty_assertions = "0.7" [dev-dependencies.cargo-husky] version = "1" diff --git a/src/index/io.rs b/src/index/io.rs index a264709..385b4ab 100644 --- a/src/index/io.rs +++ b/src/index/io.rs @@ -1,4 +1,3 @@ -use log::error; use std::collections::HashMap; use vfs::VfsPath; @@ -132,7 +131,6 @@ mod must { use crate::index::Index; use anyhow::Result; - use rand::Rng; use vfs::{MemoryFS, VfsPath}; #[test] diff --git a/src/io/error_correcting_encoder.rs b/src/io/error_correcting_encoder.rs index e5a4a93..52c7d15 100644 --- a/src/io/error_correcting_encoder.rs +++ b/src/io/error_correcting_encoder.rs @@ -14,6 +14,9 @@ pub fn encode(bytes: &[u8]) -> Result> { pub fn decode(bytes: &[u8]) -> Result> { let decoder = Decoder::new(ECC_LENGTH); + if decoder.is_corrupted(bytes) { + return Err(anyhow!("corrupted")); + } let maybe_corrected = decoder.correct(bytes, None); match maybe_corrected { Ok(corrected) => Ok(corrected.data().to_vec()), @@ -21,6 +24,7 @@ pub fn decode(bytes: &[u8]) -> Result> { } } +#[cfg(test)] mod must { use anyhow::Result; @@ -28,6 +32,8 @@ mod must { use super::{decode, encode}; + use pretty_assertions::assert_eq; + #[test] fn survive_data_corruption() -> Result<()> { let mut original: [u8; 32] = [0; 32]; @@ -35,14 +41,13 @@ mod must { let encoded = encode(&original)?; - let size = dbg!(encoded.len()); + let size = encoded.len(); let corrupt_byte_index = rand::thread_rng().gen_range::(0..size); - let mut corrupted: [u8; 32] = [0; 32]; - corrupted.copy_from_slice(&encoded); + let mut corrupted = Vec::from(encoded); corrupted[corrupt_byte_index] = rand::thread_rng().gen::(); - let decoded = decode(&corrupted)?; + let decoded = decode(&corrupted).unwrap(); assert_eq!(decoded, original);