| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | import ( |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | ) |
| 12 | |
| 13 | var _ Packet = (*SourceDescription)(nil) // assert is a Packet |
| 14 | |
| 15 | func TestSourceDescriptionUnmarshal(t *testing.T) { |
| 16 | for _, test := range []struct { |
| 17 | Name string |
| 18 | Data []byte |
| 19 | Want SourceDescription |
| 20 | WantError error |
| 21 | }{ |
| 22 | { |
| 23 | Name: "nil", |
| 24 | Data: nil, |
| 25 | WantError: errPacketTooShort, |
| 26 | }, |
| 27 | { |
| 28 | Name: "no chunks", |
| 29 | Data: []byte{ |
| 30 | // v=2, p=0, count=1, SDES, len=8 |
| 31 | 0x80, 0xca, 0x00, 0x04, |
| 32 | }, |
| 33 | Want: SourceDescription{ |
| 34 | Chunks: nil, |
| 35 | }, |
| 36 | }, |
| 37 | { |
| 38 | Name: "missing type", |
| 39 | Data: []byte{ |
| 40 | // v=2, p=0, count=1, SDES, len=8 |
| 41 | 0x81, 0xca, 0x00, 0x08, |
| 42 | // ssrc=0x00000000 |
| 43 | 0x00, 0x00, 0x00, 0x00, |
| 44 | }, |
| 45 | WantError: errPacketTooShort, |
| 46 | }, |
| 47 | { |
| 48 | Name: "bad cname length", |
| 49 | Data: []byte{ |
| 50 | // v=2, p=0, count=1, SDES, len=10 |
| 51 | 0x81, 0xca, 0x00, 0x0a, |
| 52 | // ssrc=0x00000000 |
| 53 | 0x00, 0x00, 0x00, 0x00, |
| 54 | // CNAME, len = 1 |
| 55 | 0x01, 0x01, |
| 56 | }, |
| 57 | WantError: errPacketTooShort, |
| 58 | }, |
| 59 | { |
| 60 | Name: "short cname", |
| 61 | Data: []byte{ |
| 62 | // v=2, p=0, count=1, SDES, len=9 |
| 63 | 0x81, 0xca, 0x00, 0x09, |
| 64 | // ssrc=0x00000000 |
| 65 | 0x00, 0x00, 0x00, 0x00, |
| 66 | // CNAME, Missing length |
| 67 | 0x01, |
| 68 | }, |
| 69 | WantError: errPacketTooShort, |
| 70 | }, |
| 71 | { |
| 72 | Name: "no end", |
| 73 | Data: []byte{ |
| 74 | // v=2, p=0, count=1, SDES, len=11 |
| 75 | 0x81, 0xca, 0x00, 0x0b, |
| 76 | // ssrc=0x00000000 |
| 77 | 0x00, 0x00, 0x00, 0x00, |
| 78 | // CNAME, len=1, content=A |
| 79 | 0x01, 0x02, 0x41, |
| 80 | // Missing END |
| 81 | }, |
| 82 | WantError: errPacketTooShort, |
| 83 | }, |
| 84 | { |
| 85 | Name: "bad octet count", |
| 86 | Data: []byte{ |
| 87 | // v=2, p=0, count=1, SDES, len=10 |
| 88 | 0x81, 0xca, 0x00, 0x0a, |
| 89 | // ssrc=0x00000000 |
| 90 | 0x00, 0x00, 0x00, 0x00, |
| 91 | // CNAME, len=1 |
| 92 | 0x01, 0x01, |
| 93 | }, |
| 94 | WantError: errPacketTooShort, |
| 95 | }, |
| 96 | { |
| 97 | Name: "zero item chunk", |
| 98 | Data: []byte{ |
| 99 | // v=2, p=0, count=1, SDES, len=12 |
| 100 | 0x81, 0xca, 0x00, 0x0c, |
| 101 | // ssrc=0x01020304 |
| 102 | 0x01, 0x02, 0x03, 0x04, |
| 103 | // END + padding |
| 104 | 0x00, 0x00, 0x00, 0x00, |
| 105 | }, |
| 106 | Want: SourceDescription{ |
| 107 | Chunks: []SourceDescriptionChunk{{ |
| 108 | Source: 0x01020304, |
| 109 | Items: nil, |
| 110 | }}, |
| 111 | }, |
| 112 | }, |
| 113 | { |
| 114 | Name: "wrong type", |
| 115 | Data: []byte{ |
| 116 | // v=2, p=0, count=1, SR, len=12 |
| 117 | 0x81, 0xc8, 0x00, 0x0c, |
| 118 | // ssrc=0x01020304 |
| 119 | 0x01, 0x02, 0x03, 0x04, |
| 120 | // END + padding |
| 121 | 0x00, 0x00, 0x00, 0x00, |
| 122 | }, |
| 123 | WantError: errWrongType, |
| 124 | }, |
| 125 | { |
| 126 | Name: "bad count in header", |
| 127 | Data: []byte{ |
| 128 | // v=2, p=0, count=1, SDES, len=12 |
| 129 | 0x81, 0xca, 0x00, 0x0c, |
| 130 | }, |
| 131 | WantError: errInvalidHeader, |
| 132 | }, |
| 133 | { |
| 134 | Name: "empty string", |
| 135 | Data: []byte{ |
| 136 | // v=2, p=0, count=1, SDES, len=12 |
| 137 | 0x81, 0xca, 0x00, 0x0c, |
| 138 | // ssrc=0x01020304 |
| 139 | 0x01, 0x02, 0x03, 0x04, |
| 140 | // CNAME, len=0 |
| 141 | 0x01, 0x00, |
| 142 | // END + padding |
| 143 | 0x00, 0x00, |
| 144 | }, |
| 145 | Want: *NewCNAMESourceDescription(0x01020304, ""), |
| 146 | }, |
| 147 | { |
| 148 | Name: "two items", |
| 149 | Data: []byte{ |
| 150 | // v=2, p=0, count=1, SDES, len=16 |
| 151 | 0x81, 0xca, 0x00, 0x10, |
| 152 | // ssrc=0x10000000 |
| 153 | 0x10, 0x00, 0x00, 0x00, |
| 154 | // CNAME, len=1, content=A |
| 155 | 0x01, 0x01, 0x41, |
| 156 | // PHONE, len=1, content=B |
| 157 | 0x04, 0x01, 0x42, |
| 158 | // END + padding |
| 159 | 0x00, 0x00, |
| 160 | }, |
| 161 | Want: SourceDescription{ |
| 162 | Chunks: []SourceDescriptionChunk{ |
| 163 | { |
| 164 | Source: 0x10000000, |
| 165 | Items: []SourceDescriptionItem{ |
| 166 | { |
| 167 | Type: SDESCNAME, |
| 168 | Text: "A", |
| 169 | }, |
| 170 | { |
| 171 | Type: SDESPhone, |
| 172 | Text: "B", |
| 173 | }, |
| 174 | }, |
| 175 | }, |
| 176 | }, |
| 177 | }, |
| 178 | }, |
| 179 | { |
| 180 | Name: "two chunks", |
| 181 | Data: []byte{ |
| 182 | // v=2, p=0, count=2, SDES, len=24 |
| 183 | 0x82, 0xca, 0x00, 0x18, |
| 184 | // ssrc=0x01020304 |
| 185 | 0x01, 0x02, 0x03, 0x04, |
| 186 | // Chunk 1 |
| 187 | // CNAME, len=1, content=A |
| 188 | 0x01, 0x01, 0x41, |
| 189 | // END |
| 190 | 0x00, |
| 191 | // Chunk 2 |
| 192 | // SSRC 0x05060708 |
| 193 | 0x05, 0x06, 0x07, 0x08, |
| 194 | // CNAME, len=3, content=BCD |
| 195 | 0x01, 0x03, 0x42, 0x43, 0x44, |
| 196 | // END |
| 197 | 0x00, 0x00, 0x00, |
| 198 | }, |
| 199 | Want: SourceDescription{ |
| 200 | Chunks: []SourceDescriptionChunk{ |
| 201 | { |
| 202 | Source: 0x01020304, |
| 203 | Items: []SourceDescriptionItem{ |
| 204 | { |
| 205 | Type: SDESCNAME, |
| 206 | Text: "A", |
| 207 | }, |
| 208 | }, |
| 209 | }, |
| 210 | { |
| 211 | Source: 0x05060708, |
| 212 | Items: []SourceDescriptionItem{ |
| 213 | { |
| 214 | Type: SDESCNAME, |
| 215 | Text: "BCD", |
| 216 | }, |
| 217 | }, |
| 218 | }, |
| 219 | }, |
| 220 | }, |
| 221 | }, |
| 222 | } { |
| 223 | var sdes SourceDescription |
| 224 | err := sdes.Unmarshal(test.Data) |
| 225 | assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q", test.Name) |
| 226 | assert.Equalf(t, test.Want, sdes, "Unmarshal %q", test.Name) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | func TestSourceDescriptionRoundTrip(t *testing.T) { |
| 231 | // a slice with enough SourceDescriptionChunks to overflow an 5-bit int |
| 232 | var tooManyChunks []SourceDescriptionChunk |
| 233 | var tooLongText strings.Builder |
| 234 | |
| 235 | for range 1 << 5 { |
| 236 | tooManyChunks = append(tooManyChunks, SourceDescriptionChunk{}) |
| 237 | } |
| 238 | for range 1 << 8 { |
| 239 | tooLongText.WriteString("x") |
| 240 | } |
| 241 | |
| 242 | for _, test := range []struct { |
| 243 | Name string |
| 244 | Desc SourceDescription |
| 245 | WantError error |
| 246 | }{ |
| 247 | { |
| 248 | Name: "valid", |
| 249 | Desc: SourceDescription{ |
| 250 | Chunks: []SourceDescriptionChunk{ |
| 251 | { |
| 252 | Source: 1, |
| 253 | Items: []SourceDescriptionItem{ |
| 254 | { |
| 255 | Type: SDESCNAME, |
| 256 | Text: "[email protected]", |
| 257 | }, |
| 258 | }, |
| 259 | }, |
| 260 | { |
| 261 | Source: 2, |
| 262 | Items: []SourceDescriptionItem{ |
| 263 | { |
| 264 | Type: SDESNote, |
| 265 | Text: "some note", |
| 266 | }, |
| 267 | { |
| 268 | Type: SDESNote, |
| 269 | Text: "another note", |
| 270 | }, |
| 271 | }, |
| 272 | }, |
| 273 | }, |
| 274 | }, |
| 275 | }, |
| 276 | { |
| 277 | Name: "item without type", |
| 278 | Desc: SourceDescription{ |
| 279 | Chunks: []SourceDescriptionChunk{{ |
| 280 | Source: 1, |
| 281 | Items: []SourceDescriptionItem{{ |
| 282 | Text: "[email protected]", |
| 283 | }}, |
| 284 | }}, |
| 285 | }, |
| 286 | WantError: errSDESMissingType, |
| 287 | }, |
| 288 | { |
| 289 | Name: "zero items", |
| 290 | Desc: SourceDescription{ |
| 291 | Chunks: []SourceDescriptionChunk{{ |
| 292 | Source: 1, |
| 293 | }}, |
| 294 | }, |
| 295 | }, |
| 296 | { |
| 297 | Name: "email item", |
| 298 | Desc: SourceDescription{ |
| 299 | Chunks: []SourceDescriptionChunk{{ |
| 300 | Source: 1, |
| 301 | Items: []SourceDescriptionItem{{ |
| 302 | Type: SDESEmail, |
| 303 | Text: "[email protected]", |
| 304 | }}, |
| 305 | }}, |
| 306 | }, |
| 307 | }, |
| 308 | { |
| 309 | Name: "empty text", |
| 310 | Desc: *NewCNAMESourceDescription(1, ""), |
| 311 | }, |
| 312 | { |
| 313 | Name: "text too long", |
| 314 | Desc: SourceDescription{ |
| 315 | Chunks: []SourceDescriptionChunk{{ |
| 316 | Items: []SourceDescriptionItem{{ |
| 317 | Type: SDESCNAME, |
| 318 | Text: tooLongText.String(), |
| 319 | }}, |
| 320 | }}, |
| 321 | }, |
| 322 | WantError: errSDESTextTooLong, |
| 323 | }, |
| 324 | { |
| 325 | Name: "count overflow", |
| 326 | Desc: SourceDescription{ |
| 327 | Chunks: tooManyChunks, |
| 328 | }, |
| 329 | WantError: errTooManyChunks, |
| 330 | }, |
| 331 | } { |
| 332 | data, err := test.Desc.Marshal() |
| 333 | assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name) |
| 334 | if err != nil { |
| 335 | continue |
| 336 | } |
| 337 | |
| 338 | var decoded SourceDescription |
| 339 | assert.NoErrorf(t, decoded.Unmarshal(data), "Unmarshal %q", test.Name) |
| 340 | assert.Equalf(t, test.Desc, decoded, "%s sdes round trip mismatch", test.Name) |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | func TestSourceDescriptionRoundTripMaxLength(t *testing.T) { |
| 345 | const maxPacketLength = 4 * (0xFFFF + 1) |
| 346 | remainingItemBytes := maxPacketLength - headerLength - sdesSourceLen - sdesTypeLen |
| 347 | fullItemLength := sdesTypeLen + sdesOctetCountLen + sdesMaxOctetCount |
| 348 | fullItemCount := remainingItemBytes / fullItemLength |
| 349 | lastItemLength := remainingItemBytes % fullItemLength |
| 350 | |
| 351 | items := make([]SourceDescriptionItem, 0, fullItemCount+1) |
| 352 | maxText := strings.Repeat("x", sdesMaxOctetCount) |
| 353 | for range fullItemCount { |
| 354 | items = append(items, SourceDescriptionItem{Type: SDESNote, Text: maxText}) |
| 355 | } |
| 356 | if lastItemLength > 0 { |
| 357 | items = append(items, SourceDescriptionItem{ |
| 358 | Type: SDESNote, |
| 359 | Text: strings.Repeat("y", lastItemLength-sdesTypeLen-sdesOctetCountLen), |
| 360 | }) |
| 361 | } |
| 362 | |
| 363 | desc := SourceDescription{ |
| 364 | Chunks: []SourceDescriptionChunk{{ |
| 365 | Source: 1, |
| 366 | Items: items, |
| 367 | }}, |
| 368 | } |
| 369 | |
| 370 | data, err := desc.Marshal() |
| 371 | assert.NoError(t, err) |
| 372 | assert.Len(t, data, maxPacketLength) |
| 373 | |
| 374 | var decoded SourceDescription |
| 375 | assert.NoError(t, decoded.Unmarshal(data)) |
| 376 | assert.Equal(t, desc, decoded) |
| 377 | } |