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