oxedyne/fe2o3/fe2o3_hash/tests/pow_short_artefact.rs
1.0 KiB, 1 run
created by r656224696:77766, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use oxedyne_fe2o3_hash::{ |
| 2 | hash::HashScheme, |
| 3 | pow::{ |
| 4 | PowVars, |
| 5 | Pristine, |
| 6 | ProofOfWork, |
| 7 | }, |
| 8 | }; |
| 9 | |
| 10 | use oxedyne_fe2o3_core::prelude::*; |
| 11 | |
| 12 | /// A pristine whose timestamp always passes, so that only the artefact's length is tested. |
| 13 | #[derive(Clone)] |
| 14 | struct Open; |
| 15 | |
| 16 | impl Pristine<0, 8> for Open { |
| 17 | fn to_bytes(&self) -> Outcome<[u8; 8]> { Ok([0u8; 8]) } |
| 18 | fn prefix(&self, _byts: &mut [u8]) -> Outcome<usize> { Ok(0) } |
| 19 | fn timestamp_valid(&self, _artefact: &[u8]) -> Outcome<bool> { Ok(true) } |
| 20 | } |
| 21 | |
| 22 | /// An artefact shorter than the hash it should end with, as a sender may claim, is an |
| 23 | /// error and not a subtraction that wraps. |
| 24 | #[test] |
| 25 | fn test_pow_validate_refuses_an_artefact_shorter_than_its_hash() -> Outcome<()> { |
| 26 | let power = res!(ProofOfWork::new(HashScheme::new_sha3_256())); |
| 27 | let powvars = PowVars { zbits: 0, pristine: Open }; |
| 28 | for len in [0usize, 1, 31] { |
| 29 | assert!(power.validate(&powvars, &vec![0u8; len]).is_err(), |
| 30 | "a {}-byte artefact was accepted against a 32-byte hash", len); |
| 31 | } |
| 32 | Ok(()) |
| 33 | } |