diff --git a/Cargo.lock b/Cargo.lock index a71614bd3..7732ead28 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2885,6 +2885,7 @@ dependencies = [ "hex-literal", "signature", "thiserror", + "tor-error", "tor-llcrypto", ] diff --git a/crates/tor-bytes/Cargo.toml b/crates/tor-bytes/Cargo.toml index 22427fedf..f7e43e418 100644 --- a/crates/tor-bytes/Cargo.toml +++ b/crates/tor-bytes/Cargo.toml @@ -12,6 +12,7 @@ repository = "https://gitlab.torproject.org/tpo/core/arti.git/" [dependencies] tor-llcrypto = { path = "../tor-llcrypto", version = "0.0.3"} +tor-error = { path="../tor-error", version = "0.0.1" } arrayref = "0.3" bytes = "1" diff --git a/crates/tor-bytes/src/err.rs b/crates/tor-bytes/src/err.rs index 8a61231b6..6bfeeb55e 100644 --- a/crates/tor-bytes/src/err.rs +++ b/crates/tor-bytes/src/err.rs @@ -3,7 +3,7 @@ use thiserror::Error; /// Error type for decoding Tor objects from bytes. -#[derive(Error, Debug, PartialEq, Eq, Clone)] +#[derive(Error, Debug, Clone)] #[non_exhaustive] pub enum Error { /// Tried to read something, but we didn't find enough bytes. @@ -25,5 +25,18 @@ pub enum Error { /// We use this one in lieu of calling assert() and expect() and /// unwrap() from within parsing code. #[error("internal programming error")] - Internal, + Internal(#[from] tor_error::InternalError), +} + +impl PartialEq for Error { + fn eq(&self, other: &Self) -> bool { + use Error::*; + match (self, other) { + (Truncated, Truncated) => true, + (ExtraneousBytes, ExtraneousBytes) => true, + (BadMessage(a), BadMessage(b)) => a == b, + // notably, this means that an internal error is equal to nothing, not even itself. + (_, _) => false, + } + } } diff --git a/crates/tor-bytes/src/impls.rs b/crates/tor-bytes/src/impls.rs index 56930b099..e040b7569 100644 --- a/crates/tor-bytes/src/impls.rs +++ b/crates/tor-bytes/src/impls.rs @@ -240,7 +240,8 @@ mod rsa_impls { impl Readable for RsaIdentity { fn take_from(b: &mut Reader<'_>) -> Result { let m = b.take(RSA_ID_LEN)?; - Ok(RsaIdentity::from_bytes(m).expect("take gave wrong length")) + RsaIdentity::from_bytes(m) + .ok_or_else(|| tor_error::internal!("wrong number of bytes from take").into()) } } }