| 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 = (*SliceLossIndication)(nil) // assert is a Packet |
| 13 | |
| 14 | func TestSliceLossIndicationUnmarshal(t *testing.T) { |
| 15 | for _, test := range []struct { |
| 16 | Name string |
| 17 | Data []byte |
| 18 | Want SliceLossIndication |
| 19 | WantError error |
| 20 | }{ |
| 21 | { |
| 22 | Name: "valid", |
| 23 | Data: []byte{ |
| 24 | // SliceLossIndication |
| 25 | 0x82, 0xcd, 0x0, 0x3, |
| 26 | // sender=0x902f9e2e |
| 27 | 0x90, 0x2f, 0x9e, 0x2e, |
| 28 | // media=0x902f9e2e |
| 29 | 0x90, 0x2f, 0x9e, 0x2e, |
| 30 | // nack 0xAAAA, 0x5555 |
| 31 | 0x55, 0x50, 0x00, 0x2C, |
| 32 | }, |
| 33 | Want: SliceLossIndication{ |
| 34 | SenderSSRC: 0x902f9e2e, |
| 35 | MediaSSRC: 0x902f9e2e, |
| 36 | SLI: []SLIEntry{{0xaaa, 0, 0x2C}}, |
| 37 | }, |
| 38 | }, |
| 39 | { |
| 40 | Name: "short report", |
| 41 | Data: []byte{ |
| 42 | 0x81, 0xcd, 0x0, 0x2, |
| 43 | // ssrc=0x902f9e2e |
| 44 | 0x90, 0x2f, 0x9e, 0x2e, |
| 45 | // report ends early |
| 46 | }, |
| 47 | WantError: errPacketTooShort, |
| 48 | }, |
| 49 | { |
| 50 | Name: "wrong type", |
| 51 | Data: []byte{ |
| 52 | // v=2, p=0, count=1, SR, len=7 |
| 53 | 0x81, 0xc8, 0x0, 0x7, |
| 54 | // ssrc=0x902f9e2e |
| 55 | 0x90, 0x2f, 0x9e, 0x2e, |
| 56 | // ssrc=0xbc5e9a40 |
| 57 | 0xbc, 0x5e, 0x9a, 0x40, |
| 58 | // fracLost=0, totalLost=0 |
| 59 | 0x0, 0x0, 0x0, 0x0, |
| 60 | // lastSeq=0x46e1 |
| 61 | 0x0, 0x0, 0x46, 0xe1, |
| 62 | // jitter=273 |
| 63 | 0x0, 0x0, 0x1, 0x11, |
| 64 | // lsr=0x9f36432 |
| 65 | 0x9, 0xf3, 0x64, 0x32, |
| 66 | // delay=150137 |
| 67 | 0x0, 0x2, 0x4a, 0x79, |
| 68 | }, |
| 69 | WantError: errWrongType, |
| 70 | }, |
| 71 | { |
| 72 | Name: "nil", |
| 73 | Data: nil, |
| 74 | WantError: errPacketTooShort, |
| 75 | }, |
| 76 | } { |
| 77 | var sli SliceLossIndication |
| 78 | err := sli.Unmarshal(test.Data) |
| 79 | assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q", test.Name) |
| 80 | if err != nil { |
| 81 | continue |
| 82 | } |
| 83 | |
| 84 | assert.Equalf(t, test.Want, sli, "Unmarshal %q rr", test.Name) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestSliceLossIndicationRoundTrip(t *testing.T) { |
| 89 | for _, test := range []struct { |
| 90 | Name string |
| 91 | Report SliceLossIndication |
| 92 | WantError error |
| 93 | }{ |
| 94 | { |
| 95 | Name: "valid", |
| 96 | Report: SliceLossIndication{ |
| 97 | SenderSSRC: 0x902f9e2e, |
| 98 | MediaSSRC: 0x902f9e2e, |
| 99 | SLI: []SLIEntry{{1, 0xAA, 0x1F}, {1034, 0x05, 0x6}}, |
| 100 | }, |
| 101 | }, |
| 102 | } { |
| 103 | data, err := test.Report.Marshal() |
| 104 | assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name) |
| 105 | if err != nil { |
| 106 | continue |
| 107 | } |
| 108 | |
| 109 | var decoded SliceLossIndication |
| 110 | assert.NoErrorf(t, decoded.Unmarshal(data), "Unmarshal %q", test.Name) |
| 111 | assert.Equalf(t, test.Report, decoded, "%q sli round trip mismatch", test.Name) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | func TestSliceLossIndicationUnmarshalMaxLength(t *testing.T) { |
| 116 | rawPacket := make([]byte, 4*(0xFFFF+1)) |
| 117 | rawPacket[0] = 0x82 |
| 118 | rawPacket[1] = 0xcd |
| 119 | rawPacket[2] = 0xff |
| 120 | rawPacket[3] = 0xff |
| 121 | |
| 122 | var sli SliceLossIndication |
| 123 | err := sli.Unmarshal(rawPacket) |
| 124 | assert.NoError(t, err) |
| 125 | assert.Len(t, sli.SLI, 0xFFFF-sliLength) |
| 126 | } |