This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
chann_test.go
118 lines (112 loc) · 2.53 KB
/
chann_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package turn
import (
"testing"
"gortc.io/stun"
)
func BenchmarkChannelNumber(b *testing.B) {
b.Run("AddTo", func(b *testing.B) {
b.ReportAllocs()
m := new(stun.Message)
for i := 0; i < b.N; i++ {
n := ChannelNumber(12)
if err := n.AddTo(m); err != nil {
b.Fatal(err)
}
m.Reset()
}
})
b.Run("GetFrom", func(b *testing.B) {
m := new(stun.Message)
ChannelNumber(12).AddTo(m)
for i := 0; i < b.N; i++ {
var n ChannelNumber
if err := n.GetFrom(m); err != nil {
b.Fatal(err)
}
}
})
}
func TestChannelNumber(t *testing.T) {
t.Run("String", func(t *testing.T) {
n := ChannelNumber(112)
if n.String() != "112" {
t.Errorf("bad string %s, expedted 112", n.String())
}
})
t.Run("NoAlloc", func(t *testing.T) {
m := &stun.Message{}
if wasAllocs(func() {
// Case with ChannelNumber on stack.
n := ChannelNumber(6)
n.AddTo(m)
m.Reset()
}) {
t.Error("Unexpected allocations")
}
n := ChannelNumber(12)
nP := &n
if wasAllocs(func() {
// On heap.
nP.AddTo(m)
m.Reset()
}) {
t.Error("Unexpected allocations")
}
})
t.Run("AddTo", func(t *testing.T) {
m := new(stun.Message)
n := ChannelNumber(6)
if err := n.AddTo(m); err != nil {
t.Error(err)
}
m.WriteHeader()
t.Run("GetFrom", func(t *testing.T) {
decoded := new(stun.Message)
if _, err := decoded.Write(m.Raw); err != nil {
t.Fatal("failed to decode message:", err)
}
var numDecoded ChannelNumber
if err := numDecoded.GetFrom(decoded); err != nil {
t.Fatal(err)
}
if numDecoded != n {
t.Errorf("Decoded %d, expected %d", numDecoded, n)
}
if wasAllocs(func() {
var num ChannelNumber
num.GetFrom(decoded)
}) {
t.Error("Unexpected allocations")
}
t.Run("HandleErr", func(t *testing.T) {
m := new(stun.Message)
nHandle := new(ChannelNumber)
if err := nHandle.GetFrom(m); err != stun.ErrAttributeNotFound {
t.Errorf("%v should be not found", err)
}
m.Add(stun.AttrChannelNumber, []byte{1, 2, 3})
if !stun.IsAttrSizeInvalid(nHandle.GetFrom(m)) {
t.Error("IsAttrSizeInvalid should be true")
}
})
})
})
}
func TestChannelNumber_Valid(t *testing.T) {
for _, tc := range []struct {
n ChannelNumber
value bool
}{
{MinChannelNumber - 1, false},
{MinChannelNumber, true},
{MinChannelNumber + 1, true},
{MaxChannelNumber, true},
{MaxChannelNumber + 1, false},
} {
t.Run(tc.n.String(), func(t *testing.T) {
if v := tc.n.Valid(); v != tc.value {
t.Errorf("unexpected: %v != %v", tc.value, v)
}
})
}
}