xref: /webrtc/sctp/src/param/param_test.rs (revision 5d8fe953)
1 use super::*;
2 
3 ///////////////////////////////////////////////////////////////////
4 //param_type_test
5 ///////////////////////////////////////////////////////////////////
6 use super::param_type::*;
7 
8 #[test]
test_parse_param_type_success() -> Result<()>9 fn test_parse_param_type_success() -> Result<()> {
10     let tests = vec![
11         (Bytes::from_static(&[0x0, 0x1]), ParamType::HeartbeatInfo),
12         (Bytes::from_static(&[0x0, 0xd]), ParamType::OutSsnResetReq),
13     ];
14 
15     for (mut binary, expected) in tests {
16         let pt: ParamType = binary.get_u16().into();
17         assert_eq!(pt, expected);
18     }
19 
20     Ok(())
21 }
22 
23 ///////////////////////////////////////////////////////////////////
24 //param_header_test
25 ///////////////////////////////////////////////////////////////////
26 use super::param_header::*;
27 
28 static PARAM_HEADER_BYTES: Bytes = Bytes::from_static(&[0x0, 0x1, 0x0, 0x4]);
29 
30 #[test]
test_param_header_success() -> Result<()>31 fn test_param_header_success() -> Result<()> {
32     let tests = vec![(
33         PARAM_HEADER_BYTES.clone(),
34         ParamHeader {
35             typ: ParamType::HeartbeatInfo,
36             value_length: 0,
37         },
38     )];
39 
40     for (binary, parsed) in tests {
41         let actual = ParamHeader::unmarshal(&binary)?;
42         assert_eq!(actual, parsed);
43         let b = actual.marshal()?;
44         assert_eq!(b, binary);
45     }
46 
47     Ok(())
48 }
49 
50 #[test]
test_param_header_unmarshal_failure() -> Result<()>51 fn test_param_header_unmarshal_failure() -> Result<()> {
52     let tests = vec![
53         ("header too short", PARAM_HEADER_BYTES.slice(..2)),
54         // {"wrong param type", []byte{0x0, 0x0, 0x0, 0x4}}, // Not possible to fail parseParamType atm.
55         (
56             "reported length below header length",
57             Bytes::from_static(&[0x0, 0xd, 0x0, 0x3]),
58         ),
59         ("wrong reported length", CHUNK_RECONFIG_PARAM_A.slice(0..4)),
60     ];
61 
62     for (name, binary) in tests {
63         let result = ParamHeader::unmarshal(&binary);
64         assert!(result.is_err(), "expected unmarshal: {name} to fail.");
65     }
66 
67     Ok(())
68 }
69 
70 ///////////////////////////////////////////////////////////////////
71 //param_forward_tsn_supported_test
72 ///////////////////////////////////////////////////////////////////
73 use super::param_forward_tsn_supported::*;
74 
75 static PARAM_FORWARD_TSN_SUPPORTED_BYTES: Bytes = Bytes::from_static(&[0xc0, 0x0, 0x0, 0x4]);
76 
77 #[test]
test_param_forward_tsn_supported_success() -> Result<()>78 fn test_param_forward_tsn_supported_success() -> Result<()> {
79     let tests = vec![(
80         PARAM_FORWARD_TSN_SUPPORTED_BYTES.clone(),
81         ParamForwardTsnSupported {},
82     )];
83 
84     for (binary, parsed) in tests {
85         let actual = ParamForwardTsnSupported::unmarshal(&binary)?;
86         assert_eq!(actual, parsed);
87         let b = actual.marshal()?;
88         assert_eq!(b, binary);
89     }
90 
91     Ok(())
92 }
93 
94 #[test]
test_param_forward_tsn_supported_failure() -> Result<()>95 fn test_param_forward_tsn_supported_failure() -> Result<()> {
96     let tests = vec![("param too short", Bytes::from_static(&[0x0, 0xd, 0x0]))];
97 
98     for (name, binary) in tests {
99         let result = ParamForwardTsnSupported::unmarshal(&binary);
100         assert!(result.is_err(), "expected unmarshal: {name} to fail.");
101     }
102 
103     Ok(())
104 }
105 
106 ///////////////////////////////////////////////////////////////////
107 //param_outgoing_reset_request_test
108 ///////////////////////////////////////////////////////////////////
109 use super::param_outgoing_reset_request::*;
110 
111 static CHUNK_RECONFIG_PARAM_A: Bytes = Bytes::from_static(&[
112     0x0, 0xd, 0x0, 0x16, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x3, 0x0, 0x4, 0x0,
113     0x5, 0x0, 0x6,
114 ]);
115 static CHUNK_RECONFIG_PARAM_B: Bytes = Bytes::from_static(&[
116     0x0, 0xd, 0x0, 0x10, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x3,
117 ]);
118 
119 #[test]
test_param_outgoing_reset_request_success() -> Result<()>120 fn test_param_outgoing_reset_request_success() -> Result<()> {
121     let tests = vec![
122         (
123             CHUNK_RECONFIG_PARAM_A.clone(),
124             ParamOutgoingResetRequest {
125                 reconfig_request_sequence_number: 1,
126                 reconfig_response_sequence_number: 2,
127                 sender_last_tsn: 3,
128                 stream_identifiers: vec![4, 5, 6],
129             },
130         ),
131         (
132             CHUNK_RECONFIG_PARAM_B.clone(),
133             ParamOutgoingResetRequest {
134                 reconfig_request_sequence_number: 1,
135                 reconfig_response_sequence_number: 2,
136                 sender_last_tsn: 3,
137                 stream_identifiers: vec![],
138             },
139         ),
140     ];
141 
142     for (binary, parsed) in tests {
143         let actual = ParamOutgoingResetRequest::unmarshal(&binary)?;
144         assert_eq!(actual, parsed);
145         let b = actual.marshal()?;
146         assert_eq!(b, binary);
147     }
148 
149     Ok(())
150 }
151 
152 #[test]
test_param_outgoing_reset_request_failure() -> Result<()>153 fn test_param_outgoing_reset_request_failure() -> Result<()> {
154     let tests = vec![
155         ("packet too short", CHUNK_RECONFIG_PARAM_A.slice(..8)),
156         ("param too short", Bytes::from_static(&[0x0, 0xd, 0x0, 0x4])),
157     ];
158 
159     for (name, binary) in tests {
160         let result = ParamOutgoingResetRequest::unmarshal(&binary);
161         assert!(result.is_err(), "expected unmarshal: {name} to fail.");
162     }
163 
164     Ok(())
165 }
166 
167 ///////////////////////////////////////////////////////////////////
168 //param_reconfig_response_test
169 ///////////////////////////////////////////////////////////////////
170 use super::param_reconfig_response::*;
171 use bytes::Buf;
172 
173 static CHUNK_RECONFIG_RESPONCE: Bytes =
174     Bytes::from_static(&[0x0, 0x10, 0x0, 0xc, 0x0, 0x0, 0x0, 0x1, 0x0, 0x0, 0x0, 0x1]);
175 
176 #[test]
test_param_reconfig_response_success() -> Result<()>177 fn test_param_reconfig_response_success() -> Result<()> {
178     let tests = vec![(
179         CHUNK_RECONFIG_RESPONCE.clone(),
180         ParamReconfigResponse {
181             reconfig_response_sequence_number: 1,
182             result: ReconfigResult::SuccessPerformed,
183         },
184     )];
185 
186     for (binary, parsed) in tests {
187         let actual = ParamReconfigResponse::unmarshal(&binary)?;
188         assert_eq!(actual, parsed);
189         let b = actual.marshal()?;
190         assert_eq!(b, binary);
191     }
192 
193     Ok(())
194 }
195 
196 #[test]
test_param_reconfig_response_failure() -> Result<()>197 fn test_param_reconfig_response_failure() -> Result<()> {
198     let tests = vec![
199         ("packet too short", CHUNK_RECONFIG_RESPONCE.slice(..8)),
200         (
201             "param too short",
202             Bytes::from_static(&[0x0, 0x10, 0x0, 0x4]),
203         ),
204     ];
205 
206     for (name, binary) in tests {
207         let result = ParamReconfigResponse::unmarshal(&binary);
208         assert!(result.is_err(), "expected unmarshal: {name} to fail.");
209     }
210 
211     Ok(())
212 }
213 
214 #[test]
test_reconfig_result_stringer() -> Result<()>215 fn test_reconfig_result_stringer() -> Result<()> {
216     let tests = vec![
217         (ReconfigResult::SuccessNop, "0: Success - Nothing to do"),
218         (ReconfigResult::SuccessPerformed, "1: Success - Performed"),
219         (ReconfigResult::Denied, "2: Denied"),
220         (ReconfigResult::ErrorWrongSsn, "3: Error - Wrong SSN"),
221         (
222             ReconfigResult::ErrorRequestAlreadyInProgress,
223             "4: Error - Request already in progress",
224         ),
225         (
226             ReconfigResult::ErrorBadSequenceNumber,
227             "5: Error - Bad Sequence Number",
228         ),
229         (ReconfigResult::InProgress, "6: In progress"),
230     ];
231 
232     for (result, expected) in tests {
233         let actual = result.to_string();
234         assert_eq!(actual, expected, "Test case {expected}");
235     }
236 
237     Ok(())
238 }
239 
240 ///////////////////////////////////////////////////////////////////
241 //param_test
242 ///////////////////////////////////////////////////////////////////
243 
244 #[test]
test_build_param_success() -> Result<()>245 fn test_build_param_success() -> Result<()> {
246     let tests = vec![CHUNK_RECONFIG_PARAM_A.clone()];
247 
248     for binary in tests {
249         let p = build_param(&binary)?;
250         let b = p.marshal()?;
251         assert_eq!(b, binary);
252     }
253 
254     Ok(())
255 }
256 
257 #[test]
test_build_param_failure() -> Result<()>258 fn test_build_param_failure() -> Result<()> {
259     let tests = vec![
260         ("invalid ParamType", Bytes::from_static(&[0x0, 0x0])),
261         ("build failure", CHUNK_RECONFIG_PARAM_A.slice(..8)),
262     ];
263 
264     for (name, binary) in tests {
265         let result = build_param(&binary);
266         assert!(result.is_err(), "expected unmarshal: {name} to fail.");
267     }
268 
269     Ok(())
270 }
271