Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve coverage and code #13

Merged
merged 2 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Shared workflow to automate release management and publication on [crates.io](https://crates.io) ([#14](https://github.com/monero-rs/base58-monero/pull/14))

### Fixed

- Fix a bug in the condition that validates inputs in `encode_block` and `decode_block` ([#13](https://github.com/monero-rs/base58-monero/pull/13))

## [0.3.1] - 2021-09-27

### Changed
Expand Down
44 changes: 35 additions & 9 deletions src/base58.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn u8be_to_u64(data: &[u8]) -> u64 {
}

fn encode_block(data: &[u8]) -> Result<[char; FULL_ENCODED_BLOCK_SIZE]> {
if data.is_empty() && data.len() > FULL_BLOCK_SIZE {
if data.is_empty() || data.len() > FULL_BLOCK_SIZE {
return Err(Error::InvalidBlockSize);
}
let mut res = ['1'; FULL_ENCODED_BLOCK_SIZE];
Expand All @@ -174,7 +174,7 @@ struct DecodedBlock {
}

fn decode_block(data: &[u8]) -> Result<DecodedBlock> {
if data.is_empty() && data.len() > FULL_ENCODED_BLOCK_SIZE {
if data.len() > FULL_ENCODED_BLOCK_SIZE {
return Err(Error::InvalidBlockSize);
}
let res_size = match ENCODED_BLOCK_SIZES.iter().position(|&x| x == data.len()) {
Expand All @@ -199,9 +199,7 @@ fn decode_block(data: &[u8]) -> Result<DecodedBlock> {
let max: u128 = match res_size {
8 => std::u64::MAX as u128 + 1,
0..=7 => 1 << (res_size * 8),
_ => {
return Err(Error::Overflow);
}
_ => unreachable!(),
};

let data = if (res as u128) < max {
Expand Down Expand Up @@ -255,9 +253,11 @@ where

if len == 0 {
// EOF reached, final block is created
let block_size = ENCODED_BLOCK_SIZES[clen];
for c in &encode_block(&buf[..clen])?[..block_size] {
yield *c;
if clen > 0 {
let block_size = ENCODED_BLOCK_SIZES[clen];
for c in &encode_block(&buf[..clen])?[..block_size] {
yield *c;
}
}

break;
Expand Down Expand Up @@ -470,7 +470,7 @@ where
mod tests {
use super::{
decode, decode_block, encode, encode_block, u8be_to_u64, Error, ENCODED_BLOCK_SIZES,
FULL_BLOCK_SIZE,
FULL_BLOCK_SIZE, FULL_ENCODED_BLOCK_SIZE,
};

#[cfg(feature = "check")]
Expand All @@ -483,6 +483,32 @@ mod tests {
#[cfg(feature = "stream")]
use futures_util::{pin_mut, stream::StreamExt};

#[test]
fn encode_wrong_block() {
assert_eq!(encode_block(&[0u8; 0]), Err(Error::InvalidBlockSize));
assert_eq!(
encode_block(&[0u8; FULL_BLOCK_SIZE + 1]),
Err(Error::InvalidBlockSize)
);
}

#[test]
fn encode_empty_value() {
assert_eq!(encode(&[0u8; 0]), Ok(String::from("")));
}

#[test]
fn decode_wrong_block() {
assert_eq!(decode_block(&[0u8; 1]), Err(Error::InvalidBlockSize));
assert_eq!(decode_block(&[0u8; 4]), Err(Error::InvalidBlockSize));
assert_eq!(decode_block(&[0u8; 8]), Err(Error::InvalidBlockSize));
assert_eq!(
decode_block(&[0u8; FULL_ENCODED_BLOCK_SIZE + 1]),
Err(Error::InvalidBlockSize)
);
//assert!(false);
}

macro_rules! uint_8be_to_64 {
($expected:expr, $string:expr) => {
assert_eq!($expected, u8be_to_u64($string));
Expand Down