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
/
iprange_test.go
90 lines (78 loc) · 5.36 KB
/
iprange_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
package netaddr
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func TestIPRangeFromIPNet(t *testing.T) {
net, err := ParseNet("10.0.0.0/8")
assert.Nil(t, err)
ipRange := IPRangeFromIPNet(net)
assert.Equal(t, "10.0.0.0", ipRange.First.String())
assert.Equal(t, "10.255.255.255", ipRange.Last.String())
assert.Equal(t, "[10.0.0.0,10.255.255.255]", ipRange.String())
}
func TestIPRangeFromIPNetIPv6(t *testing.T) {
net, err := ParseNet("2001::/16")
assert.Nil(t, err)
ipRange := IPRangeFromIPNet(net)
assert.Equal(t, "2001::", ipRange.First.String())
assert.Equal(t, "2001:ffff:ffff:ffff:ffff:ffff:ffff:ffff", ipRange.Last.String())
assert.Equal(t, "[2001::,2001:ffff:ffff:ffff:ffff:ffff:ffff:ffff]", ipRange.String())
}
func TestIPRangeDifference(t *testing.T) {
for _, tc := range []struct {
a, b *IPRange
result string
}{
// Give ranges [10.0.0.0-10.0.0.25], try every overlap case {9.0.0.0, 10.0.0.0, 10.0.0.24, 10.0.0.255, 11.0.0.0}
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("9.0.0.0"), ParseIP("9.0.0.0")}, "[[10.0.0.0,10.0.0.255]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("9.0.0.0"), ParseIP("10.0.0.0")}, "[[10.0.0.1,10.0.0.255]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("9.0.0.0"), ParseIP("10.0.0.24")}, "[[10.0.0.25,10.0.0.255]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("9.0.0.0"), ParseIP("10.0.0.255")}, "[]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("9.0.0.0"), ParseIP("11.0.0.0")}, "[]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.0")}, "[[10.0.0.1,10.0.0.255]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.24")}, "[[10.0.0.25,10.0.0.255]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, "[]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.0"), ParseIP("11.0.0.0")}, "[]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.24"), ParseIP("10.0.0.24")}, "[[10.0.0.0,10.0.0.23] [10.0.0.25,10.0.0.255]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.24"), ParseIP("10.0.0.255")}, "[[10.0.0.0,10.0.0.23]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.24"), ParseIP("11.0.0.0")}, "[[10.0.0.0,10.0.0.23]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.255"), ParseIP("10.0.0.255")}, "[[10.0.0.0,10.0.0.254]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.255"), ParseIP("11.0.0.0")}, "[[10.0.0.0,10.0.0.254]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("11.0.0.0"), ParseIP("11.0.0.0")}, "[[10.0.0.0,10.0.0.255]]"},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.0")}, &IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.0")}, "[]"},
} {
diff := tc.a.Minus(tc.b)
assert.Equal(t, tc.result, fmt.Sprintf("%s", diff))
}
}
func TestIPRangeContains(t *testing.T) {
for i, tc := range []struct {
a, b *IPRange
result bool
}{
// Give ranges [10.0.0.0-10.0.0.25], try every overlap case {9.0.0.0, 10.0.0.0, 10.0.0.24, 10.0.0.255, 11.0.0.0}
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("9.0.0.0"), ParseIP("9.0.0.0")}, false},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("9.0.0.0"), ParseIP("10.0.0.0")}, false},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("9.0.0.0"), ParseIP("10.0.0.24")}, false},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("9.0.0.0"), ParseIP("10.0.0.255")}, false},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("9.0.0.0"), ParseIP("11.0.0.0")}, false},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.0")}, true},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.24")}, true},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, true},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.0"), ParseIP("11.0.0.0")}, false},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.24"), ParseIP("10.0.0.24")}, true},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.24"), ParseIP("10.0.0.255")}, true},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.24"), ParseIP("11.0.0.0")}, false},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.255"), ParseIP("10.0.0.255")}, true},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("10.0.0.255"), ParseIP("11.0.0.0")}, false},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.255")}, &IPRange{ParseIP("11.0.0.0"), ParseIP("11.0.0.0")}, false},
{&IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.0")}, &IPRange{ParseIP("10.0.0.0"), ParseIP("10.0.0.0")}, true},
} {
ret := tc.a.Contains(tc.b)
if !assert.Equal(t, tc.result, ret) {
t.Logf("Test %d failed: a: %s b: %s was %v and was supposed to be %v", i, tc.a, tc.b, ret, tc.result)
}
}
}