aboutsummaryrefslogtreecommitdiffstats
path: root/src/path.rs
blob: a1aaeddbdbd5ecb50fa90c597fe1157cd5c32e20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::packet_content::PeerToPeerCipher;
use bytes::Bytes;

#[derive(PartialEq, Debug, Clone)]
pub struct Path {
    pub cipher: PeerToPeerCipher,
}

impl From<Bytes> for Path {
    fn from(value: Bytes) -> Self {
        Path {
            cipher: PeerToPeerCipher::from(value),
        }
    }
}

impl core::fmt::Display for Path {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_fmt(format_args!(
            "({:2x?}) -> ({:2x?}) MAC: {:4x?} ",
            self.cipher.source, self.cipher.destination, self.cipher.mac
        ))
    }
}

// Tests for std operations
#[cfg(test)]
mod tests {
    use crate::{
        packet::*,
        packet_content::{PacketContent, PeerToPeerCipher},
    };
    use core::str::FromStr;
    use hex::decode;
    use tinyvec::array_vec;

    use super::*;

    #[test]
    fn path() {
        let sample = "2107BA03127F7EA9221351768DD2DF32E1D02F5851379F5AFCC667AB273442FAB2943673F26DDBEB9595027474";

        let lhs_packet = Packet {
            route_type: RouteType::Flood,
            version: PayloadVersion::VersionOne,
            path: array_vec!([u16; 64] => 0xBA, 0x03, 0x12, 0x7F, 0x7E, 0xA9, 0x22),
            transport: [0, 0],
            raw_content: Bytes::copy_from_slice(
                &decode("1351768DD2DF32E1D02F5851379F5AFCC667AB273442FAB2943673F26DDBEB9595027474")
                    .unwrap(),
            ),
            content: PacketContent::Path(Path {
                cipher: PeerToPeerCipher {
                    destination: 0x13,
                    source: 0x51,
                    mac: 0x768D,
                    ciphertext: Bytes::copy_from_slice(
                        &decode("D2DF32E1D02F5851379F5AFCC667AB273442FAB2943673F26DDBEB9595027474")
                            .unwrap(),
                    ),
                    cleartext: None,
                },
            }),
            incomplete: false,
        };

        let rhs_packet = Packet::from_str(sample).unwrap();
        assert_eq!(lhs_packet, rhs_packet);

        #[cfg(feature = "std")]
        assert_eq!(
            format!("{}", lhs_packet),
            "  Flood  | v1 |            | [ba, 03, ... a9, 22] |   |    PATH    | (51) -> (13) MAC: 768d "
        );
    }
}