Debian Codegraph @

packet_test.go

raw
1 // SPDX-FileCopyrightText: 2026 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 TestPacketUnmarshal(t *testing.T) {
13 pkt := &packet{}
14
15 assert.Error(t, pkt.unmarshal(true, []byte{}), "Unmarshal should fail when a packet is too small to be SCTP")
16
17 headerOnly := []byte{0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x06, 0xa9, 0x00, 0xe1}
18 err := pkt.unmarshal(true, headerOnly)
19 assert.NoError(t, err, "Unmarshal failed for SCTP packet with no chunks")
20
21 assert.Equal(t, uint16(defaultSCTPSrcDstPort), pkt.sourcePort,
22 "Unmarshal passed for SCTP packet, but got incorrect source port exp: %d act: %d",
23 defaultSCTPSrcDstPort, pkt.sourcePort)
24 assert.Equal(t, uint16(defaultSCTPSrcDstPort), pkt.destinationPort,
25 "Unmarshal passed for SCTP packet, but got incorrect destination port exp: %d act: %d",
26 defaultSCTPSrcDstPort, pkt.destinationPort)
27 assert.Equal(t, uint32(0), pkt.verificationTag,
28 "Unmarshal passed for SCTP packet, but got incorrect verification tag exp: %d act: %d",
29 0, pkt.verificationTag)
30
31 rawChunk := []byte{
32 0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x81, 0x46, 0x9d, 0xfc, 0x01, 0x00, 0x00, 0x56, 0x55,
33 0xb9, 0x64, 0xa5, 0x00, 0x02, 0x00, 0x00, 0x04, 0x00, 0x08, 0x00, 0xe8, 0x6d, 0x10, 0x30, 0xc0, 0x00,
34 0x00, 0x04, 0x80, 0x08, 0x00, 0x09, 0xc0, 0x0f, 0xc1, 0x80, 0x82, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00,
35 0x24, 0x9f, 0xeb, 0xbb, 0x5c, 0x50, 0xc9, 0xbf, 0x75, 0x9c, 0xb1, 0x2c, 0x57, 0x4f, 0xa4, 0x5a, 0x51,
36 0xba, 0x60, 0x17, 0x78, 0x27, 0x94, 0x5c, 0x31, 0xe6, 0x5d, 0x5b, 0x09, 0x47, 0xe2, 0x22, 0x06, 0x80,
37 0x04, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x80, 0x03, 0x00, 0x06, 0x80, 0xc1, 0x00, 0x00,
38 }
39
40 assert.NoError(t, pkt.unmarshal(true, rawChunk))
41 }
42
43 func TestPacketMarshal(t *testing.T) {
44 pkt := &packet{}
45
46 headerOnly := []byte{0x13, 0x88, 0x13, 0x88, 0x00, 0x00, 0x00, 0x00, 0x06, 0xa9, 0x00, 0xe1}
47 assert.NoError(t, pkt.unmarshal(true, headerOnly), "Unmarshal failed for SCTP packet with no chunks")
48
49 headerOnlyMarshaled, err := pkt.marshal(true)
50 if assert.NoError(t, err, "Marshal failed for SCTP packet with no chunks") {
51 assert.Equal(t, headerOnly, headerOnlyMarshaled,
52 "Unmarshal/Marshaled header only packet did not match \nheaderOnly: % 02x \nheaderOnlyMarshaled % 02x",
53 headerOnly, headerOnlyMarshaled)
54 }
55 }
56
57 func BenchmarkPacketGenerateChecksum(b *testing.B) {
58 var data [1024]byte
59
60 for i := 0; i < b.N; i++ {
61 _ = generatePacketChecksum(data[:])
62 }
63 }