Skip to content

Commit cece4dc

Browse files
committed
aead: adds a dev::BicephalBuffer to smoke tests misuse of get_in
1 parent 204a4e0 commit cece4dc

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

aead/src/dev.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
//! Development-related functionality
22
pub use blobby;
33

4+
#[cfg(feature = "alloc")]
5+
use {core::fmt, inout::InOutBuf};
6+
47
/// Define AEAD test
58
#[macro_export]
69
macro_rules! new_test {
@@ -75,3 +78,67 @@ macro_rules! new_test {
7578
}
7679
};
7780
}
81+
82+
/// [`BicephalBuffer`] is meant for testing InOut-backed APIs.
83+
///
84+
/// It will split the initial buffer in two backing buffers copying the initial data.
85+
#[cfg(feature = "alloc")]
86+
pub struct BicephalBuffer {
87+
in_buf: alloc::vec::Vec<u8>,
88+
out_buf: alloc::vec::Vec<u8>,
89+
}
90+
91+
#[cfg(feature = "alloc")]
92+
impl AsRef<[u8]> for BicephalBuffer {
93+
fn as_ref(&self) -> &[u8] {
94+
&self.out_buf
95+
}
96+
}
97+
98+
#[cfg(feature = "alloc")]
99+
impl From<&[u8]> for BicephalBuffer {
100+
fn from(buf: &[u8]) -> Self {
101+
Self {
102+
in_buf: buf.to_vec(),
103+
out_buf: buf.to_vec(),
104+
}
105+
}
106+
}
107+
108+
#[cfg(feature = "alloc")]
109+
impl From<alloc::vec::Vec<u8>> for BicephalBuffer {
110+
fn from(buf: alloc::vec::Vec<u8>) -> Self {
111+
Self {
112+
in_buf: buf.clone(),
113+
out_buf: buf,
114+
}
115+
}
116+
}
117+
118+
#[cfg(feature = "alloc")]
119+
impl BicephalBuffer {
120+
/// Get an [`InOutBuf`] from a [`BicephalBuffer`]
121+
pub fn to_in_out_buf(&mut self) -> InOutBuf<'_, '_, u8> {
122+
InOutBuf::new(self.in_buf.as_slice(), self.out_buf.as_mut_slice())
123+
.expect("Invariant violation")
124+
}
125+
126+
/// Return the length of the payload
127+
#[inline]
128+
pub fn len(&self) -> usize {
129+
self.in_buf.len()
130+
}
131+
132+
/// Is the payload empty?
133+
#[inline]
134+
pub fn is_empty(&self) -> bool {
135+
self.in_buf.is_empty()
136+
}
137+
}
138+
139+
#[cfg(feature = "alloc")]
140+
impl fmt::Debug for BicephalBuffer {
141+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
142+
write!(f, "BicephalBuffer {{...}}")
143+
}
144+
}

0 commit comments

Comments
 (0)