Debian Codegraph @

util_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 TestPadByte_Success(t *testing.T) {
13 tt := []struct {
14 value []byte
15 padLen int
16 expected []byte
17 }{
18 {[]byte{0x1, 0x2}, 0, []byte{0x1, 0x2}},
19 {[]byte{0x1, 0x2}, 1, []byte{0x1, 0x2, 0x0}},
20 {[]byte{0x1, 0x2}, 2, []byte{0x1, 0x2, 0x0, 0x0}},
21 {[]byte{0x1, 0x2}, 3, []byte{0x1, 0x2, 0x0, 0x0, 0x0}},
22 {[]byte{0x1, 0x2}, -1, []byte{0x1, 0x2}},
23 }
24
25 for i, tc := range tt {
26 actual := padByte(tc.value, tc.padLen)
27
28 assert.Equalf(t, tc.expected, actual, "test %d not equal", i)
29 }
30 }
31
32 func TestSerialNumberArithmetic(t *testing.T) {
33 const div int = 16
34
35 t.Run("32-bit", func(t *testing.T) { // nolint:dupl
36 const serialBits uint32 = 32
37 const interval uint32 = uint32((uint64(1) << uint64(serialBits)) / uint64(div))
38 const maxForwardDistance uint32 = 1<<(serialBits-1) - 1
39 const maxBackwardDistance uint32 = 1 << (serialBits - 1)
40
41 for i := range uint32(div) {
42 s1 := i * interval
43 s2f := s1 + maxForwardDistance
44 s2b := s1 + maxBackwardDistance
45
46 assert.Truef(t, sna32LT(s1, s2f), "s1 < s2 should be true: s1=0x%x s2=0x%x", s1, s2f)
47 assert.Falsef(t, sna32LT(s1, s2b), "s1 < s2 should be false: s1=0x%x s2=0x%x", s1, s2b)
48
49 assert.Falsef(t, sna32GT(s1, s2f), "s1 > s2 should be fales: s1=0x%x s2=0x%x", s1, s2f)
50 assert.Truef(t, sna32GT(s1, s2b), "s1 > s2 should be true: s1=0x%x s2=0x%x", s1, s2b)
51
52 assert.Truef(t, sna32LTE(s1, s2f), "s1 <= s2 should be true: s1=0x%x s2=0x%x", s1, s2f)
53 assert.Falsef(t, sna32LTE(s1, s2b), "s1 <= s2 should be false: s1=0x%x s2=0x%x", s1, s2b)
54
55 assert.Falsef(t, sna32GTE(s1, s2f), "s1 >= s2 should be fales: s1=0x%x s2=0x%x", s1, s2f)
56 assert.Truef(t, sna32GTE(s1, s2b), "s1 >= s2 should be true: s1=0x%x s2=0x%x", s1, s2b)
57
58 assert.Truef(t, sna32EQ(s1, s1), "s1 == s1 should be true: s1=0x%x s2=0x%x", s1, s1)
59 assert.Truef(t, sna32EQ(s2b, s2b), "s2 == s2 should be true: s2=0x%x s2=0x%x", s2b, s2b)
60 assert.Falsef(t, sna32EQ(s1, s1+1), "s1 == s1+1 should be false: s1=0x%x s1+1=0x%x", s1, s1+1)
61 assert.Falsef(t, sna32EQ(s1, s1-1), "s1 == s1-1 hould be false: s1=0x%x s1-1=0x%x", s1, s1-1)
62
63 assert.Truef(t, sna32LTE(s1, s1), "s1 == s1 should be true: s1=0x%x s2=0x%x", s1, s1)
64 assert.Truef(t, sna32LTE(s2b, s2b), "s2 == s2 should be true: s2=0x%x s2=0x%x", s2b, s2b)
65
66 assert.Truef(t, sna32GTE(s1, s1), "s1 == s1 should be true: s1=0x%x s2=0x%x", s1, s1)
67 assert.Truef(t, sna32GTE(s2b, s2b), "s2 == s2 should be true: s2=0x%x s2=0x%x", s2b, s2b)
68 }
69 })
70
71 t.Run("16-bit", func(t *testing.T) { // nolint:dupl
72 const serialBits uint16 = 16
73 const interval uint16 = uint16((uint64(1) << uint64(serialBits)) / uint64(div))
74 const maxForwardDistance uint16 = 1<<(serialBits-1) - 1
75 const maxBackwardDistance uint16 = 1 << (serialBits - 1)
76
77 for i := range uint16(div) {
78 s1 := i * interval
79 s2f := s1 + maxForwardDistance
80 s2b := s1 + maxBackwardDistance
81
82 assert.Truef(t, sna16LT(s1, s2f), "s1 < s2 should be true: s1=0x%x s2=0x%x", s1, s2f)
83 assert.Falsef(t, sna16LT(s1, s2b), "s1 < s2 should be false: s1=0x%x s2=0x%x", s1, s2b)
84
85 assert.Falsef(t, sna16GT(s1, s2f), "s1 > s2 should be fales: s1=0x%x s2=0x%x", s1, s2f)
86 assert.Truef(t, sna16GT(s1, s2b), "s1 > s2 should be true: s1=0x%x s2=0x%x", s1, s2b)
87
88 assert.Truef(t, sna16LTE(s1, s2f), "s1 <= s2 should be true: s1=0x%x s2=0x%x", s1, s2f)
89 assert.Falsef(t, sna16LTE(s1, s2b), "s1 <= s2 should be false: s1=0x%x s2=0x%x", s1, s2b)
90
91 assert.Falsef(t, sna16GTE(s1, s2f), "s1 >= s2 should be fales: s1=0x%x s2=0x%x", s1, s2f)
92 assert.Truef(t, sna16GTE(s1, s2b), "s1 >= s2 should be true: s1=0x%x s2=0x%x", s1, s2b)
93
94 assert.Truef(t, sna16EQ(s1, s1), "s1 == s1 should be true: s1=0x%x s2=0x%x", s1, s1)
95 assert.Truef(t, sna16EQ(s2b, s2b), "s2 == s2 should be true: s2=0x%x s2=0x%x", s2b, s2b)
96 assert.Falsef(t, sna16EQ(s1, s1+1), "s1 == s1+1 should be false: s1=0x%x s1+1=0x%x", s1, s1+1)
97 assert.Falsef(t, sna16EQ(s1, s1-1), "s1 == s1-1 hould be false: s1=0x%x s1-1=0x%x", s1, s1-1)
98
99 assert.Truef(t, sna16LTE(s1, s1), "s1 == s1 should be true: s1=0x%x s2=0x%x", s1, s1)
100 assert.Truef(t, sna16LTE(s2b, s2b), "s2 == s2 should be true: s2=0x%x s2=0x%x", s2b, s2b)
101
102 assert.Truef(t, sna16GTE(s1, s1), "s1 == s1 should be true: s1=0x%x s2=0x%x", s1, s1)
103 assert.Truef(t, sna16GTE(s2b, s2b), "s2 == s2 should be true: s2=0x%x s2=0x%x", s2b, s2b)
104 }
105 })
106 }