-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhitelisting_test.go
178 lines (172 loc) · 3.31 KB
/
whitelisting_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
package doorman
import (
"net"
"reflect"
"testing"
"go.uber.org/zap"
)
func createNetworks(nw ...string) Networks {
res := make(Networks, 0)
for _, n := range nw {
_, netw, _ := net.ParseCIDR(n)
res = append(res, netw)
}
return res
}
func compareNetworks(t *testing.T, got, want Networks) {
if len(got) != len(want) {
t.Errorf("got: %v, want %v", got, want)
return
}
for i := range got {
nw1 := got[i]
nw2 := want[i]
if nw1.String() != nw2.String() {
t.Errorf("got: %v, want %v", got, want)
return
}
}
}
func Test_parseStaticList(t *testing.T) {
type args struct {
ips []string
}
tests := []struct {
name string
args args
want IPMap
want1 Networks
wantErr bool
}{
{
name: "single ip",
args: args{
ips: []string{"1.2.3.4"},
},
want: IPMap{"1.2.3.4": true},
want1: make(Networks, 0),
wantErr: false,
},
{
name: "illegal ip",
args: args{
ips: []string{"a.2.3.4"},
},
want: nil,
want1: nil,
wantErr: true,
},
{
name: "single network",
args: args{
ips: []string{"1.2.3.4/8"},
},
want: make(IPMap),
want1: createNetworks("1.0.0.0/8"),
wantErr: false,
},
{
name: "network and ip",
args: args{
ips: []string{"1.2.3.4/8", "2.3.4.5"},
},
want: IPMap{"2.3.4.5": true},
want1: createNetworks("1.0.0.0/8"),
wantErr: false,
},
{
name: "network with 32 mask",
args: args{
ips: []string{"1.2.3.4/32"},
},
want: IPMap{"1.2.3.4": true},
want1: make(Networks, 0),
wantErr: false,
},
{
name: "network and ip with illegal entry",
args: args{
ips: []string{"1.2.3.4/8", "2.3.4.5", "1.2.3.4/33"},
},
want: nil,
want1: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, err := parseStaticList(tt.args.ips)
if (err != nil) != tt.wantErr {
t.Errorf("parseStaticList() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseStaticList() map: got = %v, want %v", got, tt.want)
}
compareNetworks(t, got1, tt.want1)
})
}
}
func Test_isAllowed(t *testing.T) {
type args struct {
list staticWhiteList
clientip string
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
name: "empty store",
args: args{
clientip: "127.0.0.1",
},
want: false,
},
{
name: "illegal IP",
args: args{
clientip: "a.b.c.d",
},
want: false,
},
{
name: "ip is allowed",
args: args{
list: []string{"127.0.0.1"},
clientip: "127.0.0.1",
},
want: true,
},
{
name: "ip is in network",
args: args{
list: []string{"127.0.0.0/24"},
clientip: "127.0.0.1",
},
want: true,
},
{
name: "ip is in network and explicit allowed",
args: args{
list: []string{"127.0.0.1", "127.0.0.0/24"},
clientip: "127.0.0.1",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wl, err := tt.args.list.Fetch(zap.NewNop())
if (err != nil) != tt.wantErr {
t.Errorf("Fetch() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got := wl.IsAllowed(zap.NewNop(), tt.args.clientip); got != tt.want {
t.Errorf("isAllowed() = %v, want %v", got, tt.want)
}
})
}
}