sunset/
test.rs

1#[cfg(test)]
2mod tests {
3    use crate::error::Error;
4    use crate::packets::*;
5    use crate::packets::{Packet, ParseContext};
6    use crate::sshwire;
7    use crate::sshwire::BinString;
8    use simplelog::{self, LevelFilter, TestLogger};
9
10    pub fn init_log() {
11        let _ = TestLogger::init(LevelFilter::Trace, simplelog::Config::default());
12    }
13
14    fn test_roundtrip_packet(p: &Packet) -> Result<(), Error> {
15        init_log();
16        let mut buf1 = vec![99; 500];
17        let w1 = sshwire::write_ssh(&mut buf1, p)?;
18
19        let ctx = ParseContext::new();
20
21        let p2 = sshwire::packet_from_bytes(&buf1[..w1], &ctx)?;
22
23        let mut buf2 = vec![99; 500];
24        let _w2 = sshwire::write_ssh(&mut buf2, &p2)?;
25        // println!("{p:?}");
26        // println!("{p2:?}");
27        // println!("{:?}", buf1.hex_dump());
28        // println!("{:?}", buf2.hex_dump());
29
30        assert_eq!(buf1, buf2);
31        Ok(())
32    }
33
34    #[test]
35    fn roundtrip_kexinit() {
36        let k = KexInit {
37            cookie: KexCookie([
38                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
39            ]),
40            kex: "kex".try_into().unwrap(),
41            hostsig: "hostkey,another".try_into().unwrap(),
42            cipher_c2s: "chacha20-poly1305@openssh.com,aes128-ctr"
43                .try_into()
44                .unwrap(),
45            cipher_s2c: "blowfish".try_into().unwrap(),
46            mac_c2s: "hmac-sha1".try_into().unwrap(),
47            mac_s2c: "hmac-md5".try_into().unwrap(),
48            comp_c2s: "none".try_into().unwrap(),
49            comp_s2c: "".try_into().unwrap(),
50            lang_c2s: "".try_into().unwrap(),
51            lang_s2c: "".try_into().unwrap(),
52            first_follows: true,
53            reserved: 0x6148291e,
54        };
55        let p = Packet::KexInit(k);
56        test_roundtrip_packet(&p).unwrap();
57    }
58
59    #[test]
60    fn roundtrip_packet_kexdh() {
61        // XXX: this should break later if the q_c length is
62        let bs = BinString(&[0x11, 0x22, 0x33]);
63        let p = KexDHInit { q_c: bs }.into();
64
65        test_roundtrip_packet(&p).unwrap();
66        // packet format needs to be consistent
67        // test_roundtrip_packet(&p, KexType::DiffieHellman).unwrap_err();
68        // test_roundtrip_packet(&p, KexType::Unset).unwrap_err();
69    }
70}