|
| 1 | +// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package sctp |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +func testChunkIForwardTSN() []byte { |
| 13 | + return []byte{0xc2, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x3} |
| 14 | +} |
| 15 | + |
| 16 | +func TestChunkIForwardTSN_Success(t *testing.T) { |
| 17 | + tt := []struct { |
| 18 | + binary []byte |
| 19 | + }{ |
| 20 | + {testChunkIForwardTSN()}, |
| 21 | + {[]byte{0xc2, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x3, 0x0, 0x4, 0x0, 0x1, 0x0, 0x0, 0x0, 0x5}}, |
| 22 | + {[]byte{ |
| 23 | + 0xc2, 0x0, 0x0, 0x40, 0x0, 0x0, 0x0, 0x3, |
| 24 | + 0x0, 0x4, 0x0, 0x0, 0x0, 0x0, 0x0, 0x5, |
| 25 | + 0x0, 0x6, 0x0, 0x1, 0x0, 0x0, 0x0, 0x7, |
| 26 | + 0x0, 0x8, 0x0, 0x1, 0x0, 0x0, 0x0, 0x9, |
| 27 | + 0x0, 0xa, 0x0, 0x1, 0x0, 0x0, 0x0, 0xb, |
| 28 | + 0x0, 0xc, 0x0, 0x1, 0x0, 0x0, 0x0, 0xd, |
| 29 | + 0x0, 0xe, 0x0, 0x1, 0x0, 0x0, 0x0, 0xf, |
| 30 | + 0x0, 0x10, 0x0, 0x1, 0x0, 0x0, 0x0, 0x11, |
| 31 | + }}, |
| 32 | + } |
| 33 | + |
| 34 | + for i, tc := range tt { |
| 35 | + actual := &chunkIForwardTSN{} |
| 36 | + err := actual.unmarshal(tc.binary) |
| 37 | + assert.NoErrorf(t, err, "failed to unmarshal #%d", i) |
| 38 | + |
| 39 | + b, err := actual.marshal() |
| 40 | + assert.NoError(t, err) |
| 41 | + assert.Equalf(t, tc.binary, b, "test %d not equal", i) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestChunkIForwardTSNUnmarshal_Failure(t *testing.T) { |
| 46 | + tt := []struct { |
| 47 | + name string |
| 48 | + binary []byte |
| 49 | + }{ |
| 50 | + {"chunk header to short", []byte{0xc2}}, |
| 51 | + {"missing New Cumulative TSN", []byte{0xc2, 0x0, 0x0, 0x4}}, |
| 52 | + {"missing stream info", []byte{0xc2, 0x0, 0x0, 0x10, 0x0, 0x0, 0x0, 0x3, 0x0, 0x4, 0x0, 0x1}}, |
| 53 | + } |
| 54 | + |
| 55 | + for i, tc := range tt { |
| 56 | + actual := &chunkIForwardTSN{} |
| 57 | + err := actual.unmarshal(tc.binary) |
| 58 | + assert.Errorf(t, err, "expected unmarshal #%d: '%s' to fail.", i, tc.name) |
| 59 | + } |
| 60 | +} |
0 commit comments