Commit 53d5174e authored by Nick Mathewson's avatar Nick Mathewson 👁
Browse files

A few easy tests in tor-dirmgr.

parent 6484bc95
......@@ -66,3 +66,38 @@ pub(crate) fn default_authorities() -> Vec<Authority> {
auth("tor26", "14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4"),
]
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn authority() {
let key1: RsaIdentity = [9_u8; 20].into();
let key2: RsaIdentity = [10_u8; 20].into();
let auth = Authority::new("example".into(), key1.clone());
assert_eq!(auth.v3ident(), &key1);
let keyids1 = AuthCertKeyIds {
id_fingerprint: key1.clone(),
sk_fingerprint: key2.clone(),
};
assert!(auth.matches_keyid(&keyids1));
let keyids2 = AuthCertKeyIds {
id_fingerprint: key2.clone(),
sk_fingerprint: key2.clone(),
};
assert!(!auth.matches_keyid(&keyids2));
}
#[test]
fn auth() {
let dflt = default_authorities();
assert_eq!(&dflt[0].name[..], "bastet");
assert_eq!(
&dflt[0].v3ident.to_string()[..],
"$27102bc123e7af1d4741ae047e160c91adc76b21"
);
}
}
......@@ -56,9 +56,7 @@ impl RetryDelay {
/// If the base delay is less than 1000, a base delay of 1000 is
/// used instead, since that's what the C tor implemenation does.
pub fn from_msec(base_delay_msec: u32) -> Self {
// TODO: use Ord::clamp once we depend on rust 1.50
let low_bound_ms = std::cmp::max(MIN_LOW_BOUND, base_delay_msec);
let low_bound_ms = std::cmp::min(MAX_LOW_BOUND, low_bound_ms);
let low_bound_ms = base_delay_msec.clamp(MIN_LOW_BOUND, MAX_LOW_BOUND);
RetryDelay {
last_delay_ms: 0,
low_bound_ms,
......@@ -127,10 +125,7 @@ pub struct RetryConfig {
impl Default for RetryConfig {
fn default() -> Self {
RetryConfig {
num: 3.try_into().expect("3 is not 0"),
initial_delay: Duration::from_millis(1000),
}
RetryConfig::new(3, Duration::from_millis(1000))
}
}
......@@ -213,4 +208,28 @@ mod test {
assert!(delay < b_hi);
}
}
#[test]
fn config() {
// default configuration is 3 tries, 1000 msec initial delay
let cfg = RetryConfig::default();
assert_eq!(cfg.n_attempts(), 3);
let v: Vec<_> = cfg.attempts().collect();
assert_eq!(&v[..], &[0, 1, 2]);
let sched = cfg.schedule();
assert_eq!(sched.last_delay_ms, 0);
assert_eq!(sched.low_bound_ms, 1000);
// Try a zero-attempt schedule, and have it get remapped to 1,1
let cfg = RetryConfig::new(0, Duration::new(0, 0));
assert_eq!(cfg.n_attempts(), 1);
let v: Vec<_> = cfg.attempts().collect();
assert_eq!(&v[..], &[0]);
let sched = cfg.schedule();
assert_eq!(sched.last_delay_ms, 0);
assert_eq!(sched.low_bound_ms, 1000);
}
}
......@@ -9,6 +9,9 @@ use crate::{Error, Result};
/// Because you can't actually change a shared [`Arc`], this type implements
/// mutability by replacing the Arc itself with a new value. It tries
/// to avoid needless clones by taking advantage of [`Arc::new_mut`].
///
// We give this construction its own type to simplify its users, and make
// sure we don't hold the lock against any async suspend points.
#[derive(Debug)]
pub(crate) struct SharedMutArc<T> {
/// Locked reference to the current value.
......@@ -76,3 +79,33 @@ impl<T> SharedMutArc<T> {
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn shared_mut_arc() {
let val: SharedMutArc<Vec<u32>> = SharedMutArc::new();
assert_eq!(val.get(), None);
val.replace(Vec::new());
assert_eq!(val.get().unwrap().as_ref()[..], []);
val.mutate(|v| {
v.push(99);
Ok(())
})
.unwrap();
assert_eq!(val.get().unwrap().as_ref()[..], [99]);
val.clear();
assert_eq!(val.get(), None);
assert!(val
.mutate(|v| {
v.push(99);
Ok(())
})
.is_err());
}
}
......@@ -16,7 +16,8 @@ use std::{path::Path, str::Utf8Error};
/// not necessarily valid UTF-8.
pub struct DocumentText {
/// The underlying InputString. We only wrap this type to make it
/// opaque.
/// opaque to other crates, so they don't have to worry about the
/// implementation details.
s: InputString,
}
......@@ -86,8 +87,9 @@ impl InputString {
#[cfg(feature = "mmap")]
{
let mapping = unsafe {
// I'd rather have a safe option, but the crate that provides
// a safe API here is unix-only.
// I'd rather have a safe option, but that's not possible
// with mmap, since other processes could in theory replace
// the contents of the file while we're using it.
memmap::Mmap::map(&f)
};
if let Ok(m) = mapping {
......@@ -133,13 +135,16 @@ mod test {
#[test]
fn strings() {
let s: InputString = "Hello world".to_string().into();
assert_eq!(s.as_ref(), b"Hello world");
assert_eq!(s.as_str().unwrap(), "Hello world");
let s: InputString = b"Hello world".to_vec().into();
assert_eq!(s.as_ref(), b"Hello world");
assert_eq!(s.as_str().unwrap(), "Hello world");
// bad utf-8
let s: InputString = b"Hello \xff world".to_vec().into();
assert_eq!(s.as_ref(), b"Hello \xff world");
assert!(s.as_str().is_err());
}
......@@ -163,4 +168,17 @@ mod test {
let s = InputString::load(&badutf8);
assert!(s.is_err() || s.unwrap().as_str().is_err());
}
#[test]
fn doctext() {
let s: InputString = "Hello universe".to_string().into();
let dt: DocumentText = s.into();
assert_eq!(dt.as_ref(), b"Hello universe");
assert_eq!(dt.as_str(), Ok("Hello universe"));
let s: InputString = b"Hello \xff universe".to_vec().into();
let dt: DocumentText = s.into();
assert_eq!(dt.as_ref(), b"Hello \xff universe");
assert!(dt.as_str().is_err());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment