|
@@ -9,7 +9,7 @@ import (
|
|
|
)
|
|
|
|
|
|
func TestCIDRTree_List(t *testing.T) {
|
|
|
- tree := NewTree4()
|
|
|
+ tree := NewTree4[string]()
|
|
|
tree.AddCIDR(Parse("1.0.0.0/16"), "1")
|
|
|
tree.AddCIDR(Parse("1.0.0.0/8"), "2")
|
|
|
tree.AddCIDR(Parse("1.0.0.0/16"), "3")
|
|
@@ -17,13 +17,13 @@ func TestCIDRTree_List(t *testing.T) {
|
|
|
list := tree.List()
|
|
|
assert.Len(t, list, 2)
|
|
|
assert.Equal(t, "1.0.0.0/8", list[0].CIDR.String())
|
|
|
- assert.Equal(t, "2", *list[0].Value)
|
|
|
+ assert.Equal(t, "2", list[0].Value)
|
|
|
assert.Equal(t, "1.0.0.0/16", list[1].CIDR.String())
|
|
|
- assert.Equal(t, "4", *list[1].Value)
|
|
|
+ assert.Equal(t, "4", list[1].Value)
|
|
|
}
|
|
|
|
|
|
func TestCIDRTree_Contains(t *testing.T) {
|
|
|
- tree := NewTree4()
|
|
|
+ tree := NewTree4[string]()
|
|
|
tree.AddCIDR(Parse("1.0.0.0/8"), "1")
|
|
|
tree.AddCIDR(Parse("2.1.0.0/16"), "2")
|
|
|
tree.AddCIDR(Parse("3.1.1.0/24"), "3")
|
|
@@ -33,35 +33,43 @@ func TestCIDRTree_Contains(t *testing.T) {
|
|
|
tree.AddCIDR(Parse("254.0.0.0/4"), "5")
|
|
|
|
|
|
tests := []struct {
|
|
|
+ Found bool
|
|
|
Result interface{}
|
|
|
IP string
|
|
|
}{
|
|
|
- {"1", "1.0.0.0"},
|
|
|
- {"1", "1.255.255.255"},
|
|
|
- {"2", "2.1.0.0"},
|
|
|
- {"2", "2.1.255.255"},
|
|
|
- {"3", "3.1.1.0"},
|
|
|
- {"3", "3.1.1.255"},
|
|
|
- {"4a", "4.1.1.255"},
|
|
|
- {"4a", "4.1.1.1"},
|
|
|
- {"5", "240.0.0.0"},
|
|
|
- {"5", "255.255.255.255"},
|
|
|
- {nil, "239.0.0.0"},
|
|
|
- {nil, "4.1.2.2"},
|
|
|
+ {true, "1", "1.0.0.0"},
|
|
|
+ {true, "1", "1.255.255.255"},
|
|
|
+ {true, "2", "2.1.0.0"},
|
|
|
+ {true, "2", "2.1.255.255"},
|
|
|
+ {true, "3", "3.1.1.0"},
|
|
|
+ {true, "3", "3.1.1.255"},
|
|
|
+ {true, "4a", "4.1.1.255"},
|
|
|
+ {true, "4a", "4.1.1.1"},
|
|
|
+ {true, "5", "240.0.0.0"},
|
|
|
+ {true, "5", "255.255.255.255"},
|
|
|
+ {false, "", "239.0.0.0"},
|
|
|
+ {false, "", "4.1.2.2"},
|
|
|
}
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
- assert.Equal(t, tt.Result, tree.Contains(iputil.Ip2VpnIp(net.ParseIP(tt.IP))))
|
|
|
+ ok, r := tree.Contains(iputil.Ip2VpnIp(net.ParseIP(tt.IP)))
|
|
|
+ assert.Equal(t, tt.Found, ok)
|
|
|
+ assert.Equal(t, tt.Result, r)
|
|
|
}
|
|
|
|
|
|
- tree = NewTree4()
|
|
|
+ tree = NewTree4[string]()
|
|
|
tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
|
|
|
- assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0"))))
|
|
|
- assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255"))))
|
|
|
+ ok, r := tree.Contains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0")))
|
|
|
+ assert.True(t, ok)
|
|
|
+ assert.Equal(t, "cool", r)
|
|
|
+
|
|
|
+ ok, r = tree.Contains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255")))
|
|
|
+ assert.True(t, ok)
|
|
|
+ assert.Equal(t, "cool", r)
|
|
|
}
|
|
|
|
|
|
func TestCIDRTree_MostSpecificContains(t *testing.T) {
|
|
|
- tree := NewTree4()
|
|
|
+ tree := NewTree4[string]()
|
|
|
tree.AddCIDR(Parse("1.0.0.0/8"), "1")
|
|
|
tree.AddCIDR(Parse("2.1.0.0/16"), "2")
|
|
|
tree.AddCIDR(Parse("3.1.1.0/24"), "3")
|
|
@@ -71,59 +79,75 @@ func TestCIDRTree_MostSpecificContains(t *testing.T) {
|
|
|
tree.AddCIDR(Parse("254.0.0.0/4"), "5")
|
|
|
|
|
|
tests := []struct {
|
|
|
+ Found bool
|
|
|
Result interface{}
|
|
|
IP string
|
|
|
}{
|
|
|
- {"1", "1.0.0.0"},
|
|
|
- {"1", "1.255.255.255"},
|
|
|
- {"2", "2.1.0.0"},
|
|
|
- {"2", "2.1.255.255"},
|
|
|
- {"3", "3.1.1.0"},
|
|
|
- {"3", "3.1.1.255"},
|
|
|
- {"4a", "4.1.1.255"},
|
|
|
- {"4b", "4.1.1.2"},
|
|
|
- {"4c", "4.1.1.1"},
|
|
|
- {"5", "240.0.0.0"},
|
|
|
- {"5", "255.255.255.255"},
|
|
|
- {nil, "239.0.0.0"},
|
|
|
- {nil, "4.1.2.2"},
|
|
|
+ {true, "1", "1.0.0.0"},
|
|
|
+ {true, "1", "1.255.255.255"},
|
|
|
+ {true, "2", "2.1.0.0"},
|
|
|
+ {true, "2", "2.1.255.255"},
|
|
|
+ {true, "3", "3.1.1.0"},
|
|
|
+ {true, "3", "3.1.1.255"},
|
|
|
+ {true, "4a", "4.1.1.255"},
|
|
|
+ {true, "4b", "4.1.1.2"},
|
|
|
+ {true, "4c", "4.1.1.1"},
|
|
|
+ {true, "5", "240.0.0.0"},
|
|
|
+ {true, "5", "255.255.255.255"},
|
|
|
+ {false, "", "239.0.0.0"},
|
|
|
+ {false, "", "4.1.2.2"},
|
|
|
}
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
- assert.Equal(t, tt.Result, tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP(tt.IP))))
|
|
|
+ ok, r := tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP(tt.IP)))
|
|
|
+ assert.Equal(t, tt.Found, ok)
|
|
|
+ assert.Equal(t, tt.Result, r)
|
|
|
}
|
|
|
|
|
|
- tree = NewTree4()
|
|
|
+ tree = NewTree4[string]()
|
|
|
tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
|
|
|
- assert.Equal(t, "cool", tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0"))))
|
|
|
- assert.Equal(t, "cool", tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255"))))
|
|
|
+ ok, r := tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0")))
|
|
|
+ assert.True(t, ok)
|
|
|
+ assert.Equal(t, "cool", r)
|
|
|
+
|
|
|
+ ok, r = tree.MostSpecificContains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255")))
|
|
|
+ assert.True(t, ok)
|
|
|
+ assert.Equal(t, "cool", r)
|
|
|
}
|
|
|
|
|
|
func TestCIDRTree_Match(t *testing.T) {
|
|
|
- tree := NewTree4()
|
|
|
+ tree := NewTree4[string]()
|
|
|
tree.AddCIDR(Parse("4.1.1.0/32"), "1a")
|
|
|
tree.AddCIDR(Parse("4.1.1.1/32"), "1b")
|
|
|
|
|
|
tests := []struct {
|
|
|
+ Found bool
|
|
|
Result interface{}
|
|
|
IP string
|
|
|
}{
|
|
|
- {"1a", "4.1.1.0"},
|
|
|
- {"1b", "4.1.1.1"},
|
|
|
+ {true, "1a", "4.1.1.0"},
|
|
|
+ {true, "1b", "4.1.1.1"},
|
|
|
}
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
- assert.Equal(t, tt.Result, tree.Match(iputil.Ip2VpnIp(net.ParseIP(tt.IP))))
|
|
|
+ ok, r := tree.Match(iputil.Ip2VpnIp(net.ParseIP(tt.IP)))
|
|
|
+ assert.Equal(t, tt.Found, ok)
|
|
|
+ assert.Equal(t, tt.Result, r)
|
|
|
}
|
|
|
|
|
|
- tree = NewTree4()
|
|
|
+ tree = NewTree4[string]()
|
|
|
tree.AddCIDR(Parse("1.1.1.1/0"), "cool")
|
|
|
- assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0"))))
|
|
|
- assert.Equal(t, "cool", tree.Contains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255"))))
|
|
|
+ ok, r := tree.Contains(iputil.Ip2VpnIp(net.ParseIP("0.0.0.0")))
|
|
|
+ assert.True(t, ok)
|
|
|
+ assert.Equal(t, "cool", r)
|
|
|
+
|
|
|
+ ok, r = tree.Contains(iputil.Ip2VpnIp(net.ParseIP("255.255.255.255")))
|
|
|
+ assert.True(t, ok)
|
|
|
+ assert.Equal(t, "cool", r)
|
|
|
}
|
|
|
|
|
|
func BenchmarkCIDRTree_Contains(b *testing.B) {
|
|
|
- tree := NewTree4()
|
|
|
+ tree := NewTree4[string]()
|
|
|
tree.AddCIDR(Parse("1.1.0.0/16"), "1")
|
|
|
tree.AddCIDR(Parse("1.2.1.1/32"), "1")
|
|
|
tree.AddCIDR(Parse("192.2.1.1/32"), "1")
|
|
@@ -145,7 +169,7 @@ func BenchmarkCIDRTree_Contains(b *testing.B) {
|
|
|
}
|
|
|
|
|
|
func BenchmarkCIDRTree_Match(b *testing.B) {
|
|
|
- tree := NewTree4()
|
|
|
+ tree := NewTree4[string]()
|
|
|
tree.AddCIDR(Parse("1.1.0.0/16"), "1")
|
|
|
tree.AddCIDR(Parse("1.2.1.1/32"), "1")
|
|
|
tree.AddCIDR(Parse("192.2.1.1/32"), "1")
|