forked from ctulek/stun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_test.go
168 lines (154 loc) · 3.83 KB
/
message_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package stun
import (
"bytes"
"net"
"testing"
)
var b_BINDING_REQUEST_HEADER_ONLY = []byte{
0x00, 0x01, 0x00, 0x00,
0x21, 0x12, 0xA4, 0x42,
0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C,
}
var b_BINDING_RESPONSE_HEADER_ONLY = []byte{
0x01, 0x01, 0x00, 0x00,
0x21, 0x12, 0xA4, 0x42,
0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C,
}
func TestUnPackRequestHeaderOnly(t *testing.T) {
m := new(Message)
err := m.UnPack(b_BINDING_REQUEST_HEADER_ONLY)
if err != nil {
t.Error(err)
}
if m.Header.Type != BINDING_REQUEST {
t.Error("Message Type is wrong")
}
if m.Header.Length != 0 {
t.Error("Message Length is not zero")
}
if m.Header.Cookie != RFC5389_COOKIE {
t.Error("Cookie is not equal to RFC5389 COOKIE")
}
if !bytes.Equal(m.Header.TransactionId[:], b_BINDING_REQUEST_HEADER_ONLY[8:20]) {
t.Error("Transaction Id does not match")
}
}
func TestUnPackResponseHeaderOnly(t *testing.T) {
m := new(Message)
err := m.UnPack(b_BINDING_RESPONSE_HEADER_ONLY)
if err != nil {
t.Error(err)
}
if m.Header.Type != BINDING_RESPONSE {
t.Error("Message Type is wrong")
}
if m.Header.Length != 0 {
t.Error("Message Length is not zero")
}
if m.Header.Cookie != RFC5389_COOKIE {
t.Error("Cookie is not equal to RFC5389 COOKIE")
}
if !bytes.Equal(m.Header.TransactionId[:], b_BINDING_RESPONSE_HEADER_ONLY[8:20]) {
t.Error("Transaction Id does not match")
}
}
func TestPackRequestHeaderOnly(t *testing.T) {
m, _ := New(BINDING_REQUEST, RFC5389_COOKIE,
[12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
RTO_INIT, RETRY_MAX)
b := make([]byte, m.MinBufferSize())
if len(b) != 20 {
t.Error("Buffer length is wrong")
}
err := m.Pack(b)
if err != nil {
t.Error(err)
}
if !bytes.Equal(b, b_BINDING_REQUEST_HEADER_ONLY) {
t.Log(b)
t.Log(b_BINDING_REQUEST_HEADER_ONLY)
t.Error("Pack failed")
}
}
func TestPackResponseHeaderOnly(t *testing.T) {
m, _ := New(BINDING_RESPONSE, RFC5389_COOKIE,
[12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
RTO_INIT, RETRY_MAX)
b := make([]byte, m.MinBufferSize())
if len(b) != 20 {
t.Error("Buffer length is wrong")
}
err := m.Pack(b)
if err != nil {
t.Error(err)
}
if !bytes.Equal(b, b_BINDING_RESPONSE_HEADER_ONLY) {
t.Log(b)
t.Log(b_BINDING_RESPONSE_HEADER_ONLY)
t.Error("Pack failed")
}
}
var b_BINDING_RESPONSE_WITH_MAPPED_ADDRESS = []byte{
0x01, 0x01, 0x00, 0x0C,
0x21, 0x12, 0xA4, 0x42,
0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08,
0x09, 0x0A, 0x0B, 0x0C,
0x00, 0x01, 0x00, 0x08, // Mapped Address
0x00, 0x01, 0x04, 0x00,
0xC0, 0xA8, 0x00, 0x01, // 192.168.0.1
}
func TestUnPackWithAttribute(t *testing.T) {
m := new(Message)
err := m.UnPack(b_BINDING_RESPONSE_WITH_MAPPED_ADDRESS)
if err != nil {
t.Error(err)
}
if len(m.Attributes) != 1 {
t.Error("Attribute count is wrong")
}
if m.Attributes[0].Type() != ATTR_TYPE_MAPPED_ADDRESS {
t.Error("Attribute type is wrong")
}
if m.Attributes[0].Length() != 8 {
t.Error("Attribute length is wrong")
}
mapped := m.Attributes[0].(*MappedAddress)
if mapped.Family != 0x01 {
t.Error("Wrong Family")
}
if mapped.Port != 1024 {
t.Error("Wrong Port", mapped.Port)
}
if mapped.IP.String() != "192.168.0.1" {
t.Error("Wrong IP", mapped.IP)
}
}
func TestPackWithAttribute(t *testing.T) {
m, _ := New(BINDING_RESPONSE, RFC5389_COOKIE,
[12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
RTO_INIT, RETRY_MAX)
mapped := new(MappedAddress)
mapped.Family = 0x01
mapped.Port = 1024
mapped.IP = net.ParseIP("192.168.0.1")
t.Log(mapped.IP)
m.AddAttribute(mapped)
b := make([]byte, m.MinBufferSize())
if len(b) != 32 {
t.Error("Buffer length is wrong")
}
err := m.Pack(b)
if err != nil {
t.Error(err)
}
if bytes.Equal(b, b_BINDING_RESPONSE_WITH_MAPPED_ADDRESS) == false {
t.Log(b)
t.Log(b_BINDING_RESPONSE_WITH_MAPPED_ADDRESS)
t.Error("Pack failed")
}
}