| 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 = (*PictureLossIndication)(nil) // assert is a Packet |
| 13 | |
| 14 | func TestPictureLossIndicationUnmarshal(t *testing.T) { |
| 15 | for _, test := range []struct { |
| 16 | Name string |
| 17 | Data []byte |
| 18 | Want PictureLossIndication |
| 19 | WantError error |
| 20 | }{ |
| 21 | { |
| 22 | Name: "valid", |
| 23 | Data: []byte{ |
| 24 | // v=2, p=0, FMT=1, PSFB, len=1 |
| 25 | 0x81, 0xce, 0x00, 0x02, |
| 26 | // ssrc=0x0 |
| 27 | 0x00, 0x00, 0x00, 0x00, |
| 28 | // ssrc=0x4bc4fcb4 |
| 29 | 0x4b, 0xc4, 0xfc, 0xb4, |
| 30 | }, |
| 31 | Want: PictureLossIndication{ |
| 32 | SenderSSRC: 0x0, |
| 33 | MediaSSRC: 0x4bc4fcb4, |
| 34 | }, |
| 35 | }, |
| 36 | { |
| 37 | Name: "packet too short", |
| 38 | Data: []byte{ |
| 39 | 0x00, 0x00, 0x00, 0x00, |
| 40 | }, |
| 41 | WantError: errPacketTooShort, |
| 42 | }, |
| 43 | { |
| 44 | Name: "invalid header", |
| 45 | Data: []byte{ |
| 46 | 0x00, 0x00, 0x00, 0x00, |
| 47 | 0x00, 0x00, 0x00, 0x00, |
| 48 | 0x00, 0x00, 0x00, 0x00, |
| 49 | }, |
| 50 | WantError: errBadVersion, |
| 51 | }, |
| 52 | { |
| 53 | Name: "wrong type", |
| 54 | Data: []byte{ |
| 55 | // v=2, p=0, FMT=1, RR, len=1 |
| 56 | 0x81, 0xc9, 0x00, 0x02, |
| 57 | // ssrc=0x0 |
| 58 | 0x00, 0x00, 0x00, 0x00, |
| 59 | // ssrc=0x4bc4fcb4 |
| 60 | 0x4b, 0xc4, 0xfc, 0xb4, |
| 61 | }, |
| 62 | WantError: errWrongType, |
| 63 | }, |
| 64 | { |
| 65 | Name: "wrong fmt", |
| 66 | Data: []byte{ |
| 67 | // v=2, p=0, FMT=2, RR, len=1 |
| 68 | 0x82, 0xc9, 0x00, 0x02, |
| 69 | // ssrc=0x0 |
| 70 | 0x00, 0x00, 0x00, 0x00, |
| 71 | // ssrc=0x4bc4fcb4 |
| 72 | 0x4b, 0xc4, 0xfc, 0xb4, |
| 73 | }, |
| 74 | WantError: errWrongType, |
| 75 | }, |
| 76 | } { |
| 77 | var pli PictureLossIndication |
| 78 | err := pli.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, pli, "Unmarshal %q", test.Name) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | func TestPictureLossIndicationRoundTrip(t *testing.T) { |
| 89 | for _, test := range []struct { |
| 90 | Name string |
| 91 | Packet PictureLossIndication |
| 92 | WantError error |
| 93 | }{ |
| 94 | { |
| 95 | Name: "valid", |
| 96 | Packet: PictureLossIndication{ |
| 97 | SenderSSRC: 1, |
| 98 | MediaSSRC: 2, |
| 99 | }, |
| 100 | }, |
| 101 | { |
| 102 | Name: "also valid", |
| 103 | Packet: PictureLossIndication{ |
| 104 | SenderSSRC: 5000, |
| 105 | MediaSSRC: 6000, |
| 106 | }, |
| 107 | }, |
| 108 | } { |
| 109 | data, err := test.Packet.Marshal() |
| 110 | assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name) |
| 111 | if err != nil { |
| 112 | continue |
| 113 | } |
| 114 | |
| 115 | var decoded PictureLossIndication |
| 116 | assert.NoErrorf(t, decoded.Unmarshal(data), "Unmarshal %q", test.Name) |
| 117 | assert.Equalf(t, test.Packet, decoded, "%q rr round trip mismatch", test.Name) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | func TestPictureLossIndicationUnmarshalHeader(t *testing.T) { |
| 122 | for _, test := range []struct { |
| 123 | Name string |
| 124 | Data []byte |
| 125 | Want Header |
| 126 | WantError error |
| 127 | }{ |
| 128 | { |
| 129 | Name: "valid header", |
| 130 | Data: []byte{ |
| 131 | // v=2, p=0, FMT=1, PSFB, len=1 |
| 132 | 0x81, 0xce, 0x00, 0x02, |
| 133 | // ssrc=0x0 |
| 134 | 0x00, 0x00, 0x00, 0x00, |
| 135 | // ssrc=0x4bc4fcb4 |
| 136 | 0x4b, 0xc4, 0xfc, 0xb4, |
| 137 | }, |
| 138 | Want: Header{ |
| 139 | Count: FormatPLI, |
| 140 | Type: TypePayloadSpecificFeedback, |
| 141 | Length: pliLength, |
| 142 | }, |
| 143 | }, |
| 144 | } { |
| 145 | var pli PictureLossIndication |
| 146 | err := pli.Unmarshal(test.Data) |
| 147 | assert.ErrorIsf(t, err, test.WantError, "Unmarshal header %q", test.Name) |
| 148 | if err != nil { |
| 149 | continue |
| 150 | } |
| 151 | |
| 152 | assert.Equalf(t, test.Want, pli.Header(), "Unmarshal header %q", test.Name) |
| 153 | } |
| 154 | } |