mirror of
https://github.com/trussed-dev/flexiber.git
synced 2026-06-20 04:16:10 -07:00
Two tests
This commit is contained in:
@@ -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;
|
||||
|
||||
+13
-2
@@ -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};
|
||||
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ use core::{convert::Infallible, fmt};
|
||||
/// Result type.
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
|
||||
/// Error type
|
||||
/// Error type.
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
pub struct Error {
|
||||
/// Kind of error
|
||||
|
||||
@@ -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)]
|
||||
|
||||
+2
-3
@@ -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<V> {
|
||||
tag: Tag,
|
||||
value: V,
|
||||
}
|
||||
|
||||
/// Raw SIMPLE-TLV data object: TaggedValue with Slice as value.
|
||||
/// Raw SIMPLE-TLV data object `TaggedValue<Slice<'_>>`.
|
||||
pub type TaggedSlice<'a> = TaggedValue<Slice<'a>>;
|
||||
|
||||
impl<V> TaggedValue<V>
|
||||
@@ -24,7 +24,6 @@ impl<V> TaggedValue<V>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl<'a, E> TaggedValue<&'a E>
|
||||
where
|
||||
E: Encodable
|
||||
|
||||
+8
-4
@@ -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<X> 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<T>;
|
||||
}
|
||||
|
||||
impl<TC> Encodable for TC
|
||||
impl<TaggedContainer> Encodable for TaggedContainer
|
||||
where
|
||||
TC: Tagged + Container,
|
||||
TaggedContainer: Tagged + Container
|
||||
{
|
||||
fn encoded_length(&self) -> Result<Length> {
|
||||
let value_length = self.fields(|encodables| Length::try_from(encodables))?;
|
||||
|
||||
Reference in New Issue
Block a user