1 use super::*; 2 3 #[test] 4 fn test_goodbye_unmarshal() { 5 let tests = vec![ 6 ( 7 "valid", 8 Bytes::from_static(&[ 9 0x81, 0xcb, 0x00, 0x0c, // v=2, p=0, count=1, BYE, len=12 10 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e 11 0x03, 0x46, 0x4f, 0x4f, // len=3, text=FOO 12 ]), 13 Goodbye { 14 sources: vec![0x902f9e2e], 15 reason: Bytes::from_static(b"FOO"), 16 }, 17 None, 18 ), 19 ( 20 "invalid octet count", 21 Bytes::from_static(&[ 22 0x81, 0xcb, 0x00, 0x0c, // v=2, p=0, count=1, BYE, len=12 23 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e 24 0x04, 0x46, 0x4f, 0x4f, // len=4, text=FOO 25 ]), 26 Goodbye { 27 sources: vec![], 28 reason: Bytes::from_static(b""), 29 }, 30 Some(Error::PacketTooShort), 31 ), 32 ( 33 "wrong type", 34 Bytes::from_static(&[ 35 0x81, 0xca, 0x00, 0x0c, // v=2, p=0, count=1, SDES, len=12 36 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e 37 0x03, 0x46, 0x4f, 0x4f, // len=3, text=FOO 38 ]), 39 Goodbye { 40 sources: vec![], 41 reason: Bytes::from_static(b""), 42 }, 43 Some(Error::WrongType), 44 ), 45 ( 46 "short reason", 47 Bytes::from_static(&[ 48 0x81, 0xcb, 0x00, 0x0c, // v=2, p=0, count=1, BYE, len=12 49 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e 50 0x01, 0x46, 0x00, 0x00, // len=3, text=F + padding 51 ]), 52 Goodbye { 53 sources: vec![0x902f9e2e], 54 reason: Bytes::from_static(b"F"), 55 }, 56 None, 57 ), 58 ( 59 "not byte aligned", 60 Bytes::from_static(&[ 61 0x81, 0xcb, 0x00, 0x0a, // v=2, p=0, count=1, BYE, len=10 62 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e 63 0x01, 0x46, // len=1, text=F 64 ]), 65 Goodbye { 66 sources: vec![], 67 reason: Bytes::from_static(b""), 68 }, 69 Some(Error::PacketTooShort), 70 ), 71 ( 72 "bad count in header", 73 Bytes::from_static(&[ 74 0x82, 0xcb, 0x00, 0x0c, // v=2, p=0, count=2, BYE, len=8 75 0x90, 0x2f, 0x9e, 0x2e, // ssrc=0x902f9e2e 76 ]), 77 Goodbye { 78 sources: vec![], 79 reason: Bytes::from_static(b""), 80 }, 81 Some(Error::PacketTooShort), 82 ), 83 ( 84 "empty packet", 85 Bytes::from_static(&[ 86 // v=2, p=0, count=0, BYE, len=4 87 0x80, 0xcb, 0x00, 0x04, 88 ]), 89 Goodbye { 90 sources: vec![], 91 reason: Bytes::from_static(b""), 92 }, 93 None, 94 ), 95 ( 96 "nil", 97 Bytes::from_static(&[]), 98 Goodbye { 99 sources: vec![], 100 reason: Bytes::from_static(b""), 101 }, 102 Some(Error::PacketTooShort), 103 ), 104 ]; 105 106 for (name, mut data, want, want_error) in tests { 107 let got = Goodbye::unmarshal(&mut data); 108 109 assert_eq!( 110 got.is_err(), 111 want_error.is_some(), 112 "Unmarshal {} bye: err = {:?}, want {:?}", 113 name, 114 got, 115 want_error 116 ); 117 118 if let Some(err) = want_error { 119 let got_err = got.err().unwrap(); 120 assert_eq!( 121 err, got_err, 122 "Unmarshal {} rr: err = {:?}, want {:?}", 123 name, got_err, err, 124 ); 125 } else { 126 let actual = got.unwrap(); 127 assert_eq!( 128 actual, want, 129 "Unmarshal {} rr: got {:?}, want {:?}", 130 name, actual, want 131 ); 132 } 133 } 134 } 135 136 #[test] 137 fn test_goodbye_round_trip() { 138 let too_many_sources = vec![0u32; 1 << 5]; 139 140 let mut too_long_text = String::new(); 141 for _ in 0..1 << 8 { 142 too_long_text.push('x'); 143 } 144 145 let tests = vec![ 146 ( 147 "empty", 148 Goodbye { 149 sources: vec![], 150 ..Default::default() 151 }, 152 None, 153 ), 154 ( 155 "valid", 156 Goodbye { 157 sources: vec![0x01020304, 0x05060708], 158 reason: Bytes::from_static(b"because"), 159 }, 160 None, 161 ), 162 ( 163 "empty reason", 164 Goodbye { 165 sources: vec![0x01020304], 166 reason: Bytes::from_static(b""), 167 }, 168 None, 169 ), 170 ( 171 "reason no source", 172 Goodbye { 173 sources: vec![], 174 reason: Bytes::from_static(b"foo"), 175 }, 176 None, 177 ), 178 ( 179 "short reason", 180 Goodbye { 181 sources: vec![], 182 reason: Bytes::from_static(b"f"), 183 }, 184 None, 185 ), 186 ( 187 "count overflow", 188 Goodbye { 189 sources: too_many_sources, 190 reason: Bytes::from_static(b""), 191 }, 192 Some(Error::TooManySources), 193 ), 194 ( 195 "reason too long", 196 Goodbye { 197 sources: vec![], 198 reason: Bytes::copy_from_slice(too_long_text.as_bytes()), 199 }, 200 Some(Error::ReasonTooLong), 201 ), 202 ]; 203 204 for (name, want, want_error) in tests { 205 let got = want.marshal(); 206 207 assert_eq!( 208 got.is_ok(), 209 want_error.is_none(), 210 "Marshal {}: err = {:?}, want {:?}", 211 name, 212 got, 213 want_error 214 ); 215 216 if let Some(err) = want_error { 217 let got_err = got.err().unwrap(); 218 assert_eq!( 219 err, got_err, 220 "Unmarshal {} rr: err = {:?}, want {:?}", 221 name, got_err, err, 222 ); 223 } else { 224 let mut data = got.ok().unwrap(); 225 let actual = 226 Goodbye::unmarshal(&mut data).unwrap_or_else(|_| panic!("Unmarshal {}", name)); 227 228 assert_eq!( 229 actual, want, 230 "{} round trip: got {:?}, want {:?}", 231 name, actual, want 232 ) 233 } 234 } 235 } 236