-
Notifications
You must be signed in to change notification settings - Fork 1
/
udp_test.go
146 lines (133 loc) · 4.44 KB
/
udp_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
//go:build linux && !appengine
// +build linux,!appengine
package dns
import (
"bytes"
"net"
"runtime"
"strings"
"testing"
"time"
"golang.org/x/net/ipv4"
"golang.org/x/net/ipv6"
)
func TestSetUDPSocketOptions(t *testing.T) {
// returns an error if we cannot resolve that address
testFamily := func(n, addr string) error {
a, err := net.ResolveUDPAddr(n, addr)
if err != nil {
return err
}
c, err := net.ListenUDP(n, a)
if err != nil {
return err
}
if err := setUDPSocketOptions(c); err != nil {
t.Fatalf("failed to set socket options: %v", err)
}
ch := make(chan *SessionUDP)
go func() {
// Set some deadline so this goroutine doesn't hang forever
c.SetReadDeadline(time.Now().Add(time.Minute))
b := make([]byte, 1)
_, sess, err := ReadFromSessionUDP(c, b)
if err != nil {
t.Errorf("failed to read from conn: %v", err)
// fallthrough to chan send below
}
ch <- sess
}()
c2, err := net.Dial("udp", c.LocalAddr().String())
if err != nil {
t.Fatalf("failed to dial udp: %v", err)
}
if _, err := c2.Write([]byte{1}); err != nil {
t.Fatalf("failed to write to conn: %v", err)
}
sess := <-ch
if sess == nil {
// t.Error was already called in the goroutine above.
t.FailNow()
}
if len(sess.context) == 0 {
t.Fatalf("empty session context: %v", sess)
}
ip := parseDstFromOOB(sess.context)
if ip == nil {
t.Fatalf("failed to parse dst: %v", sess)
}
if !strings.Contains(c.LocalAddr().String(), ip.String()) {
t.Fatalf("dst was different than listen addr: %v != %v", ip.String(), c.LocalAddr().String())
}
return nil
}
// we require that ipv4 be supported
if err := testFamily("udp4", "127.0.0.1:0"); err != nil {
t.Fatalf("failed to test socket options on IPv4: %v", err)
}
// IPv6 might not be supported so these will just log
if err := testFamily("udp6", "[::1]:0"); err != nil {
t.Logf("failed to test socket options on IPv6-only: %v", err)
}
if err := testFamily("udp", "[::1]:0"); err != nil {
t.Logf("failed to test socket options on IPv6/IPv4: %v", err)
}
}
func TestParseDstFromOOB(t *testing.T) {
if runtime.GOARCH != "amd64" {
// The cmsghdr struct differs in the width (32/64-bit) of
// lengths and the struct padding between architectures.
// The data below was only written with amd64 in mind, and
// thus the test must be skipped on other architectures.
t.Skip("skipping test on unsupported architecture")
}
// dst is :ffff:100.100.100.100
oob := []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 100, 100, 100, 100, 2, 0, 0, 0}
dst := parseDstFromOOB(oob)
dst4 := dst.To4()
if dst4 == nil {
t.Errorf("failed to parse IPv4 in IPv6: %v", dst)
} else if dst4.String() != "100.100.100.100" {
t.Errorf("unexpected IPv4: %v", dst4)
}
// dst is 2001:db8::1
oob = []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}
dst = parseDstFromOOB(oob)
dst6 := dst.To16()
if dst6 == nil {
t.Errorf("failed to parse IPv6: %v", dst)
} else if dst6.String() != "2001:db8::1" {
t.Errorf("unexpected IPv6: %v", dst4)
}
// dst is 100.100.100.100 but was received on 10.10.10.10
oob = []byte{28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 2, 0, 0, 0, 10, 10, 10, 10, 100, 100, 100, 100, 0, 0, 0, 0}
dst = parseDstFromOOB(oob)
dst4 = dst.To4()
if dst4 == nil {
t.Errorf("failed to parse IPv4: %v", dst)
} else if dst4.String() != "100.100.100.100" {
t.Errorf("unexpected IPv4: %v", dst4)
}
}
func TestCorrectSource(t *testing.T) {
if runtime.GOARCH != "amd64" {
// See comment above in TestParseDstFromOOB.
t.Skip("skipping test on unsupported architecture")
}
// dst is :ffff:100.100.100.100 which should be counted as IPv4
oob := []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 100, 100, 100, 100, 2, 0, 0, 0}
soob := correctSource(oob)
cm4 := new(ipv4.ControlMessage)
cm4.Src = net.ParseIP("100.100.100.100")
if !bytes.Equal(soob, cm4.Marshal()) {
t.Errorf("unexpected oob for ipv4 address: %v", soob)
}
// dst is 2001:db8::1
oob = []byte{36, 0, 0, 0, 0, 0, 0, 0, 41, 0, 0, 0, 50, 0, 0, 0, 32, 1, 13, 184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0}
soob = correctSource(oob)
cm6 := new(ipv6.ControlMessage)
cm6.Src = net.ParseIP("2001:db8::1")
if !bytes.Equal(soob, cm6.Marshal()) {
t.Errorf("unexpected oob for IPv6 address: %v", soob)
}
}