| 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 = (*TransportLayerNack)(nil) // assert is a Packet |
| 13 | |
| 14 | func TestTransportLayerNackUnmarshal(t *testing.T) { |
| 15 | for _, test := range []struct { |
| 16 | Name string |
| 17 | Data []byte |
| 18 | Want TransportLayerNack |
| 19 | WantError error |
| 20 | }{ |
| 21 | { |
| 22 | Name: "valid", |
| 23 | Data: []byte{ |
| 24 | // TransportLayerNack |
| 25 | 0x81, 0xcd, 0x0, 0x3, |
| 26 | // sender=0x902f9e2e |
| 27 | 0x90, 0x2f, 0x9e, 0x2e, |
| 28 | // media=0x902f9e2e |
| 29 | 0x90, 0x2f, 0x9e, 0x2e, |
| 30 | // nack 0xAAAA, 0x5555 |
| 31 | 0xaa, 0xaa, 0x55, 0x55, |
| 32 | }, |
| 33 | Want: TransportLayerNack{ |
| 34 | SenderSSRC: 0x902f9e2e, |
| 35 | MediaSSRC: 0x902f9e2e, |
| 36 | Nacks: []NackPair{{0xaaaa, 0x5555}}, |
| 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: "bad length", |
| 51 | Data: []byte{ |
| 52 | // TransportLayerNack |
| 53 | 0x81, 0xcd, 0x0, 0x2, |
| 54 | // sender=0x902f9e2e |
| 55 | 0x90, 0x2f, 0x9e, 0x2e, |
| 56 | // media=0x902f9e2e |
| 57 | 0x90, 0x2f, 0x9e, 0x2e, |
| 58 | }, |
| 59 | WantError: errBadLength, |
| 60 | }, |
| 61 | { |
| 62 | Name: "wrong type", |
| 63 | Data: []byte{ |
| 64 | // v=2, p=0, count=1, SR, len=7 |
| 65 | 0x81, 0xc8, 0x0, 0x7, |
| 66 | // ssrc=0x902f9e2e |
| 67 | 0x90, 0x2f, 0x9e, 0x2e, |
| 68 | // ssrc=0xbc5e9a40 |
| 69 | 0xbc, 0x5e, 0x9a, 0x40, |
| 70 | // fracLost=0, totalLost=0 |
| 71 | 0x0, 0x0, 0x0, 0x0, |
| 72 | // lastSeq=0x46e1 |
| 73 | 0x0, 0x0, 0x46, 0xe1, |
| 74 | // jitter=273 |
| 75 | 0x0, 0x0, 0x1, 0x11, |
| 76 | // lsr=0x9f36432 |
| 77 | 0x9, 0xf3, 0x64, 0x32, |
| 78 | // delay=150137 |
| 79 | 0x0, 0x2, 0x4a, 0x79, |
| 80 | }, |
| 81 | WantError: errWrongType, |
| 82 | }, |
| 83 | { |
| 84 | Name: "nil", |
| 85 | Data: nil, |
| 86 | WantError: errPacketTooShort, |
| 87 | }, |
| 88 | } { |
| 89 | var tln TransportLayerNack |
| 90 | err := tln.Unmarshal(test.Data) |
| 91 | assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q", test.Name) |
| 92 | assert.Equalf(t, test.Want, tln, "Unmarshal %q", test.Name) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestTransportLayerNackRoundTrip(t *testing.T) { |
| 97 | for _, test := range []struct { |
| 98 | Name string |
| 99 | Report TransportLayerNack |
| 100 | WantError error |
| 101 | }{ |
| 102 | { |
| 103 | Name: "valid", |
| 104 | Report: TransportLayerNack{ |
| 105 | SenderSSRC: 0x902f9e2e, |
| 106 | MediaSSRC: 0x902f9e2e, |
| 107 | Nacks: []NackPair{{1, 0xAA}, {1034, 0x05}}, |
| 108 | }, |
| 109 | }, |
| 110 | } { |
| 111 | data, err := test.Report.Marshal() |
| 112 | assert.ErrorIsf(t, err, test.WantError, "Marshal err on %q", test.Name) |
| 113 | |
| 114 | var decoded TransportLayerNack |
| 115 | assert.NoErrorf(t, decoded.Unmarshal(data), "Unmarshal %q", test.Name) |
| 116 | assert.Equalf(t, test.Report, decoded, "Unmarshal %q: decoded mismatch", test.Name) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func TestTransportLayerNackUnmarshalMaxLength(t *testing.T) { |
| 121 | rawPacket := make([]byte, 4*(0xFFFF+1)) |
| 122 | rawPacket[0] = 0x81 |
| 123 | rawPacket[1] = 0xcd |
| 124 | rawPacket[2] = 0xff |
| 125 | rawPacket[3] = 0xff |
| 126 | |
| 127 | var nack TransportLayerNack |
| 128 | err := nack.Unmarshal(rawPacket) |
| 129 | assert.NoError(t, err) |
| 130 | assert.Len(t, nack.Nacks, 0xFFFF-tlnLength) |
| 131 | } |
| 132 | |
| 133 | func testNackPair(t *testing.T, s []uint16, n NackPair) { |
| 134 | t.Helper() |
| 135 | |
| 136 | l := n.PacketList() |
| 137 | assert.Equalf(t, s, l, "NackPair %v mismatch", n) |
| 138 | } |
| 139 | |
| 140 | func TestNackPair(t *testing.T) { |
| 141 | testNackPair(t, []uint16{42}, NackPair{42, 0}) |
| 142 | testNackPair(t, []uint16{42, 43}, NackPair{42, 1}) |
| 143 | testNackPair(t, []uint16{42, 44}, NackPair{42, 2}) |
| 144 | testNackPair(t, []uint16{42, 43, 44}, NackPair{42, 3}) |
| 145 | testNackPair(t, []uint16{42, 42 + 16}, NackPair{42, 0x8000}) |
| 146 | } |
| 147 | |
| 148 | func TestNackPairRange(t *testing.T) { |
| 149 | pair := NackPair{42, 2} |
| 150 | |
| 151 | out := make([]uint16, 0) |
| 152 | pair.Range(func(s uint16) bool { |
| 153 | out = append(out, s) |
| 154 | |
| 155 | return true |
| 156 | }) |
| 157 | assert.Equal(t, []uint16{42, 44}, out) |
| 158 | |
| 159 | out = make([]uint16, 0) |
| 160 | pair.Range(func(s uint16) bool { |
| 161 | out = append(out, s) |
| 162 | |
| 163 | return false |
| 164 | }) |
| 165 | assert.Equal(t, []uint16{42}, out) |
| 166 | } |
| 167 | |
| 168 | func TestTransportLayerNackPairGeneration(t *testing.T) { |
| 169 | for _, test := range []struct { |
| 170 | Name string |
| 171 | SequenceNumbers []uint16 |
| 172 | Expected []NackPair |
| 173 | }{ |
| 174 | { |
| 175 | "No Sequence Numbers", |
| 176 | []uint16{}, |
| 177 | []NackPair{}, |
| 178 | }, |
| 179 | { |
| 180 | "Single Sequence Number", |
| 181 | []uint16{100}, |
| 182 | []NackPair{ |
| 183 | {PacketID: 100, LostPackets: 0x0}, |
| 184 | }, |
| 185 | }, |
| 186 | { |
| 187 | "Multiple in range, Single NACKPair", |
| 188 | []uint16{100, 101, 105, 115}, |
| 189 | []NackPair{ |
| 190 | {PacketID: 100, LostPackets: 0x4011}, |
| 191 | }, |
| 192 | }, |
| 193 | { |
| 194 | "Multiple Ranges, Multiple NACKPair", |
| 195 | []uint16{100, 117, 500, 501, 502}, |
| 196 | []NackPair{ |
| 197 | {PacketID: 100, LostPackets: 0}, |
| 198 | {PacketID: 117, LostPackets: 0}, |
| 199 | {PacketID: 500, LostPackets: 0x3}, |
| 200 | }, |
| 201 | }, |
| 202 | } { |
| 203 | actual := NackPairsFromSequenceNumbers(test.SequenceNumbers) |
| 204 | assert.Equalf(t, test.Expected, actual, "%q NackPair generation mismatch", test.Name) |
| 205 | } |
| 206 | } |