forked from pires/go-proxyproto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_test.go
213 lines (177 loc) · 5.18 KB
/
policy_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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package proxyproto
import (
"net"
"testing"
)
type failingAddr struct{}
func (f failingAddr) Network() string { return "failing" }
func (f failingAddr) String() string { return "failing" }
func TestWhitelistPolicyReturnsErrorOnInvalidAddress(t *testing.T) {
var cases = []struct {
name string
policy PolicyFunc
}{
{"strict whitelist policy", MustStrictWhiteListPolicy([]string{"10.0.0.2", "10.0.0.3", "10.0.0.4", "10.0.0.0/30"})},
{"lax whitelist policy", MustLaxWhiteListPolicy([]string{"10.0.0.2", "10.0.0.3", "10.0.0.4", "10.0.0.0/30"})},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.policy(failingAddr{})
if err == nil {
t.Fatal("Expected error, got none")
}
})
}
}
func TestStrictWhitelistPolicyReturnsRejectWhenUpstreamIpAddrNotInWhitelist(t *testing.T) {
p := MustStrictWhiteListPolicy([]string{"10.0.0.2", "10.0.0.3", "10.0.0.4", "10.0.0.0/30"})
upstream, err := net.ResolveTCPAddr("tcp", "10.0.0.5:45738")
if err != nil {
t.Fatalf("err: %v", err)
}
policy, err := p(upstream)
if err != nil {
t.Fatalf("err: %v", err)
}
if policy != REJECT {
t.Fatalf("Expected policy REJECT, got %v", policy)
}
}
func TestLaxWhitelistPolicyReturnsIgnoreWhenUpstreamIpAddrNotInWhitelist(t *testing.T) {
p := MustLaxWhiteListPolicy([]string{"10.0.0.2", "10.0.0.3", "10.0.0.4", "10.0.0.0/30"})
upstream, err := net.ResolveTCPAddr("tcp", "10.0.0.5:45738")
if err != nil {
t.Fatalf("err: %v", err)
}
policy, err := p(upstream)
if err != nil {
t.Fatalf("err: %v", err)
}
if policy != IGNORE {
t.Fatalf("Expected policy IGNORE, got %v", policy)
}
}
func TestWhitelistPolicyReturnsUseWhenUpstreamIpAddrInWhitelist(t *testing.T) {
var cases = []struct {
name string
policy PolicyFunc
}{
{"strict whitelist policy", MustStrictWhiteListPolicy([]string{"10.0.0.2", "10.0.0.3", "10.0.0.4"})},
{"lax whitelist policy", MustLaxWhiteListPolicy([]string{"10.0.0.2", "10.0.0.3", "10.0.0.4"})},
}
upstream, err := net.ResolveTCPAddr("tcp", "10.0.0.3:45738")
if err != nil {
t.Fatalf("err: %v", err)
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
policy, err := tc.policy(upstream)
if err != nil {
t.Fatalf("err: %v", err)
}
if policy != USE {
t.Fatalf("Expected policy USE, got %v", policy)
}
})
}
}
func TestWhitelistPolicyReturnsUseWhenUpstreamIpAddrInWhitelistRange(t *testing.T) {
var cases = []struct {
name string
policy PolicyFunc
}{
{"strict whitelist policy", MustStrictWhiteListPolicy([]string{"10.0.0.0/29"})},
{"lax whitelist policy", MustLaxWhiteListPolicy([]string{"10.0.0.0/29"})},
}
upstream, err := net.ResolveTCPAddr("tcp", "10.0.0.3:45738")
if err != nil {
t.Fatalf("err: %v", err)
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
policy, err := tc.policy(upstream)
if err != nil {
t.Fatalf("err: %v", err)
}
if policy != USE {
t.Fatalf("Expected policy USE, got %v", policy)
}
})
}
}
func Test_CreateWhitelistPolicyWithInvalidCidrReturnsError(t *testing.T) {
_, err := StrictWhiteListPolicy([]string{"20/80"})
if err == nil {
t.Error("Expected error, got none")
}
}
func Test_CreateWhitelistPolicyWithInvalidIpAddressReturnsError(t *testing.T) {
_, err := StrictWhiteListPolicy([]string{"855.222.233.11"})
if err == nil {
t.Error("Expected error, got none")
}
}
func Test_CreateLaxPolicyWithInvalidCidrReturnsError(t *testing.T) {
_, err := LaxWhiteListPolicy([]string{"20/80"})
if err == nil {
t.Error("Expected error, got none")
}
}
func Test_CreateLaxPolicyWithInvalidIpAddresseturnsError(t *testing.T) {
_, err := LaxWhiteListPolicy([]string{"855.222.233.11"})
if err == nil {
t.Error("Expected error, got none")
}
}
func Test_MustLaxWhiteListPolicyPanicsWithInvalidIpAddress(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected a panic, but got none")
}
}()
MustLaxWhiteListPolicy([]string{"855.222.233.11"})
}
func Test_MustLaxWhiteListPolicyPanicsWithInvalidIpRange(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected a panic, but got none")
}
}()
MustLaxWhiteListPolicy([]string{"20/80"})
}
func Test_MustStrictWhiteListPolicyPanicsWithInvalidIpAddress(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected a panic, but got none")
}
}()
MustStrictWhiteListPolicy([]string{"855.222.233.11"})
}
func Test_MustStrictWhiteListPolicyPanicsWithInvalidIpRange(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Error("Expected a panic, but got none")
}
}()
MustStrictWhiteListPolicy([]string{"20/80"})
}
func TestSkipProxyHeaderForCIDR(t *testing.T) {
_, cidr, _ := net.ParseCIDR("192.0.2.1/24")
f := SkipProxyHeaderForCIDR(cidr, REJECT)
upstream, _ := net.ResolveTCPAddr("tcp", "192.0.2.255:12345")
policy, err := f(upstream)
if err != nil {
t.Fatalf("err: %v", err)
}
if policy != SKIP {
t.Errorf("Expected a SKIP policy for the %s address", upstream)
}
upstream, _ = net.ResolveTCPAddr("tcp", "8.8.8.8:12345")
policy, err = f(upstream)
if err != nil {
t.Fatalf("err: %v", err)
}
if policy != REJECT {
t.Errorf("Expected a REJECT policy for the %s address", upstream)
}
}