115 lines
2.1 KiB
Go
115 lines
2.1 KiB
Go
package netalloc
|
|
|
|
import (
|
|
"net"
|
|
"testing"
|
|
)
|
|
|
|
func TestMemAlloc(t *testing.T) {
|
|
var mem Allocator
|
|
mem, err := NewMemAlloc("192.168.1.0/24")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
ip, err := mem.Alloc()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !net.ParseIP("192.168.1.1").Equal(ip.IP) {
|
|
t.Errorf("expected '192.168.1.1' got '%s'", ip.IP)
|
|
}
|
|
cidr, err := mem.AllocCIDR()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if cidr != "192.168.1.2/24" {
|
|
t.Errorf("expected '192.168.1.2/24' got '%s'", cidr)
|
|
}
|
|
err = mem.Free(ip)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
cidr, err = mem.AllocCIDR()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if cidr != "192.168.1.1/24" {
|
|
t.Errorf("expected '192.168.1.1/24' got '%s'", cidr)
|
|
}
|
|
err = mem.FreeCIDR("192.168.1.2/24")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
|
|
func TestMemAllocFull(t *testing.T) {
|
|
var mem Allocator
|
|
mem, err := NewMemAlloc("192.168.1.0/30")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
ip, err := mem.Alloc()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
_, err = mem.Alloc()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
_, err = mem.Alloc()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
_, err = mem.Alloc()
|
|
if err != ErrPrefixFull {
|
|
t.Errorf("expected '%s' got '%s'", ErrPrefixFull, err)
|
|
}
|
|
err = mem.Free(ip)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
ip2, err := mem.Alloc()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !ip.IP.Equal(ip2.IP) {
|
|
t.Errorf("expected '192.168.1.1/30' got '%s'", ip2.IP)
|
|
}
|
|
}
|
|
|
|
func TestMemAllocCIDR(t *testing.T) {
|
|
var mem Allocator
|
|
mem, err := NewMemAlloc("192.168.1.0/30")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
ip, err := mem.Alloc()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if !net.ParseIP("192.168.1.1").Equal(ip.IP) {
|
|
t.Errorf("expected '192.168.1.1' got '%s'", ip.IP)
|
|
}
|
|
cidr, err := mem.AllocCIDR()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if cidr != "192.168.1.2/30" {
|
|
t.Errorf("expected '192.168.1.2/30' got '%s'", cidr)
|
|
}
|
|
err = mem.Free(ip)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
cidr, err = mem.AllocCIDR()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if cidr != "192.168.1.1/30" {
|
|
t.Errorf("expected '192.168.1.1/30' got '%s'", cidr)
|
|
}
|
|
err = mem.FreeCIDR("192.168.1.2/30")
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|