tor_hsservice/
ipt_lid.rs

1//! [`IptLocalId`]
2
3use crate::internal_prelude::*;
4
5/// Persistent local identifier for an introduction point
6///
7/// Changes when the IPT relay changes, or the IPT key material changes.
8/// (Different for different `.onion` services, obviously)
9///
10/// Is a randomly-generated byte string, currently 32 long.
11#[derive(Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd, Deftly)]
12#[derive_deftly(SerdeStringOrTransparent)]
13#[cfg_attr(test, derive(derive_more::From))]
14pub(crate) struct IptLocalId([u8; 32]);
15
16impl_debug_hex!(IptLocalId.0);
17
18impl Display for IptLocalId {
19    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
20        for v in self.0 {
21            write!(f, "{v:02x}")?;
22        }
23        Ok(())
24    }
25}
26
27/// Invalid [`IptLocalId`] - for example bad string representation
28#[derive(Debug, Error, Clone, Eq, PartialEq)]
29#[error("invalid IptLocalId")]
30#[non_exhaustive]
31pub(crate) struct InvalidIptLocalId {}
32
33impl FromStr for IptLocalId {
34    type Err = InvalidIptLocalId;
35    fn from_str(s: &str) -> Result<Self, Self::Err> {
36        let mut b = [0; 32];
37        hex::decode_to_slice(s, &mut b).map_err(|_: hex::FromHexError| InvalidIptLocalId {})?;
38        Ok(IptLocalId(b))
39    }
40}
41
42impl KeySpecifierComponentViaDisplayFromStr for IptLocalId {}
43
44impl IptLocalId {
45    /// Return a fixed dummy `IptLocalId`, for testing etc.
46    ///
47    /// The id is made by repeating `which` 32 times.
48    #[cfg(test)]
49    pub(crate) fn dummy(which: u8) -> Self {
50        IptLocalId([which; 32]) // I can't think of a good way not to specify 32 again here
51    }
52}
53
54impl rand::distr::Distribution<IptLocalId> for rand::distr::StandardUniform {
55    fn sample<R: rand::Rng + ?Sized>(&self, rng: &mut R) -> IptLocalId {
56        IptLocalId(rng.random())
57    }
58}
59
60#[cfg(test)]
61pub(crate) mod test {
62    // @@ begin test lint list maintained by maint/add_warning @@
63    #![allow(clippy::bool_assert_comparison)]
64    #![allow(clippy::clone_on_copy)]
65    #![allow(clippy::dbg_macro)]
66    #![allow(clippy::mixed_attributes_style)]
67    #![allow(clippy::print_stderr)]
68    #![allow(clippy::print_stdout)]
69    #![allow(clippy::single_char_pattern)]
70    #![allow(clippy::unwrap_used)]
71    #![allow(clippy::unchecked_duration_subtraction)]
72    #![allow(clippy::useless_vec)]
73    #![allow(clippy::needless_pass_by_value)]
74    //! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
75    use super::*;
76    use itertools::{chain, Itertools};
77
78    #[derive(Serialize, Deserialize, Eq, PartialEq, Debug)]
79    struct IptLidTest {
80        lid: IptLocalId,
81    }
82
83    #[test]
84    fn lid_serde() {
85        let t = IptLidTest {
86            lid: IptLocalId::dummy(7),
87        };
88        let json = serde_json::to_string(&t).unwrap();
89        assert_eq!(
90            json,
91            // This also tests <IptLocalId as Display> since that's how we serialise it
92            r#"{"lid":"0707070707070707070707070707070707070707070707070707070707070707"}"#,
93        );
94        let u: IptLidTest = serde_json::from_str(&json).unwrap();
95        assert_eq!(t, u);
96
97        let mpack = rmp_serde::to_vec_named(&t).unwrap();
98        assert_eq!(
99            mpack,
100            chain!(&[129, 163], b"lid", &[220, 0, 32], &[0x07; 32],)
101                .cloned()
102                .collect_vec()
103        );
104        let u: IptLidTest = rmp_serde::from_slice(&mpack).unwrap();
105        assert_eq!(t, u);
106    }
107}