2019-05-03 00:05:14 +08:00
|
|
|
package fakeip
|
|
|
|
|
|
|
|
import (
|
2021-11-23 22:01:49 +08:00
|
|
|
"fmt"
|
2022-04-12 00:31:04 +08:00
|
|
|
"net/netip"
|
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"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/profile/cachefile"
|
|
|
|
"github.com/metacubex/mihomo/component/trie"
|
2024-08-14 22:38:17 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2019-07-26 19:09:13 +08:00
|
|
|
|
2024-08-22 09:24:27 +08:00
|
|
|
"github.com/metacubex/bbolt"
|
2019-07-26 19:09:13 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
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
|
|
|
|
}
|
2023-11-03 21:01:45 +08:00
|
|
|
f, err := os.CreateTemp("", "mihomo")
|
2021-10-11 20:48:58 +08:00
|
|
|
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) {
|
2022-04-12 00:31:04 +08:00
|
|
|
ipnet := netip.MustParsePrefix("192.168.0.0/28")
|
2021-10-11 20:48:58 +08:00
|
|
|
pools, tempfile, err := createPools(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2021-10-11 20:48:58 +08:00
|
|
|
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)
|
|
|
|
|
2022-10-09 11:07:24 +08:00
|
|
|
assert.True(t, first == netip.AddrFrom4([4]byte{192, 168, 0, 4}))
|
|
|
|
assert.True(t, pool.Lookup("foo.com") == netip.AddrFrom4([4]byte{192, 168, 0, 4}))
|
|
|
|
assert.True(t, last == netip.AddrFrom4([4]byte{192, 168, 0, 5}))
|
2021-10-11 20:48:58 +08:00
|
|
|
assert.True(t, exist)
|
|
|
|
assert.Equal(t, bar, "bar.com")
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.True(t, pool.Gateway() == netip.AddrFrom4([4]byte{192, 168, 0, 1}))
|
|
|
|
assert.True(t, pool.Broadcast() == netip.AddrFrom4([4]byte{192, 168, 0, 15}))
|
2021-10-11 20:48:58 +08:00
|
|
|
assert.Equal(t, pool.IPNet().String(), ipnet.String())
|
2022-10-09 11:07:24 +08:00
|
|
|
assert.True(t, pool.Exist(netip.AddrFrom4([4]byte{192, 168, 0, 5})))
|
|
|
|
assert.False(t, pool.Exist(netip.AddrFrom4([4]byte{192, 168, 0, 6})))
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.False(t, pool.Exist(netip.MustParseAddr("::1")))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPool_BasicV6(t *testing.T) {
|
|
|
|
ipnet := netip.MustParsePrefix("2001:4860:4860::8888/118")
|
|
|
|
pools, tempfile, err := createPools(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2022-04-12 00:31:04 +08:00
|
|
|
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)
|
|
|
|
|
2022-10-09 11:07:24 +08:00
|
|
|
assert.True(t, first == netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8804"))
|
|
|
|
assert.True(t, pool.Lookup("foo.com") == netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8804"))
|
|
|
|
assert.True(t, last == netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8805"))
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.True(t, exist)
|
|
|
|
assert.Equal(t, bar, "bar.com")
|
|
|
|
assert.True(t, pool.Gateway() == netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8801"))
|
|
|
|
assert.True(t, pool.Broadcast() == netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8bff"))
|
|
|
|
assert.Equal(t, pool.IPNet().String(), ipnet.String())
|
2022-10-09 11:07:24 +08:00
|
|
|
assert.True(t, pool.Exist(netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8805")))
|
|
|
|
assert.False(t, pool.Exist(netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8806")))
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.False(t, pool.Exist(netip.MustParseAddr("127.0.0.1")))
|
2021-10-11 20:48:58 +08:00
|
|
|
}
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|
|
|
|
|
2022-08-12 13:47:51 +08:00
|
|
|
func TestPool_Case_Insensitive(t *testing.T) {
|
2022-11-12 20:43:48 +08:00
|
|
|
ipnet := netip.MustParsePrefix("192.168.0.1/29")
|
2022-08-12 13:47:51 +08:00
|
|
|
pools, tempfile, err := createPools(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2022-08-12 13:47:51 +08:00
|
|
|
Size: 10,
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
defer os.Remove(tempfile)
|
|
|
|
|
|
|
|
for _, pool := range pools {
|
|
|
|
first := pool.Lookup("foo.com")
|
|
|
|
last := pool.Lookup("Foo.Com")
|
|
|
|
foo, exist := pool.LookBack(last)
|
|
|
|
|
2022-11-12 20:43:48 +08:00
|
|
|
assert.Equal(t, first, pool.Lookup("Foo.Com"))
|
2022-08-12 13:47:51 +08:00
|
|
|
assert.Equal(t, pool.Lookup("fOo.cOM"), first)
|
|
|
|
assert.True(t, exist)
|
|
|
|
assert.Equal(t, foo, "foo.com")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
func TestPool_CycleUsed(t *testing.T) {
|
2022-04-12 00:31:04 +08:00
|
|
|
ipnet := netip.MustParsePrefix("192.168.0.16/28")
|
2021-10-11 20:48:58 +08:00
|
|
|
pools, tempfile, err := createPools(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2021-10-11 20:48:58 +08:00
|
|
|
Size: 10,
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
defer os.Remove(tempfile)
|
|
|
|
|
|
|
|
for _, pool := range pools {
|
2021-11-23 22:01:49 +08:00
|
|
|
foo := pool.Lookup("foo.com")
|
|
|
|
bar := pool.Lookup("bar.com")
|
2022-10-09 11:07:24 +08:00
|
|
|
for i := 0; i < 9; i++ {
|
2021-11-23 22:01:49 +08:00
|
|
|
pool.Lookup(fmt.Sprintf("%d.com", i))
|
|
|
|
}
|
|
|
|
baz := pool.Lookup("baz.com")
|
|
|
|
next := pool.Lookup("foo.com")
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.True(t, foo == baz)
|
|
|
|
assert.True(t, next == bar)
|
2021-10-11 20:48:58 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-26 19:09:13 +08:00
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
func TestPool_Skip(t *testing.T) {
|
2022-04-12 00:31:04 +08:00
|
|
|
ipnet := netip.MustParsePrefix("192.168.0.1/29")
|
2022-11-05 13:08:50 +08:00
|
|
|
tree := trie.New[struct{}]()
|
2024-08-15 07:42:59 +08:00
|
|
|
assert.NoError(t, tree.Insert("example.com", struct{}{}))
|
|
|
|
assert.False(t, tree.IsEmpty())
|
2021-10-11 20:48:58 +08:00
|
|
|
pools, tempfile, err := createPools(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2021-10-11 20:48:58 +08:00
|
|
|
Size: 10,
|
2024-08-28 12:25:45 +08:00
|
|
|
Host: []C.DomainMatcher{tree.NewDomainSet()},
|
2021-10-11 20:48:58 +08:00
|
|
|
})
|
|
|
|
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"))
|
|
|
|
}
|
2019-07-26 19:09:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPool_MaxCacheSize(t *testing.T) {
|
2022-04-12 00:31:04 +08:00
|
|
|
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
2021-10-11 20:48:58 +08:00
|
|
|
pool, _ := New(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2021-10-11 20:48:58 +08:00
|
|
|
Size: 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
|
|
|
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.False(t, first == next)
|
2019-05-03 00:05:14 +08:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:01:16 +08:00
|
|
|
func TestPool_DoubleMapping(t *testing.T) {
|
2022-04-12 00:31:04 +08:00
|
|
|
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
2021-10-11 20:48:58 +08:00
|
|
|
pool, _ := New(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2021-10-11 20:48:58 +08:00
|
|
|
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)
|
|
|
|
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.False(t, bazIP == newBazIP)
|
2019-10-11 14:01:16 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 20:48:58 +08:00
|
|
|
func TestPool_Clone(t *testing.T) {
|
2022-04-12 00:31:04 +08:00
|
|
|
ipnet := netip.MustParsePrefix("192.168.0.1/24")
|
2021-10-11 20:48:58 +08:00
|
|
|
pool, _ := New(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2021-10-11 20:48:58 +08:00
|
|
|
Size: 2,
|
|
|
|
})
|
|
|
|
|
|
|
|
first := pool.Lookup("foo.com")
|
|
|
|
last := pool.Lookup("bar.com")
|
2022-10-09 11:07:24 +08:00
|
|
|
assert.True(t, first == netip.AddrFrom4([4]byte{192, 168, 0, 4}))
|
|
|
|
assert.True(t, last == netip.AddrFrom4([4]byte{192, 168, 0, 5}))
|
2021-10-11 20:48:58 +08:00
|
|
|
|
|
|
|
newPool, _ := New(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2021-10-11 20:48:58 +08:00
|
|
|
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) {
|
2022-04-12 00:31:04 +08:00
|
|
|
ipnet := netip.MustParsePrefix("192.168.0.1/31")
|
2021-10-11 20:48:58 +08:00
|
|
|
_, err := New(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2021-10-11 20:48:58 +08:00
|
|
|
Size: 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
|
|
|
}
|
2022-03-23 01:05:43 +08:00
|
|
|
|
|
|
|
func TestPool_FlushFileCache(t *testing.T) {
|
2022-04-12 00:31:04 +08:00
|
|
|
ipnet := netip.MustParsePrefix("192.168.0.1/28")
|
2022-03-23 01:05:43 +08:00
|
|
|
pools, tempfile, err := createPools(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2022-03-23 01:05:43 +08:00
|
|
|
Size: 10,
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
defer os.Remove(tempfile)
|
|
|
|
|
|
|
|
for _, pool := range pools {
|
|
|
|
foo := pool.Lookup("foo.com")
|
|
|
|
bar := pool.Lookup("baz.com")
|
|
|
|
bax := pool.Lookup("baz.com")
|
|
|
|
fox := pool.Lookup("foo.com")
|
|
|
|
|
|
|
|
err = pool.FlushFakeIP()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
next := pool.Lookup("baz.com")
|
2022-04-19 17:46:13 +08:00
|
|
|
baz := pool.Lookup("foo.com")
|
2022-03-23 01:05:43 +08:00
|
|
|
nero := pool.Lookup("foo.com")
|
|
|
|
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.True(t, foo == fox)
|
2022-04-19 17:46:13 +08:00
|
|
|
assert.True(t, foo == next)
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.False(t, foo == baz)
|
|
|
|
assert.True(t, bar == bax)
|
2022-04-19 17:46:13 +08:00
|
|
|
assert.True(t, bar == baz)
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.False(t, bar == next)
|
|
|
|
assert.True(t, baz == nero)
|
2022-03-23 01:05:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPool_FlushMemoryCache(t *testing.T) {
|
2022-04-12 00:31:04 +08:00
|
|
|
ipnet := netip.MustParsePrefix("192.168.0.1/28")
|
2022-03-23 01:05:43 +08:00
|
|
|
pool, _ := New(Options{
|
2023-10-26 10:39:54 +08:00
|
|
|
IPNet: ipnet,
|
2022-03-23 01:05:43 +08:00
|
|
|
Size: 10,
|
|
|
|
})
|
|
|
|
|
|
|
|
foo := pool.Lookup("foo.com")
|
|
|
|
bar := pool.Lookup("baz.com")
|
|
|
|
bax := pool.Lookup("baz.com")
|
|
|
|
fox := pool.Lookup("foo.com")
|
|
|
|
|
|
|
|
err := pool.FlushFakeIP()
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
next := pool.Lookup("baz.com")
|
2022-04-19 17:46:13 +08:00
|
|
|
baz := pool.Lookup("foo.com")
|
2022-03-23 01:05:43 +08:00
|
|
|
nero := pool.Lookup("foo.com")
|
|
|
|
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.True(t, foo == fox)
|
2022-04-19 17:46:13 +08:00
|
|
|
assert.True(t, foo == next)
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.False(t, foo == baz)
|
|
|
|
assert.True(t, bar == bax)
|
2022-04-19 17:46:13 +08:00
|
|
|
assert.True(t, bar == baz)
|
2022-04-12 00:31:04 +08:00
|
|
|
assert.False(t, bar == next)
|
|
|
|
assert.True(t, baz == nero)
|
2022-03-23 01:05:43 +08:00
|
|
|
}
|