Skip to content

Commit 781b2c3

Browse files
committed
SQUASHME: Fix tests
Fix doc tests, e2e tests, and fuzz tests. This entails: * Using the new `manticore::Result` in tests * Allow error helper macros to be called from external crates (e.g. the e2e crate) and use them to generate errors where needed
1 parent a3acc20 commit 781b2c3

File tree

8 files changed

+37
-35
lines changed

8 files changed

+37
-35
lines changed

e2e/src/support/rot.rs

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use manticore::protocol::spdm;
2929
use manticore::server;
3030
use manticore::server::pa_rot::PaRot;
3131
use manticore::session::ring::Session;
32+
use manticore::Result;
3233

3334
use crate::support::fakes;
3435
use crate::support::tcp;

e2e/src/support/tcp.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ use manticore::protocol::wire::WireEnum;
4747
use manticore::protocol::Command;
4848
use manticore::protocol::Message;
4949
use manticore::server;
50+
use manticore::Result;
51+
use manticore::{check, fail};
5052

5153
/// Sends `req` to a virtual RoT listening on `localhost:{port}`, using
5254
/// Cerberus-over-TCP.
@@ -84,7 +86,7 @@ pub fn send_cerberus<
8486
Ok(Ok(FromWire::from_wire(&mut r, arena)?))
8587
} else if header.command == cerberus::CommandType::Error {
8688
log::info!("deserializing {}", type_name::<protocol::Error<'a, Cmd>>());
87-
Ok(Err(FromWire::from_wire(&mut r, arena)?))
89+
Ok(Err(fail!(FromWire::from_wire(&mut r, arena)?)))
8890
} else {
8991
Err(net::Error::BadHeader.into())
9092
}
@@ -125,7 +127,7 @@ pub fn send_spdm<'a, Cmd: Command<'a, CommandType = spdm::CommandType>>(
125127
Ok(Ok(FromWire::from_wire(&mut r, arena)?))
126128
} else if header.command == spdm::CommandType::Error {
127129
log::info!("deserializing {}", type_name::<protocol::Error<'a, Cmd>>());
128-
Ok(Err(FromWire::from_wire(&mut r, arena)?))
130+
Ok(Err(fail!(FromWire::from_wire(&mut r, arena)?)))
129131
} else {
130132
Err(net::Error::BadHeader.into())
131133
}
@@ -140,7 +142,7 @@ impl io::Read for TcpReader {
140142
fn read_bytes(&mut self, out: &mut [u8]) -> Result<(), io::Error> {
141143
let Self { tcp, len } = self;
142144
if *len < out.len() {
143-
return Err(io::Error::BufferExhausted);
145+
return Err(fail!(io::Error::BufferExhausted));
144146
}
145147
tcp.read_exact(out).map_err(|e| {
146148
log::error!("{}", e);
@@ -365,22 +367,22 @@ impl<'req, H: Header + 'req> HostRequest<'req, H> for Inner<H> {
365367
fn header(&self) -> Result<H, net::Error> {
366368
if self.output_buffer.is_some() {
367369
log::error!("header() called out-of-order");
368-
return Err(net::Error::OutOfOrder);
370+
return Err(fail!(net::Error::OutOfOrder));
369371
}
370372
self.stream
371373
.as_ref()
372374
.map(|(h, _, _)| *h)
373-
.ok_or(net::Error::Disconnected)
375+
.ok_or_else(|| fail!(net::Error::Disconnected))
374376
}
375377

376378
fn payload(&mut self) -> Result<&mut dyn io::ReadZero<'req>, net::Error> {
377379
if self.stream.is_none() {
378380
log::error!("payload() called out-of-order");
379-
return Err(net::Error::Disconnected);
381+
return Err(fail!(net::Error::Disconnected));
380382
}
381383
if self.output_buffer.is_some() {
382384
log::error!("payload() called out-of-order");
383-
return Err(net::Error::OutOfOrder);
385+
return Err(fail!(net::Error::OutOfOrder));
384386
}
385387

386388
Ok(self)
@@ -392,11 +394,11 @@ impl<'req, H: Header + 'req> HostRequest<'req, H> for Inner<H> {
392394
) -> Result<&mut dyn HostResponse<'req>, net::Error> {
393395
if self.stream.is_none() {
394396
log::error!("payload() called out-of-order");
395-
return Err(net::Error::Disconnected);
397+
return Err(fail!(net::Error::Disconnected));
396398
}
397399
if self.output_buffer.is_some() {
398400
log::error!("payload() called out-of-order");
399-
return Err(net::Error::OutOfOrder);
401+
return Err(fail!(net::Error::OutOfOrder));
400402
}
401403

402404
self.output_buffer = Some(Writer::new(header));
@@ -408,13 +410,13 @@ impl<'req, H: Header + 'req> HostResponse<'req> for Inner<H> {
408410
fn sink(&mut self) -> Result<&mut dyn io::Write, net::Error> {
409411
if self.stream.is_none() {
410412
log::error!("sink() called out-of-order");
411-
return Err(net::Error::Disconnected);
413+
return Err(fail!(net::Error::Disconnected));
412414
}
413415

414416
self.output_buffer
415417
.as_mut()
416418
.map(|w| w as &mut dyn io::Write)
417-
.ok_or(net::Error::OutOfOrder)
419+
.ok_or_else(|| fail!(net::Error::OutOfOrder))
418420
}
419421

420422
fn finish(&mut self) -> Result<(), net::Error> {
@@ -434,7 +436,7 @@ impl<'req, H: Header + 'req> HostResponse<'req> for Inner<H> {
434436
self.output_buffer = None;
435437
Ok(())
436438
}
437-
_ => Err(net::Error::Disconnected),
439+
_ => Err(fail!(net::Error::Disconnected)),
438440
}
439441
}
440442
}
@@ -443,9 +445,7 @@ impl<H> io::Read for Inner<H> {
443445
fn read_bytes(&mut self, out: &mut [u8]) -> Result<(), io::Error> {
444446
let (_, len, stream) =
445447
self.stream.as_mut().ok_or(io::Error::Internal)?;
446-
if *len < out.len() {
447-
return Err(io::Error::BufferExhausted);
448-
}
448+
check!(*len >= out.len(), io::Error::BufferExhausted);
449449
stream.read_exact(out).map_err(|e| {
450450
log::error!("{}", e);
451451
io::Error::Internal

fuzz/targets/x509_unsigned.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,13 @@ use manticore::cert::Cert;
1313
use manticore::cert::CertFormat;
1414
use manticore::crypto::sig;
1515
use manticore::protocol::cerberus::capabilities;
16+
use manticore::Result;
1617

1718
/// A `Ciphers` that blindly accepts all signatures.
1819
struct NoVerify;
1920

2021
impl sig::Verify for NoVerify {
21-
fn verify(
22-
&mut self,
23-
_: &[&[u8]],
24-
_: &[u8],
25-
) -> Result<(), sig::Error> {
22+
fn verify(&mut self, _: &[&[u8]], _: &[u8]) -> Result<(), sig::Error> {
2623
Ok(())
2724
}
2825
}
@@ -39,12 +36,7 @@ impl sig::Ciphers for NoVerify {
3936
}
4037

4138
fuzz_target!(|data: &[u8]| {
42-
let _ = Cert::parse(
43-
data,
44-
CertFormat::RiotX509,
45-
None,
46-
&mut NoVerify,
47-
);
39+
let _ = Cert::parse(data, CertFormat::RiotX509, None, &mut NoVerify);
4840

4941
// NOTE: we might actually succeed at creating a valid cert, so we can't
5042
// check for is_err() here.

src/debug.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,12 @@ macro_rules! debug_from {
198198
///
199199
/// If the condition does not hold, constructs the given error, logs it, and
200200
/// returns out of the current function with it.
201+
#[macro_export]
201202
macro_rules! check {
202203
($cond:expr, $error:expr) => {
203204
if !$cond {
204205
let error = $error;
205-
return Err(fail!(
206+
return Err($crate::fail!(
206207
error,
207208
"check failure: `{}`; returned {:?}",
208209
stringify!($cond),
@@ -218,19 +219,21 @@ macro_rules! check {
218219
///
219220
/// For example, instead of writing `foo.ok_or(MyError)`, instead write
220221
/// `foo.ok_or_else(|| fail!(MyError))`.
222+
#[macro_export]
221223
macro_rules! fail {
222224
($error:expr, $($format:tt)+) => {{
223-
error!($($format)+);
224-
$crate::debug::Error::__new($error)
225+
$crate::error!($($format)+);
226+
$crate::Error::__new($error)
225227
}};
226228
($error:expr) => {{
227229
let error = $error;
228-
error!("generated error: `{:?}`", error);
229-
$crate::debug::Error::__new(error)
230+
$crate::error!("generated error: `{:?}`", error);
231+
$crate::Error::__new(error)
230232
}};
231233
}
232234

233235
/// Redactable version of [`log::trace!()`].
236+
#[macro_export]
234237
macro_rules! trace {
235238
($($args:tt)*) => {
236239
#[cfg(feature = "log")]
@@ -239,6 +242,7 @@ macro_rules! trace {
239242
}
240243

241244
/// Redactable version of [`log::info!()`].
245+
#[macro_export]
242246
macro_rules! info {
243247
($($args:tt)*) => {
244248
#[cfg(feature = "log")]
@@ -247,6 +251,7 @@ macro_rules! info {
247251
}
248252

249253
/// Redactable version of [`log::warn!()`].
254+
#[macro_export]
250255
macro_rules! warn {
251256
($($args:tt)*) => {
252257
#[cfg(feature = "log")]
@@ -255,6 +260,7 @@ macro_rules! warn {
255260
}
256261

257262
/// Redactable version of [`log::error!()`].
263+
#[macro_export]
258264
macro_rules! error {
259265
($($args:tt)*) => {
260266
#[cfg(feature = "log")]

src/hardware/flash.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,9 @@ pub unsafe trait Flash {
9696
/// the following should be a sufficient starting point:
9797
/// ```
9898
/// # use core::alloc::Layout;
99-
/// # use manticore::mem::*;
99+
/// # use manticore::Result;
100100
/// # use manticore::hardware::flash::*;
101+
/// # use manticore::mem::*;
101102
/// # struct Foo;
102103
/// # impl Foo {
103104
/// # fn read(&self, offset: u32, out: &mut [u8]) -> Result<(), Error> {

src/io/cursor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
//! buffer was read or written. This is especialy useful when used in
1414
//! conjunction with [`ToWire`]:
1515
//! ```
16+
//! # use manticore::Result;
1617
//! # use manticore::io::*;
1718
//! # use manticore::protocol::wire::{self, *};
1819
//! # struct MyMessage;
@@ -119,7 +120,7 @@ impl<'a> Cursor<'a> {
119120
/// cursor.seek(SeekPos::Abs(mark))?;
120121
/// assert_eq!(cursor.consumed_bytes(), b"bfooar");
121122
///
122-
/// # Ok::<(), Error>(())
123+
/// # Ok::<(), manticore::Error<Error>>(())
123124
/// ```
124125
pub fn seek(&mut self, pos: SeekPos) -> Result<(), io::Error> {
125126
let offset = match pos {

src/mem/arena.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl<'arena, A: Arena + ?Sized> ArenaExt<'arena> for &'arena A {
207207
/// arena.reset();
208208
/// let buf3 = arena.alloc_slice::<u8>(64)?;
209209
/// assert_eq!(buf3.len(), 64);
210-
/// # Ok::<(), OutOfMemory>(())
210+
/// # Ok::<(), manticore::Error<OutOfMemory>>(())
211211
/// ```
212212
///
213213
/// Note that `BumpArena`'s interface protects callers from mistakedly

src/net/host.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::Result;
2626
/// `HostPort` uses the traits [`HostRequest`] and [`HostResponse`] to describe
2727
/// a generic state machine, where a request is processed and replied to.
2828
/// ```
29+
/// # use manticore::Result;
2930
/// # use manticore::net::{*, host::*};
3031
/// fn process_request<'req, Header>(port: &mut impl HostPort<'req, Header>) -> Result<(), Error> {
3132
/// let req = port.receive()?;
@@ -198,7 +199,7 @@ pub trait HostResponse<'req> {
198199
/// &mut resp_bytes, &arena
199200
/// ).unwrap();
200201
/// assert_eq!(resp.version, &[0xba; 32]);
201-
/// # Ok::<(), manticore::net::Error>(())
202+
/// # Ok::<(), manticore::Error<manticore::net::Error>>(())
202203
/// ```
203204
pub struct InMemHost<'buf, Header>(InMemInner<'buf, Header>);
204205

0 commit comments

Comments
 (0)