forked from projectcalico/calico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptree_test.go
119 lines (106 loc) · 3.24 KB
/
iptree_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
// Copyright (c) 2018 Tigera, Inc. All rights reserved.
package iptree
import (
"testing"
. "github.com/onsi/gomega"
)
func TestMainline(t *testing.T) {
for _, test := range []struct {
Name string
CIDRsToAdd []string
CIDRsToExpect []string
}{
{"empty", nil, nil},
{"zero", []string{"0.0.0.0/0"}, []string{"0.0.0.0/0"}},
{"128.0.0.0/1", []string{"128.0.0.0/1"}, []string{"128.0.0.0/1"}},
{"1.0.0.0/8", []string{"1.0.0.0/8"}, []string{"1.0.0.0/8"}},
{"0.0.0.0/1 and 128.0.0.0/1",
[]string{"128.0.0.0/1", "0.0.0.0/1"},
[]string{"0.0.0.0/0"}},
{"Covering a 24",
[]string{"10.0.0.1", "10.0.0.2", "10.0.0.128/25", "10.0.0.64/26", "10.0.0.32/27",
"10.0.0.16/28", "10.0.0.8/29", "10.0.0.4/30", "10.0.0.3", "10.0.0.0"},
[]string{"10.0.0.0/24"}},
} {
test := test
t.Run(test.Name, func(t *testing.T) {
RegisterTestingT(t)
tree := New(4)
for _, c := range test.CIDRsToAdd {
tree.AddCIDRString(c)
}
var cidrsAsIface []interface{}
for _, c := range test.CIDRsToExpect {
cidrsAsIface = append(cidrsAsIface, c)
}
Expect(tree.CoveringCIDRStrings()).To(ConsistOf(cidrsAsIface...))
})
}
}
func TestIntesection(t *testing.T) {
for _, test := range []struct {
Name string
A, B []string
CIDRsToExpect []string
}{
{"empty", nil, nil, nil},
{"zero", []string{"0.0.0.0/0"}, []string{"0.0.0.0/0"}, []string{"0.0.0.0/0"}},
{"zero-0&128-1", []string{"0.0.0.0/0"}, []string{"128.0.0.0/1"}, []string{"128.0.0.0/1"}},
{"zero-1&128-1", []string{"0.0.0.0/1"}, []string{"128.0.0.0/1"}, []string{}},
{"128-1&zero-0", []string{"128.0.0.0/1"}, []string{"0.0.0.0/0"}, []string{"128.0.0.0/1"}},
} {
test := test
t.Run(test.Name, func(t *testing.T) {
RegisterTestingT(t)
treeA := New(4)
for _, c := range test.A {
treeA.AddCIDRString(c)
}
treeB := New(4)
for _, c := range test.B {
treeB.AddCIDRString(c)
}
intersection := Intersect(treeA, treeB)
var cidrsAsIface []interface{}
for _, c := range test.CIDRsToExpect {
cidrsAsIface = append(cidrsAsIface, c)
}
Expect(intersection.CoveringCIDRStrings()).To(ConsistOf(cidrsAsIface...))
})
}
}
func TestSubtraction(t *testing.T) {
for _, test := range []struct {
Name string
A, B []string
CIDRsToExpect []string
}{
{"empty", nil, nil, nil},
{"zero", []string{"0.0.0.0/0"}, []string{"0.0.0.0/0"}, []string{}},
{"zero-0 - 128-1", []string{"0.0.0.0/0"}, []string{"128.0.0.0/1"}, []string{"0.0.0.0/1"}},
{"zero-1 - 128-1", []string{"0.0.0.0/1"}, []string{"128.0.0.0/1"}, []string{"0.0.0.0/1"}},
{"128-1 - zero-0", []string{"128.0.0.0/1"}, []string{"0.0.0.0/0"}, []string{}},
{"coalesce child nodes",
[]string{"10.0.0.0/25", "10.0.1.0/25"},
[]string{"10.0.0.0/23"}, []string{}},
} {
test := test
t.Run(test.Name, func(t *testing.T) {
RegisterTestingT(t)
treeA := New(4)
for _, c := range test.A {
treeA.AddCIDRString(c)
}
treeB := New(4)
for _, c := range test.B {
treeB.AddCIDRString(c)
}
intersection := Subtract(treeA, treeB)
var cidrsAsIface []interface{}
for _, c := range test.CIDRsToExpect {
cidrsAsIface = append(cidrsAsIface, c)
}
Expect(intersection.CoveringCIDRStrings()).To(ConsistOf(cidrsAsIface...))
})
}
}