From 066c82710484d652e89ac7d801bd4fba42c77e5f Mon Sep 17 00:00:00 2001 From: Nicolas Stalder Date: Wed, 17 Feb 2021 21:37:43 +0100 Subject: [PATCH] Two tests --- src/decoder.rs | 12 ++++++++++++ src/encoder.rs | 15 +++++++++++++-- src/error.rs | 2 +- src/lib.rs | 4 ++++ src/tagged.rs | 5 ++--- src/traits.rs | 12 ++++++++---- 6 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index d254492..6eb5d52 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -126,6 +126,18 @@ impl<'a> From<&'a [u8]> for Decoder<'a> { } } +#[cfg(test)] +mod tests { + use core::convert::TryFrom; + use crate::{Decodable, Tag, TaggedSlice}; + + #[test] + fn zero_length() { + let buf: &[u8] = &[0x2A, 0x00]; + let ts = TaggedSlice::from_bytes(buf).unwrap(); + assert_eq!(ts, TaggedSlice::from(Tag::try_from(42).unwrap(), &[]).unwrap()); + } +} // #[cfg(test)] // mod tests { // use super::Decoder; diff --git a/src/encoder.rs b/src/encoder.rs index 089dee2..da5831d 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -140,8 +140,19 @@ impl<'a> Encoder<'a> { } } -// #[cfg(test)] -// mod tests { +#[cfg(test)] +mod tests { + use core::convert::TryFrom; + use crate::{Encodable, Tag, TaggedSlice}; + + #[test] + fn zero_length() { + let tv = TaggedSlice::from(Tag::try_from(42).unwrap(), &[]).unwrap(); + let mut buf = [0u8; 4]; + assert_eq!(tv.encode_to_slice(&mut buf).unwrap(), &[0x2A, 0x00]); + } +} + // use super::Encoder; // use crate::{Encodable, ErrorKind, Length}; diff --git a/src/error.rs b/src/error.rs index 2bf104f..42b3b98 100644 --- a/src/error.rs +++ b/src/error.rs @@ -8,7 +8,7 @@ use core::{convert::Infallible, fmt}; /// Result type. pub type Result = core::result::Result; -/// Error type +/// Error type. #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub struct Error { /// Kind of error diff --git a/src/lib.rs b/src/lib.rs index baf9c01..e1b1250 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,10 @@ //! - not requiring references to ASN.1 (e.g., since SIMPLE-TLV does not have any) //! - not requiring allocations or memmoves (like ring, derp, x509:der) //! - adding a type layer on top of SIMPLE-TLV's byte slice values +//! +//! The core idea taken from `der` is to have `Encodable` require an `encoded_length` method. +//! By calling this recursively in a first pass, allocations required in other approaches are +//! avoided. #![no_std] #![forbid(unsafe_code)] diff --git a/src/tagged.rs b/src/tagged.rs index 389b135..029735d 100644 --- a/src/tagged.rs +++ b/src/tagged.rs @@ -3,14 +3,14 @@ use crate::{Decodable, Decoder, Encodable, Encoder, ErrorKind, header::Header, Length, Result, Slice, Tag}; -/// SIMPLE-TLV data object +/// SIMPLE-TLV data object. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct TaggedValue { tag: Tag, value: V, } -/// Raw SIMPLE-TLV data object: TaggedValue with Slice as value. +/// Raw SIMPLE-TLV data object `TaggedValue>`. pub type TaggedSlice<'a> = TaggedValue>; impl TaggedValue @@ -24,7 +24,6 @@ impl TaggedValue } } - impl<'a, E> TaggedValue<&'a E> where E: Encodable diff --git a/src/traits.rs b/src/traits.rs index b435b2e..98370e5 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -17,7 +17,7 @@ use { crate::{Error, ErrorKind}, }; -/// Decoding trait: +/// Decoding trait. /// /// Decode out of decoder, which essentially is a slice of bytes. /// @@ -45,7 +45,7 @@ where } } -/// Encoding trait +/// Encoding trait. /// /// Encode into encoder, which essentially is a mutable slice of bytes. /// @@ -152,12 +152,16 @@ impl Taggable for X where X: Sized {} // } /// Types with an associated SIMPLE-TLV [`Tag`]. +/// +/// A tagged type implementing `Container` has a blanked implementation of `Encodable`. pub trait Tagged { /// The tag fn tag() -> Tag; } /// Multiple encodables in a container. +/// +/// A container implementing `Tagged` has a blanked implementation of `Encodable`. pub trait Container { /// Call the provided function with a slice of [`Encodable`] trait objects /// representing the fields of this message. @@ -170,9 +174,9 @@ pub trait Container { F: FnOnce(&[&dyn Encodable]) -> Result; } -impl Encodable for TC +impl Encodable for TaggedContainer where - TC: Tagged + Container, + TaggedContainer: Tagged + Container { fn encoded_length(&self) -> Result { let value_length = self.fields(|encodables| Length::try_from(encodables))?;