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