-
Notifications
You must be signed in to change notification settings - Fork 2
/
map_test.go
189 lines (172 loc) · 4.45 KB
/
map_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
package ipv4
import (
"testing"
)
func TestSettingRange(t *testing.T) {
set := NewIntervalMap(100)
// Smoke test to sort out major pass-by-val vs. pass-by-ref problems
err := set.AddRange("20.0.0.0", "20.0.0.255", true)
if err != nil || set.Len() != 1 {
t.Fatalf("Adding failed: err=%v, len=%d", err, set.Len())
}
table := []struct {
left string
right string
ok bool
}{
{"10.0.0.0", "11.0.0.0", false}, // class A
{"1.0.0.0", "1.255.255.255", true},
{"12.1.0.0", "12.0.0.0", false},
{"2.0.0.0", "2.0.0.0", true},
{"Busted", "2.0.0.0", false},
{"2.0.0.0", "Busted", false},
}
for pos, test := range table {
err := set.AddRange(test.left, test.right, true)
if err == nil && test.ok == false {
t.Errorf("test %d: Expected an error with input [%s, %s]", pos, test.left, test.right)
} else if err != nil && test.ok == true {
t.Errorf("test %d: Got an error with input [%s, %s]: %s", pos, test.left, test.right, err)
}
err = set.Valid()
if err != nil {
t.Errorf("Set state is invalid: %s", err)
}
}
src := set.Go()
if len(src) == 0 {
t.Errorf("Output was empty")
}
}
func TestSettingCIDR(t *testing.T) {
set := NewIntervalMap(100)
addtable := []struct {
cidr string
ok bool
}{
{"Busted", false},
{"2.0.0.0/", false},
{"2.0.0.0/Busted", false},
{"192.128.1.0/7", false},
{"1.0.0.0/8", true},
{"10.0.0.0/32", true},
{"10.0.0.1/32", true},
{"10.0.0.3", true},
}
for pos, test := range addtable {
err := set.Add(test.cidr, pos)
if err == nil && test.ok == false {
t.Errorf("test %d: Expected an error with input %q", pos, test.cidr)
} else if err != nil && test.ok == true {
t.Errorf("test %d: Got an error with input %q: %s", pos, test.cidr, err)
}
err = set.Valid()
if err != nil {
t.Errorf("Set state is invalid: %s", err)
}
}
if set.Len() != 4 {
t.Errorf("expected len of 4, got %d", set.Len())
}
err := set.Add("10.0.0.4", 7)
if err != nil {
t.Fatalf("set.Add(%q, %d) err: %v", "10.0.0.4", 7, err)
}
if set.Len() != 4 {
t.Errorf("expected len of 4, got %d", set.Len())
}
//fmt.Printf("---> INTERNAL TREE:\n %s\n", set)
table := []struct {
dots string
result interface{}
}{
{"0.0.0.0", nil},
{"0.0.0.255", nil},
{"1.0.0.0", 4},
{"1.255.255.255", 4},
{"2.0.0.0", nil},
{"9.255.255.255", nil},
{"10.0.0.0", 5},
{"10.0.0.1", 6},
{"10.0.0.2", nil},
{"10.0.0.3", 7},
{"10.0.0.4", 7},
{"255.255.255.255", nil},
}
for pos, test := range table {
val := set.Contains(test.dots)
if val != test.result {
t.Errorf("test %d: Contains(%q) is %v, expected %v", pos, test.dots, val, test.result)
}
}
}
/*
func BenchmarkLookup(b *testing.B) {
for n := 0; n < b.N; n++ {
Find("0.255.255.255")
}
}
*/
// This test various way intervals can overlap with each other
// the code should consolidate them appropriately
func TestOverlaps(t *testing.T) {
set := NewIntervalMap(100)
table := []struct {
left string
right string
count int
}{
{"10.0.0.0", "10.0.1.127", 1}, // class A
{"10.0.0.0", "10.0.1.127", 1}, // dup
{"12.0.0.0", "12.1.0.0", 2}, // disjoint
{"10.0.0.0", "10.0.1.132", 2}, // extension
{"10.0.0.10", "10.0.1.131", 2}, // subset
{"10.0.0.10", "10.0.1.132", 2}, // subset
{"10.0.0.10", "10.0.1.135", 2}, // overlap
{"10.0.0.135", "10.0.1.140", 2}, // continuation
}
for pos, test := range table {
err := set.AddRange(test.left, test.right, pos)
if err != nil {
t.Fatalf("test %d: Error with input [%s, %s]: %s", pos, test.left, test.right, err)
}
if set.Len() != test.count {
t.Errorf("Got bad count: expected %d got %d", test.count, set.Len())
}
}
}
// This test various way intervals can overlap with each other
// the code should consolidate them appropriately
func TestOverlaps2(t *testing.T) {
set := NewIntervalMap(100)
table := []string{
"64.39.96.0/20",
"64.39.106.0/20",
"64.39.106.208",
}
for pos, test := range table {
err := set.Add(test, pos)
if err != nil {
t.Fatalf("test %d: Error with input %s: %s", pos, test, err)
}
err = set.Valid()
if err != nil {
t.Errorf("Set state is invalid: %s", err)
}
}
}
func TestEmpty(t *testing.T) {
set := NewIntervalMap(100)
err := set.Valid()
if err != nil {
t.Errorf("Set state is invalid: %s", err)
}
result := set.Contains("127.0.0.1")
if result != nil {
t.Errorf("Empty set contained something!!")
}
result = set.Contains("junk")
if result != nil {
t.Errorf("Empty set contained something and parsed invalid input!!")
}
}