-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsets_test.go
415 lines (402 loc) · 11.7 KB
/
sets_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) 2017 Pani Networks
// All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package ipset
import (
"strings"
"testing"
"github.com/pkg/errors"
)
func TestRender(t *testing.T) {
cases := []struct {
name string
sets Ipset
rType RenderType
expect string
}{
{
name: "Render set and member for test",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashNet, Members: []Member{{Elem: "foo"}}},
}},
rType: RenderTest,
expect: "test super foo",
},
{
name: "Render set for test",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashNet},
}},
rType: RenderTest,
expect: "test super",
},
{
name: "Render set and members",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashNet, Members: []Member{{Elem: "foo"}, {Elem: "bar"}}},
}},
rType: RenderSave,
expect: "create super hash:net \nadd super foo\nadd super bar\n",
},
{
name: "Render set and members with counters",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashIP, Header: Header{Counters: new(string)},
Members: []Member{{Elem: "foo", Packets: 42, Bytes: 1024}}},
}},
rType: RenderSave,
expect: "create super hash:ip counters\nadd super foo packets 42 bytes 1024\n",
},
{
name: "Render set and members with timeout",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashIP, Header: Header{Timeout: 600},
Members: []Member{{Elem: "foo", Timeout: 300}, {Elem: "bar"}}},
}},
rType: RenderSave,
expect: "create super hash:ip timeout 600\nadd super foo timeout 300\nadd super bar\n",
},
{
name: "Render set and members with nomatch",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashNet,
Members: []Member{{Elem: "foo"}, {Elem: "bar", NoMatch: new(string)}}},
}},
rType: RenderSave,
expect: "create super hash:net \nadd super foo\nadd super bar nomatch\n",
},
{
name: "Render set and members with comment",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashIP, Header: Header{Comment: new(string)},
Members: []Member{{Elem: "foo", Comment: "allow access to SMB share on fileserv"}}},
}},
rType: RenderSave,
expect: "create super hash:ip comment\nadd super foo comment allow access to SMB share on fileserv\n",
},
{
name: "Render set and members with skb",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashIP, Header: Header{SKBInfo: new(string)},
Members: []Member{{Elem: "foo", SKBMark: "0x1111/0xff00ffff", SKBPrio: "1:10", SKBQueue: "10"}}},
}},
rType: RenderSave,
expect: "create super hash:ip skbinfo\nadd super foo skbmark 0x1111/0xff00ffff skbprio 1:10 skbqueue 10\n",
},
{
name: "Render set and members with hashsize",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashIP, Header: Header{Hashsize: 1536}}}},
rType: RenderSave,
expect: "create super hash:ip hashsize 1536\n",
},
{
name: "Render set and members with maxelem",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashIP, Header: Header{Maxelem: 2048}}}},
rType: RenderSave,
expect: "create super hash:ip maxelem 2048\n",
},
{
name: "Render set and members with family",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashIP, Header: Header{Family: MemberFamilyInet6}}}},
rType: RenderSave,
expect: "create super hash:ip family inet6\n",
},
{
name: "Render set and members with forceadd",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashIP, Header: Header{Forceadd: NoVal}}}},
rType: RenderSave,
expect: "create super hash:ip forceadd\n",
},
{
name: "Render set with size",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetListSet, Header: Header{Size: 8}}}},
rType: RenderSave,
expect: "create super list:set size 8\n",
},
{
name: "Render set and members with range",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashIP, Header: Header{Range: "192.168.0.0/16"}}}},
rType: RenderSave,
expect: "create super hash:ip range 192.168.0.0/16\n",
},
{
name: "Render set and members with range and mac",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetBitmapIPMac, Header: Header{Range: "192.168.0.0/16"},
Members: []Member{{Elem: "192.168.1/24"}}},
}},
rType: RenderSave,
expect: "create super bitmap:ip,mac range 192.168.0.0/16\nadd super 192.168.1/24\n",
},
{
name: "Render set and members with range and port",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetBitmapPort, Header: Header{Range: "0-1024"},
Members: []Member{{Elem: "80"}}},
}},
rType: RenderSave,
expect: "create super bitmap:port range 0-1024\nadd super 80\n",
},
{
name: "Render set and members with netmask",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashIP, Header: Header{Netmask: 30},
Members: []Member{{Elem: "192.168.1.0/24"}}},
}},
rType: RenderSave,
expect: "create super hash:ip netmask 30\nadd super 192.168.1.0/24\n",
},
{
name: "Render set with header",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashNet, Header: Header{Family: "inet", Hashsize: 4}},
}},
rType: RenderSave,
expect: "create super hash:net family inet hashsize 4\n",
},
{
name: "Render set members for creation",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashNet, Members: []Member{{Elem: "foo"}, {Elem: "bar"}}},
}},
rType: RenderAdd,
expect: "add super foo\nadd super bar\n",
},
{
name: "Render set members for deleteion",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashNet, Members: []Member{{Elem: "foo"}, {Elem: "bar"}}},
}},
rType: RenderDelete,
expect: "del super foo\ndel super bar\n",
},
{
name: "Render sets for flush",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashNet, Members: []Member{{Elem: "foo"}, {Elem: "bar"}}},
}},
rType: RenderFlush,
expect: "flush super\n",
},
{
name: "Render sets for destroy",
sets: Ipset{Sets: []*Set{
{Name: "super", Type: SetHashNet, Members: []Member{{Elem: "foo"}, {Elem: "bar"}}},
}},
rType: RenderDestroy,
expect: "destroy super\n",
},
{
name: "Render flush all",
sets: Ipset{},
rType: RenderFlush,
expect: "flush\n",
},
{
name: "Render destroy all",
sets: Ipset{},
rType: RenderDestroy,
expect: "destroy\n",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
res := tc.sets.Render(tc.rType)
if res != tc.expect {
t.Fatalf("Expected:\n%s\ngot:\n%s\n", []byte(tc.expect), []byte(res))
}
})
}
}
func TestNewSet(t *testing.T) {
cases := []struct {
name string
setName string
setType SetType
args []SetOpt
expect func(*Set, error) bool
}{
{
name: "make simple set",
setName: "super",
setType: SetHashNet,
args: []SetOpt{SetWithFamily("inet"), SetWithRevision(1)},
expect: func(s *Set, e error) bool { return s.Header.Family == "inet" && e == nil },
},
{
name: "test set size",
setName: "super",
setType: SetListSet,
args: []SetOpt{SetWithSize(4)},
expect: func(s *Set, e error) bool { return s.Header.Size == 4 && e == nil },
},
{
name: "test set size with error",
setName: "super",
setType: SetHashNet,
args: []SetOpt{SetWithSize(4)},
expect: func(s *Set, e error) bool { return strings.Contains(e.Error(), "incompatible") },
},
{
name: "test set range",
setName: "super",
setType: SetBitmapIP,
args: []SetOpt{SetWithRange("192.168.0.0/16")},
expect: func(s *Set, e error) bool { return s.Header.Range == "192.168.0.0/16" && e == nil },
},
{
name: "test set range with error",
setName: "super",
setType: SetHashNet,
args: []SetOpt{SetWithRange("192.168.0.0/16")},
expect: func(s *Set, e error) bool { return strings.Contains(e.Error(), "incompatible") },
},
{
name: "test hashsize",
setName: "super",
setType: SetHashNet,
args: []SetOpt{SetWithHashsize(4)},
expect: func(s *Set, e error) bool { return s.Header.Hashsize == 4 && e == nil },
},
{
name: "test hashsize with error",
setName: "super",
setType: SetBitmapIP,
args: []SetOpt{SetWithHashsize(4)},
expect: func(s *Set, e error) bool { return strings.Contains(e.Error(), "incompatible") },
},
{
name: "test maxelem",
setName: "super",
setType: SetHashNet,
args: []SetOpt{SetWithMaxelem(4)},
expect: func(s *Set, e error) bool { return s.Header.Maxelem == 4 && e == nil },
},
{
name: "test netmask with error",
setName: "super",
setType: SetHashNet,
args: []SetOpt{SetWithNetmask(30)},
expect: func(s *Set, e error) bool { return strings.Contains(e.Error(), "incompatible") },
},
{
name: "test netmask",
setName: "super",
setType: SetBitmapIP,
args: []SetOpt{SetWithNetmask(30)},
expect: func(s *Set, e error) bool { return s.Header.Netmask == 30 && e == nil },
},
{
name: "test forceadd with error",
setName: "super",
setType: SetBitmapIP,
args: []SetOpt{SetWithForceadd()},
expect: func(s *Set, e error) bool { return strings.Contains(e.Error(), "incompatible") },
},
{
name: "test forceadd",
setName: "super",
setType: SetHashNet,
args: []SetOpt{SetWithForceadd()},
expect: func(s *Set, e error) bool { return s.Header.Forceadd == NoVal && e == nil },
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
s, err := NewSet(tc.setName, tc.setType, tc.args...)
if !tc.expect(s, err) {
if s != nil {
t.Logf("Header: %+v", s.Header)
}
t.Fatalf("Unexpected NewSet() %+v, %s", s, err)
}
})
}
}
func TestAddMember(t *testing.T) {
cases := []struct {
name string
set Set
elem string
args []MemberOpt
expect func(*Set, error) bool
}{
{
name: "add member to set",
set: Set{Name: "super", Type: SetHashNet, Header: Header{}},
elem: "foo",
args: []MemberOpt{},
expect: func(s *Set, e error) bool {
return e == nil
},
},
{
name: "error when adding duplicate set",
set: Set{Name: "super", Type: SetHashNet, Header: Header{}, Members: []Member{Member{Elem: "foo"}}},
elem: "foo",
args: []MemberOpt{},
expect: func(s *Set, e error) bool {
return errors.Cause(e) == ErrorItemExist
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
m, _ := NewMember(tc.elem, nil, tc.args...)
err := tc.set.AddMember(m)
if !tc.expect(&tc.set, err) {
t.Fatalf("Unexpected NewMember() %+v, %s", tc.set, err)
}
})
}
}
func TestAddSet(t *testing.T) {
cases := []struct {
name string
sets Ipset
set Set
expect func(*Ipset, error) bool
}{
{
name: "add set",
sets: Ipset{},
set: Set{Name: "super", Type: SetHashNet, Header: Header{}},
expect: func(s *Ipset, e error) bool {
return e == nil
},
},
{
name: "error when adding duplicate set",
set: Set{Name: "super", Type: SetHashNet},
sets: Ipset{Sets: []*Set{&Set{Name: "super"}}},
expect: func(s *Ipset, e error) bool {
return errors.Cause(e) == ErrorItemExist
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
err := tc.sets.AddSet(&tc.set)
if !tc.expect(&tc.sets, err) {
t.Fatalf("Unexpected NewMember() %+v, %s", tc.sets, err)
}
})
}
}