package awgserver import "testing" func TestIPAllocator_Basic(t *testing.T) { alloc, err := NewIPAllocator("10.14.0.0/24") if err != nil { t.Fatalf("NewIPAllocator() error: %v", err) } if got := alloc.ServerIP().String(); got != "10.14.0.1" { t.Errorf("ServerIP() = %s, want 10.14.0.1", got) } ip, err := alloc.Allocate(nil) if err != nil { t.Fatalf("Allocate() error: %v", err) } if ip != "10.14.0.2/32" { t.Errorf("first allocation = %s, want 10.14.0.2/32", ip) } } func TestIPAllocator_SkipsUsed(t *testing.T) { alloc, err := NewIPAllocator("10.14.0.0/24") if err != nil { t.Fatalf("NewIPAllocator() error: %v", err) } used := []string{"10.14.0.2/32", "10.14.0.3/32"} ip, err := alloc.Allocate(used) if err != nil { t.Fatalf("Allocate() error: %v", err) } if ip != "10.14.0.4/32" { t.Errorf("allocation = %s, want 10.14.0.4/32", ip) } } func TestIPAllocator_Exhaustion(t *testing.T) { alloc, err := NewIPAllocator("10.14.0.0/30") // Only 4 IPs: .0 (network), .1 (server), .2 (peer), .3 (broadcast) if err != nil { t.Fatalf("NewIPAllocator() error: %v", err) } ip, err := alloc.Allocate(nil) if err != nil { t.Fatalf("first Allocate() error: %v", err) } if ip != "10.14.0.2/32" { t.Errorf("allocation = %s, want 10.14.0.2/32", ip) } _, err = alloc.Allocate([]string{"10.14.0.2/32"}) if err == nil { t.Errorf("expected error on exhausted subnet, got nil") } } func TestIPAllocator_16Subnet(t *testing.T) { alloc, err := NewIPAllocator("10.14.0.0/16") if err != nil { t.Fatalf("NewIPAllocator() error: %v", err) } ip, err := alloc.Allocate(nil) if err != nil { t.Fatalf("Allocate() error: %v", err) } if ip != "10.14.0.2/32" { t.Errorf("allocation = %s, want 10.14.0.2/32", ip) } }