| 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 = (*RapidResynchronizationRequest)(nil) // assert is a Packet |
| 13 | |
| 14 | func TestRapidResynchronizationRequestUnmarshal(t *testing.T) { |
| 15 | for _, test := range []struct { |
| 16 | Name string |
| 17 | Data []byte |
| 18 | Want RapidResynchronizationRequest |
| 19 | WantError error |
| 20 | }{ |
| 21 | { |
| 22 | Name: "valid", |
| 23 | Data: []byte{ |
| 24 | // RapidResynchronizationRequest |
| 25 | 0x85, 0xcd, 0x0, 0x2, |
| 26 | // sender=0x902f9e2e |
| 27 | 0x90, 0x2f, 0x9e, 0x2e, |
| 28 | // media=0x902f9e2e |
| 29 | 0x90, 0x2f, 0x9e, 0x2e, |
| 30 | }, |
| 31 | Want: RapidResynchronizationRequest{ |
| 32 | SenderSSRC: 0x902f9e2e, |
| 33 | MediaSSRC: 0x902f9e2e, |
| 34 | }, |
| 35 | }, |
| 36 | { |
| 37 | Name: "short report", |
| 38 | Data: []byte{ |
| 39 | 0x85, 0xcd, 0x0, 0x2, |
| 40 | // ssrc=0x902f9e2e |
| 41 | 0x90, 0x2f, 0x9e, 0x2e, |
| 42 | // report ends early |
| 43 | }, |
| 44 | WantError: errPacketTooShort, |
| 45 | }, |
| 46 | { |
| 47 | Name: "wrong type", |
| 48 | Data: []byte{ |
| 49 | // v=2, p=0, count=1, SR, len=7 |
| 50 | 0x81, 0xc8, 0x0, 0x7, |
| 51 | // ssrc=0x902f9e2e |
| 52 | 0x90, 0x2f, 0x9e, 0x2e, |
| 53 | // ssrc=0xbc5e9a40 |
| 54 | 0xbc, 0x5e, 0x9a, 0x40, |
| 55 | // fracLost=0, totalLost=0 |
| 56 | 0x0, 0x0, 0x0, 0x0, |
| 57 | // lastSeq=0x46e1 |
| 58 | 0x0, 0x0, 0x46, 0xe1, |
| 59 | // jitter=273 |
| 60 | 0x0, 0x0, 0x1, 0x11, |
| 61 | // lsr=0x9f36432 |
| 62 | 0x9, 0xf3, 0x64, 0x32, |
| 63 | // delay=150137 |
| 64 | 0x0, 0x2, 0x4a, 0x79, |
| 65 | }, |
| 66 | WantError: errWrongType, |
| 67 | }, |
| 68 | { |
| 69 | Name: "nil", |
| 70 | Data: nil, |
| 71 | WantError: errPacketTooShort, |
| 72 | }, |
| 73 | } { |
| 74 | var rrr RapidResynchronizationRequest |
| 75 | err := rrr.Unmarshal(test.Data) |
| 76 | assert.ErrorIsf(t, err, test.WantError, "Unmarshal %q", test.Name) |
| 77 | if err != nil { |
| 78 | continue |
| 79 | } |
| 80 | assert.Equalf(t, test.Want, rrr, "Unmarshal %q", test.Name) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestRapidResynchronizationRequestRoundTrip(t *testing.T) { |
| 85 | for _, test := range []struct { |
| 86 | Name string |
| 87 | Report RapidResynchronizationRequest |
| 88 | WantError error |
| 89 | }{ |
| 90 | { |
| 91 | Name: "valid", |
| 92 | Report: RapidResynchronizationRequest{ |
| 93 | SenderSSRC: 0x902f9e2e, |
| 94 | MediaSSRC: 0x902f9e2e, |
| 95 | }, |
| 96 | }, |
| 97 | } { |
| 98 | data, err := test.Report.Marshal() |
| 99 | assert.ErrorIsf(t, err, test.WantError, "Marshal %q", test.Name) |
| 100 | if err != nil { |
| 101 | continue |
| 102 | } |
| 103 | |
| 104 | var decoded RapidResynchronizationRequest |
| 105 | assert.NoErrorf(t, decoded.Unmarshal(data), "Unmarshal %q", test.Name) |
| 106 | assert.Equalf(t, test.Report, decoded, "%q round trip", test.Name) |
| 107 | } |
| 108 | } |