Skip to content

Commit 847df74

Browse files
updated fw port tests
1 parent 75c4f39 commit 847df74

1 file changed

Lines changed: 87 additions & 23 deletions

File tree

daemon/firewall/nftables/exprs/port_test.go

Lines changed: 87 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
)
1414

1515
type portTestsT struct {
16+
proto string
17+
direction string
1618
port string
1719
portVal int
1820
cmp expr.CmpOp
@@ -27,15 +29,20 @@ func TestExprPort(t *testing.T) {
2729
nftest.Fw.Conn = conn
2830

2931
portTests := []portTestsT{
30-
{"53", 53, expr.CmpOpEq, false},
31-
{"80", 80, expr.CmpOpEq, false},
32-
{"65535", 65535, expr.CmpOpEq, false},
33-
{"45,", 0, expr.CmpOpEq, true},
34-
{"", 0, expr.CmpOpEq, true},
32+
{"udp", "dport", "53", 53, expr.CmpOpEq, false},
33+
{"tcp", "sport", "80", 80, expr.CmpOpEq, false},
34+
{"tcp", "dport", "65535", 65535, expr.CmpOpEq, false},
35+
{"udp", "sport", "45,", 0, expr.CmpOpEq, true},
36+
{"udp", "dport", "", 0, expr.CmpOpEq, true},
37+
{"udp", "dport", " ", 0, expr.CmpOpEq, true},
38+
{"udp", "dport", ".", 0, expr.CmpOpEq, true},
39+
{"udp", "dport", "a", 0, expr.CmpOpEq, true},
3540
}
3641

3742
for _, test := range portTests {
3843
t.Run(fmt.Sprint("test-", test.port), func(t *testing.T) {
44+
exprProto, _ := exprs.NewExprProtocol(test.proto)
45+
exprPDir, _ := exprs.NewExprPortDirection(test.direction)
3946
portExpr, err := exprs.NewExprPort(test.port, &test.cmp)
4047
if err != nil {
4148
if !test.shouldFail {
@@ -44,20 +51,44 @@ func TestExprPort(t *testing.T) {
4451
return
4552
}
4653
//fmt.Printf("%s, %+v\n", test.port, *portExpr)
47-
r, _ := nftest.AddTestRule(t, conn, portExpr)
54+
exprList := []expr.Any{}
55+
// expr.Meta
56+
exprList = append(exprList, *exprProto...)
57+
// expr.Payload
58+
exprList = append(exprList, []expr.Any{exprPDir}...)
59+
// expr.Cmp
60+
exprList = append(exprList, *portExpr...)
61+
r, _ := nftest.AddTestRule(t, conn, &exprList)
4862
if r == nil {
49-
t.Errorf("Error adding rule with port (%s) expression", test.port)
63+
t.Errorf("Error adding rule with port (%s) expression: %v", test.port, r)
5064
return
5165
}
66+
// meta[0] + cmp[1]
5267
e := r.Exprs[0]
53-
cmp, ok := e.(*expr.Cmp)
68+
_, ok := e.(*expr.Meta)
5469
if !ok {
55-
t.Errorf("%s - invalid port expr: %T", test.port, e)
70+
t.Errorf("%s - invalid port expr meta: %T", test.port, e)
71+
return
72+
}
73+
74+
// payload[2]
75+
e1 := r.Exprs[2]
76+
_, ok1 := e1.(*expr.Payload)
77+
if !ok1 {
78+
t.Errorf("%s - invalid port expr payload: %T", test.port, e1)
79+
return
80+
}
81+
82+
// cmp[3] (port)
83+
e2 := r.Exprs[3]
84+
cmp, ok2 := e2.(*expr.Cmp)
85+
if !ok2 {
86+
t.Errorf("%s - invalid port expr cmp: %T", test.port, e2)
5687
return
5788
}
5889
//fmt.Printf("%s, %+v\n", reflect.TypeOf(e).String(), e)
59-
if reflect.TypeOf(e).String() != "*expr.Cmp" {
60-
t.Errorf("%s - first expression should be *expr.Cmp, instead of: %s", test.port, reflect.TypeOf(e))
90+
if reflect.TypeOf(e2).String() != "*expr.Cmp" {
91+
t.Errorf("%s - first expression should be *expr.Cmp, instead of: %s", test.port, reflect.TypeOf(e2))
6192
return
6293
}
6394
portVal := binaryutil.BigEndian.PutUint16(uint16(test.portVal))
@@ -78,37 +109,70 @@ func TestExprPortRange(t *testing.T) {
78109
nftest.Fw.Conn = conn
79110

80111
portTests := []portTestsT{
81-
{"53-5353", 53, expr.CmpOpEq, false},
82-
{"80-8080", 80, expr.CmpOpEq, false},
83-
{"1-65535", 65535, expr.CmpOpEq, false},
84-
{"1,45,", 0, expr.CmpOpEq, true},
85-
{"1-2.", 0, expr.CmpOpEq, true},
112+
{"udp", "dport", "53-5353", 53, expr.CmpOpEq, false},
113+
{"tcp", "sport", "80-8080", 80, expr.CmpOpEq, false},
114+
{"udp", "dport", "1-65535", 65535, expr.CmpOpEq, false},
115+
{"tcp", "sport", "1,45,", 0, expr.CmpOpEq, true},
116+
{"udp", "dport", "1-2.", 0, expr.CmpOpEq, true},
117+
{"udp", "dport", "-2", 0, expr.CmpOpEq, true},
118+
{"udp", "dport", "-", 0, expr.CmpOpEq, true},
119+
{"udp", "dport", " ", 0, expr.CmpOpEq, true},
86120
}
87121

88122
for _, test := range portTests {
89123
t.Run(fmt.Sprint("test-", test.port), func(t *testing.T) {
124+
exprProto, _ := exprs.NewExprProtocol(test.proto)
125+
exprPDir, _ := exprs.NewExprPortDirection(test.direction)
90126
portExpr, err := exprs.NewExprPortRange(test.port, &test.cmp)
91127
if err != nil {
92128
if !test.shouldFail {
93129
t.Errorf("Error creating expr port range: %v, %s", test, err)
94130
}
95131
return
96132
}
97-
//fmt.Printf("%s, %+v\n", test.port, *portExpr)
98-
r, _ := nftest.AddTestRule(t, conn, portExpr)
133+
fmt.Printf("%s, %+v\n", test.port, *portExpr)
134+
135+
exprList := []expr.Any{}
136+
// expr.Meta
137+
exprList = append(exprList, *exprProto...)
138+
// expr.Payload
139+
exprList = append(exprList, []expr.Any{exprPDir}...)
140+
// expr.Range
141+
exprList = append(exprList, *portExpr...)
142+
143+
r, _ := nftest.AddTestRule(t, conn, &exprList)
99144
if r == nil {
100145
t.Errorf("Error adding rule with port range (%s) expression", test.port)
101146
return
102147
}
148+
if len(r.Exprs) != 4 {
149+
t.Errorf("Error adding rule with port range (%s): expected 4 Exprs, found: %q", test.port, r.Exprs)
150+
return
151+
}
152+
// meta + cmp
103153
e := r.Exprs[0]
104-
_, ok := e.(*expr.Range)
154+
_, ok := e.(*expr.Meta)
105155
if !ok {
106-
t.Errorf("%s - invalid port range expr: %T", test.port, e)
156+
t.Errorf("%s - invalid port expr meta: %T, %q", test.port, e, r.Exprs)
157+
return
158+
}
159+
160+
// payload
161+
e1 := r.Exprs[2]
162+
_, ok1 := e1.(*expr.Payload)
163+
if !ok1 {
164+
t.Errorf("%s - invalid port expr payload: %T, exprs: %q", test.port, e1, r.Exprs)
165+
return
166+
}
167+
e2 := r.Exprs[3]
168+
_, ok2 := e2.(*expr.Range)
169+
if !ok2 {
170+
t.Errorf("%s - invalid port range expr: %T, exprs: %q", test.port, 2, r.Exprs)
107171
return
108172
}
109-
fmt.Printf("%s, %+v\n", reflect.TypeOf(e).String(), e)
110-
if reflect.TypeOf(e).String() != "*expr.Range" {
111-
t.Errorf("%s - first expression should be *expr.Cmp, instead of: %s", test.port, reflect.TypeOf(e))
173+
//fmt.Printf("%s, %+v\n", reflect.TypeOf(e2).String(), e2)
174+
if reflect.TypeOf(e2).String() != "*expr.Range" {
175+
t.Errorf("%s - first expression should be *expr.Cmp, instead of: %s", test.port, reflect.TypeOf(e2))
112176
}
113177
/*portVal := binaryutil.BigEndian.PutUint16(uint16(test.portVal))
114178
if !bytes.Equal(range.FromData, portVal) {

0 commit comments

Comments
 (0)