mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-08 09:14:18 +01:00
40 lines
866 B
Go
40 lines
866 B
Go
|
package server
|
||
|
|
||
|
import (
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"net"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestNewNetwork(t *testing.T) {
|
||
|
network := NewNetwork()
|
||
|
|
||
|
// generated net should be a subnet of a larger 100.64.0.0/10 net
|
||
|
ipNet := net.IPNet{IP: net.ParseIP("100.64.0.0"), Mask: net.IPMask{255, 192, 0, 0}}
|
||
|
assert.Equal(t, ipNet.Contains(network.Net.IP), true)
|
||
|
}
|
||
|
|
||
|
func TestAllocatePeerIP(t *testing.T) {
|
||
|
|
||
|
ipNet := net.IPNet{IP: net.ParseIP("100.64.0.0"), Mask: net.IPMask{255, 255, 255, 0}}
|
||
|
var ips []net.IP
|
||
|
for i := 0; i < 253; i++ {
|
||
|
ip, err := AllocatePeerIP(ipNet, ips)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
ips = append(ips, ip)
|
||
|
}
|
||
|
|
||
|
assert.Len(t, ips, 253)
|
||
|
|
||
|
uniq := make(map[string]struct{})
|
||
|
for _, ip := range ips {
|
||
|
if _, ok := uniq[ip.String()]; !ok {
|
||
|
uniq[ip.String()] = struct{}{}
|
||
|
} else {
|
||
|
t.Errorf("found duplicate IP %s", ip.String())
|
||
|
}
|
||
|
}
|
||
|
}
|