Skip to content

Commit dbbce46

Browse files
Make decode invalid byte test more specific
1 parent 78619fb commit dbbce46

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

src/engine/fast_portable/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,23 @@ pub(crate) const fn decode_table(alphabet: &Alphabet) -> [u8; 256] {
216216
return decode_table;
217217
}
218218

219+
// fn decode_aligned(symbol: u8, decode_table: &[u8; 256]) -> u8 {
220+
// let mut result: u8 = 0x00;
221+
// // If `symbol` is inside the printable range, one of these two derived indices will be equal to
222+
// // the original index, and the decoded byte will end up in `result`. If `symbol` is not
223+
// // printable, neither will equal the original symbol, and so both decoded bytes will have 0x00
224+
// // as a mask.
225+
// // TODO invalid bytes decoded to 0x00 instead of 0xFF?
226+
// let idx: [u8; 2] = [symbol % 64, symbol % 64 + 64];
227+
// for i in 0..2 {
228+
// let symbol_eq_mod = idx[i] == symbol;
229+
// // if symbol equals its mod flavor, 0xFF, else 0x00
230+
// let mask = ((symbol_eq_mod) as i8 - 1) as u8;
231+
// result = result | (decode_table[idx[i] as usize] & mask);
232+
// }
233+
// result
234+
// }
235+
219236
#[inline]
220237
fn read_u64(s: &[u8]) -> u64 {
221238
u64::from_be_bytes(s[..8].try_into().unwrap())

src/engine/tests.rs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rstest_reuse::{apply, template};
77
use std::iter;
88

99
use crate::{
10-
alphabet::STANDARD,
10+
alphabet::{Alphabet, STANDARD},
1111
encode,
1212
engine::{fast_portable, naive, Engine},
1313
tests::random_alphabet,
@@ -451,7 +451,8 @@ fn decode_invalid_byte_error<E: EngineWrapper>(engine_wrapper: E) {
451451
let len_range = Uniform::new(1, 1_000);
452452

453453
for _ in 0..10_000 {
454-
let engine = E::random(&mut rng);
454+
let alphabet = random_alphabet(&mut rng);
455+
let engine = E::random_alphabet(&mut rng, &alphabet);
455456

456457
orig_data.clear();
457458
encode_buf.clear();
@@ -470,7 +471,15 @@ fn decode_invalid_byte_error<E: EngineWrapper>(engine_wrapper: E) {
470471
decode_buf.resize(orig_len, 0);
471472

472473
// replace one encoded byte with an invalid byte
473-
let invalid_byte = 0x07; // BEL, non-printing, so never in an alphabet
474+
let invalid_byte: u8 = loop {
475+
let byte: u8 = rng.gen();
476+
477+
if alphabet.symbols.contains(&byte) {
478+
continue;
479+
} else {
480+
break byte;
481+
}
482+
};
474483

475484
let invalid_range = Uniform::new(0, orig_len);
476485
let invalid_index = invalid_range.sample(&mut rng);
@@ -799,10 +808,14 @@ trait EngineWrapper {
799808
/// Return an engine configured for RFC standard base64
800809
fn standard() -> Self::Engine;
801810

802-
/// Return an engine configured for RFC standard
811+
/// Return an engine configured for RFC standard base64 that allows invalid trailing bits
803812
fn standard_forgiving() -> Self::Engine;
804813

814+
/// Return an engine configured with a randomized alphabet and config
805815
fn random<R: Rng>(rng: &mut R) -> Self::Engine;
816+
817+
/// Return an engine configured with the specified alphabet and randomized config
818+
fn random_alphabet<R: Rng>(rng: &mut R, alphabet: &Alphabet) -> Self::Engine;
806819
}
807820

808821
struct FastPortableWrapper {}
@@ -824,6 +837,10 @@ impl EngineWrapper for FastPortableWrapper {
824837
fn random<R: Rng>(rng: &mut R) -> Self::Engine {
825838
let alphabet = random_alphabet(rng);
826839

840+
Self::random_alphabet(rng, &alphabet)
841+
}
842+
843+
fn random_alphabet<R: Rng>(rng: &mut R, alphabet: &Alphabet) -> Self::Engine {
827844
let config = fast_portable::FastPortableConfig::from(rng.gen(), rng.gen());
828845

829846
fast_portable::FastPortable::from(alphabet, config)
@@ -858,6 +875,10 @@ impl EngineWrapper for NaiveWrapper {
858875
fn random<R: Rng>(rng: &mut R) -> Self::Engine {
859876
let alphabet = random_alphabet(rng);
860877

878+
Self::random_alphabet(rng, alphabet)
879+
}
880+
881+
fn random_alphabet<R: Rng>(rng: &mut R, alphabet: &Alphabet) -> Self::Engine {
861882
let config = naive::NaiveConfig {
862883
padding: rng.gen(),
863884
decode_allow_trailing_bits: rng.gen(),

0 commit comments

Comments
 (0)