-
Notifications
You must be signed in to change notification settings - Fork 8
/
objects_test.go
902 lines (883 loc) · 24.5 KB
/
objects_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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
/*
Copyright 2023 The Kubernetes Authors.
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 knftables
import (
"fmt"
"strings"
"testing"
"time"
)
func getObjType(object Object) string {
fullType := fmt.Sprintf("%T", object)
parts := strings.Split(fullType, ".")
return parts[len(parts)-1]
}
func TestObjects(t *testing.T) {
tested := make(map[string]map[verb]struct{})
for _, tc := range []struct {
name string
verb verb
object Object
err string
out string
}{
// Tables
{
name: "add table",
verb: addVerb,
object: &Table{},
out: `add table ip mytable`,
},
{
name: "add table with comment",
verb: addVerb,
object: &Table{Comment: PtrTo("foo")},
out: `add table ip mytable { comment "foo" ; }`,
},
{
name: "create table",
verb: createVerb,
object: &Table{},
out: `create table ip mytable`,
},
{
name: "flush table",
verb: flushVerb,
object: &Table{},
out: `flush table ip mytable`,
},
{
name: "delete table",
verb: deleteVerb,
object: &Table{},
out: `delete table ip mytable`,
},
{
name: "delete table by handle",
verb: deleteVerb,
object: &Table{Handle: PtrTo(5)},
out: `delete table ip handle 5`,
},
{
name: "invalid insert table",
verb: insertVerb,
object: &Table{},
err: "not implemented",
},
{
name: "invalid replace table",
verb: replaceVerb,
object: &Table{},
err: "not implemented",
},
{
name: "invalid add table with Handle",
verb: addVerb,
object: &Table{Handle: PtrTo(5)},
err: "cannot specify Handle",
},
// Flowtables
{
name: "add flowtable",
verb: addVerb,
object: &Flowtable{
Name: "myflowtable",
},
out: `add flowtable ip mytable myflowtable { }`,
},
{
name: "create flowtable",
verb: createVerb,
object: &Flowtable{
Name: "myflowtable",
},
out: `create flowtable ip mytable myflowtable { }`,
},
{
name: "create flowtable with priority math",
verb: createVerb,
object: &Flowtable{
Name: "myflowtable",
Priority: PtrTo(FilterIngressPriority + "+5"),
},
out: `create flowtable ip mytable myflowtable { hook ingress priority filter+5 ; }`,
},
{
name: "create flowtable with devices",
verb: createVerb,
object: &Flowtable{
Name: "myflowtable",
Devices: []string{"eth0", "eth1"},
},
out: `create flowtable ip mytable myflowtable { devices = { eth0, eth1 } ; }`,
},
{
name: "create flowtable with devices and default priority",
verb: createVerb,
object: &Flowtable{
Name: "myflowtable",
Priority: PtrTo(FilterIngressPriority),
Devices: []string{"eth0", "eth1"},
},
out: `create flowtable ip mytable myflowtable { hook ingress priority filter ; devices = { eth0, eth1 } ; }`,
},
{
name: "flush flowtable",
verb: flushVerb,
object: &Flowtable{
Name: "myflowtable",
},
err: "not implemented",
},
{
name: "delete flowtable",
verb: deleteVerb,
object: &Flowtable{
Name: "myflowtable",
},
out: `delete flowtable ip mytable myflowtable`,
},
{
name: "delete flowtable by handle",
verb: deleteVerb,
object: &Flowtable{
Name: "myflowtable",
Handle: PtrTo(5),
},
out: `delete flowtable ip mytable handle 5`,
},
{
name: "invalid insert flowtable",
verb: insertVerb,
object: &Flowtable{
Name: "myflowtable",
}, err: "not implemented",
},
{
name: "invalid replace flowtable",
verb: replaceVerb,
object: &Flowtable{
Name: "myflowtable",
},
err: "not implemented",
},
{
name: "invalid add flowtable with Handle",
verb: addVerb,
object: &Flowtable{
Name: "myflowtable",
Handle: PtrTo(5),
},
err: "cannot specify Handle",
},
// Chains
{
name: "add chain",
verb: addVerb,
object: &Chain{Name: "mychain"},
out: `add chain ip mytable mychain`,
},
{
name: "add chain with comment",
verb: addVerb,
object: &Chain{Name: "mychain", Comment: PtrTo("foo")},
out: `add chain ip mytable mychain { comment "foo" ; }`,
},
{
name: "add base chain",
verb: addVerb,
object: &Chain{Name: "mychain", Type: PtrTo(NATType), Hook: PtrTo(PostroutingHook), Priority: PtrTo(SNATPriority)},
out: `add chain ip mytable mychain { type nat hook postrouting priority 100 ; }`,
},
{
name: "add base chain with priority math",
verb: addVerb,
object: &Chain{Name: "mychain", Type: PtrTo(NATType), Hook: PtrTo(PostroutingHook), Priority: PtrTo(SNATPriority + "+5")},
out: `add chain ip mytable mychain { type nat hook postrouting priority 105 ; }`,
},
{
name: "add base chain with unrecognized priority",
verb: addVerb,
object: &Chain{Name: "mychain", Type: PtrTo(NATType), Hook: PtrTo(PostroutingHook), Priority: PtrTo(BaseChainPriority("futurevalue"))},
out: `add chain ip mytable mychain { type nat hook postrouting priority futurevalue ; }`,
},
{
name: "add base chain with comment",
verb: addVerb,
object: &Chain{Name: "mychain", Type: PtrTo(NATType), Hook: PtrTo(PostroutingHook), Priority: PtrTo(SNATPriority), Comment: PtrTo("foo")},
out: `add chain ip mytable mychain { type nat hook postrouting priority 100 ; comment "foo" ; }`,
},
{
name: "add base chain with device",
verb: addVerb,
object: &Chain{Name: "mychain", Type: PtrTo(NATType), Hook: PtrTo(IngressHook), Device: PtrTo("eth0"), Priority: PtrTo(SNATPriority)},
out: `add chain ip mytable mychain { type nat hook ingress device "eth0" priority 100 ; }`,
},
{
name: "create chain",
verb: createVerb,
object: &Chain{Name: "mychain"},
out: `create chain ip mytable mychain`,
},
{
name: "flush chain",
verb: flushVerb,
object: &Chain{Name: "mychain"},
out: `flush chain ip mytable mychain`,
},
{
name: "delete chain",
verb: deleteVerb,
object: &Chain{Name: "mychain"},
out: `delete chain ip mytable mychain`,
},
{
name: "delete chain by handle",
verb: deleteVerb,
object: &Chain{Name: "mychain", Handle: PtrTo(5)},
out: `delete chain ip mytable handle 5`,
},
{
name: "delete chain by handle (without name)",
verb: deleteVerb,
object: &Chain{Handle: PtrTo(5)},
out: `delete chain ip mytable handle 5`,
},
{
name: "invalid insert chain",
verb: insertVerb,
object: &Chain{Name: "mychain"},
err: "not implemented",
},
{
name: "invalid replace chain",
verb: replaceVerb,
object: &Chain{Name: "mychain"},
err: "not implemented",
},
{
name: "invalid add chain without name",
verb: addVerb,
object: &Chain{},
err: "no name",
},
{
name: "invalid add chain with handle",
verb: addVerb,
object: &Chain{Name: "mychain", Handle: PtrTo(5)},
err: "cannot specify Handle",
},
{
name: "invalid add base chain with no Type",
verb: addVerb,
object: &Chain{Name: "mychain", Hook: PtrTo(PostroutingHook), Priority: PtrTo(SNATPriority)},
err: "must specify Type and Priority",
},
{
name: "invalid add base chain with no Priority",
verb: addVerb,
object: &Chain{Name: "mychain", Type: PtrTo(NATType), Hook: PtrTo(PostroutingHook)},
err: "must specify Type and Priority",
},
{
name: "invalid add base chain with no Type or Priority",
verb: addVerb,
object: &Chain{Name: "mychain", Hook: PtrTo(PostroutingHook)},
err: "must specify Type and Priority",
},
{
name: "invalid add non-base chain with Type and Priority",
verb: addVerb,
object: &Chain{Name: "mychain", Type: PtrTo(NATType), Priority: PtrTo(SNATPriority)},
err: "must not specify Type or Priority",
},
{
name: "invalid add non-base chain with Type",
verb: addVerb,
object: &Chain{Name: "mychain", Type: PtrTo(NATType)},
err: "must not specify Type or Priority",
},
{
name: "invalid add non-base chain with Priority",
verb: addVerb,
object: &Chain{Name: "mychain", Priority: PtrTo(SNATPriority)},
err: "must not specify Type or Priority",
},
{
name: "invalid add non-base chain with device",
verb: addVerb,
object: &Chain{Name: "mychain", Device: PtrTo("eth0")},
err: "must not specify Device",
},
// Rules
{
name: "add rule",
verb: addVerb,
object: &Rule{Chain: "mychain", Rule: "drop"},
out: `add rule ip mytable mychain drop`,
},
{
name: "add rule with comment",
verb: addVerb,
object: &Rule{Chain: "mychain", Rule: "drop", Comment: PtrTo("comment")},
out: `add rule ip mytable mychain drop comment "comment"`,
},
{
name: "add rule relative to index",
verb: addVerb,
object: &Rule{Chain: "mychain", Rule: "drop", Index: PtrTo(2)},
out: `add rule ip mytable mychain index 2 drop`,
},
{
name: "add rule relative to handle",
verb: addVerb,
object: &Rule{Chain: "mychain", Rule: "drop", Handle: PtrTo(2)},
out: `add rule ip mytable mychain handle 2 drop`,
},
{
name: "insert rule",
verb: insertVerb,
object: &Rule{Chain: "mychain", Rule: "drop"},
out: `insert rule ip mytable mychain drop`,
},
{
name: "insert rule with comment relative to handle",
verb: insertVerb,
object: &Rule{Chain: "mychain", Rule: "drop", Comment: PtrTo("comment"), Handle: PtrTo(2)},
out: `insert rule ip mytable mychain handle 2 drop comment "comment"`,
},
{
name: "replace rule",
verb: replaceVerb,
object: &Rule{Chain: "mychain", Rule: "drop", Handle: PtrTo(2)},
out: `replace rule ip mytable mychain handle 2 drop`,
},
{
name: "delete rule",
verb: deleteVerb,
object: &Rule{Chain: "mychain", Rule: "drop", Handle: PtrTo(2)},
out: `delete rule ip mytable mychain handle 2`,
},
{
name: "delete rule without Rule",
verb: deleteVerb,
object: &Rule{Chain: "mychain", Handle: PtrTo(2)},
out: `delete rule ip mytable mychain handle 2`,
},
{
name: "invalid create rule",
verb: createVerb,
object: &Rule{Chain: "mychain", Rule: "drop"},
err: "not implemented",
},
{
name: "invalid flush rule",
verb: flushVerb,
object: &Rule{Chain: "mychain", Rule: "drop"},
err: "not implemented",
},
{
name: "invalid add rule with no Chain",
verb: addVerb,
object: &Rule{Rule: "drop"},
err: "no chain name",
},
{
name: "invalid add rule with no Rule",
verb: addVerb,
object: &Rule{Chain: "mychain"},
err: "no rule",
},
{
name: "invalid add rule with both Index and Handle",
verb: addVerb,
object: &Rule{Chain: "mychain", Rule: "drop", Index: PtrTo(2), Handle: PtrTo(5)},
err: "both Index and Handle",
},
{
name: "invalid replace rule with no Handle",
verb: replaceVerb,
object: &Rule{Chain: "mychain", Rule: "drop"},
err: "must specify Handle",
},
{
name: "invalid delete rule with no Handle",
verb: deleteVerb,
object: &Rule{Chain: "mychain", Rule: "drop"},
err: "must specify Handle",
},
// Sets
{
name: "add set",
verb: addVerb,
object: &Set{Name: "myset", Type: "ipv4_addr"},
out: `add set ip mytable myset { type ipv4_addr ; }`,
},
{
name: "add set with TypeOf",
verb: addVerb,
object: &Set{Name: "myset", TypeOf: "ip saddr"},
out: `add set ip mytable myset { typeof ip saddr ; }`,
},
{
name: "add set with all properties",
verb: addVerb,
object: &Set{
Name: "myset",
Type: "ipv4_addr",
Flags: []SetFlag{DynamicFlag, IntervalFlag},
Timeout: PtrTo(3 * time.Minute),
GCInterval: PtrTo(time.Hour),
Size: PtrTo[uint64](1000),
Policy: PtrTo(PerformancePolicy),
AutoMerge: PtrTo(true),
Comment: PtrTo("that's a lot of options"),
},
out: `add set ip mytable myset { type ipv4_addr ; flags dynamic,interval ; timeout 180s ; gc-interval 3600s ; size 1000 ; policy performance ; auto-merge ; comment "that's a lot of options" ; }`,
},
{
name: "create set",
verb: createVerb,
object: &Set{Name: "myset", Type: "ipv4_addr"},
out: `create set ip mytable myset { type ipv4_addr ; }`,
},
{
name: "flush set",
verb: flushVerb,
object: &Set{Name: "myset"},
out: `flush set ip mytable myset`,
},
{
name: "flush set with extraneous Type",
verb: flushVerb,
object: &Set{Name: "myset", Type: "ipv4_addr"},
out: `flush set ip mytable myset`,
},
{
name: "delete set",
verb: deleteVerb,
object: &Set{Name: "myset"},
out: `delete set ip mytable myset`,
},
{
name: "delete set by handle",
verb: deleteVerb,
object: &Set{Name: "myset", Handle: PtrTo(5)},
out: `delete set ip mytable handle 5`,
},
{
name: "delete set by handle without Name",
verb: deleteVerb,
object: &Set{Handle: PtrTo(5)},
out: `delete set ip mytable handle 5`,
},
{
name: "invalid insert set",
verb: insertVerb,
object: &Set{Name: "myset", Type: "ipv4_addr"},
err: "not implemented",
},
{
name: "invalid replace set",
verb: replaceVerb,
object: &Set{Name: "myset", Type: "ipv4_addr"},
err: "not implemented",
},
{
name: "invalid add set without Name",
verb: addVerb,
object: &Set{Type: "ipv4_addr"},
err: "no name",
},
{
name: "invalid add set without Type or TypeOf",
verb: addVerb,
object: &Set{Name: "myset"},
err: "must specify either Type or TypeOf",
},
{
name: "invalid add set with both Type and TypeOf",
verb: addVerb,
object: &Set{Name: "myset", Type: "ipv4_addr", TypeOf: "ip addr"},
err: "must specify either Type or TypeOf",
},
{
name: "invalid add set with Handle",
verb: addVerb,
object: &Set{Name: "myset", Type: "ipv4_addr", Handle: PtrTo(5)},
err: "cannot specify Handle",
},
// Maps
{
name: "add map",
verb: addVerb,
object: &Map{Name: "mymap", Type: "ipv4_addr : ipv4_addr"},
out: `add map ip mytable mymap { type ipv4_addr : ipv4_addr ; }`,
},
{
name: "add map with TypeOf",
verb: addVerb,
object: &Map{Name: "mymap", TypeOf: "ip saddr : ip saddr"},
out: `add map ip mytable mymap { typeof ip saddr : ip saddr ; }`,
},
{
name: "add map with all properties",
verb: addVerb,
object: &Map{
Name: "mymap",
Type: "ipv4_addr : ipv4_addr",
Flags: []SetFlag{DynamicFlag, IntervalFlag},
Timeout: PtrTo(3 * time.Minute),
GCInterval: PtrTo(time.Hour),
Size: PtrTo[uint64](1000),
Policy: PtrTo(PerformancePolicy),
Comment: PtrTo("that's a lot of options"),
},
out: `add map ip mytable mymap { type ipv4_addr : ipv4_addr ; flags dynamic,interval ; timeout 180s ; gc-interval 3600s ; size 1000 ; policy performance ; comment "that's a lot of options" ; }`,
},
{
name: "create map",
verb: createVerb,
object: &Map{Name: "mymap", Type: "ipv4_addr : ipv4_addr"},
out: `create map ip mytable mymap { type ipv4_addr : ipv4_addr ; }`,
},
{
name: "flush map",
verb: flushVerb,
object: &Map{Name: "mymap"},
out: `flush map ip mytable mymap`,
},
{
name: "flush map with extraneous Type",
verb: flushVerb,
object: &Map{Name: "mymap", Type: "ipv4_addr : ipv4_addr"},
out: `flush map ip mytable mymap`,
},
{
name: "delete map",
verb: deleteVerb,
object: &Map{Name: "mymap"},
out: `delete map ip mytable mymap`,
},
{
name: "delete map by Handle",
verb: deleteVerb,
object: &Map{Name: "mymap", Handle: PtrTo(5)},
out: `delete map ip mytable handle 5`,
},
{
name: "delete map by Handle without Name",
verb: deleteVerb,
object: &Map{Handle: PtrTo(5)},
out: `delete map ip mytable handle 5`,
},
{
name: "invalid insert map",
verb: insertVerb,
object: &Map{Name: "mymap", Type: "ipv4_addr : ipv4_addr"},
err: "not implemented",
},
{
name: "invalid replace map",
verb: replaceVerb,
object: &Map{Name: "mymap", Type: "ipv4_addr : ipv4_addr"},
err: "not implemented",
},
{
name: "invalid add map without Name",
verb: addVerb,
object: &Map{Type: "ipv4_addr : ipv4_addr"},
err: "no name",
},
{
name: "invalid add map without Type of TypeOf",
verb: addVerb,
object: &Map{Name: "mymap"},
err: "must specify either Type or TypeOf",
},
{
name: "invalid add map with both Type and TypeOf",
verb: addVerb,
object: &Map{Name: "mymap", Type: "ipv4_addr : ipv4_addr", TypeOf: "ip addr : ip addr"},
err: "must specify either Type or TypeOf",
},
{
name: "invalid add map with Handle",
verb: addVerb,
object: &Map{Name: "mymap", Type: "ipv4_addr : ipv4_addr", Handle: PtrTo(5)},
err: "cannot specify Handle",
},
// Elements
{
name: "add (set) element",
verb: addVerb,
object: &Element{Set: "myset", Key: []string{"10.0.0.1"}},
out: `add element ip mytable myset { 10.0.0.1 }`,
},
{
name: "add (map) element",
verb: addVerb,
object: &Element{Map: "mymap", Key: []string{"10.0.0.1"}, Value: []string{"192.168.1.1"}},
out: `add element ip mytable mymap { 10.0.0.1 : 192.168.1.1 }`,
},
{
name: "create (set) element with comment",
verb: createVerb,
object: &Element{Set: "myset", Key: []string{"10.0.0.1"}, Comment: PtrTo("comment")},
out: `create element ip mytable myset { 10.0.0.1 comment "comment" }`,
},
{
name: "create (map) element with comment",
verb: addVerb,
object: &Element{Map: "mymap", Key: []string{"10.0.0.1"}, Value: []string{"192.168.1.1"}, Comment: PtrTo("comment")},
out: `add element ip mytable mymap { 10.0.0.1 comment "comment" : 192.168.1.1 }`,
},
{
name: "delete (set) element",
verb: deleteVerb,
object: &Element{Set: "myset", Key: []string{"10.0.0.1"}},
out: `delete element ip mytable myset { 10.0.0.1 }`,
},
{
name: "delete (map) element with unnecessary Value",
verb: deleteVerb,
object: &Element{Map: "mymap", Key: []string{"10.0.0.1"}, Value: []string{"192.168.1.1"}},
out: `delete element ip mytable mymap { 10.0.0.1 }`,
},
{
name: "delete (map) element",
verb: deleteVerb,
object: &Element{Map: "mymap", Key: []string{"10.0.0.1"}},
out: `delete element ip mytable mymap { 10.0.0.1 }`,
},
{
name: "invalid add element with no Set",
verb: addVerb,
object: &Element{Key: []string{"10.0.0.1"}},
err: "no set/map name",
},
{
name: "invalid add element with no Map",
verb: addVerb,
object: &Element{Key: []string{"10.0.0.1"}, Value: []string{"80"}},
err: "no set/map name",
},
{
name: "invalid add element with both Set and Map",
verb: addVerb,
object: &Element{Set: "myset", Map: "mymap", Key: []string{"10.0.0.1"}},
err: "both",
},
{
name: "invalid add element with no Key",
verb: addVerb,
object: &Element{Set: "myset"},
err: "no key",
},
{
name: "invalid add element with Value but no Key",
verb: addVerb,
object: &Element{Map: "mymap", Value: []string{"192.168.1.1"}},
err: "no key",
},
{
name: "invalid flush element",
verb: flushVerb,
object: &Element{Set: "myset", Key: []string{"10.0.0.1"}},
err: "not implemented",
},
{
name: "invalid insert element",
verb: insertVerb,
object: &Element{Set: "myset", Key: []string{"10.0.0.1"}},
err: "not implemented",
},
{
name: "invalid replace element",
verb: replaceVerb,
object: &Element{Set: "myset", Key: []string{"10.0.0.1"}},
err: "not implemented",
},
} {
t.Run(tc.name, func(t *testing.T) {
err := tc.object.validate(tc.verb)
if err == nil {
if tc.err != "" {
t.Errorf("expected error with %q but got none", tc.err)
}
} else if tc.err == "" {
t.Errorf("expected no error but got %q", err)
} else if !strings.Contains(err.Error(), tc.err) {
t.Errorf("expected error with %q but got %q", tc.err, err)
}
objType := getObjType(tc.object)
if tested[objType] == nil {
tested[objType] = make(map[verb]struct{})
}
tested[objType][tc.verb] = struct{}{}
if err == nil && tc.err == "" {
b := &strings.Builder{}
ctx := &nftContext{family: IPv4Family, table: "mytable"}
tc.object.writeOperation(tc.verb, ctx, b)
out := strings.TrimSuffix(b.String(), "\n")
if out != tc.out {
t.Errorf("expected %q but got %q", tc.out, out)
}
}
})
}
// add, create, flush, insert, replace, delete
numVerbs := 6
for objType, verbs := range tested {
if len(verbs) != numVerbs {
t.Errorf("expected to test %d verbs for %s, got %d (%v)", numVerbs, objType, len(verbs), verbs)
}
}
}
func TestNoObjectComments(t *testing.T) {
for _, tc := range []struct {
name string
object Object
out string
}{
{
name: "add table with comment",
object: &Table{Comment: PtrTo("foo")},
out: `add table ip mytable`,
},
{
name: "add chain with comment",
object: &Chain{Name: "mychain", Comment: PtrTo("foo")},
out: `add chain ip mytable mychain`,
},
{
name: "add base chain with comment",
object: &Chain{Name: "mychain", Type: PtrTo(NATType), Hook: PtrTo(PostroutingHook), Priority: PtrTo(SNATPriority), Comment: PtrTo("foo")},
out: `add chain ip mytable mychain { type nat hook postrouting priority 100 ; }`,
},
{
name: "add rule with comment",
object: &Rule{Chain: "mychain", Rule: "drop", Comment: PtrTo("comment")},
out: `add rule ip mytable mychain drop comment "comment"`,
},
{
name: "add set with comment",
object: &Set{Name: "myset", Type: "ipv4_addr", Comment: PtrTo("comment")},
out: `add set ip mytable myset { type ipv4_addr ; }`,
},
{
name: "add map with comment",
object: &Map{Name: "mymap", Type: "ipv4_addr : ipv4_addr", Comment: PtrTo("comment")},
out: `add map ip mytable mymap { type ipv4_addr : ipv4_addr ; }`,
},
{
name: "add (set) element with comment",
object: &Element{Set: "myset", Key: []string{"10.0.0.1"}, Comment: PtrTo("comment")},
out: `add element ip mytable myset { 10.0.0.1 comment "comment" }`,
},
{
name: "add (map) element with comment",
object: &Element{Map: "mymap", Key: []string{"10.0.0.1"}, Value: []string{"192.168.1.1"}, Comment: PtrTo("comment")},
out: `add element ip mytable mymap { 10.0.0.1 comment "comment" : 192.168.1.1 }`,
},
} {
t.Run(tc.name, func(t *testing.T) {
b := &strings.Builder{}
ctx := &nftContext{family: IPv4Family, table: "mytable", noObjectComments: true}
tc.object.writeOperation(addVerb, ctx, b)
out := strings.TrimSuffix(b.String(), "\n")
if out != tc.out {
t.Errorf("expected %q but got %q", tc.out, out)
}
})
}
}
func TestParsePriority(t *testing.T) {
for _, tc := range []struct {
name string
family Family
priority string
err bool
out int
}{
{
name: "basic",
family: IPv4Family,
priority: "dstnat",
out: -100,
},
{
name: "bridge family",
family: BridgeFamily,
priority: "dstnat",
out: -300,
},
{
name: "numeric",
family: IPv4Family,
priority: "35",
out: 35,
},
{
name: "numeric, negative",
family: IPv4Family,
priority: "-35",
out: -35,
},
{
name: "addition",
family: IPv4Family,
priority: "srcnat+1",
out: 101,
},
{
name: "subtraction",
family: IPv4Family,
priority: "srcnat-1",
out: 99,
},
{
name: "unknown",
family: IPv4Family,
priority: "blah",
err: true,
},
{
name: "unknown with math",
family: IPv4Family,
priority: "blah+1",
err: true,
},
{
name: "bad math",
family: IPv4Family,
priority: "dstnat+one",
err: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
val, err := ParsePriority(tc.family, tc.priority)
if tc.err {
if err == nil {
t.Errorf("expected error, got %d", val)
}
} else {
if err != nil {
t.Errorf("expected %d, got error %v", tc.out, err)
} else if val != tc.out {
t.Errorf("expected %d, got %d", tc.out, val)
}
}
})
}
}