-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_test.go
162 lines (137 loc) · 3.51 KB
/
proxy_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
package netx
import (
"fmt"
"io"
"net"
"reflect"
"testing"
)
type readOneByOne struct {
b []byte
}
func (r *readOneByOne) Read(b []byte) (n int, err error) {
if len(r.b) == 0 {
err = io.EOF
} else if len(b) != 0 {
n, b[0], r.b = 1, r.b[0], r.b[1:]
}
return
}
func TestProxyProtoV1(t *testing.T) {
tests := []struct {
src net.Addr
dst net.Addr
str string
}{
{
src: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 56789},
dst: &net.TCPAddr{IP: net.ParseIP("192.1.0.123"), Port: 4242},
str: "PROXY TCP4 127.0.0.1 192.1.0.123 56789 4242\r\n",
},
{
src: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 56789},
dst: &net.TCPAddr{IP: net.ParseIP("fe80::f65c:89ff:feac:29cb"), Port: 4242},
str: "PROXY TCP6 ::1 fe80::f65c:89ff:feac:29cb 56789 4242\r\n",
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s->%s", test.src, test.dst), func(t *testing.T) {
b := appendProxyProtoV1(nil, test.src, test.dst)
if s := string(b); s != test.str {
t.Error("bad proxy proto header:", s)
return
}
r := &readOneByOne{b}
a1, a2, buf, local, err := parseProxyProto(r)
if err != nil {
t.Error(err)
}
if len(r.b) != 0 || len(buf) != 0 {
t.Error("unexpected trailing bytes")
}
if !reflect.DeepEqual(test.src, a1) {
t.Errorf("bad source: %#v", a1)
}
if !reflect.DeepEqual(test.dst, a2) {
t.Errorf("bad destination: %#v", a2)
}
if local {
t.Errorf("bad local: %t", local)
}
})
}
}
func TestProxyProtoV2(t *testing.T) {
tests := []struct {
src net.Addr
dst net.Addr
str string
}{
{
src: &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 56789},
dst: &net.TCPAddr{IP: net.ParseIP("192.1.0.123"), Port: 4242},
},
{
src: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 56789},
dst: &net.TCPAddr{IP: net.ParseIP("fe80::f65c:89ff:feac:29cb"), Port: 4242},
},
{
src: &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 56789},
dst: &net.UDPAddr{IP: net.ParseIP("192.1.0.123"), Port: 4242},
},
{
src: &net.UDPAddr{IP: net.ParseIP("::1"), Port: 56789},
dst: &net.UDPAddr{IP: net.ParseIP("fe80::f65c:89ff:feac:29cb"), Port: 4242},
},
{
src: &net.UnixAddr{Net: "unix", Name: "/var/lib/src.sock"},
dst: &net.UnixAddr{Net: "unix", Name: "/var/lib/dst.sock"},
},
{
src: &net.UnixAddr{Net: "unixgram", Name: "/var/lib/src.sock"},
dst: &net.UnixAddr{Net: "unixgram", Name: "/var/lib/dst.sock"},
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s://%s->%s", test.src.Network(), test.src, test.dst), func(t *testing.T) {
b := appendProxyProtoV2(nil, test.src, test.dst, false)
r := &readOneByOne{b}
a1, a2, buf, local, err := parseProxyProto(r)
if err != nil {
t.Error(err)
}
if len(r.b) != 0 || len(buf) != 0 {
t.Errorf("unexpected trailing bytes: %#v %#v", r.b, buf)
}
if !reflect.DeepEqual(test.src, a1) {
t.Errorf("bad source: %#v", a1)
}
if !reflect.DeepEqual(test.dst, a2) {
t.Errorf("bad destination: %#v", a2)
}
if local {
t.Errorf("bad local state: %t", local)
}
})
}
}
func TestProxyProtoV2Local(t *testing.T) {
b := appendProxyProtoV2(nil, &NetAddr{}, &NetAddr{}, true)
r := &readOneByOne{b}
src, dst, buf, local, err := parseProxyProto(r)
if err != nil {
t.Error(err)
}
if len(r.b) != 0 || len(buf) != 0 {
t.Errorf("unexpected trailing bytes: %#v %#v", r.b, buf)
}
if src != nil {
t.Errorf("bad source: %#v", src)
}
if dst != nil {
t.Errorf("bad destination: %#v", dst)
}
if !local {
t.Errorf("bad local state: %t", local)
}
}