2015-10-04 21:18:32 +00:00
|
|
|
package ipamutils
|
|
|
|
|
|
|
|
import (
|
2018-01-18 22:30:51 +00:00
|
|
|
"net"
|
2015-10-04 21:18:32 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-08-31 15:55:22 +00:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2015-10-04 21:18:32 +00:00
|
|
|
)
|
|
|
|
|
2018-01-18 22:30:51 +00:00
|
|
|
func initBroadPredefinedNetworks() []*net.IPNet {
|
|
|
|
pl := make([]*net.IPNet, 0, 31)
|
|
|
|
mask := []byte{255, 255, 0, 0}
|
|
|
|
for i := 17; i < 32; i++ {
|
|
|
|
pl = append(pl, &net.IPNet{IP: []byte{172, byte(i), 0, 0}, Mask: mask})
|
|
|
|
}
|
|
|
|
mask20 := []byte{255, 255, 240, 0}
|
|
|
|
for i := 0; i < 16; i++ {
|
|
|
|
pl = append(pl, &net.IPNet{IP: []byte{192, 168, byte(i << 4), 0}, Mask: mask20})
|
|
|
|
}
|
|
|
|
return pl
|
|
|
|
}
|
|
|
|
|
|
|
|
func initGranularPredefinedNetworks() []*net.IPNet {
|
|
|
|
pl := make([]*net.IPNet, 0, 256*256)
|
|
|
|
mask := []byte{255, 255, 255, 0}
|
|
|
|
for i := 0; i < 256; i++ {
|
|
|
|
for j := 0; j < 256; j++ {
|
|
|
|
pl = append(pl, &net.IPNet{IP: []byte{10, byte(i), byte(j), 0}, Mask: mask})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pl
|
2016-04-04 13:50:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 19:46:59 +00:00
|
|
|
func initGlobalScopeNetworks() []*net.IPNet {
|
|
|
|
pl := make([]*net.IPNet, 0, 256*256)
|
|
|
|
mask := []byte{255, 255, 255, 0}
|
|
|
|
for i := 0; i < 256; i++ {
|
|
|
|
for j := 0; j < 256; j++ {
|
|
|
|
pl = append(pl, &net.IPNet{IP: []byte{30, byte(i), byte(j), 0}, Mask: mask})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return pl
|
|
|
|
}
|
|
|
|
|
2018-01-18 22:30:51 +00:00
|
|
|
func TestDefaultNetwork(t *testing.T) {
|
2023-01-17 01:09:42 +00:00
|
|
|
for _, nw := range GetGlobalScopeDefaultNetworks() {
|
2015-10-04 21:18:32 +00:00
|
|
|
if ones, bits := nw.Mask.Size(); bits != 32 || ones != 24 {
|
|
|
|
t.Fatalf("Unexpected size for network in granular list: %v", nw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 01:09:42 +00:00
|
|
|
for _, nw := range GetLocalScopeDefaultNetworks() {
|
2015-10-04 21:18:32 +00:00
|
|
|
if ones, bits := nw.Mask.Size(); bits != 32 || (ones != 20 && ones != 16) {
|
|
|
|
t.Fatalf("Unexpected size for network in broad list: %v", nw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-18 22:30:51 +00:00
|
|
|
originalBroadNets := initBroadPredefinedNetworks()
|
|
|
|
m := make(map[string]bool)
|
|
|
|
for _, v := range originalBroadNets {
|
|
|
|
m[v.String()] = true
|
|
|
|
}
|
2023-01-17 01:09:42 +00:00
|
|
|
for _, nw := range GetLocalScopeDefaultNetworks() {
|
2018-01-18 22:30:51 +00:00
|
|
|
_, ok := m[nw.String()]
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.Check(t, ok)
|
2018-01-18 22:30:51 +00:00
|
|
|
delete(m, nw.String())
|
|
|
|
}
|
|
|
|
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.Check(t, is.Len(m, 0))
|
2018-01-18 22:30:51 +00:00
|
|
|
|
|
|
|
originalGranularNets := initGranularPredefinedNetworks()
|
|
|
|
|
|
|
|
m = make(map[string]bool)
|
|
|
|
for _, v := range originalGranularNets {
|
|
|
|
m[v.String()] = true
|
|
|
|
}
|
2023-01-17 01:09:42 +00:00
|
|
|
for _, nw := range GetGlobalScopeDefaultNetworks() {
|
2018-07-24 19:46:59 +00:00
|
|
|
_, ok := m[nw.String()]
|
|
|
|
assert.Check(t, ok)
|
|
|
|
delete(m, nw.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Check(t, is.Len(m, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConfigGlobalScopeDefaultNetworks(t *testing.T) {
|
|
|
|
err := ConfigGlobalScopeDefaultNetworks([]*NetworkToSplit{{"30.0.0.0/8", 24}})
|
|
|
|
assert.NilError(t, err)
|
|
|
|
|
|
|
|
originalGlobalScopeNetworks := initGlobalScopeNetworks()
|
|
|
|
m := make(map[string]bool)
|
|
|
|
for _, v := range originalGlobalScopeNetworks {
|
|
|
|
m[v.String()] = true
|
|
|
|
}
|
2023-01-17 01:09:42 +00:00
|
|
|
for _, nw := range GetGlobalScopeDefaultNetworks() {
|
2018-01-18 22:30:51 +00:00
|
|
|
_, ok := m[nw.String()]
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.Check(t, ok)
|
2018-01-18 22:30:51 +00:00
|
|
|
delete(m, nw.String())
|
|
|
|
}
|
|
|
|
|
2018-07-04 09:01:52 +00:00
|
|
|
assert.Check(t, is.Len(m, 0))
|
2018-01-18 22:30:51 +00:00
|
|
|
}
|