This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
iptree_test.go
80 lines (72 loc) · 1.6 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
package netaddr
import (
"errors"
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestValidateNilTree(t *testing.T) {
var tree *ipTree
assert.Equal(t, []error{}, tree.validate())
}
func TestValidateNoNetwork(t *testing.T) {
tree := &ipTree{}
assert.Equal(t, []error{
errors.New("each node in tree must have a network"),
}, tree.validate())
}
func TestValidateBadCidrBadUp(t *testing.T) {
_, ten24, _ := net.ParseCIDR("10.0.0.0/24")
ten24.IP = net.ParseIP("10.0.0.1")
tree := &ipTree{
net: ten24,
up: &ipTree{},
}
assert.Equal(t, []error{
errors.New("root up must be nil"),
errors.New("cidr invalid: 10.0.0.1/24"),
}, tree.validate())
}
func TestValidateBadLinkageLeft(t *testing.T) {
tree := &ipTree{
net: TenOne24,
left: &ipTree{
net: Ten24,
},
}
assert.Equal(t, []error{
errors.New("linkage error: left.up node must equal node"),
}, tree.validate())
}
func TestValidateBadLinkageRight(t *testing.T) {
tree := &ipTree{
net: Ten24,
right: &ipTree{
net: TenOne24,
},
}
assert.Equal(t, []error{
errors.New("linkage error: right.up node must equal node"),
}, tree.validate())
}
func TestValidateOutOfOrder(t *testing.T) {
tree := &ipTree{}
tree.left = &ipTree{
up: tree,
net: TenTwo24,
}
tree.net = TenOne24
tree.right = &ipTree{
up: tree,
net: Ten24,
}
tree.right.right = &ipTree{
up: tree.right,
net: Ten24,
}
assert.Equal(t, []error{
errors.New("nodes must be in order: 10.0.2.0 !< 10.0.1.0"),
errors.New("nodes must be in order: 10.0.1.0 !< 10.0.0.0"),
errors.New("nodes must be in order: 10.0.0.0 !< 10.0.0.0"),
}, tree.validate())
}