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
/
evenport_test.go
78 lines (75 loc) · 1.72 KB
/
evenport_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
package turn
import (
"testing"
"gortc.io/stun"
)
func TestEvenPort(t *testing.T) {
t.Run("String", func(t *testing.T) {
p := EvenPort{}
if p.String() != "reserve: false" {
t.Errorf("bad value %q for reselve: false", p.String())
}
p.ReservePort = true
if p.String() != "reserve: true" {
t.Errorf("bad value %q for reselve: true", p.String())
}
})
t.Run("False", func(t *testing.T) {
m := new(stun.Message)
p := EvenPort{
ReservePort: false,
}
if err := p.AddTo(m); err != nil {
t.Error(err)
}
m.WriteHeader()
decoded := new(stun.Message)
var port EvenPort
decoded.Write(m.Raw)
if err := port.GetFrom(m); err != nil {
t.Fatal(err)
}
if port != p {
t.Fatal("not equal")
}
})
t.Run("AddTo", func(t *testing.T) {
m := new(stun.Message)
p := EvenPort{
ReservePort: true,
}
if err := p.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)
}
port := EvenPort{}
if err := port.GetFrom(decoded); err != nil {
t.Fatal(err)
}
if port != p {
t.Errorf("Decoded %q, expected %q", port.String(), p.String())
}
if wasAllocs(func() {
port.GetFrom(decoded)
}) {
t.Error("Unexpected allocations")
}
t.Run("HandleErr", func(t *testing.T) {
m := new(stun.Message)
var handle EvenPort
if err := handle.GetFrom(m); err != stun.ErrAttributeNotFound {
t.Errorf("%v should be not found", err)
}
m.Add(stun.AttrEvenPort, []byte{1, 2, 3})
if !stun.IsAttrSizeInvalid(handle.GetFrom(m)) {
t.Error("IsAttrSizeInvalid should be true")
}
})
})
})
}