From 62ddd18cf0b2e1e8128a1cc0d72767975fc62dec Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Fri, 11 Feb 2022 18:56:45 -0500 Subject: [PATCH 1/3] Avoid failure with cargo test --all-targets --no-default-features Annotate the parts of the tests that require the std feature. This is an initial (but not complete) cleanup for #2. A proper fix would include new tests for the low level interface as well. --- src/decode_impl.rs | 4 ++++ src/encode_impl.rs | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/decode_impl.rs b/src/decode_impl.rs index 0963a0c..fc4c339 100644 --- a/src/decode_impl.rs +++ b/src/decode_impl.rs @@ -261,9 +261,11 @@ pub fn decode(input: &str, output: &mut Vec, bits: u64) -> Result<(), ZBase3 #[cfg(test)] mod tests { + #[cfg(feature = "std")] use super::decode; use crate::test_data::{TestCase, RANDOM_TEST_DATA, STANDARD_TEST_DATA}; + #[cfg(feature = "std")] fn run_tests(test_cases: &[TestCase]) { let mut buffer = Vec::new(); for test in test_cases { @@ -274,11 +276,13 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_decode_standard() { run_tests(STANDARD_TEST_DATA); } #[test] + #[cfg(feature = "std")] fn test_decode_random() { run_tests(RANDOM_TEST_DATA); } diff --git a/src/encode_impl.rs b/src/encode_impl.rs index 49aab2d..d0f9760 100644 --- a/src/encode_impl.rs +++ b/src/encode_impl.rs @@ -250,9 +250,11 @@ pub fn encode(input: &[u8], output: &mut String, bits: u64) -> Result<(), ZBase3 #[cfg(test)] mod tests { + #[cfg(feature = "std")] use super::encode; use crate::test_data::{TestCase, RANDOM_TEST_DATA, STANDARD_TEST_DATA}; + #[cfg(feature = "std")] fn run_tests(test_cases: &[TestCase]) { let mut buffer = String::new(); for test in test_cases { @@ -263,11 +265,13 @@ mod tests { } #[test] + #[cfg(feature = "std")] fn test_encode_standard() { run_tests(STANDARD_TEST_DATA); } #[test] + #[cfg(feature = "std")] fn test_encode_random() { run_tests(RANDOM_TEST_DATA); } From 845ebb28c6c3c59d75c66f569d1c009a744213b5 Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Fri, 11 Feb 2022 18:19:38 -0500 Subject: [PATCH 2/3] Add tests for low-level API This also fixes `cargo test --no-default-features`. Closes: #2 --- src/decode_impl.rs | 22 ++++++++++++++++++++++ src/encode_impl.rs | 22 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/decode_impl.rs b/src/decode_impl.rs index fc4c339..2342074 100644 --- a/src/decode_impl.rs +++ b/src/decode_impl.rs @@ -263,6 +263,8 @@ pub fn decode(input: &str, output: &mut Vec, bits: u64) -> Result<(), ZBase3 mod tests { #[cfg(feature = "std")] use super::decode; + use super::decode_slices; + use super::required_octets_buffer_len; use crate::test_data::{TestCase, RANDOM_TEST_DATA, STANDARD_TEST_DATA}; #[cfg(feature = "std")] @@ -275,6 +277,16 @@ mod tests { } } + fn run_low_level_tests(test_cases: &[TestCase]) { + // the largest sample in src/test_data.rs has 128 bits. + let mut buffer: [u8; 256] = [0; 256]; + for test in test_cases { + let outsz:usize = required_octets_buffer_len(test.bits).unwrap(); + decode_slices(test.encoded.as_bytes(), &mut buffer[..outsz], test.bits).unwrap(); + assert_eq!(&buffer[..outsz], test.unencoded); + } + } + #[test] #[cfg(feature = "std")] fn test_decode_standard() { @@ -286,4 +298,14 @@ mod tests { fn test_decode_random() { run_tests(RANDOM_TEST_DATA); } + + #[test] + fn test_decode_low_level_standard() { + run_low_level_tests(STANDARD_TEST_DATA); + } + + #[test] + fn test_decode_low_level_random() { + run_low_level_tests(RANDOM_TEST_DATA); + } } diff --git a/src/encode_impl.rs b/src/encode_impl.rs index d0f9760..4e6a539 100644 --- a/src/encode_impl.rs +++ b/src/encode_impl.rs @@ -252,6 +252,8 @@ pub fn encode(input: &[u8], output: &mut String, bits: u64) -> Result<(), ZBase3 mod tests { #[cfg(feature = "std")] use super::encode; + use super::encode_slices; + use super::required_quintets_buffer_len; use crate::test_data::{TestCase, RANDOM_TEST_DATA, STANDARD_TEST_DATA}; #[cfg(feature = "std")] @@ -264,6 +266,16 @@ mod tests { } } + fn run_low_level_tests(test_cases: &[TestCase]) { + // the largest sample in src/test_data.rs has 128 bits. + let mut buffer: [u8; 256] = [0; 256]; + for test in test_cases { + let outsz:usize = required_quintets_buffer_len(test.bits).unwrap(); + encode_slices(test.unencoded, &mut buffer[..outsz], test.bits).unwrap(); + assert_eq!(&buffer[..outsz], test.encoded.as_bytes()); + } + } + #[test] #[cfg(feature = "std")] fn test_encode_standard() { @@ -275,4 +287,14 @@ mod tests { fn test_encode_random() { run_tests(RANDOM_TEST_DATA); } + + #[test] + fn test_encode_low_level_standard() { + run_low_level_tests(STANDARD_TEST_DATA); + } + + #[test] + fn test_encode_low_level_random() { + run_low_level_tests(RANDOM_TEST_DATA); + } } From 3e4cba5880560697cb976e10c70ab69fc152ba1f Mon Sep 17 00:00:00 2001 From: Daniel Kahn Gillmor Date: Fri, 11 Feb 2022 22:36:21 -0500 Subject: [PATCH 3/3] Add some tests of error conditions There are a number of possible decoding errors. Make sure that those get raised by the underlying functions. --- src/decode_impl.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/decode_impl.rs b/src/decode_impl.rs index 2342074..30d0a3c 100644 --- a/src/decode_impl.rs +++ b/src/decode_impl.rs @@ -308,4 +308,32 @@ mod tests { fn test_decode_low_level_random() { run_low_level_tests(RANDOM_TEST_DATA); } + + #[test] + fn test_decode_non_zbase32_characters() { + let badcharacters = [ "0", "L", "💩", " ", "!", "_", "2", "v"]; + let mut out:[u8; 1] = [0]; + for character in badcharacters { + assert!(decode_slices(character.as_bytes(), &mut out, 5).is_err(), + "'{}' is not valid zbase32 character", character); + } + } + + #[test] + fn test_decode_non_full_byte_strings() { + // Should really also test different length encodings and bit + // boundaries, not just two chars in, one octet out. But it's + // clumsy to do this in no_std (without allocation), so just a + // simple test for now. + + // these two-character strings have some bits set in the + // trailing bits after the last full octet: + let badstrings = [ "99", "y9", "on", "yt", "zb"]; + let mut out:[u8;1] = [0]; + for string in badstrings { + assert!(decode_slices(string.as_bytes(), &mut out, 8).is_err(), + "'{}' should have produced bits beyond bit 8, should not have decoded, got {:#?}", + string, out); + } + } }