xref: /webrtc/rtcp/src/goodbye/goodbye_test.rs (revision 259fddd2)
1 use super::*;
2 
3 use std::io::BufReader;
4 
5 use util::Error;
6 
7 #[test]
8 fn test_goodbye_unmarshal() -> Result<(), Error> {
9     let tests = vec![
10         (
11             "valid",
12             vec![
13                 // v=2, p=0, count=1, BYE, len=12
14                 0x81, 0xcb, 0x00, 0x0c, // ssrc=0x902f9e2e
15                 0x90, 0x2f, 0x9e, 0x2e, // len=3, text=FOO
16                 0x03, 0x46, 0x4f, 0x4f,
17             ],
18             Goodbye {
19                 sources: vec![0x902f9e2e],
20                 reason: "FOO".to_string(),
21             },
22             None,
23         ),
24         (
25             "invalid octet count",
26             vec![
27                 // v=2, p=0, count=1, BYE, len=12
28                 0x81, 0xcb, 0x00, 0x0c, // ssrc=0x902f9e2e
29                 0x90, 0x2f, 0x9e, 0x2e, // len=4, text=FOO
30                 0x04, 0x46, 0x4f, 0x4f,
31             ],
32             Goodbye {
33                 sources: vec![],
34                 reason: "".to_string(),
35             },
36             Some(ERR_PACKET_TOO_SHORT.clone()),
37         ),
38         (
39             "wrong type",
40             vec![
41                 // v=2, p=0, count=1, SDES, len=12
42                 0x81, 0xca, 0x00, 0x0c, // ssrc=0x902f9e2e
43                 0x90, 0x2f, 0x9e, 0x2e, // len=3, text=FOO
44                 0x03, 0x46, 0x4f, 0x4f,
45             ],
46             Goodbye {
47                 sources: vec![],
48                 reason: "".to_string(),
49             },
50             Some(ERR_WRONG_TYPE.clone()),
51         ),
52         (
53             "short reason",
54             vec![
55                 // v=2, p=0, count=1, BYE, len=12
56                 0x81, 0xcb, 0x00, 0x0c, // ssrc=0x902f9e2e
57                 0x90, 0x2f, 0x9e, 0x2e, // len=3, text=F + padding
58                 0x01, 0x46, 0x00, 0x00,
59             ],
60             Goodbye {
61                 sources: vec![0x902f9e2e],
62                 reason: "F".to_string(),
63             },
64             None,
65         ),
66         (
67             "not byte aligned",
68             vec![
69                 // v=2, p=0, count=1, BYE, len=10
70                 0x81, 0xcb, 0x00, 0x0a, // ssrc=0x902f9e2e
71                 0x90, 0x2f, 0x9e, 0x2e, // len=1, text=F
72                 0x01, 0x46,
73             ],
74             Goodbye {
75                 sources: vec![],
76                 reason: "".to_string(),
77             },
78             Some(ERR_FAILED_TO_FILL_WHOLE_BUFFER.clone()),
79         ),
80         (
81             "bad count in header",
82             vec![
83                 // v=2, p=0, count=2, BYE, len=8
84                 0x82, 0xcb, 0x00, 0x0c, // ssrc=0x902f9e2e
85                 0x90, 0x2f, 0x9e, 0x2e,
86             ],
87             Goodbye {
88                 sources: vec![],
89                 reason: "".to_string(),
90             },
91             Some(ERR_FAILED_TO_FILL_WHOLE_BUFFER.clone()),
92         ),
93         (
94             "empty packet",
95             vec![
96                 // v=2, p=0, count=0, BYE, len=4
97                 0x80, 0xcb, 0x00, 0x04,
98             ],
99             Goodbye {
100                 sources: vec![],
101                 reason: "".to_string(),
102             },
103             None,
104         ),
105         (
106             "nil",
107             vec![],
108             Goodbye {
109                 sources: vec![],
110                 reason: "".to_string(),
111             },
112             Some(ERR_FAILED_TO_FILL_WHOLE_BUFFER.clone()),
113         ),
114     ];
115 
116     for (name, data, want, want_error) in tests {
117         let mut reader = BufReader::new(data.as_slice());
118         let result = Goodbye::unmarshal(&mut reader);
119         if let Some(err) = want_error {
120             if let Err(got) = result {
121                 assert_eq!(
122                     got, err,
123                     "Unmarshal {} header: err = {}, want {}",
124                     name, got, err
125                 );
126             } else {
127                 assert!(false, "want error in test {}", name);
128             }
129         } else {
130             if let Ok(got) = result {
131                 assert_eq!(
132                     got, want,
133                     "Unmarshal {} header: got {:?}, want {:?}",
134                     name, got, want,
135                 )
136             } else {
137                 assert!(false, "must no error in test {}", name);
138             }
139         }
140     }
141 
142     Ok(())
143 }
144