fixed corruption test itself

This commit is contained in:
Cyryl Płotnicki 2021-05-15 21:20:30 +01:00
parent a95b5d840f
commit b7e74d9437
4 changed files with 47 additions and 6 deletions

37
Cargo.lock generated
View file

@ -1,5 +1,14 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # 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]] [[package]]
name = "anyhow" name = "anyhow"
version = "1.0.38" version = "1.0.38"
@ -38,6 +47,7 @@ dependencies = [
"hex", "hex",
"log", "log",
"nix 0.19.1", "nix 0.19.1",
"pretty_assertions",
"proptest", "proptest",
"rand 0.8.3", "rand 0.8.3",
"rayon", "rayon",
@ -195,6 +205,12 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "diff"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e25ea47919b1560c4e3b7fe0aaab9becf5b84a10325ddf7db0f0ba5e1026499"
[[package]] [[package]]
name = "digest" name = "digest"
version = "0.9.0" version = "0.9.0"
@ -420,12 +436,33 @@ version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" 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]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.10" version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" 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]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.24" version = "1.0.24"

View file

@ -34,6 +34,7 @@ walkdir = "2.3"
[dev-dependencies] [dev-dependencies]
proptest = "0.10" proptest = "0.10"
two-rusty-forks = "0.4.0" two-rusty-forks = "0.4.0"
pretty_assertions = "0.7"
[dev-dependencies.cargo-husky] [dev-dependencies.cargo-husky]
version = "1" version = "1"

View file

@ -1,4 +1,3 @@
use log::error;
use std::collections::HashMap; use std::collections::HashMap;
use vfs::VfsPath; use vfs::VfsPath;
@ -132,7 +131,6 @@ mod must {
use crate::index::Index; use crate::index::Index;
use anyhow::Result; use anyhow::Result;
use rand::Rng;
use vfs::{MemoryFS, VfsPath}; use vfs::{MemoryFS, VfsPath};
#[test] #[test]

View file

@ -14,6 +14,9 @@ pub fn encode(bytes: &[u8]) -> Result<Vec<u8>> {
pub fn decode(bytes: &[u8]) -> Result<Vec<u8>> { pub fn decode(bytes: &[u8]) -> Result<Vec<u8>> {
let decoder = Decoder::new(ECC_LENGTH); let decoder = Decoder::new(ECC_LENGTH);
if decoder.is_corrupted(bytes) {
return Err(anyhow!("corrupted"));
}
let maybe_corrected = decoder.correct(bytes, None); let maybe_corrected = decoder.correct(bytes, None);
match maybe_corrected { match maybe_corrected {
Ok(corrected) => Ok(corrected.data().to_vec()), Ok(corrected) => Ok(corrected.data().to_vec()),
@ -21,6 +24,7 @@ pub fn decode(bytes: &[u8]) -> Result<Vec<u8>> {
} }
} }
#[cfg(test)]
mod must { mod must {
use anyhow::Result; use anyhow::Result;
@ -28,6 +32,8 @@ mod must {
use super::{decode, encode}; use super::{decode, encode};
use pretty_assertions::assert_eq;
#[test] #[test]
fn survive_data_corruption() -> Result<()> { fn survive_data_corruption() -> Result<()> {
let mut original: [u8; 32] = [0; 32]; let mut original: [u8; 32] = [0; 32];
@ -35,14 +41,13 @@ mod must {
let encoded = encode(&original)?; let encoded = encode(&original)?;
let size = dbg!(encoded.len()); let size = encoded.len();
let corrupt_byte_index = rand::thread_rng().gen_range::<usize, _>(0..size); let corrupt_byte_index = rand::thread_rng().gen_range::<usize, _>(0..size);
let mut corrupted: [u8; 32] = [0; 32]; let mut corrupted = Vec::from(encoded);
corrupted.copy_from_slice(&encoded);
corrupted[corrupt_byte_index] = rand::thread_rng().gen::<u8>(); corrupted[corrupt_byte_index] = rand::thread_rng().gen::<u8>();
let decoded = decode(&corrupted)?; let decoded = decode(&corrupted).unwrap();
assert_eq!(decoded, original); assert_eq!(decoded, original);