diff --git a/tor-checkable/src/timed.rs b/tor-checkable/src/timed.rs index 77fb06b5b..168d10e10 100644 --- a/tor-checkable/src/timed.rs +++ b/tor-checkable/src/timed.rs @@ -1,6 +1,6 @@ //! Convenience implementation of a TimeBound object. -use std::ops::RangeBounds; +use std::ops::{Bound, RangeBounds}; use std::time; /// A TimeBound object that is valid for a specified range of time. @@ -26,60 +26,54 @@ use std::time; /// Err(TimeValidityError::Expired(one_hour))); /// /// ``` -pub struct TimerangeBound -where - U: RangeBounds, -{ +pub struct TimerangeBound { obj: T, - range: U, + start: Option, + end: Option, } -impl TimerangeBound -where - U: RangeBounds, -{ - /// Construct a new TimerangeBound object from a given object and range. - pub fn new(obj: T, range: U) -> Self { - Self { obj, range } +fn unwrap_bound(b: Bound<&'_ time::SystemTime>) -> Option { + match b { + Bound::Included(x) => Some(*x), + Bound::Excluded(x) => Some(*x), + _ => None, } } -impl crate::Timebound for TimerangeBound -where - U: RangeBounds, -{ +impl TimerangeBound { + /// Construct a new TimerangeBound object from a given object and range. + /// + /// Note that we do not distinguish between inclusive and + /// exclusive bounds: `x..y` and `x..=y` are treated the same + /// here. + pub fn new(obj: T, range: U) -> Self + where + U: RangeBounds, + { + let start = unwrap_bound(range.start_bound()); + let end = unwrap_bound(range.end_bound()); + Self { obj, start, end } + } +} + +impl crate::Timebound for TimerangeBound { type Error = crate::TimeValidityError; fn is_valid_at(&self, t: &time::SystemTime) -> Result<(), Self::Error> { use crate::TimeValidityError; - use std::ops::Bound::{self, *}; - - fn unwrap_bound<'a, 'b>( - b: &'a Bound<&'b time::SystemTime>, - ) -> Option<&'b time::SystemTime> { - match b { - Included(x) => Some(x), - Excluded(x) => Some(x), - _ => None, - } - } - - if self.range.contains(t) { - return Ok(()); - } - - if let Some(end) = unwrap_bound(&self.range.end_bound()) { - if let Ok(d) = t.duration_since(*end) { - return Err(TimeValidityError::Expired(d)); - } - } - if let Some(start) = unwrap_bound(&self.range.start_bound()) { + if let Some(start) = self.start { if let Ok(d) = start.duration_since(*t) { return Err(TimeValidityError::NotYetValid(d)); } } - Err(TimeValidityError::Unspecified) + if let Some(end) = self.end { + if let Ok(d) = t.duration_since(end) { + return Err(TimeValidityError::Expired(d)); + } + } + + Ok(()) } fn dangerously_assume_timely(self) -> T { diff --git a/tor-netdoc/src/authcert.rs b/tor-netdoc/src/authcert.rs index 2cfff8cf9..c751a1869 100644 --- a/tor-netdoc/src/authcert.rs +++ b/tor-netdoc/src/authcert.rs @@ -78,8 +78,7 @@ pub struct AuthCert { /// An authority certificate whose signature and validity time we /// haven't checked. -pub type UncheckedAuthCert = - signed::SignatureGated>>; +pub type UncheckedAuthCert = signed::SignatureGated>; impl AuthCert { /// Parse an authority certificate from a string. diff --git a/tor-netdoc/src/netstatus.rs b/tor-netdoc/src/netstatus.rs index 5e15e4603..8a4c6a3b8 100644 --- a/tor-netdoc/src/netstatus.rs +++ b/tor-netdoc/src/netstatus.rs @@ -928,8 +928,7 @@ impl Signature { /// A MDConsensus object that has been parsed, but not checked for signatures /// and time. -pub type UncheckedMDConsensus = - TimerangeBound>; +pub type UncheckedMDConsensus = TimerangeBound; impl MDConsensus { /// Try to parse a single networkstatus document from a string. diff --git a/tor-netdoc/src/routerdesc.rs b/tor-netdoc/src/routerdesc.rs index 06b5c89f4..2c93ce43c 100644 --- a/tor-netdoc/src/routerdesc.rs +++ b/tor-netdoc/src/routerdesc.rs @@ -272,8 +272,7 @@ impl RouterAnnotation { /// A parsed router descriptor whose signatures and/or validity times /// may or may not be invalid. -pub type UncheckedRouterDesc = - signed::SignatureGated>>; +pub type UncheckedRouterDesc = signed::SignatureGated>; // XXXX use the correct value. Is it specified? const ROUTER_EXPIRY_SECONDS: u64 = 30 * 86400;