forked from client9/ipcat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipset_test.go
121 lines (103 loc) · 2.93 KB
/
ipset_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
package ipcat
import (
"testing"
)
func TestSetting(t *testing.T) {
const (
dcName = "foobar"
dcURL = "http://junk"
)
set := NewIntervalSet(100)
if err := set.AddRange("10.0.0.0", "10.0.1.0", dcName, dcURL); err != nil {
// Smoke test
t.Fatalf("Adding failed: %s", err)
}
if set.Len() != 1 {
// Smoke test
// major pass-by-val, not pass-by-ref problem
t.Fatalf("Added entry but size %d != 1", set.Len())
}
if err := set.AddRange("1.0.0.0", "1.255.255.255", dcName, dcURL); err != nil {
t.Errorf("Unable to add full class A network: %s", err)
}
if err := set.AddRange("10.1.0.0", "10.0.0.0", dcName, dcURL); err == nil {
t.Errorf("Allowed backwards interval")
}
if err := set.AddRange("2.0.0.0", "2.0.0.0", dcName, dcURL); err != nil {
t.Errorf("Unable to add single IP: %s", err)
}
if err := set.AddRange("Busted", "2.0.0.0", dcName, dcURL); err == nil {
t.Errorf("Allows busted input1")
}
if err := set.AddRange("2.0.0.0", "Busted", dcName, dcURL); err == nil {
t.Errorf("Allows busted input2")
}
}
var ipStringTests = []string{
"4.4.4.4",
"192.168.1.1",
"fe80:cd00::211e:729c",
}
func TestIPString(t *testing.T) {
for _, tt := range ipStringTests {
ip := IPParse(tt)
if got := IPString(ip); got != tt {
t.Errorf("IPString(%d) = %q, want %q", ip, got, tt)
}
}
}
var cidr2RangeTests = []struct {
cidr string
start string
end string
}{
{"10.0.0.0/8", "10.0.0.0", "10.255.255.255"},
{"192.168.0.0/24", "192.168.0.0", "192.168.0.255"},
}
func TestCIDR2Range(t *testing.T) {
for _, tt := range cidr2RangeTests {
start, end, err := CIDR2Range(tt.cidr)
if err != nil {
t.Fatal(err)
}
if start != tt.start {
t.Errorf("CIDR2Range(%q) start = %q, want %q", tt.cidr, start, tt.start)
}
if end != tt.end {
t.Errorf("CIDR2Range(%q) end = %q, want %q", tt.cidr, end, tt.start)
}
}
}
func TestMergeAdjacent(t *testing.T) {
ipset := NewIntervalSet(256)
ipset.AddCIDR("1.1.1.1/24", "Test Range", "url")
ipset.AddCIDR("1.1.2.1/24", "Test Range", "url")
ipset.AddCIDR("1.1.4.1/24", "Test Range", "url")
ipset.Contains("1.1.1.1") // calls sort()
if ipset.Len() != 2 {
t.Errorf("ipset contains %d items, want %d", ipset.Len(), 2)
}
}
func TestIntervalSet(t *testing.T) {
ipset := NewIntervalSet(256)
ipset.AddCIDR("1.1.1.1/24", "Test Range", "Test URL")
rec, err := ipset.Contains("1.1.1.2")
if err != nil {
t.Fatalf("ipset.Contains(%q) error: %s", "1.1.1.2", err)
}
if rec == nil {
t.Errorf("ipset.Contains(%q) record is nil", "1.1.1.2")
}
if _, err := ipset.Contains("0"); err == nil {
t.Errorf("ipset.Contains(%q) = %v, want error", "1.1.1.2", err)
}
ipset.AddCIDR("2.2.2.2/24", "Another Test Range", "Another Test URL")
ipset.DeleteByName("Test Range")
rec, err = ipset.Contains("1.1.1.2")
if err != nil {
t.Fatalf("ipset.Contains(%q) error: %s", "1.1.1.2", err)
}
if rec != nil {
t.Errorf("ipset.Contains(%q) record is not nil after DeleteByName: %v", "1.1.1.2", rec)
}
}