@@ -9,63 +9,88 @@ import (
9
9
)
10
10
11
11
var (
12
- TCP4AddressesAndPorts = strings .Join ([]string {IP4_ADDR , IP4_ADDR , strconv .Itoa (PORT ), strconv .Itoa (PORT )}, separator )
13
- TCP4AddressesAndInvalidPorts = strings .Join ([]string {IP4_ADDR , IP4_ADDR , strconv .Itoa (INVALID_PORT ), strconv .Itoa (INVALID_PORT )}, separator )
14
- TCP6AddressesAndPorts = strings .Join ([]string {IP6_ADDR , IP6_ADDR , strconv .Itoa (PORT ), strconv .Itoa (PORT )}, separator )
12
+ IPv4AddressesAndPorts = strings .Join ([]string {IP4_ADDR , IP4_ADDR , strconv .Itoa (PORT ), strconv .Itoa (PORT )}, separator )
13
+ IPv4AddressesAndInvalidPorts = strings .Join ([]string {IP4_ADDR , IP4_ADDR , strconv .Itoa (INVALID_PORT ), strconv .Itoa (INVALID_PORT )}, separator )
14
+ IPv6AddressesAndPorts = strings .Join ([]string {IP6_ADDR , IP6_ADDR , strconv .Itoa (PORT ), strconv .Itoa (PORT )}, separator )
15
15
16
- fixtureTCP4V1 = "PROXY TCP4 " + TCP4AddressesAndPorts + crlf + "GET /"
17
- fixtureTCP6V1 = "PROXY TCP6 " + TCP6AddressesAndPorts + crlf + "GET /"
16
+ fixtureTCP4V1 = "PROXY TCP4 " + IPv4AddressesAndPorts + crlf + "GET /"
17
+ fixtureTCP6V1 = "PROXY TCP6 " + IPv6AddressesAndPorts + crlf + "GET /"
18
+
19
+ fixtureUnknown = "PROXY UNKNOWN" + crlf
20
+ fixtureUnknownWithAddresses = "PROXY UNKNOWN " + IPv4AddressesAndInvalidPorts + crlf
18
21
)
19
22
20
23
var invalidParseV1Tests = []struct {
24
+ desc string
21
25
reader * bufio.Reader
22
26
expectedError error
23
27
}{
24
28
{
25
- newBufioReader ([]byte ("PROX" )),
26
- ErrNoProxyProtocol ,
29
+ desc : "no signature" ,
30
+ reader : newBufioReader ([]byte (NO_PROTOCOL )),
31
+ expectedError : ErrNoProxyProtocol ,
32
+ },
33
+ {
34
+ desc : "prox" ,
35
+ reader : newBufioReader ([]byte ("PROX" )),
36
+ expectedError : ErrNoProxyProtocol ,
37
+ },
38
+ {
39
+ desc : "proxy lf" ,
40
+ reader : newBufioReader ([]byte ("PROXY \n " )),
41
+ expectedError : ErrLineMustEndWithCrlf ,
27
42
},
28
43
{
29
- newBufioReader ([]byte (NO_PROTOCOL )),
30
- ErrNoProxyProtocol ,
44
+ desc : "proxy crlf" ,
45
+ reader : newBufioReader ([]byte ("PROXY " + crlf )),
46
+ expectedError : ErrCantReadAddressFamilyAndProtocol ,
31
47
},
32
48
{
33
- newBufioReader ([]byte ("PROXY \r \n " )),
34
- ErrCantReadProtocolVersionAndCommand ,
49
+ desc : "proxy something crlf" ,
50
+ reader : newBufioReader ([]byte ("PROXY SOMETHING" + crlf )),
51
+ expectedError : ErrCantReadAddressFamilyAndProtocol ,
35
52
},
36
53
{
37
- newBufioReader ([]byte ("PROXY TCP4 " + TCP4AddressesAndPorts )),
38
- ErrCantReadProtocolVersionAndCommand ,
54
+ desc : "incomplete signature TCP4" ,
55
+ reader : newBufioReader ([]byte ("PROXY TCP4 " + IPv4AddressesAndPorts )),
56
+ expectedError : ErrLineMustEndWithCrlf ,
39
57
},
40
58
{
41
- newBufioReader ([]byte ("PROXY TCP6 " + TCP4AddressesAndPorts + crlf )),
42
- ErrInvalidAddress ,
59
+ desc : "TCP6 with IPv4 addresses" ,
60
+ reader : newBufioReader ([]byte ("PROXY TCP6 " + IPv4AddressesAndPorts + crlf )),
61
+ expectedError : ErrInvalidAddress ,
43
62
},
44
63
{
45
- newBufioReader ([]byte ("PROXY TCP4 " + TCP6AddressesAndPorts + crlf )),
46
- ErrInvalidAddress ,
64
+ desc : "TCP4 with IPv6 addresses" ,
65
+ reader : newBufioReader ([]byte ("PROXY TCP4 " + IPv6AddressesAndPorts + crlf )),
66
+ expectedError : ErrInvalidAddress ,
47
67
},
48
- // PROXY TCP IPv4
49
- {newBufioReader ([]byte ("PROXY TCP4 " + TCP4AddressesAndInvalidPorts + crlf )),
50
- ErrInvalidPortNumber ,
68
+ {
69
+ desc : "TCP4 with invalid port" ,
70
+ reader : newBufioReader ([]byte ("PROXY TCP4 " + IPv4AddressesAndInvalidPorts + crlf )),
71
+ expectedError : ErrInvalidPortNumber ,
51
72
},
52
73
}
53
74
54
75
func TestReadV1Invalid (t * testing.T ) {
55
76
for _ , tt := range invalidParseV1Tests {
56
- if _ , err := Read (tt .reader ); err != tt .expectedError {
57
- t .Fatalf ("TestReadV1Invalid: expected %s, actual %s" , tt .expectedError , err .Error ())
58
- }
77
+ t .Run (tt .desc , func (t * testing.T ) {
78
+ if _ , err := Read (tt .reader ); err != tt .expectedError {
79
+ t .Fatalf ("expected %s, actual %s" , tt .expectedError , err .Error ())
80
+ }
81
+ })
59
82
}
60
83
}
61
84
62
85
var validParseAndWriteV1Tests = []struct {
86
+ desc string
63
87
reader * bufio.Reader
64
88
expectedHeader * Header
65
89
}{
66
90
{
67
- bufio .NewReader (strings .NewReader (fixtureTCP4V1 )),
68
- & Header {
91
+ desc : "TCP4" ,
92
+ reader : bufio .NewReader (strings .NewReader (fixtureTCP4V1 )),
93
+ expectedHeader : & Header {
69
94
Version : 1 ,
70
95
Command : PROXY ,
71
96
TransportProtocol : TCPv4 ,
@@ -74,47 +99,74 @@ var validParseAndWriteV1Tests = []struct {
74
99
},
75
100
},
76
101
{
77
- bufio .NewReader (strings .NewReader (fixtureTCP6V1 )),
78
- & Header {
102
+ desc : "TCP6" ,
103
+ reader : bufio .NewReader (strings .NewReader (fixtureTCP6V1 )),
104
+ expectedHeader : & Header {
79
105
Version : 1 ,
80
106
Command : PROXY ,
81
107
TransportProtocol : TCPv6 ,
82
108
SourceAddr : v6addr ,
83
109
DestinationAddr : v6addr ,
84
110
},
85
111
},
112
+ {
113
+ desc : "unknown" ,
114
+ reader : bufio .NewReader (strings .NewReader (fixtureUnknown )),
115
+ expectedHeader : & Header {
116
+ Version : 1 ,
117
+ Command : PROXY ,
118
+ TransportProtocol : UNSPEC ,
119
+ SourceAddr : nil ,
120
+ DestinationAddr : nil ,
121
+ },
122
+ },
123
+ {
124
+ desc : "unknown with addresses and ports" ,
125
+ reader : bufio .NewReader (strings .NewReader (fixtureUnknownWithAddresses )),
126
+ expectedHeader : & Header {
127
+ Version : 1 ,
128
+ Command : PROXY ,
129
+ TransportProtocol : UNSPEC ,
130
+ SourceAddr : nil ,
131
+ DestinationAddr : nil ,
132
+ },
133
+ },
86
134
}
87
135
88
136
func TestParseV1Valid (t * testing.T ) {
89
137
for _ , tt := range validParseAndWriteV1Tests {
90
- header , err := Read (tt .reader )
91
- if err != nil {
92
- t .Fatal ("TestParseV1Valid: unexpected error" , err .Error ())
93
- }
94
- if ! header .EqualsTo (tt .expectedHeader ) {
95
- t .Fatalf ("TestParseV1Valid: expected %#v, actual %#v" , tt .expectedHeader , header )
96
- }
138
+ t .Run (tt .desc , func (t * testing.T ) {
139
+ header , err := Read (tt .reader )
140
+ if err != nil {
141
+ t .Fatal ("unexpected error" , err .Error ())
142
+ }
143
+ if ! header .EqualsTo (tt .expectedHeader ) {
144
+ t .Fatalf ("expected %#v, actual %#v" , tt .expectedHeader , header )
145
+ }
146
+ })
97
147
}
98
148
}
99
149
100
150
func TestWriteV1Valid (t * testing.T ) {
101
151
for _ , tt := range validParseAndWriteV1Tests {
102
- var b bytes.Buffer
103
- w := bufio .NewWriter (& b )
104
- if _ , err := tt .expectedHeader .WriteTo (w ); err != nil {
105
- t .Fatal ("TestWriteV1Valid: Unexpected error " , err )
106
- }
107
- w .Flush ()
152
+ t .Run (tt .desc , func (t * testing.T ) {
153
+ var b bytes.Buffer
154
+ w := bufio .NewWriter (& b )
155
+ if _ , err := tt .expectedHeader .WriteTo (w ); err != nil {
156
+ t .Fatal ("unexpected error " , err )
157
+ }
158
+ w .Flush ()
108
159
109
- // Read written bytes to validate written header
110
- r := bufio .NewReader (& b )
111
- newHeader , err := Read (r )
112
- if err != nil {
113
- t .Fatal ("TestWriteV1Valid: Unexpected error " , err )
114
- }
160
+ // Read written bytes to validate written header
161
+ r := bufio .NewReader (& b )
162
+ newHeader , err := Read (r )
163
+ if err != nil {
164
+ t .Fatal ("unexpected error " , err )
165
+ }
115
166
116
- if ! newHeader .EqualsTo (tt .expectedHeader ) {
117
- t .Fatalf ("TestWriteV1Valid: expected %#v, actual %#v" , tt .expectedHeader , newHeader )
118
- }
167
+ if ! newHeader .EqualsTo (tt .expectedHeader ) {
168
+ t .Fatalf ("expected %#v, actual %#v" , tt .expectedHeader , newHeader )
169
+ }
170
+ })
119
171
}
120
172
}
0 commit comments