Skip to content

Commit 6686704

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

1 file changed

Lines changed: 62 additions & 2 deletions

File tree

aead/src/dev.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
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 {
710
($name:ident, $test_name:expr, $cipher:ty $(,)?) => {
811
#[test]
912
fn $name() {
1013
use aead::{
11-
Aead, KeyInit, Payload,
12-
array::{Array, typenum::Unsigned},
14+
array::{typenum::Unsigned, Array},
1315
dev::blobby::Blob6Iterator,
16+
Aead, KeyInit, Payload,
1417
};
1518

1619
fn run_test(
@@ -75,3 +78,60 @@ 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+
pub fn len(&self) -> usize {
128+
self.in_buf.len()
129+
}
130+
}
131+
132+
#[cfg(feature = "alloc")]
133+
impl fmt::Debug for BicephalBuffer {
134+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135+
write!(f, "BicephalBuffer {{...}}")
136+
}
137+
}

0 commit comments

Comments
 (0)