This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmessage_test.go
168 lines (153 loc) · 3.84 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
// Copyright 2015 Mikio Hara. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package stun_test
import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"testing"
"github.com/mikioh/stun"
)
var typeClassMethodTests = []struct {
in stun.Type
c stun.Class
m stun.Method
}{
{0x0001, stun.ClassRequest, stun.MethodBinding},
{0x0011, stun.ClassIndication, stun.MethodBinding},
{0x0101, stun.ClassSuccessResponse, stun.MethodBinding},
{0x0111, stun.ClassErrorResponse, stun.MethodBinding},
{0x3eef, stun.ClassRequest, 0xfff},
{0x3eff, stun.ClassIndication, 0xfff},
{0x3fef, stun.ClassSuccessResponse, 0xfff},
{0x3fff, stun.ClassErrorResponse, 0xfff},
}
func TestTypeClassMethod(t *testing.T) {
for i, tt := range typeClassMethodTests {
c := tt.in.Class()
if c != tt.c {
t.Errorf("#%d: got %v; want %v", i, c, tt.c)
}
m := tt.in.Method()
if m != tt.m {
t.Errorf("#%d: got %v; want %v", i, m, tt.m)
}
out := stun.MessageType(c, m)
if out != tt.in {
t.Errorf("#%d: got %v; want %v", i, out, tt.in)
}
}
}
func TestParseFuzzHeader(t *testing.T) {
defer func() {
if p := recover(); p != nil {
t.Fatalf("panicked: %v", p)
}
}()
for i := 0; i < 1500; i++ {
b := make([]byte, i)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
t.Fatal(err)
}
if _, _, err := stun.ParseHeader(b); err != nil {
fmt.Fprintf(ioutil.Discard, "%v", err)
}
}
}
func TestParseRandomFuzzMessage(t *testing.T) {
defer func() {
if p := recover(); p != nil {
t.Fatalf("panicked: %v", p)
}
}()
for i := 0; i < 1500; i++ {
b := make([]byte, i)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
t.Fatal(err)
}
if _, _, err := stun.ParseMessage(b, nil); err != nil {
fmt.Fprintf(ioutil.Discard, "%v", err)
}
}
}
var parseGoFuzzMessageTests = [][]byte{
[]byte("00\x00\x100000000000000000\x800\x00\x04000000000000"),
}
func TestParseGoFuzzMessage(t *testing.T) {
defer func() {
if p := recover(); p != nil {
t.Fatalf("panicked: %v", p)
}
}()
for _, tt := range parseGoFuzzMessageTests {
if _, _, err := stun.ParseMessage(tt, nil); err != nil {
fmt.Fprintf(ioutil.Discard, "%v", err)
}
}
}
var channelDataTests = []rfc5769Test{
{
raw: &stun.ChannelData{
Number: 0x4000,
Data: []byte("\xb7\xe7\xa7\x01\xbc\x34\xd6\x86\xfa\x87\xff"),
},
wire: "\x40\x00\x00\x0b\xb7\xe7\xa7\x01\xbc\x34\xd6\x86\xfa\x87\xff\x00",
},
{
raw: &stun.ChannelData{
Number: 0x7fff,
Data: []byte("\xff"), // TURN over UDP is not required 4-byte alignment
},
wire: "\x7f\xff\x00\x01\xff",
},
}
func TestMarshalAndParseMessage(t *testing.T) {
var wire []byte
tests := append(rfc5769Tests, channelDataTests...)
for i, tt := range tests {
b := make([]byte, tt.raw.Len())
n, err := tt.raw.Marshal(b, tt.hash)
if err != nil {
t.Fatalf("#%d: %v", i, err)
}
if n%4 != 0 {
t.Fatalf("#%d: not multiple of 4 bytes: %d", i, n)
}
wire = append(wire, b...)
}
for i, tt := range tests {
if _, _, err := stun.ParseHeader(wire); err != nil {
t.Fatalf("#%d: %v", i, err)
}
n, m, err := stun.ParseMessage(wire, tt.hash)
if err != nil {
t.Fatalf("#%d: %v", i, err)
}
switch m := m.(type) {
case *stun.Control:
if n%4 != 0 {
t.Fatalf("#%d: not multiple of 4 bytes: %d", i, n)
}
if n != len(tt.wire) {
t.Fatalf("#%d: got %d; want %d", i, n, len(tt.wire))
}
case *stun.ChannelData:
if len(tt.wire)%4 == 0 {
if n%4 != 0 {
t.Fatalf("#%d: not multiple of 4 bytes: %d", i, n)
}
if n != len(tt.wire) {
t.Fatalf("#%d: got %d; want %d", i, n, len(tt.wire))
}
}
if len(m.Data) != len(tt.raw.(*stun.ChannelData).Data) {
t.Fatalf("#%d: got %d; want %d", i, len(m.Data), len(tt.raw.(*stun.ChannelData).Data))
}
default:
t.Fatalf("#%d: unknown type: %T", i, m)
}
wire = wire[n:]
}
}