2019-05-03 00:05:14 +08:00
|
|
|
package fakeip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
2019-07-26 19:09:13 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-03 00:05:14 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPool_Basic(t *testing.T) {
|
2019-07-26 19:09:13 +08:00
|
|
|
_, ipnet, _ := net.ParseCIDR("192.168.0.1/29")
|
|
|
|
pool, _ := New(ipnet, 10)
|
2019-05-03 00:05:14 +08:00
|
|
|
|
2019-07-26 19:09:13 +08:00
|
|
|
first := pool.Lookup("foo.com")
|
|
|
|
last := pool.Lookup("bar.com")
|
|
|
|
bar, exist := pool.LookBack(last)
|
2019-05-03 00:05:14 +08:00
|
|
|
|
2019-07-26 19:09:13 +08:00
|
|
|
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, exist)
|
|
|
|
assert.Equal(t, bar, "bar.com")
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPool_Cycle(t *testing.T) {
|
|
|
|
_, ipnet, _ := net.ParseCIDR("192.168.0.1/30")
|
2019-07-26 19:09:13 +08:00
|
|
|
pool, _ := New(ipnet, 10)
|
|
|
|
|
|
|
|
first := pool.Lookup("foo.com")
|
|
|
|
same := pool.Lookup("baz.com")
|
|
|
|
|
|
|
|
assert.True(t, first.Equal(same))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPool_MaxCacheSize(t *testing.T) {
|
|
|
|
_, ipnet, _ := net.ParseCIDR("192.168.0.1/24")
|
|
|
|
pool, _ := New(ipnet, 2)
|
2019-05-03 00:05:14 +08:00
|
|
|
|
2019-07-26 19:09:13 +08:00
|
|
|
first := pool.Lookup("foo.com")
|
|
|
|
pool.Lookup("bar.com")
|
|
|
|
pool.Lookup("baz.com")
|
|
|
|
next := pool.Lookup("foo.com")
|
2019-05-03 00:05:14 +08:00
|
|
|
|
2019-07-26 19:09:13 +08:00
|
|
|
assert.False(t, first.Equal(next))
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPool_Error(t *testing.T) {
|
|
|
|
_, ipnet, _ := net.ParseCIDR("192.168.0.1/31")
|
2019-07-26 19:09:13 +08:00
|
|
|
_, err := New(ipnet, 10)
|
2019-05-03 00:05:14 +08:00
|
|
|
|
2019-07-26 19:09:13 +08:00
|
|
|
assert.Error(t, err)
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|