| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | import ( |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/stretchr/testify/assert" |
| 10 | ) |
| 11 | |
| 12 | var _ Packet = (*ReceiverReport)(nil) // assert is a Packet |
| 13 | |
| 14 | func TestReceiverReportUnmarshal(t *testing.T) { |
| 15 | for _, test := range []struct { |
| 16 | Name string |
| 17 | Data []byte |
| 18 | Want ReceiverReport |
| 19 | WantError error |
| 20 | }{ |
| 21 | { |
| 22 | Name: "valid", |
| 23 | Data: []byte{ |
| 24 | // v=2, p=0, count=1, RR, len=7 |
| 25 | 0x81, 0xc9, 0x0, 0x7, |
| 26 | // ssrc=0x902f9e2e |
| 27 | 0x90, 0x2f, 0x9e, 0x2e, |
| 28 | // ssrc=0xbc5e9a40 |
| 29 | 0xbc, 0x5e, 0x9a, 0x40, |
| 30 | // fracLost=0, totalLost=0 |
| 31 | 0x0, 0x0, 0x0, 0x0, |
| 32 | // lastSeq=0x46e1 |
| 33 | 0x0, 0x0, 0x46, 0xe1, |
| 34 | // jitter=273 |
| 35 | 0x0, 0x0, 0x1, 0x11, |
| 36 | // lsr=0x9f36432 |
| 37 | 0x9, 0xf3, 0x64, 0x32, |
| 38 | // delay=150137 |
| 39 | 0x0, 0x2, 0x4a, 0x79, |
| 40 | }, |
| 41 | Want: ReceiverReport{ |
| 42 | SSRC: 0x902f9e2e, |
| 43 | Reports: []ReceptionReport{{ |
| 44 | SSRC: 0xbc5e9a40, |
| 45 | FractionLost: 0, |
| 46 | TotalLost: 0, |
| 47 | LastSequenceNumber: 0x46e1, |
| 48 | Jitter: 273, |
| 49 | LastSenderReport: 0x9f36432, |
| 50 | Delay: 150137, |
| 51 | }}, |
| 52 | ProfileExtensions: []byte{}, |
| 53 | }, |
| 54 | }, |
| 55 | { |
| 56 | Name: "valid with extension data", |
| 57 | Data: []byte{ |
| 58 | // v=2, p=0, count=1, RR, len=9 |
| 59 | 0x81, 0xc9, 0x0, 0x9, |
| 60 | // ssrc=0x902f9e2e |
| 61 | 0x90, 0x2f, 0x9e, 0x2e, |
| 62 | // ssrc=0xbc5e9a40 |
| 63 | 0xbc, 0x5e, 0x9a, 0x40, |
| 64 | // fracLost=0, totalLost=0 |
| 65 | 0x0, 0x0, 0x0, 0x0, |
| 66 | // lastSeq=0x46e1 |
| 67 | 0x0, 0x0, 0x46, 0xe1, |
| 68 | // jitter=273 |
| 69 | 0x0, 0x0, 0x1, 0x11, |
| 70 | // lsr=0x9f36432 |
| 71 | 0x9, 0xf3, 0x64, 0x32, |
| 72 | // delay=150137 |
| 73 | 0x0, 0x2, 0x4a, 0x79, |
| 74 | // profile-specific extension data |
| 75 | 0x54, 0x45, 0x53, 0x54, |
| 76 | 0x44, 0x41, 0x54, 0x41, |
| 77 | }, |
| 78 | Want: ReceiverReport{ |
| 79 | SSRC: 0x902f9e2e, |
| 80 | Reports: []ReceptionReport{{ |
| 81 | SSRC: 0xbc5e9a40, |
| 82 | FractionLost: 0, |
| 83 | TotalLost: 0, |
| 84 | LastSequenceNumber: 0x46e1, |
| 85 | Jitter: 273, |
| 86 | LastSenderReport: 0x9f36432, |
| 87 | Delay: 150137, |
| 88 | }}, |
| 89 | ProfileExtensions: []byte{ |
| 90 | 0x54, 0x45, 0x53, 0x54, |
| 91 | 0x44, 0x41, 0x54, 0x41, |
| 92 | }, |
| 93 | }, |
| 94 | }, |
| 95 | { |
| 96 | Name: "short report", |
| 97 | Data: []byte{ |
| 98 | // v=2, p=0, count=1, RR, len=7 |
| 99 | 0x81, 0xc9, 0x00, 0x0c, |
| 100 | // ssrc=0x902f9e2e |
| 101 | 0x90, 0x2f, 0x9e, 0x2e, |
| 102 | // fracLost=0, totalLost=0 |
| 103 | 0x00, 0x00, 0x00, 0x00, |
| 104 | // report ends early |
| 105 | }, |
| 106 | WantError: errPacketTooShort, |
| 107 | }, |
| 108 | { |
| 109 | Name: "wrong type", |
| 110 | Data: []byte{ |
| 111 | // v=2, p=0, count=1, SR, len=7 |
| 112 | 0x81, 0xc8, 0x0, 0x7, |
| 113 | // ssrc=0x902f9e2e |
| 114 | 0x90, 0x2f, 0x9e, 0x2e, |
| 115 | // ssrc=0xbc5e9a40 |
| 116 | 0xbc, 0x5e, 0x9a, 0x40, |
| 117 | // fracLost=0, totalLost=0 |
| 118 | 0x0, 0x0, 0x0, 0x0, |
| 119 | // lastSeq=0x46e1 |
| 120 | 0x0, 0x0, 0x46, 0xe1, |
| 121 | // jitter=273 |
| 122 | 0x0, 0x0, 0x1, 0x11, |
| 123 | // lsr=0x9f36432 |
| 124 | 0x9, 0xf3, 0x64, 0x32, |
| 125 | // delay=150137 |
| 126 | 0x0, 0x2, 0x4a, 0x79, |
| 127 | }, |
| 128 | WantError: errWrongType, |
| 129 | }, |
| 130 | { |
| 131 | Name: "bad count in header", |
| 132 | Data: []byte{ |
| 133 | // v=2, p=0, count=2, RR, len=7 |
| 134 | 0x82, 0xc9, 0x0, 0x7, |
| 135 | // ssrc=0x902f9e2e |
| 136 | 0x90, 0x2f, 0x9e, 0x2e, |
| 137 | // ssrc=0xbc5e9a40 |
| 138 | 0xbc, 0x5e, 0x9a, 0x40, |
| 139 | // fracLost=0, totalLost=0 |
| 140 | 0x0, 0x0, 0x0, 0x0, |
| 141 | // lastSeq=0x46e1 |
| 142 | 0x0, 0x0, 0x46, 0xe1, |
| 143 | // jitter=273 |
| 144 | 0x0, 0x0, 0x1, 0x11, |
| 145 | // lsr=0x9f36432 |
| 146 | 0x9, 0xf3, 0x64, 0x32, |
| 147 | // delay=150137 |
| 148 | 0x0, 0x2, 0x4a, 0x79, |
| 149 | }, |
| 150 | WantError: errInvalidHeader, |
| 151 | }, |
| 152 | { |
| 153 | Name: "nil", |
| 154 | Data: nil, |
| 155 | WantError: errPacketTooShort, |
| 156 | }, |
| 157 | } { |
| 158 | var rr ReceiverReport |
| 159 | err := rr.Unmarshal(test.Data) |
| 160 | assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q", test.Name) |
| 161 | if err != nil { |
| 162 | continue |
| 163 | } |
| 164 | |
| 165 | assert.Equalf(t, test.Want, rr, "Unmarshal %q", test.Name) |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | func tooManyReports() []ReceptionReport { |
| 170 | // a slice with enough ReceptionReports to overflow an 5-bit int |
| 171 | var tooManyReports []ReceptionReport |
| 172 | for range 1 << 5 { |
| 173 | tooManyReports = append(tooManyReports, ReceptionReport{ |
| 174 | SSRC: 2, |
| 175 | FractionLost: 2, |
| 176 | TotalLost: 3, |
| 177 | LastSequenceNumber: 4, |
| 178 | Jitter: 5, |
| 179 | LastSenderReport: 6, |
| 180 | Delay: 7, |
| 181 | }) |
| 182 | } |
| 183 | |
| 184 | return tooManyReports |
| 185 | } |
| 186 | |
| 187 | func TestReceiverReportRoundTrip(t *testing.T) { |
| 188 | for _, test := range []struct { |
| 189 | Name string |
| 190 | Report ReceiverReport |
| 191 | WantError error |
| 192 | }{ |
| 193 | { |
| 194 | Name: "valid", |
| 195 | Report: ReceiverReport{ |
| 196 | SSRC: 1, |
| 197 | Reports: []ReceptionReport{ |
| 198 | { |
| 199 | SSRC: 2, |
| 200 | FractionLost: 2, |
| 201 | TotalLost: 3, |
| 202 | LastSequenceNumber: 4, |
| 203 | Jitter: 5, |
| 204 | LastSenderReport: 6, |
| 205 | Delay: 7, |
| 206 | }, |
| 207 | { |
| 208 | SSRC: 0, |
| 209 | }, |
| 210 | }, |
| 211 | ProfileExtensions: []byte{}, |
| 212 | }, |
| 213 | }, |
| 214 | { |
| 215 | Name: "also valid", |
| 216 | Report: ReceiverReport{ |
| 217 | SSRC: 2, |
| 218 | Reports: []ReceptionReport{ |
| 219 | { |
| 220 | SSRC: 999, |
| 221 | FractionLost: 30, |
| 222 | TotalLost: 12345, |
| 223 | LastSequenceNumber: 99, |
| 224 | Jitter: 22, |
| 225 | LastSenderReport: 92, |
| 226 | Delay: 46, |
| 227 | }, |
| 228 | }, |
| 229 | ProfileExtensions: []byte{}, |
| 230 | }, |
| 231 | }, |
| 232 | { |
| 233 | Name: "totallost overflow", |
| 234 | Report: ReceiverReport{ |
| 235 | SSRC: 1, |
| 236 | Reports: []ReceptionReport{{ |
| 237 | TotalLost: 1 << 25, |
| 238 | }}, |
| 239 | }, |
| 240 | WantError: errInvalidTotalLost, |
| 241 | }, |
| 242 | { |
| 243 | Name: "count overflow", |
| 244 | Report: ReceiverReport{ |
| 245 | SSRC: 1, |
| 246 | Reports: tooManyReports(), |
| 247 | }, |
| 248 | WantError: errTooManyReports, |
| 249 | }, |
| 250 | } { |
| 251 | data, err := test.Report.Marshal() |
| 252 | assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name) |
| 253 | if err != nil { |
| 254 | continue |
| 255 | } |
| 256 | |
| 257 | var decoded ReceiverReport |
| 258 | assert.NoErrorf(t, decoded.Unmarshal(data), "Unmarshal %q", test.Name) |
| 259 | assert.Equalf(t, test.Report, decoded, "%s rr round trip mismatch", test.Name) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | func TestReceiverReportUnmarshalMaxLength(t *testing.T) { |
| 264 | rawPacket := make([]byte, 4*(0xFFFF+1)) |
| 265 | rawPacket[0] = 0x80 | countMax |
| 266 | rawPacket[1] = 0xc9 |
| 267 | rawPacket[2] = 0xff |
| 268 | rawPacket[3] = 0xff |
| 269 | |
| 270 | var rr ReceiverReport |
| 271 | err := rr.Unmarshal(rawPacket) |
| 272 | assert.NoError(t, err) |
| 273 | assert.Len(t, rr.Reports, countMax) |
| 274 | assert.Len(t, rr.ProfileExtensions, len(rawPacket)-rrReportOffset-countMax*receptionReportLength) |
| 275 | } |