forked from mailgun/proxyproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1_test.go
191 lines (184 loc) · 5.89 KB
/
v1_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
180
181
182
183
184
185
186
187
188
189
190
191
package proxyproto_test
import (
"net"
"strings"
"testing"
"github.com/stratumfarm/proxyproto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestParseV1Header(t *testing.T) {
tests := []struct {
name string
header string
src net.Addr
dest net.Addr
unknown []byte
err string
}{
{
name: "TCP4 Minimal",
header: "PROXY TCP4 1.1.1.5 1.1.1.6 2 3\r\n",
src: &net.TCPAddr{IP: net.ParseIP("1.1.1.5"), Port: 2},
dest: &net.TCPAddr{IP: net.ParseIP("1.1.1.6"), Port: 3},
},
{
name: "TCP4 Maximal",
header: "PROXY TCP4 255.255.255.255 255.255.255.254 65535 65535\r\n",
src: &net.TCPAddr{IP: net.ParseIP("255.255.255.255"), Port: 65535},
dest: &net.TCPAddr{IP: net.ParseIP("255.255.255.254"), Port: 65535},
},
{
name: "TCP6 Minimal",
header: "PROXY TCP6 ::1 ::2 3 4\r\n",
src: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 3},
dest: &net.TCPAddr{IP: net.ParseIP("::2"), Port: 4},
},
{
name: "TCP6 Maximal",
header: "PROXY TCP6 0000:0000:0000:0000:0000:0000:0000:0002 0000:0000:0000:0000:0000:0000:0000:0001 65535 65535\r\n",
src: &net.TCPAddr{IP: net.ParseIP("0000:0000:0000:0000:0000:0000:0000:0002"), Port: 65535},
dest: &net.TCPAddr{IP: net.ParseIP("0000:0000:0000:0000:0000:0000:0000:0001"), Port: 65535},
},
{
name: "UNKNOWN Minimal",
header: "PROXY UNKNOWN\r\n",
unknown: []byte("PROXY UNKNOWN"),
},
{
name: "UNKNOWN Maximal",
header: "PROXY UNKNOWN 0000:0000:0000:0000:0000:0000:0000:0002 0000:0000:0000:0000:0000:0000:0000:0001 65535 65535\r\n",
unknown: []byte("PROXY UNKNOWN 0000:0000:0000:0000:0000:0000:0000:0002 0000:0000:0000:0000:0000:0000:0000:0001 65535 65535"),
},
{
name: "TCP6 Empty",
header: "PROXY TCP6\r\n",
src: &net.TCPAddr{IP: net.ParseIP("::1"), Port: 3},
dest: &net.TCPAddr{IP: net.ParseIP("::2"), Port: 4},
err: "while reading proxy proto identifier: unexpected EOF",
},
{
name: "TCP6 cRLF Not found",
header: "PROXY TCP6 0000:0000:0000:0000:0000:0000:0000:0001 0000:0000:0000:0000:0000:0000:0000:0001 65535 65535XXXX\r\n",
err: "while parsing proxy proto v1 header: while looking for cRLF after proto: gave up after 107 bytes",
},
{
name: "UNKNOWN cRLF Not found",
header: "PROXY UNKNOWN 0000:0000:0000:0000:0000:0000:0000:0001 0000:0000:0000:0000:0000:0000:0000:0001 65535 65535X\r\n",
err: "while parsing proxy proto v1 header: while looking for cRLF after UNKNOWN proto: gave up after 107 bytes",
},
{
name: "UNKNOWN No cRLF",
header: "PROXY UNKNOWN",
err: "while parsing proxy proto v1 header: while looking for cRLF after UNKNOWN proto: expected to read more bytes, but got none",
},
{
name: "Only cRLF Header",
header: "\r\n",
err: "while reading proxy proto identifier: unexpected EOF",
},
{
name: "Empty Header",
header: "",
err: "while reading proxy proto identifier: EOF",
},
{
name: "Garbage Header",
header: "ASDFASDGSAG@#!@#$!WDFGASDGASDFG#TAGASDFASDG@",
err: "expected proxy protocol; found '00000000 41 53 44 46 41 53 44 47 53 41 47 40 23 00 |ASDFASDGSAG@#.|\n' instead",
},
{
name: "TCP4 Incomplete",
header: "PROXY TCP4 garbage\r\n",
err: "while parsing proxy proto v1 header: while reading tcp4 addresses: unexpected EOF",
},
{
name: "TCP6 Incomplete",
header: "PROXY TCP6 garbage\r\n",
err: "while parsing proxy proto v1 header: while reading tcp6 addresses: unexpected EOF",
},
{
name: "unrecognized proto",
header: "PROXY UNIX :1 :1 234 234\r\n",
err: "while parsing proxy proto v1 header: unrecognized protocol 'UNIX'",
},
{
name: "Invalid src ip",
header: "PROXY TCP4 NOT-AN-IP 192.168.1.1 22 2345\r\n",
err: "while parsing proxy proto v1 header: invalid ip 'NOT-AN-IP' at pos '0'",
},
{
name: "Invalid dest ip",
header: "PROXY TCP4 192.168.1.1 NOT-AN-IP 22 2345\r\n",
err: "while parsing proxy proto v1 header: invalid ip 'NOT-AN-IP' at pos '1'",
},
{
name: "Invalid src port",
header: "PROXY TCP4 192.168.1.1 192.168.1.1 NOT-A-PORT 2345\r\n",
err: "while parsing proxy proto v1 header: invalid port 'NOT-A-PORT' at pos '2'",
},
{
name: "Invalid dest port",
header: "PROXY TCP4 192.168.1.1 192.168.1.1 22 NOT-A-PORT\r\n",
err: "while parsing proxy proto v1 header: invalid port 'NOT-A-PORT' at pos '3'",
},
{
name: "Corrupted address line",
header: "PROXY TCP4 192.168.1.1 192.168.1.1 2345\r\n",
err: "while parsing proxy proto v1 header: address line '192.168.1.1 192.168.1.1 2345' corrupted",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := strings.NewReader(tt.header)
h, err := proxyproto.ReadHeader(r)
if err != nil {
require.Equal(t, err.Error(), tt.err)
return
}
require.NotNil(t, h)
assert.Equal(t, tt.dest, h.Destination)
assert.Equal(t, tt.src, h.Source)
assert.Equal(t, tt.unknown, h.Unknown)
assert.Equal(t, 1, h.Version)
})
}
}
func BenchmarkReadHeaderV1(b *testing.B) {
tests := []struct {
name string
header string
}{
{
name: "TCP4-Minimal",
header: "PROXY TCP4 1.1.1.5 1.1.1.6 2 3\r\n",
},
{
name: "TCP4-Maximal",
header: "PROXY TCP4 255.255.255.255 255.255.255.254 65535 65535\r\n",
},
{
name: "TCP4-Typical",
header: "PROXY TCP4 67.11.173.63 67.11.173.63 65535 65535\r\n",
},
{
name: "TCP6-Minimal",
header: "PROXY TCP6 ::1 ::2 3 4\r\n",
},
{
name: "TCP6-Maximal",
header: "PROXY TCP6 0000:0000:0000:0000:0000:0000:0000:0002 0000:0000:0000:0000:0000:0000:0000:0001 65535 65535\r\n",
},
}
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
r := strings.NewReader(tt.header)
_, err := proxyproto.ReadHeader(r)
if err != nil {
b.Errorf("ReadHeader err: %s", err)
}
}
})
}
}