-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iprange_test.go
72 lines (68 loc) · 2.17 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
package connectip
import (
"fmt"
"net/netip"
"testing"
"github.com/stretchr/testify/require"
)
func TestIPRanges(t *testing.T) {
tests := []struct {
start, end netip.Addr
want []netip.Prefix
}{
{
start: netip.MustParseAddr("192.168.1.1"),
end: netip.MustParseAddr("192.168.1.1"),
want: []netip.Prefix{netip.MustParsePrefix("192.168.1.1/32")},
},
{
start: netip.MustParseAddr("192.168.1.0"),
end: netip.MustParseAddr("192.168.1.1"),
want: []netip.Prefix{netip.MustParsePrefix("192.168.1.0/31")},
},
{
start: netip.MustParseAddr("192.168.1.1"),
end: netip.MustParseAddr("192.168.1.2"),
want: []netip.Prefix{netip.MustParsePrefix("192.168.1.1/32"), netip.MustParsePrefix("192.168.1.2/32")},
},
{
start: netip.MustParseAddr("192.168.1.0"),
end: netip.MustParseAddr("192.168.1.255"),
want: []netip.Prefix{netip.MustParsePrefix("192.168.1.0/24")},
},
{
start: netip.MustParseAddr("10.0.0.0"),
end: netip.MustParseAddr("10.1.0.255"),
want: []netip.Prefix{netip.MustParsePrefix("10.0.0.0/16"), netip.MustParsePrefix("10.1.0.0/24")},
},
{
start: netip.MustParseAddr("2001:0db8:85a3::8a2e:0370:7334"),
end: netip.MustParseAddr("2001:0db8:85a3::8a2e:0370:7334"),
want: []netip.Prefix{netip.MustParsePrefix("2001:0db8:85a3::8a2e:0370:7334/128")},
},
{
start: netip.MustParseAddr("2001:db8::0"),
end: netip.MustParseAddr("2001:db8::ffff:ffff:ffff:ffff"),
want: []netip.Prefix{netip.MustParsePrefix("2001:db8::/64")},
},
{
start: netip.MustParseAddr("2001:db8::1"),
end: netip.MustParseAddr("2001:db8::2"),
want: []netip.Prefix{netip.MustParsePrefix("2001:db8::1/128"), netip.MustParsePrefix("2001:db8::2/128")},
},
{
start: netip.MustParseAddr("2001:db8:1234:5678::"),
end: netip.MustParseAddr("2001:db8:1234:5679::"),
want: []netip.Prefix{
netip.MustParsePrefix("2001:db8:1234:5678::/64"),
netip.MustParsePrefix("2001:db8:1234:5679::/128"),
},
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("%s-%s", test.start, test.end), func(t *testing.T) {
prefixes := rangeToPrefixes(test.start, test.end)
require.Equal(t, test.want, prefixes)
})
}
}