Exclude second last IP from allocation to use it in the Fake DNS (#912)

This commit is contained in:
Givi Khojanashvili 2023-05-30 18:26:44 +04:00 committed by GitHub
parent 79736197cd
commit 874c290205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 7 deletions

View File

@ -119,14 +119,15 @@ func generateIPs(ipNet *net.IPNet, exclusions map[string]struct{}) ([]net.IP, in
}
}
// remove network address and broadcast address
// remove network address, broadcast and Fake DNS resolver address
lenIPs := len(ips)
switch {
case lenIPs < 2:
return ips, lenIPs
default:
case lenIPs < 3:
return ips[1 : len(ips)-1], lenIPs - 2
default:
return ips[1 : len(ips)-2], lenIPs - 3
}
}

View File

@ -1,9 +1,10 @@
package server
import (
"github.com/stretchr/testify/assert"
"net"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewNetwork(t *testing.T) {
@ -15,10 +16,9 @@ func TestNewNetwork(t *testing.T) {
}
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++ {
for i := 0; i < 252; i++ {
ip, err := AllocatePeerIP(ipNet, ips)
if err != nil {
t.Fatal(err)
@ -26,7 +26,7 @@ func TestAllocatePeerIP(t *testing.T) {
ips = append(ips, ip)
}
assert.Len(t, ips, 253)
assert.Len(t, ips, 252)
uniq := make(map[string]struct{})
for _, ip := range ips {
@ -37,3 +37,15 @@ func TestAllocatePeerIP(t *testing.T) {
}
}
}
func TestGenerateIPs(t *testing.T) {
ipNet := net.IPNet{IP: net.ParseIP("100.64.0.0"), Mask: net.IPMask{255, 255, 255, 0}}
ips, ipsLen := generateIPs(&ipNet, map[string]struct{}{"100.64.0.0": {}})
if ipsLen != 252 {
t.Errorf("expected 252 ips, got %d", len(ips))
return
}
if ips[len(ips)-1].String() != "100.64.0.253" {
t.Errorf("expected last ip to be: 100.64.0.253, got %s", ips[len(ips)-1].String())
}
}