-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipfilter_test.go
154 lines (148 loc) · 3.42 KB
/
ipfilter_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
package ipfilter_test
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
ipfilter "github.com/crazy-max/echo-ipfilter"
jpillorafilter "github.com/jpillora/ipfilter"
"github.com/labstack/echo/v4"
)
func TestMiddleware(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.RemoteAddr = "10.0.0.1"
res := httptest.NewRecorder()
c := e.NewContext(req, res)
h := ipfilter.Middleware()(func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})
if err := h(c); err == nil {
t.Error("expected error, got nil")
}
}
func TestMiddlewareWithConfig(t *testing.T) {
cases := []struct {
name string
config ipfilter.Config
ip string
err error
}{
{
name: "blocked with whitelist",
config: ipfilter.Config{
WhiteList: []string{
"123.123.123.123",
},
BlockByDefault: true,
},
ip: "223.123.123.123:1234",
err: echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("IP address %s not allowed", "223.123.123.123")),
},
{
name: "not blocked",
config: ipfilter.Config{
WhiteList: []string{
"223.123.123.123",
},
},
ip: "223.123.123.123:1234",
// Not blocked.
err: nil,
},
{
name: "not blocked cidr",
config: ipfilter.Config{
WhiteList: []string{
"223.123.123.0/24",
},
},
ip: "223.123.123.230:1234",
err: nil,
},
{
name: "blocked by blacklist",
config: ipfilter.Config{
WhiteList: []string{
"223.123.123.0/24",
},
BlackList: []string{
"223.123.123.230",
},
},
ip: "223.123.123.230:1234",
err: echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("IP address %s not allowed", "223.123.123.230")),
},
{
name: "allowed by default whitelist",
config: ipfilter.Config{},
ip: "123.123.123.123:1234",
err: nil,
},
{
name: "allowed by whitelist",
config: ipfilter.Config{
WhiteList: []string{
"10.1.2.0/24",
"10.1.4.0/24",
},
BlockByDefault: true,
},
ip: "10.1.4.1:80",
err: nil,
},
{
name: "dynamically allowed by whitelist",
config: ipfilter.Config{
WhiteList: []string{
"10.1.2.0/24",
// this will be dynamically added "10.1.4.0/24",
},
BlockByDefault: true,
CreatedFilter: func(filter *jpillorafilter.IPFilter) {
filter.AllowIP("10.1.4.0/24")
},
},
ip: "10.1.4.1:80",
err: nil,
},
{
name: "dynamically allowed by whitelist",
config: ipfilter.Config{
WhiteList: []string{
"10.1.2.0/24", // will be dynamicaly blocked
"10.1.4.0/24",
},
BlockByDefault: true,
CreatedFilter: func(filter *jpillorafilter.IPFilter) {
filter.BlockIP("10.1.2.0/24")
},
},
ip: "10.1.2.7:80",
err: echo.NewHTTPError(http.StatusForbidden, fmt.Sprintf("IP address %s not allowed", "10.1.2.7")),
},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.RemoteAddr = tt.ip
res := httptest.NewRecorder()
c := e.NewContext(req, res)
h := ipfilter.MiddlewareWithConfig(tt.config)(func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})
switch err := h(c); err.(type) {
case nil:
if tt.err != nil {
t.Errorf("expected error %v, got nil", tt.err)
}
case *echo.HTTPError:
if tt.err.Error() != err.Error() {
t.Errorf("expected error %v, got %v", tt.err, err)
}
}
})
}
}