-
Notifications
You must be signed in to change notification settings - Fork 11
/
tcp_md5_sig_linux_test.go
179 lines (169 loc) · 4.18 KB
/
tcp_md5_sig_linux_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
169
170
171
172
173
174
175
176
177
178
179
package corebgp
import (
"context"
"fmt"
"net"
"net/netip"
"syscall"
"testing"
"time"
)
func TestSetTCPMD5Signature(t *testing.T) {
// setup AF_INET wildcard socket
lis, err := net.Listen("tcp4", ":0")
if err != nil {
t.Fatalf("error listening: %v", err)
}
defer lis.Close()
_, port, err := net.SplitHostPort(lis.Addr().String())
if err != nil {
t.Fatalf("error splitting host/port: %v", err)
}
tlis, ok := lis.(*net.TCPListener)
if !ok {
t.Fatal("not tcp listener")
}
raw, err := tlis.SyscallConn()
if err != nil {
t.Fatalf("error getting raw conn: %v", err)
}
// set key w/nil addr, this should fail
var seterr error
err = raw.Control(func(fdPtr uintptr) {
fd := int(fdPtr)
// nil address
seterr = SetTCPMD5Signature(fd, netip.Addr{}, 32,
"password")
})
if err != nil {
t.Fatalf("control err: %v", err)
}
if seterr == nil {
t.Fatal("nil address should fail")
}
// set ipv6 addr on AF_INET socket, this should fail
err = raw.Control(func(fdPtr uintptr) {
fd := int(fdPtr)
// ipv6 address
seterr = SetTCPMD5Signature(fd, netip.MustParseAddr("2001:db8::1"),
128, "password")
})
if err != nil {
t.Fatalf("control err: %v", err)
}
if seterr == nil {
t.Fatal("ipv6 address on ipv4 socket should fail")
}
// set valid ipv4 addr/key on AF_INET socket
err = raw.Control(func(fdPtr uintptr) {
fd := int(fdPtr)
// valid
seterr = SetTCPMD5Signature(fd, netip.MustParseAddr("127.0.0.1"),
32, "password")
})
if err != nil {
t.Fatalf("control err: %v", err)
}
if seterr != nil {
t.Fatalf("unexpected error: %v", err)
}
// dial w/password from previously set addr, this should succeed
laddr, err := net.ResolveTCPAddr("tcp",
net.JoinHostPort("127.0.0.1", "0"))
if err != nil {
t.Fatalf("error resolving laddr: %v", err)
}
dialer := &net.Dialer{
Timeout: time.Second,
LocalAddr: laddr,
Control: func(network, address string, c syscall.RawConn) error {
err := c.Control(func(fdPtr uintptr) {
fd := int(fdPtr)
seterr = SetTCPMD5Signature(fd, netip.MustParseAddr("127.0.0.1"),
32, "password")
})
if err != nil {
return err
}
return seterr
},
}
conn, err := dialer.Dial("tcp", fmt.Sprintf("127.0.0.1:%s",
port))
if err != nil {
t.Fatalf("error dialing w/md5: %v", err)
}
defer conn.Close()
// unset previously set password
err = raw.Control(func(fdPtr uintptr) {
fd := int(fdPtr)
// unset
seterr = SetTCPMD5Signature(fd, netip.MustParseAddr("127.0.0.1"),
32, "")
})
if err != nil {
t.Fatalf("control err: %v", err)
}
if seterr != nil {
t.Fatalf("error unsetting: %v", err)
}
// dial w/o password, this should succeed
conn, err = net.Dial("tcp", fmt.Sprintf("127.0.0.1:%s", port))
if err != nil {
t.Fatalf("error dialing w/o md5: %v", err)
}
defer conn.Close()
// create wildcard dual stack socket and set password for IPv4 addr, this
// should succeed
lc := &net.ListenConfig{
Control: func(network, address string, c syscall.RawConn) error {
err := c.Control(func(fdPtr uintptr) {
fd := int(fdPtr)
seterr = SetTCPMD5Signature(fd, netip.MustParseAddr("127.0.0.1"),
32, "password")
})
if err != nil {
return err
}
return seterr
},
}
lis, err = lc.Listen(context.Background(), "tcp",
net.JoinHostPort("::", "0"))
if err != nil {
t.Fatalf("error listening: %v", err)
}
defer lis.Close()
_, port, err = net.SplitHostPort(lis.Addr().String())
if err != nil {
t.Fatalf("error splitting host/port: %v", err)
}
laddr, err = net.ResolveTCPAddr("tcp",
net.JoinHostPort("127.0.0.1", "0"))
if err != nil {
t.Fatalf("error resolving laddr: %v", err)
}
// dial the wildcard dual stack socket using IPv4 and previously set
// password, this should succeed
dialer = &net.Dialer{
Timeout: time.Second,
LocalAddr: laddr,
Control: func(network, address string, c syscall.RawConn) error {
err := c.Control(func(fdPtr uintptr) {
fd := int(fdPtr)
seterr = SetTCPMD5Signature(fd, netip.MustParseAddr("127.0.0.1"),
32, "password")
})
if err != nil {
return err
}
return seterr
},
}
conn, err = dialer.Dial("tcp4", fmt.Sprintf("127.0.0.1:%s",
port))
if err != nil {
t.Fatalf("error dialing: %v", err)
}
defer conn.Close()
}