| 1 | // SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 | // SPDX-License-Identifier: MIT |
| 3 | |
| 4 | package rtcp |
| 5 | |
| 6 | import ( |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/stretchr/testify/assert" |
| 11 | ) |
| 12 | |
| 13 | var _ Packet = (*Goodbye)(nil) // assert is a Packet |
| 14 | |
| 15 | func TestGoodbyeUnmarshal(t *testing.T) { |
| 16 | for _, test := range []struct { |
| 17 | Name string |
| 18 | Data []byte |
| 19 | Want Goodbye |
| 20 | WantError error |
| 21 | }{ |
| 22 | { |
| 23 | Name: "valid", |
| 24 | Data: []byte{ |
| 25 | // v=2, p=0, count=1, BYE, len=12 |
| 26 | 0x81, 0xcb, 0x00, 0x0c, |
| 27 | // ssrc=0x902f9e2e |
| 28 | 0x90, 0x2f, 0x9e, 0x2e, |
| 29 | // len=3, text=FOO |
| 30 | 0x03, 0x46, 0x4f, 0x4f, |
| 31 | }, |
| 32 | Want: Goodbye{ |
| 33 | Sources: []uint32{0x902f9e2e}, |
| 34 | Reason: "FOO", |
| 35 | }, |
| 36 | }, |
| 37 | { |
| 38 | Name: "invalid octet count", |
| 39 | Data: []byte{ |
| 40 | // v=2, p=0, count=1, BYE, len=12 |
| 41 | 0x81, 0xcb, 0x00, 0x0c, |
| 42 | // ssrc=0x902f9e2e |
| 43 | 0x90, 0x2f, 0x9e, 0x2e, |
| 44 | // len=4, text=FOO |
| 45 | 0x04, 0x46, 0x4f, 0x4f, |
| 46 | }, |
| 47 | WantError: errPacketTooShort, |
| 48 | }, |
| 49 | { |
| 50 | Name: "wrong type", |
| 51 | Data: []byte{ |
| 52 | // v=2, p=0, count=1, SDES, len=12 |
| 53 | 0x81, 0xca, 0x00, 0x0c, |
| 54 | // ssrc=0x902f9e2e |
| 55 | 0x90, 0x2f, 0x9e, 0x2e, |
| 56 | // len=3, text=FOO |
| 57 | 0x03, 0x46, 0x4f, 0x4f, |
| 58 | }, |
| 59 | WantError: errWrongType, |
| 60 | }, |
| 61 | { |
| 62 | Name: "short reason", |
| 63 | Data: []byte{ |
| 64 | // v=2, p=0, count=1, BYE, len=12 |
| 65 | 0x81, 0xcb, 0x00, 0x0c, |
| 66 | // ssrc=0x902f9e2e |
| 67 | 0x90, 0x2f, 0x9e, 0x2e, |
| 68 | // len=3, text=F + padding |
| 69 | 0x01, 0x46, 0x00, 0x00, |
| 70 | }, |
| 71 | Want: Goodbye{ |
| 72 | Sources: []uint32{0x902f9e2e}, |
| 73 | Reason: "F", |
| 74 | }, |
| 75 | }, |
| 76 | { |
| 77 | Name: "not byte aligned", |
| 78 | Data: []byte{ |
| 79 | // v=2, p=0, count=1, BYE, len=10 |
| 80 | 0x81, 0xcb, 0x00, 0x0a, |
| 81 | // ssrc=0x902f9e2e |
| 82 | 0x90, 0x2f, 0x9e, 0x2e, |
| 83 | // len=1, text=F |
| 84 | 0x01, 0x46, |
| 85 | }, |
| 86 | WantError: errPacketTooShort, |
| 87 | }, |
| 88 | { |
| 89 | Name: "bad count in header", |
| 90 | Data: []byte{ |
| 91 | // v=2, p=0, count=2, BYE, len=8 |
| 92 | 0x82, 0xcb, 0x00, 0x0c, |
| 93 | // ssrc=0x902f9e2e |
| 94 | 0x90, 0x2f, 0x9e, 0x2e, |
| 95 | }, |
| 96 | WantError: errPacketTooShort, |
| 97 | }, |
| 98 | { |
| 99 | Name: "empty packet", |
| 100 | Data: []byte{ |
| 101 | // v=2, p=0, count=0, BYE, len=4 |
| 102 | 0x80, 0xcb, 0x00, 0x04, |
| 103 | }, |
| 104 | Want: Goodbye{ |
| 105 | Sources: []uint32{}, |
| 106 | Reason: "", |
| 107 | }, |
| 108 | }, |
| 109 | { |
| 110 | Name: "nil", |
| 111 | Data: nil, |
| 112 | WantError: errPacketTooShort, |
| 113 | }, |
| 114 | } { |
| 115 | var bye Goodbye |
| 116 | err := bye.Unmarshal(test.Data) |
| 117 | assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q bye mismatch", test.Name) |
| 118 | if err != nil { |
| 119 | continue |
| 120 | } |
| 121 | |
| 122 | assert.Equalf(t, test.Want, bye, "Unmarshal %q bye mismatch", test.Name) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestGoodbyeRoundTrip(t *testing.T) { |
| 127 | // a slice with enough sources to overflow an 5-bit int |
| 128 | var tooManySources []uint32 |
| 129 | var tooLongText strings.Builder |
| 130 | |
| 131 | for range 1 << 5 { |
| 132 | tooManySources = append(tooManySources, 0x00) |
| 133 | } |
| 134 | for range 1 << 8 { |
| 135 | tooLongText.WriteString("x") |
| 136 | } |
| 137 | |
| 138 | for _, test := range []struct { |
| 139 | Name string |
| 140 | Bye Goodbye |
| 141 | WantError error |
| 142 | }{ |
| 143 | { |
| 144 | Name: "empty", |
| 145 | Bye: Goodbye{ |
| 146 | Sources: []uint32{}, |
| 147 | }, |
| 148 | }, |
| 149 | { |
| 150 | Name: "valid", |
| 151 | Bye: Goodbye{ |
| 152 | Sources: []uint32{ |
| 153 | 0x01020304, |
| 154 | 0x05060708, |
| 155 | }, |
| 156 | Reason: "because", |
| 157 | }, |
| 158 | }, |
| 159 | { |
| 160 | Name: "empty reason", |
| 161 | Bye: Goodbye{ |
| 162 | Sources: []uint32{0x01020304}, |
| 163 | Reason: "", |
| 164 | }, |
| 165 | }, |
| 166 | { |
| 167 | Name: "reason no source", |
| 168 | Bye: Goodbye{ |
| 169 | Sources: []uint32{}, |
| 170 | Reason: "foo", |
| 171 | }, |
| 172 | }, |
| 173 | { |
| 174 | Name: "short reason", |
| 175 | Bye: Goodbye{ |
| 176 | Sources: []uint32{}, |
| 177 | Reason: "f", |
| 178 | }, |
| 179 | }, |
| 180 | { |
| 181 | Name: "count overflow", |
| 182 | Bye: Goodbye{ |
| 183 | Sources: tooManySources, |
| 184 | }, |
| 185 | WantError: errTooManySources, |
| 186 | }, |
| 187 | { |
| 188 | Name: "reason too long", |
| 189 | Bye: Goodbye{ |
| 190 | Sources: []uint32{}, |
| 191 | Reason: tooLongText.String(), |
| 192 | }, |
| 193 | WantError: errReasonTooLong, |
| 194 | }, |
| 195 | } { |
| 196 | data, err := test.Bye.Marshal() |
| 197 | assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name) |
| 198 | if err != nil { |
| 199 | continue |
| 200 | } |
| 201 | |
| 202 | var bye Goodbye |
| 203 | assert.NoErrorf(t, bye.Unmarshal(data), "Unmarshal %q", test.Name) |
| 204 | assert.Equalf(t, test.Bye, bye, "%q bye round trip mismatch", test.Name) |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | func TestGoodbyeRoundTripMaxFieldSizes(t *testing.T) { |
| 209 | sources := make([]uint32, countMax) |
| 210 | for i := range sources { |
| 211 | sources[i] = uint32(i + 1) |
| 212 | } |
| 213 | |
| 214 | bye := Goodbye{ |
| 215 | Sources: sources, |
| 216 | Reason: strings.Repeat("x", sdesMaxOctetCount), |
| 217 | } |
| 218 | |
| 219 | data, err := bye.Marshal() |
| 220 | assert.NoError(t, err) |
| 221 | assert.Len(t, data, bye.MarshalSize()) |
| 222 | |
| 223 | var decoded Goodbye |
| 224 | assert.NoError(t, decoded.Unmarshal(data)) |
| 225 | assert.Equal(t, bye, decoded) |
| 226 | } |