| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "slices" |
| 9 | "testing" |
| 10 | |
| 11 | "github.com/stretchr/testify/assert" |
| 12 | ) |
| 13 | |
| 14 | var _ Packet = (*SenderReport)(nil) // assert is a Packet |
| 15 | |
| 16 | func TestSenderReportUnmarshal(t *testing.T) { |
| 17 | for _, test := range []struct { |
| 18 | Name string |
| 19 | Data []byte |
| 20 | Want SenderReport |
| 21 | WantError error |
| 22 | }{ |
| 23 | { |
| 24 | Name: "nil", |
| 25 | Data: nil, |
| 26 | WantError: errPacketTooShort, |
| 27 | }, |
| 28 | { |
| 29 | Name: "valid", |
| 30 | Data: []byte{ |
| 31 | // v=2, p=0, count=1, SR, len=7 |
| 32 | 0x81, 0xc8, 0x0, 0x7, |
| 33 | // ssrc=0x902f9e2e |
| 34 | 0x90, 0x2f, 0x9e, 0x2e, |
| 35 | // ntp=0xda8bd1fcdddda05a |
| 36 | 0xda, 0x8b, 0xd1, 0xfc, |
| 37 | 0xdd, 0xdd, 0xa0, 0x5a, |
| 38 | // rtp=0xaaf4edd5 |
| 39 | 0xaa, 0xf4, 0xed, 0xd5, |
| 40 | // packetCount=1 |
| 41 | 0x00, 0x00, 0x00, 0x01, |
| 42 | // octetCount=2 |
| 43 | 0x00, 0x00, 0x00, 0x02, |
| 44 | // ssrc=0xbc5e9a40 |
| 45 | 0xbc, 0x5e, 0x9a, 0x40, |
| 46 | // fracLost=0, totalLost=0 |
| 47 | 0x0, 0x0, 0x0, 0x0, |
| 48 | // lastSeq=0x46e1 |
| 49 | 0x0, 0x0, 0x46, 0xe1, |
| 50 | // jitter=273 |
| 51 | 0x0, 0x0, 0x1, 0x11, |
| 52 | // lsr=0x9f36432 |
| 53 | 0x9, 0xf3, 0x64, 0x32, |
| 54 | // delay=150137 |
| 55 | 0x0, 0x2, 0x4a, 0x79, |
| 56 | }, |
| 57 | Want: SenderReport{ |
| 58 | SSRC: 0x902f9e2e, |
| 59 | NTPTime: 0xda8bd1fcdddda05a, |
| 60 | RTPTime: 0xaaf4edd5, |
| 61 | PacketCount: 1, |
| 62 | OctetCount: 2, |
| 63 | Reports: []ReceptionReport{{ |
| 64 | SSRC: 0xbc5e9a40, |
| 65 | FractionLost: 0, |
| 66 | TotalLost: 0, |
| 67 | LastSequenceNumber: 0x46e1, |
| 68 | Jitter: 273, |
| 69 | LastSenderReport: 0x9f36432, |
| 70 | Delay: 150137, |
| 71 | }}, |
| 72 | }, |
| 73 | }, |
| 74 | { |
| 75 | Name: "wrong type", |
| 76 | Data: []byte{ |
| 77 | // v=2, p=0, count=1, RR, len=7 |
| 78 | 0x81, 0xc9, 0x0, 0x7, |
| 79 | // ssrc=0x902f9e2e |
| 80 | 0x90, 0x2f, 0x9e, 0x2e, |
| 81 | // ntp=0xda8bd1fcdddda05a |
| 82 | 0xda, 0x8b, 0xd1, 0xfc, |
| 83 | 0xdd, 0xdd, 0xa0, 0x5a, |
| 84 | // rtp=0xaaf4edd5 |
| 85 | 0xaa, 0xf4, 0xed, 0xd5, |
| 86 | // packetCount=1 |
| 87 | 0x00, 0x00, 0x00, 0x01, |
| 88 | // octetCount=2 |
| 89 | 0x00, 0x00, 0x00, 0x02, |
| 90 | // ssrc=0xbc5e9a40 |
| 91 | 0xbc, 0x5e, 0x9a, 0x40, |
| 92 | // fracLost=0, totalLost=0 |
| 93 | 0x0, 0x0, 0x0, 0x0, |
| 94 | // lastSeq=0x46e1 |
| 95 | 0x0, 0x0, 0x46, 0xe1, |
| 96 | // jitter=273 |
| 97 | 0x0, 0x0, 0x1, 0x11, |
| 98 | // lsr=0x9f36432 |
| 99 | 0x9, 0xf3, 0x64, 0x32, |
| 100 | // delay=150137 |
| 101 | 0x0, 0x2, 0x4a, 0x79, |
| 102 | }, |
| 103 | WantError: errWrongType, |
| 104 | }, |
| 105 | { |
| 106 | Name: "bad count in header", |
| 107 | Data: []byte{ |
| 108 | // v=2, p=0, count=1, SR, len=7 |
| 109 | 0x82, 0xc8, 0x0, 0x7, |
| 110 | // ssrc=0x902f9e2e |
| 111 | 0x90, 0x2f, 0x9e, 0x2e, |
| 112 | // ntp=0xda8bd1fcdddda05a |
| 113 | 0xda, 0x8b, 0xd1, 0xfc, |
| 114 | 0xdd, 0xdd, 0xa0, 0x5a, |
| 115 | // rtp=0xaaf4edd5 |
| 116 | 0xaa, 0xf4, 0xed, 0xd5, |
| 117 | // packetCount=1 |
| 118 | 0x00, 0x00, 0x00, 0x01, |
| 119 | // octetCount=2 |
| 120 | 0x00, 0x00, 0x00, 0x02, |
| 121 | // ssrc=0xbc5e9a40 |
| 122 | 0xbc, 0x5e, 0x9a, 0x40, |
| 123 | // fracLost=0, totalLost=0 |
| 124 | 0x0, 0x0, 0x0, 0x0, |
| 125 | // lastSeq=0x46e1 |
| 126 | 0x0, 0x0, 0x46, 0xe1, |
| 127 | // jitter=273 |
| 128 | 0x0, 0x0, 0x1, 0x11, |
| 129 | // lsr=0x9f36432 |
| 130 | 0x9, 0xf3, 0x64, 0x32, |
| 131 | // delay=150137 |
| 132 | 0x0, 0x2, 0x4a, 0x79, |
| 133 | }, |
| 134 | WantError: errPacketTooShort, |
| 135 | }, |
| 136 | { |
| 137 | Name: "with extension", // issue #447 |
| 138 | Data: []byte{ |
| 139 | // v=2, p=0, count=0, SR, len=6 |
| 140 | 0x80, 0xc8, 0x0, 0x6, |
| 141 | // ssrc=0x2b7ec0c5 |
| 142 | 0x2b, 0x7e, 0xc0, 0xc5, |
| 143 | // ntp=0xe020a2a952a53fc0 |
| 144 | 0xe0, 0x20, 0xa2, 0xa9, |
| 145 | 0x52, 0xa5, 0x3f, 0xc0, |
| 146 | // rtp=0x2e48a552 |
| 147 | 0x2e, 0x48, 0xa5, 0x52, |
| 148 | // packetCount=70 |
| 149 | 0x0, 0x0, 0x0, 0x46, |
| 150 | // octetCount=4637 |
| 151 | 0x0, 0x0, 0x12, 0x1d, |
| 152 | // profile-specific extension |
| 153 | 0x81, 0xca, 0x0, 0x6, |
| 154 | 0x2b, 0x7e, 0xc0, 0xc5, |
| 155 | 0x1, 0x10, 0x4c, 0x63, |
| 156 | 0x49, 0x66, 0x7a, 0x58, |
| 157 | 0x6f, 0x6e, 0x44, 0x6f, |
| 158 | 0x72, 0x64, 0x53, 0x65, |
| 159 | 0x57, 0x36, 0x0, 0x0, |
| 160 | }, |
| 161 | Want: SenderReport{ |
| 162 | SSRC: 0x2b7ec0c5, |
| 163 | NTPTime: 0xe020a2a952a53fc0, |
| 164 | RTPTime: 0x2e48a552, |
| 165 | PacketCount: 70, |
| 166 | OctetCount: 4637, |
| 167 | ProfileExtensions: []byte{ |
| 168 | 0x81, 0xca, 0x0, 0x6, |
| 169 | 0x2b, 0x7e, 0xc0, 0xc5, |
| 170 | 0x1, 0x10, 0x4c, 0x63, |
| 171 | 0x49, 0x66, 0x7a, 0x58, |
| 172 | 0x6f, 0x6e, 0x44, 0x6f, |
| 173 | 0x72, 0x64, 0x53, 0x65, |
| 174 | 0x57, 0x36, 0x0, 0x0, |
| 175 | }, |
| 176 | }, |
| 177 | WantError: nil, |
| 178 | }, |
| 179 | } { |
| 180 | var sr SenderReport |
| 181 | err := sr.Unmarshal(test.Data) |
| 182 | assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q sr", test.Name) |
| 183 | if err != nil { |
| 184 | continue |
| 185 | } |
| 186 | assert.Equalf(t, test.Want, sr, "Unmarshal %q sr", test.Name) |
| 187 | |
| 188 | var ssrcFound bool |
| 189 | dstSsrc := sr.DestinationSSRC() |
| 190 | if slices.Contains(dstSsrc, sr.SSRC) { |
| 191 | ssrcFound = true |
| 192 | } |
| 193 | |
| 194 | assert.Truef(t, ssrcFound, "Unmarshal %q sr: sr's DestinationSSRC should include it's SSRC field", test.Name) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | func TestSenderReportRoundTrip(t *testing.T) { |
| 199 | for _, test := range []struct { |
| 200 | Name string |
| 201 | Report SenderReport |
| 202 | WantError error |
| 203 | }{ |
| 204 | { |
| 205 | Name: "valid", |
| 206 | Report: SenderReport{ |
| 207 | SSRC: 1, |
| 208 | NTPTime: 999, |
| 209 | RTPTime: 555, |
| 210 | PacketCount: 32, |
| 211 | OctetCount: 11, |
| 212 | Reports: []ReceptionReport{ |
| 213 | { |
| 214 | SSRC: 2, |
| 215 | FractionLost: 2, |
| 216 | TotalLost: 3, |
| 217 | LastSequenceNumber: 4, |
| 218 | Jitter: 5, |
| 219 | LastSenderReport: 6, |
| 220 | Delay: 7, |
| 221 | }, |
| 222 | { |
| 223 | SSRC: 0, |
| 224 | }, |
| 225 | }, |
| 226 | }, |
| 227 | }, |
| 228 | { |
| 229 | Name: "also valid", |
| 230 | Report: SenderReport{ |
| 231 | SSRC: 2, |
| 232 | Reports: []ReceptionReport{ |
| 233 | { |
| 234 | SSRC: 999, |
| 235 | FractionLost: 30, |
| 236 | TotalLost: 12345, |
| 237 | LastSequenceNumber: 99, |
| 238 | Jitter: 22, |
| 239 | LastSenderReport: 92, |
| 240 | Delay: 46, |
| 241 | }, |
| 242 | }, |
| 243 | }, |
| 244 | }, |
| 245 | { |
| 246 | Name: "extension", |
| 247 | Report: SenderReport{ |
| 248 | SSRC: 2, |
| 249 | Reports: []ReceptionReport{ |
| 250 | { |
| 251 | SSRC: 999, |
| 252 | FractionLost: 30, |
| 253 | TotalLost: 12345, |
| 254 | LastSequenceNumber: 99, |
| 255 | Jitter: 22, |
| 256 | LastSenderReport: 92, |
| 257 | Delay: 46, |
| 258 | }, |
| 259 | }, |
| 260 | ProfileExtensions: []byte{1, 2, 3, 4}, |
| 261 | }, |
| 262 | }, |
| 263 | { |
| 264 | Name: "count overflow", |
| 265 | Report: SenderReport{ |
| 266 | SSRC: 1, |
| 267 | Reports: tooManyReports(), |
| 268 | }, |
| 269 | WantError: errTooManyReports, |
| 270 | }, |
| 271 | } { |
| 272 | data, err := test.Report.Marshal() |
| 273 | assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name) |
| 274 | if err != nil { |
| 275 | continue |
| 276 | } |
| 277 | |
| 278 | var decoded SenderReport |
| 279 | assert.NoErrorf(t, decoded.Unmarshal(data), "Unmarshal %q", test.Name) |
| 280 | assert.Equalf(t, test.Report, decoded, "%q sr round trip", test.Name) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | func TestSenderReportRoundTripMaxLength(t *testing.T) { |
| 285 | report := SenderReport{ |
| 286 | SSRC: 1, |
| 287 | NTPTime: 2, |
| 288 | RTPTime: 3, |
| 289 | PacketCount: 4, |
| 290 | OctetCount: 5, |
| 291 | Reports: make([]ReceptionReport, countMax), |
| 292 | } |
| 293 | |
| 294 | for i := range report.Reports { |
| 295 | report.Reports[i] = ReceptionReport{SSRC: uint32(i + 1)} |
| 296 | } |
| 297 | |
| 298 | baseSize := report.MarshalSize() |
| 299 | report.ProfileExtensions = bytes.Repeat([]byte{0xab}, 4*(0xFFFF+1)-baseSize) |
| 300 | |
| 301 | data, err := report.Marshal() |
| 302 | assert.NoError(t, err) |
| 303 | assert.Len(t, data, 4*(0xFFFF+1)) |
| 304 | |
| 305 | var decoded SenderReport |
| 306 | assert.NoError(t, decoded.Unmarshal(data)) |
| 307 | assert.Equal(t, report, decoded) |
| 308 | } |