| 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 | // An RTCP packet from a packet dump. |
| 13 | func realPacket() []byte { |
| 14 | return []byte{ |
| 15 | // Receiver Report (offset=0) |
| 16 | // v=2, p=0, count=1, RR, len=7 |
| 17 | 0x81, 0xc9, 0x0, 0x7, |
| 18 | // ssrc=0x902f9e2e |
| 19 | 0x90, 0x2f, 0x9e, 0x2e, |
| 20 | // ssrc=0xbc5e9a40 |
| 21 | 0xbc, 0x5e, 0x9a, 0x40, |
| 22 | // fracLost=0, totalLost=0 |
| 23 | 0x0, 0x0, 0x0, 0x0, |
| 24 | // lastSeq=0x46e1 |
| 25 | 0x0, 0x0, 0x46, 0xe1, |
| 26 | // jitter=273 |
| 27 | 0x0, 0x0, 0x1, 0x11, |
| 28 | // lsr=0x9f36432 |
| 29 | 0x9, 0xf3, 0x64, 0x32, |
| 30 | // delay=150137 |
| 31 | 0x0, 0x2, 0x4a, 0x79, |
| 32 | |
| 33 | // Source Description (offset=32) |
| 34 | // v=2, p=0, count=1, SDES, len=12 |
| 35 | 0x81, 0xca, 0x0, 0xc, |
| 36 | // ssrc=0x902f9e2e |
| 37 | 0x90, 0x2f, 0x9e, 0x2e, |
| 38 | // CNAME, len=38 |
| 39 | 0x1, 0x26, |
| 40 | // text="{9c00eb92-1afb-9d49-a47d-91f64eee69f5}" |
| 41 | 0x7b, 0x39, 0x63, 0x30, |
| 42 | 0x30, 0x65, 0x62, 0x39, |
| 43 | 0x32, 0x2d, 0x31, 0x61, |
| 44 | 0x66, 0x62, 0x2d, 0x39, |
| 45 | 0x64, 0x34, 0x39, 0x2d, |
| 46 | 0x61, 0x34, 0x37, 0x64, |
| 47 | 0x2d, 0x39, 0x31, 0x66, |
| 48 | 0x36, 0x34, 0x65, 0x65, |
| 49 | 0x65, 0x36, 0x39, 0x66, |
| 50 | 0x35, 0x7d, |
| 51 | // END + padding |
| 52 | 0x0, 0x0, 0x0, 0x0, |
| 53 | |
| 54 | // Goodbye (offset=84) |
| 55 | // v=2, p=0, count=1, BYE, len=1 |
| 56 | 0x81, 0xcb, 0x0, 0x1, |
| 57 | // source=0x902f9e2e |
| 58 | 0x90, 0x2f, 0x9e, 0x2e, |
| 59 | |
| 60 | // Picture Loss Indication (offset=92) |
| 61 | 0x81, 0xce, 0x0, 0x2, |
| 62 | // sender=0x902f9e2e |
| 63 | 0x90, 0x2f, 0x9e, 0x2e, |
| 64 | // media=0x902f9e2e |
| 65 | 0x90, 0x2f, 0x9e, 0x2e, |
| 66 | |
| 67 | // RapidResynchronizationRequest (offset=104) |
| 68 | 0x85, 0xcd, 0x0, 0x2, |
| 69 | // sender=0x902f9e2e |
| 70 | 0x90, 0x2f, 0x9e, 0x2e, |
| 71 | // media=0x902f9e2e |
| 72 | 0x90, 0x2f, 0x9e, 0x2e, |
| 73 | |
| 74 | // ApplicationDefined (offset=116) |
| 75 | 0x80, 0xcc, 0x00, 0x03, |
| 76 | // sender=0x4baae1ab |
| 77 | 0x4b, 0xaa, 0xe1, 0xab, |
| 78 | // name='NAME' |
| 79 | 0x4E, 0x41, 0x4D, 0x45, |
| 80 | // data='ABCD' |
| 81 | 0x41, 0x42, 0x43, 0x44, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestUnmarshal(t *testing.T) { |
| 86 | packet, err := Unmarshal(realPacket()) |
| 87 | assert.NoError(t, err) |
| 88 | |
| 89 | expected := []Packet{ |
| 90 | &ReceiverReport{ |
| 91 | SSRC: 0x902f9e2e, |
| 92 | Reports: []ReceptionReport{{ |
| 93 | SSRC: 0xbc5e9a40, |
| 94 | FractionLost: 0, |
| 95 | TotalLost: 0, |
| 96 | LastSequenceNumber: 0x46e1, |
| 97 | Jitter: 273, |
| 98 | LastSenderReport: 0x9f36432, |
| 99 | Delay: 150137, |
| 100 | }}, |
| 101 | ProfileExtensions: []byte{}, |
| 102 | }, |
| 103 | NewCNAMESourceDescription(0x902f9e2e, "{9c00eb92-1afb-9d49-a47d-91f64eee69f5}"), |
| 104 | &Goodbye{ |
| 105 | Sources: []uint32{0x902f9e2e}, |
| 106 | }, |
| 107 | &PictureLossIndication{ |
| 108 | SenderSSRC: 0x902f9e2e, |
| 109 | MediaSSRC: 0x902f9e2e, |
| 110 | }, |
| 111 | &RapidResynchronizationRequest{ |
| 112 | SenderSSRC: 0x902f9e2e, |
| 113 | MediaSSRC: 0x902f9e2e, |
| 114 | }, |
| 115 | &ApplicationDefined{ |
| 116 | SSRC: 0x4baae1ab, |
| 117 | Name: "NAME", |
| 118 | Data: []byte{0x41, 0x42, 0x43, 0x44}, |
| 119 | }, |
| 120 | } |
| 121 | |
| 122 | assert.Equal(t, expected, packet) |
| 123 | } |
| 124 | |
| 125 | func TestUnmarshalNil(t *testing.T) { |
| 126 | _, err := Unmarshal(nil) |
| 127 | assert.ErrorIs(t, err, errInvalidHeader) |
| 128 | } |
| 129 | |
| 130 | func TestInvalidHeaderLength(t *testing.T) { |
| 131 | invalidPacket := []byte{ |
| 132 | // Receiver Report (offset=0) |
| 133 | // v=2, p=0, count=1, RR, len=100 |
| 134 | 0x81, 0xc9, 0x0, 0x64, |
| 135 | } |
| 136 | |
| 137 | _, err := Unmarshal(invalidPacket) |
| 138 | assert.ErrorIs(t, err, errPacketTooShort) |
| 139 | } |
| 140 | |
| 141 | func TestUnmarshalMaxLengthRawPacket(t *testing.T) { |
| 142 | rawPacket := make([]byte, 4*(0xFFFF+1)) |
| 143 | rawPacket[0] = 0x80 |
| 144 | rawPacket[1] = 0xfa |
| 145 | rawPacket[2] = 0xff |
| 146 | rawPacket[3] = 0xff |
| 147 | |
| 148 | packets, err := Unmarshal(rawPacket) |
| 149 | assert.NoError(t, err) |
| 150 | assert.Len(t, packets, 1) |
| 151 | |
| 152 | parsed, ok := packets[0].(*RawPacket) |
| 153 | assert.True(t, ok) |
| 154 | assert.Equal(t, rawPacket, []byte(*parsed)) |
| 155 | } |