use std::sync::Arc;
use crate::types::misc::LongIdent;
use crate::util::intern::InternCache;
use crate::{Error, Result};
use tor_llcrypto::pk::rsa::RsaIdentity;
#[derive(Clone, Debug, Default, Hash, Eq, PartialEq)]
pub struct RelayFamily(Vec<RsaIdentity>);
static FAMILY_CACHE: InternCache<RelayFamily> = InternCache::new();
impl RelayFamily {
pub fn new() -> Self {
RelayFamily::default()
}
pub fn push(&mut self, rsa_id: RsaIdentity) {
self.0.push(rsa_id);
}
fn normalize(&mut self) {
self.0.sort();
self.0.dedup();
}
pub fn intern(mut self) -> Arc<Self> {
self.normalize();
FAMILY_CACHE.intern(self)
}
pub fn contains(&self, rsa_id: &RsaIdentity) -> bool {
self.0.contains(rsa_id)
}
pub fn members(&self) -> impl Iterator<Item = &RsaIdentity> {
self.0.iter()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl std::str::FromStr for RelayFamily {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
let v: Result<Vec<RsaIdentity>> = s
.split(crate::parse::tokenize::is_sp)
.map(|e| e.parse::<LongIdent>().map(|v| v.into()))
.filter(Result::is_ok)
.collect();
Ok(RelayFamily(v?))
}
}
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::mixed_attributes_style)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
#![allow(clippy::unchecked_duration_subtraction)]
#![allow(clippy::useless_vec)]
#![allow(clippy::needless_pass_by_value)]
use super::*;
use crate::Result;
#[test]
fn family() -> Result<()> {
let f = "nickname1 nickname2 $ffffffffffffffffffffffffffffffffffffffff=foo eeeeeeeeeeeeeeeeeeeEEEeeeeeeeeeeeeeeeeee ddddddddddddddddddddddddddddddddd $cccccccccccccccccccccccccccccccccccccccc~blarg ".parse::<RelayFamily>()?;
let v = vec![
RsaIdentity::from_bytes(
&hex::decode("ffffffffffffffffffffffffffffffffffffffff").unwrap()[..],
)
.unwrap(),
RsaIdentity::from_bytes(
&hex::decode("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee").unwrap()[..],
)
.unwrap(),
RsaIdentity::from_bytes(
&hex::decode("cccccccccccccccccccccccccccccccccccccccc").unwrap()[..],
)
.unwrap(),
];
assert_eq!(f.0, v);
Ok(())
}
#[test]
fn test_contains() -> Result<()> {
let family =
"ffffffffffffffffffffffffffffffffffffffff eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
.parse::<RelayFamily>()?;
let in_family = RsaIdentity::from_bytes(
&hex::decode("ffffffffffffffffffffffffffffffffffffffff").unwrap()[..],
)
.unwrap();
let not_in_family = RsaIdentity::from_bytes(
&hex::decode("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").unwrap()[..],
)
.unwrap();
assert!(family.contains(&in_family), "Relay not found in family");
assert!(
!family.contains(¬_in_family),
"Extra relay found in family"
);
Ok(())
}
#[test]
fn mutable() {
let mut family = RelayFamily::default();
let key = RsaIdentity::from_hex("ffffffffffffffffffffffffffffffffffffffff").unwrap();
assert!(!family.contains(&key));
family.push(key);
assert!(family.contains(&key));
}
}