mihomo/component/fakeip/pool_test.go

188 lines
4.1 KiB
Go
Raw Normal View History

2019-05-03 00:05:14 +08:00
package fakeip
import (
"net"
2021-10-11 20:48:58 +08:00
"os"
2019-05-03 00:05:14 +08:00
"testing"
2021-10-11 20:48:58 +08:00
"time"
"github.com/Dreamacro/clash/component/profile/cachefile"
"github.com/Dreamacro/clash/component/trie"
"github.com/stretchr/testify/assert"
2021-10-11 20:48:58 +08:00
"go.etcd.io/bbolt"
2019-05-03 00:05:14 +08:00
)
2021-10-11 20:48:58 +08:00
func createPools(options Options) ([]*Pool, string, error) {
pool, err := New(options)
if err != nil {
return nil, "", err
}
filePool, tempfile, err := createCachefileStore(options)
if err != nil {
return nil, "", err
}
return []*Pool{pool, filePool}, tempfile, nil
}
2019-05-03 00:05:14 +08:00
2021-10-11 20:48:58 +08:00
func createCachefileStore(options Options) (*Pool, string, error) {
pool, err := New(options)
if err != nil {
return nil, "", err
}
f, err := os.CreateTemp("", "clash")
if err != nil {
return nil, "", err
}
db, err := bbolt.Open(f.Name(), 0o666, &bbolt.Options{Timeout: time.Second})
if err != nil {
return nil, "", err
}
pool.store = &cachefileStore{
cache: &cachefile.CacheFile{DB: db},
}
return pool, f.Name(), nil
}
2019-05-03 00:05:14 +08:00
2021-10-11 20:48:58 +08:00
func TestPool_Basic(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("192.168.0.1/29")
pools, tempfile, err := createPools(Options{
IPNet: ipnet,
Size: 10,
})
assert.Nil(t, err)
defer os.Remove(tempfile)
for _, pool := range pools {
first := pool.Lookup("foo.com")
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, exist)
assert.Equal(t, bar, "bar.com")
assert.Equal(t, pool.Gateway(), net.IP{192, 168, 0, 1})
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.False(t, pool.Exist(net.ParseIP("::1")))
}
2019-05-03 00:05:14 +08:00
}
2021-10-11 20:48:58 +08:00
func TestPool_CycleUsed(t *testing.T) {
2019-05-03 00:05:14 +08:00
_, ipnet, _ := net.ParseCIDR("192.168.0.1/30")
2021-10-11 20:48:58 +08:00
pools, tempfile, err := createPools(Options{
IPNet: ipnet,
Size: 10,
})
assert.Nil(t, err)
defer os.Remove(tempfile)
for _, pool := range pools {
first := pool.Lookup("foo.com")
same := pool.Lookup("baz.com")
assert.True(t, first.Equal(same))
}
}
2021-10-11 20:48:58 +08:00
func TestPool_Skip(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("192.168.0.1/30")
tree := trie.New()
tree.Insert("example.com", tree)
pools, tempfile, err := createPools(Options{
IPNet: ipnet,
Size: 10,
Host: tree,
})
assert.Nil(t, err)
defer os.Remove(tempfile)
for _, pool := range pools {
assert.True(t, pool.ShouldSkipped("example.com"))
assert.False(t, pool.ShouldSkipped("foo.com"))
}
}
func TestPool_MaxCacheSize(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("192.168.0.1/24")
2021-10-11 20:48:58 +08:00
pool, _ := New(Options{
IPNet: ipnet,
Size: 2,
})
2019-05-03 00:05:14 +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
assert.False(t, first.Equal(next))
2019-05-03 00:05:14 +08:00
}
2019-10-11 14:01:16 +08:00
func TestPool_DoubleMapping(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("192.168.0.1/24")
2021-10-11 20:48:58 +08:00
pool, _ := New(Options{
IPNet: ipnet,
Size: 2,
})
2019-10-11 14:01:16 +08:00
// fill cache
fooIP := pool.Lookup("foo.com")
bazIP := pool.Lookup("baz.com")
// make foo.com hot
pool.Lookup("foo.com")
// should drop baz.com
barIP := pool.Lookup("bar.com")
_, fooExist := pool.LookBack(fooIP)
_, bazExist := pool.LookBack(bazIP)
_, barExist := pool.LookBack(barIP)
newBazIP := pool.Lookup("baz.com")
assert.True(t, fooExist)
assert.False(t, bazExist)
assert.True(t, barExist)
assert.False(t, bazIP.Equal(newBazIP))
}
2021-10-11 20:48:58 +08:00
func TestPool_Clone(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("192.168.0.1/24")
pool, _ := New(Options{
IPNet: ipnet,
Size: 2,
})
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}))
newPool, _ := New(Options{
IPNet: ipnet,
Size: 2,
})
newPool.CloneFrom(pool)
_, firstExist := newPool.LookBack(first)
_, lastExist := newPool.LookBack(last)
assert.True(t, firstExist)
assert.True(t, lastExist)
}
2019-05-03 00:05:14 +08:00
func TestPool_Error(t *testing.T) {
_, ipnet, _ := net.ParseCIDR("192.168.0.1/31")
2021-10-11 20:48:58 +08:00
_, err := New(Options{
IPNet: ipnet,
Size: 10,
})
2019-05-03 00:05:14 +08:00
assert.Error(t, err)
2019-05-03 00:05:14 +08:00
}