generated from xiachufang/go-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfilter_test.go
119 lines (116 loc) · 2.48 KB
/
filter_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
package ipfilter
import (
"testing"
"github.com/yl2chen/cidranger"
)
func TestCIDRFilter_Allow(t *testing.T) {
type fields struct {
allowRanger cidranger.Ranger
denyRanger cidranger.Ranger
}
type args struct {
ip string
}
tests := []struct {
name string
cfg *Config
args map[string]bool
want bool
}{
{
name: "allow only",
cfg: &Config{
Allow: []string{
"127.0.0.1",
"127.0.0.2",
"127.0.0.3",
"192.168.1.0/24",
"9ff1:4027:9788:741c:7c56:1970:227a:033e",
"4ff1:4027:9788:741c:0000:0000:0000:0000/64",
},
},
args: map[string]bool{
"127.0.0.1": true,
"127.0.0.2": true,
"127.0.0.3": true,
"127.0.0.4": false,
"192.168.1.1": true,
"191.168.1.1": false,
"9ff1:4027:9788:741c:7c56:1970:227a:033e": true,
"4ff1:4027:9788:741c:7c56:1970:227a:033e": true,
"5ff1:4027:9788:741c:7c56:1970:227a:033e": false,
},
},
{
name: "deny only",
cfg: &Config{
Deny: []string{
"127.0.0.1",
"127.0.0.2",
"127.0.0.3",
"192.168.1.0/24",
"9ff1:4027:9788:741c:7c56:1970:227a:033e",
"4ff1:4027:9788:741c:0000:0000:0000:0000/64",
},
},
args: map[string]bool{
"127.0.0.1": false,
"127.0.0.2": false,
"127.0.0.3": false,
"127.0.0.4": true,
"192.168.1.1": false,
"191.168.1.1": true,
"9ff1:4027:9788:741c:7c56:1970:227a:033e": false,
"4ff1:4027:9788:741c:7c56:1970:227a:033e": false,
"5ff1:4027:9788:741c:7c56:1970:227a:033e": true,
},
},
{
name: "allow and deny",
cfg: &Config{
Allow: []string{
"127.0.0.0/24",
"4ff1:4027:9788:741c:0000:0000:0000:0000/64",
},
Deny: []string{
"127.0.0.1",
"4ff1:4027:9788:741c:7c56:1970:227a:033e",
},
},
args: map[string]bool{
"127.0.0.1": false,
"127.0.0.2": true,
"192.168.1.1": false,
"4ff1:4027:9788:741c:7c56:1970:227a:033e": false,
"4ff1:4027:9788:741c:7c56:1970:227a:133e": true,
"9ff1:4027:9788:741c:7c56:1970:227a:133e": false,
},
},
{
name: "invalid ip",
cfg: &Config{
Allow: []string{
"127.0.0.0/24",
},
Deny: []string{
"127.0.0.1",
},
},
args: map[string]bool{
"": false,
"a.b.c.d": false,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ranger := NewIPFilter(tt.cfg)
for ip, want := range tt.args {
got := ranger.Allow(ip)
if got != want {
t.Errorf("Allow(%s) = %v, want %v", ip, got, want)
}
}
})
}
}