Chore: make fake ip pool start with the third ip

This commit is contained in:
yaling888
2022-03-18 05:17:47 +08:00
parent 8d0ae4284d
commit 546f2fa739
4 changed files with 32 additions and 28 deletions

View File

@ -147,10 +147,10 @@ type Options struct {
// New return Pool instance
func New(options Options) (*Pool, error) {
min := ipToUint(options.IPNet.IP) + 2
min := ipToUint(options.IPNet.IP) + 3
ones, bits := options.IPNet.Mask.Size()
total := 1<<uint(bits-ones) - 3
total := 1<<uint(bits-ones) - 4
if total <= 0 {
return nil, errors.New("ipnet don't have valid ip")
@ -160,7 +160,7 @@ func New(options Options) (*Pool, error) {
pool := &Pool{
min: min,
max: max,
gateway: min - 1,
gateway: min - 2,
broadcast: max + 1,
host: options.Host,
ipnet: options.IPNet,

View File

@ -49,7 +49,7 @@ func createCachefileStore(options Options) (*Pool, string, error) {
}
func TestPool_Basic(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("192.168.0.1/29")
_, ipnet, _ := net.ParseCIDR("192.168.0.0/28")
pools, tempfile, err := createPools(Options{
IPNet: ipnet,
Size: 10,
@ -62,21 +62,22 @@ func TestPool_Basic(t *testing.T) {
last := pool.Lookup("bar.com")
bar, exist := pool.LookBack(last)
assert.True(t, first.Equal(net.IP{192, 168, 0, 2}))
assert.Equal(t, pool.Lookup("foo.com"), net.IP{192, 168, 0, 2})
assert.True(t, last.Equal(net.IP{192, 168, 0, 3}))
assert.True(t, first.Equal(net.IP{192, 168, 0, 3}))
assert.Equal(t, pool.Lookup("foo.com"), net.IP{192, 168, 0, 3})
assert.True(t, last.Equal(net.IP{192, 168, 0, 4}))
assert.True(t, exist)
assert.Equal(t, bar, "bar.com")
assert.Equal(t, pool.Gateway(), net.IP{192, 168, 0, 1})
assert.Equal(t, pool.Broadcast(), net.IP{192, 168, 0, 15})
assert.Equal(t, pool.IPNet().String(), ipnet.String())
assert.True(t, pool.Exist(net.IP{192, 168, 0, 3}))
assert.False(t, pool.Exist(net.IP{192, 168, 0, 4}))
assert.True(t, pool.Exist(net.IP{192, 168, 0, 4}))
assert.False(t, pool.Exist(net.IP{192, 168, 0, 5}))
assert.False(t, pool.Exist(net.ParseIP("::1")))
}
}
func TestPool_CycleUsed(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("192.168.0.1/29")
_, ipnet, _ := net.ParseCIDR("192.168.0.16/28")
pools, tempfile, err := createPools(Options{
IPNet: ipnet,
Size: 10,
@ -87,7 +88,7 @@ func TestPool_CycleUsed(t *testing.T) {
for _, pool := range pools {
foo := pool.Lookup("foo.com")
bar := pool.Lookup("bar.com")
for i := 0; i < 2; i++ {
for i := 0; i < 9; i++ {
pool.Lookup(fmt.Sprintf("%d.com", i))
}
baz := pool.Lookup("baz.com")
@ -98,7 +99,7 @@ func TestPool_CycleUsed(t *testing.T) {
}
func TestPool_Skip(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("192.168.0.1/30")
_, ipnet, _ := net.ParseCIDR("192.168.0.1/29")
tree := trie.New()
tree.Insert("example.com", tree)
pools, tempfile, err := createPools(Options{
@ -169,8 +170,8 @@ func TestPool_Clone(t *testing.T) {
first := pool.Lookup("foo.com")
last := pool.Lookup("bar.com")
assert.True(t, first.Equal(net.IP{192, 168, 0, 2}))
assert.True(t, last.Equal(net.IP{192, 168, 0, 3}))
assert.True(t, first.Equal(net.IP{192, 168, 0, 3}))
assert.True(t, last.Equal(net.IP{192, 168, 0, 4}))
newPool, _ := New(Options{
IPNet: ipnet,