@@ -2,6 +2,7 @@ package proxyproto
2
2
3
3
import (
4
4
"bufio"
5
+ "bytes"
5
6
"net"
6
7
"testing"
7
8
"time"
@@ -34,13 +35,13 @@ func TestReadTimeoutV1Invalid(t *testing.T) {
34
35
reader := bufio .NewReader (& b )
35
36
_ , err := ReadTimeout (reader , 50 * time .Millisecond )
36
37
if err == nil {
37
- t .Fatalf ("TestReadTimeoutV1Invalid: expected error %s" , ErrNoProxyProtocol )
38
+ t .Fatalf ("expected error %s" , ErrNoProxyProtocol )
38
39
} else if err != ErrNoProxyProtocol {
39
- t .Fatalf ("TestReadTimeoutV1Invalid: expected %s, actual %s" , ErrNoProxyProtocol , err )
40
+ t .Fatalf ("expected %s, actual %s" , ErrNoProxyProtocol , err )
40
41
}
41
42
}
42
43
43
- func TestEqualTo (t * testing.T ) {
44
+ func TestEqualsTo (t * testing.T ) {
44
45
var headersEqual = []struct {
45
46
this , that * Header
46
47
expected bool
@@ -104,7 +105,171 @@ func TestEqualTo(t *testing.T) {
104
105
105
106
for _ , tt := range headersEqual {
106
107
if actual := tt .this .EqualsTo (tt .that ); actual != tt .expected {
107
- t .Fatalf ("TestEqualTo: expected %t, actual %t" , tt .expected , actual )
108
+ t .Fatalf ("expected %t, actual %t" , tt .expected , actual )
109
+ }
110
+ }
111
+ }
112
+
113
+ // This is here just because of coveralls
114
+ func TestEqualTo (t * testing.T ) {
115
+ TestEqualsTo (t )
116
+ }
117
+
118
+ func TestLocalAddr (t * testing.T ) {
119
+ var headers = []struct {
120
+ header * Header
121
+ expectedAddr net.Addr
122
+ expected bool
123
+ }{
124
+ {
125
+ & Header {
126
+ Version : 1 ,
127
+ Command : PROXY ,
128
+ TransportProtocol : TCPv4 ,
129
+ SourceAddress : net .ParseIP ("10.1.1.1" ),
130
+ SourcePort : 1000 ,
131
+ DestinationAddress : net .ParseIP ("20.2.2.2" ),
132
+ DestinationPort : 2000 ,
133
+ },
134
+ & net.TCPAddr {
135
+ IP : net .ParseIP ("20.2.2.2" ),
136
+ Port : 2000 ,
137
+ },
138
+ true ,
139
+ },
140
+ {
141
+ & Header {
142
+ Version : 1 ,
143
+ Command : PROXY ,
144
+ TransportProtocol : TCPv4 ,
145
+ SourceAddress : net .ParseIP ("10.1.1.1" ),
146
+ SourcePort : 1000 ,
147
+ DestinationAddress : net .ParseIP ("20.2.2.2" ),
148
+ DestinationPort : 2000 ,
149
+ },
150
+ & net.TCPAddr {
151
+ IP : net .ParseIP ("10.1.1.1" ),
152
+ Port : 1000 ,
153
+ },
154
+ false ,
155
+ },
156
+ }
157
+
158
+ for _ , tt := range headers {
159
+ actualAddr := tt .header .LocalAddr ()
160
+ if actual := actualAddr .String () == tt .expectedAddr .String (); actual != tt .expected {
161
+ t .Fatalf ("expected %t, actual %t for expectedAddr %+v and actualAddr %+v" , tt .expected , actual , tt .expectedAddr , actualAddr )
162
+ }
163
+ }
164
+ }
165
+
166
+ func TestRemoteAddr (t * testing.T ) {
167
+ var headers = []struct {
168
+ header * Header
169
+ expectedAddr net.Addr
170
+ expected bool
171
+ }{
172
+ {
173
+ & Header {
174
+ Version : 1 ,
175
+ Command : PROXY ,
176
+ TransportProtocol : TCPv4 ,
177
+ SourceAddress : net .ParseIP ("10.1.1.1" ),
178
+ SourcePort : 1000 ,
179
+ DestinationAddress : net .ParseIP ("20.2.2.2" ),
180
+ DestinationPort : 2000 ,
181
+ },
182
+ & net.TCPAddr {
183
+ IP : net .ParseIP ("20.2.2.2" ),
184
+ Port : 2000 ,
185
+ },
186
+ true ,
187
+ },
188
+ {
189
+ & Header {
190
+ Version : 1 ,
191
+ Command : PROXY ,
192
+ TransportProtocol : TCPv4 ,
193
+ SourceAddress : net .ParseIP ("10.1.1.1" ),
194
+ SourcePort : 1000 ,
195
+ DestinationAddress : net .ParseIP ("20.2.2.2" ),
196
+ DestinationPort : 2000 ,
197
+ },
198
+ & net.TCPAddr {
199
+ IP : net .ParseIP ("10.1.1.1" ),
200
+ Port : 1000 ,
201
+ },
202
+ false ,
203
+ },
204
+ }
205
+
206
+ for _ , tt := range headers {
207
+ actualAddr := tt .header .LocalAddr ()
208
+ if actual := actualAddr .String () == tt .expectedAddr .String (); actual != tt .expected {
209
+ t .Fatalf ("expected %t, actual %t for expectedAddr %+v and actualAddr %+v" , tt .expected , actual , tt .expectedAddr , actualAddr )
210
+ }
211
+ }
212
+ }
213
+
214
+ func TestWriteTo (t * testing.T ) {
215
+ var buf bytes.Buffer
216
+
217
+ validHeader := & Header {
218
+ Version : 1 ,
219
+ Command : PROXY ,
220
+ TransportProtocol : TCPv4 ,
221
+ SourceAddress : net .ParseIP ("10.1.1.1" ),
222
+ SourcePort : 1000 ,
223
+ DestinationAddress : net .ParseIP ("20.2.2.2" ),
224
+ DestinationPort : 2000 ,
225
+ }
226
+
227
+ if _ , err := validHeader .WriteTo (& buf ); err != nil {
228
+ t .Fatalf ("shouldn't have thrown error %q" , err .Error ())
229
+ }
230
+
231
+ invalidHeader := & Header {
232
+ SourceAddress : net .ParseIP ("10.1.1.1" ),
233
+ SourcePort : 1000 ,
234
+ DestinationAddress : net .ParseIP ("20.2.2.2" ),
235
+ DestinationPort : 2000 ,
236
+ }
237
+
238
+ if _ , err := invalidHeader .WriteTo (& buf ); err == nil {
239
+ t .Fatalf ("should have thrown error %q" , err .Error ())
240
+ }
241
+ }
242
+
243
+ func TestFormat (t * testing.T ) {
244
+ validHeader := & Header {
245
+ Version : 1 ,
246
+ Command : PROXY ,
247
+ TransportProtocol : TCPv4 ,
248
+ SourceAddress : net .ParseIP ("10.1.1.1" ),
249
+ SourcePort : 1000 ,
250
+ DestinationAddress : net .ParseIP ("20.2.2.2" ),
251
+ DestinationPort : 2000 ,
252
+ }
253
+
254
+ if _ , err := validHeader .Format (); err != nil {
255
+ t .Fatalf ("shouldn't have thrown error %q" , err .Error ())
256
+ }
257
+
258
+ invalidHeader := & Header {
259
+ Version : 3 ,
260
+ Command : PROXY ,
261
+ TransportProtocol : TCPv4 ,
262
+ SourceAddress : net .ParseIP ("10.1.1.1" ),
263
+ SourcePort : 1000 ,
264
+ DestinationAddress : net .ParseIP ("20.2.2.2" ),
265
+ DestinationPort : 2000 ,
266
+ }
267
+
268
+ if _ , err := invalidHeader .Format (); err == nil {
269
+ t .Fatalf ("should have thrown error %q" , err .Error ())
270
+ } else {
271
+ if err != ErrUnknownProxyProtocolVersion {
272
+ t .Fatalf ("expected %q, actual %q" , ErrUnknownProxyProtocolVersion .Error (), err .Error ())
108
273
}
109
274
}
110
275
}
0 commit comments