Skip to content

Commit 3781ff3

Browse files
committed
add test for buildMaskForRange
1 parent 616bccc commit 3781ff3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

exec/network/tc/network_tc_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tc
22

33
import (
4+
"math/rand"
45
"testing"
56
)
67

@@ -47,3 +48,37 @@ func TestBuildTargetFilterPortAndIp(t *testing.T) {
4748
}
4849
}
4950
}
51+
52+
func TestBuildMaskForRange(t *testing.T) {
53+
start := rand.Int31n(65535)
54+
end := rand.Int31n(65535)
55+
if start > end {
56+
temp := start
57+
start = end
58+
end = temp
59+
}
60+
masks := buildMaskForRange(int(start), int(end))
61+
for i := 1; i <= 65535; i++ {
62+
if i < int(start) || i > int(end) {
63+
if isMatch(masks, uint16(i)) {
64+
t.Errorf("unexpected result: %d matched mask for [%d, %d]", i, start, end)
65+
}
66+
} else {
67+
if !isMatch(masks, uint16(i)) {
68+
t.Errorf("unexpected result: %d not matched mask for [%d, %d]", i, start, end)
69+
}
70+
}
71+
}
72+
}
73+
74+
func isMatch(masks [][]uint16, target uint16) bool {
75+
76+
for _, mask := range masks {
77+
v := mask[0]
78+
m := mask[1]
79+
if (target & m) == v {
80+
return true
81+
}
82+
}
83+
return false
84+
}

0 commit comments

Comments
 (0)