Skip to main content

h2/hpack/huffman/
mod.rs

1mod decode;
2mod encode;
3mod table;
4
5pub use self::decode::decode;
6pub use self::encode::encode;
7
8#[cfg(test)]
9mod test {
10    use super::*;
11
12    use bytes::BytesMut;
13    use rand::rngs::StdRng;
14    use rand::{Rng, SeedableRng};
15
16    fn decode(src: &[u8]) -> BytesMut {
17        let mut buf = BytesMut::new();
18        super::decode(src, &mut buf).unwrap()
19    }
20
21    #[test]
22    fn encode_decode_str() {
23        const DATA: &[&str] = &[
24            "hello world",
25            ":method",
26            ":scheme",
27            ":authority",
28            "yahoo.co.jp",
29            "GET",
30            "http",
31            ":path",
32            "/images/top/sp2/cmn/logo-ns-130528.png",
33            "example.com",
34            "hpack-test",
35            "xxxxxxx1",
36            "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:16.0) Gecko/20100101 Firefox/16.0",
37            "accept",
38            "Accept",
39            "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
40            "cookie",
41            "B=76j09a189a6h4&b=3&s=0b",
42            "TE",
43            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi non bibendum libero. \
44             Etiam ultrices lorem ut.",
45        ];
46
47        for s in DATA {
48            let mut dst = BytesMut::with_capacity(s.len());
49            encode(s.as_bytes(), &mut dst);
50            let decoded = decode(&dst);
51            assert_eq!(&decoded[..], s.as_bytes());
52        }
53    }
54
55    #[test]
56    fn encode_decode_u8() {
57        const DATA: &[&[u8]] = &[b"\0", b"\0\0\0", b"\0\x01\x02\x03\x04\x05", b"\xFF\xF8"];
58
59        for s in DATA {
60            let mut dst = BytesMut::with_capacity(s.len());
61            encode(s, &mut dst);
62            let decoded = decode(&dst);
63            assert_eq!(&decoded[..], &s[..]);
64        }
65    }
66
67    #[test]
68    fn encode_decode_all_octets() {
69        let src: Vec<_> = (0..=u8::MAX).collect();
70        let mut encoded = BytesMut::new();
71        encode(&src, &mut encoded);
72        assert_eq!(decode(&encoded), src);
73    }
74
75    // '0' has a shortest possible code (5 bits), so all-'0' strings expand
76    // the most when decoding, hitting the decoder's output reservation
77    // exactly.
78    #[test]
79    fn encode_decode_max_expansion() {
80        for len in 0..100 {
81            let src = vec![b'0'; len];
82            let mut encoded = BytesMut::new();
83            encode(&src, &mut encoded);
84            assert_eq!(&decode(&encoded)[..], &src[..], "len={}", len);
85        }
86    }
87
88    // Round-trip strings of random bytes of every length
89    #[test]
90    fn encode_decode_random() {
91        let mut rng = StdRng::seed_from_u64(0x123456789abcdef);
92
93        for len in 0..600 {
94            let src: Vec<u8> = (0..len).map(|_| rng.gen()).collect();
95            let mut encoded = BytesMut::new();
96            encode(&src, &mut encoded);
97            assert_eq!(&decode(&encoded)[..], &src[..], "len={}", len);
98        }
99    }
100}