Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
wwqgtxx committed Sep 6, 2024
1 parent 80a3920 commit 0f2d4c1
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 48 deletions.
23 changes: 2 additions & 21 deletions component/fakeip/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ func (p *Pool) LookBack(ip netip.Addr) (string, bool) {
p.mux.Lock()
defer p.mux.Unlock()

if !ip.Is4() {
return "", false
}

return p.store.GetByIP(ip)
}

Expand All @@ -92,10 +88,6 @@ func (p *Pool) Exist(ip netip.Addr) bool {
p.mux.Lock()
defer p.mux.Unlock()

if !ip.Is4() {
return false
}

return p.store.Exist(ip)
}

Expand Down Expand Up @@ -171,19 +163,6 @@ func (p *Pool) restoreState() {
}
}

func ipToUint(addr netip.Addr) uint32 {
ip := addr.AsSlice()
v := uint32(ip[0]) << 24
v += uint32(ip[1]) << 16
v += uint32(ip[2]) << 8
v += uint32(ip[3])
return v
}

func uintToIP(v uint32) netip.Addr {
return netip.AddrFrom4([4]byte{byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)})
}

type Options struct {
IPNet netip.Prefix
Host []C.DomainMatcher
Expand Down Expand Up @@ -229,5 +208,7 @@ func New(options Options) (*Pool, error) {
pool.store = newMemoryStore(options.Size)
}

pool.restoreState()

return pool, nil
}
84 changes: 58 additions & 26 deletions component/fakeip/pool_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fakeip

import (
"fmt"
"net/netip"
"os"
"testing"
Expand Down Expand Up @@ -49,7 +50,7 @@ func createCachefileStore(options Options) (*Pool, string, error) {
}

func TestPool_Basic(t *testing.T) {
ipnet := netip.MustParsePrefix("192.168.0.1/29")
ipnet := netip.MustParsePrefix("192.168.0.0/28")
pools, tempfile, err := createPools(Options{
IPNet: ipnet,
Size: 10,
Expand All @@ -68,13 +69,42 @@ func TestPool_Basic(t *testing.T) {
assert.True(t, exist)
assert.Equal(t, bar, "bar.com")
assert.Equal(t, pool.Gateway(), netip.AddrFrom4([4]byte{192, 168, 0, 1}))
assert.Equal(t, pool.Broadcast(), netip.AddrFrom4([4]byte{192, 168, 0, 15}))
assert.Equal(t, pool.IPNet().String(), ipnet.String())
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})))
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{
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.Equal(t, first, netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8804"))
assert.Equal(t, pool.Lookup("foo.com"), netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8804"))
assert.Equal(t, last, netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8805"))
assert.True(t, exist)
assert.Equal(t, bar, "bar.com")
assert.Equal(t, pool.Gateway(), netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8801"))
assert.Equal(t, pool.Broadcast(), netip.MustParseAddr("2001:4860:4860:0000:0000:0000:0000:8bff"))
assert.Equal(t, pool.IPNet().String(), ipnet.String())
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")))
assert.False(t, pool.Exist(netip.MustParseAddr("127.0.0.1")))
}
}

func TestPool_Case_Insensitive(t *testing.T) {
ipnet := netip.MustParsePrefix("192.168.0.1/29")
pools, tempfile, err := createPools(Options{
Expand All @@ -97,7 +127,7 @@ func TestPool_Case_Insensitive(t *testing.T) {
}

func TestPool_CycleUsed(t *testing.T) {
ipnet := netip.MustParsePrefix("192.168.0.1/29")
ipnet := netip.MustParsePrefix("192.168.0.16/28")
pools, tempfile, err := createPools(Options{
IPNet: ipnet,
Size: 10,
Expand All @@ -106,18 +136,20 @@ func TestPool_CycleUsed(t *testing.T) {
defer os.Remove(tempfile)

for _, pool := range pools {
assert.Equal(t, netip.AddrFrom4([4]byte{192, 168, 0, 4}), pool.Lookup("2.com"))
assert.Equal(t, netip.AddrFrom4([4]byte{192, 168, 0, 5}), pool.Lookup("3.com"))
assert.Equal(t, netip.AddrFrom4([4]byte{192, 168, 0, 6}), pool.Lookup("4.com"))
assert.Equal(t, netip.AddrFrom4([4]byte{192, 168, 0, 7}), pool.Lookup("5.com"))
assert.Equal(t, netip.AddrFrom4([4]byte{192, 168, 0, 8}), pool.Lookup("6.com"))
assert.Equal(t, netip.AddrFrom4([4]byte{192, 168, 0, 4}), pool.Lookup("12.com"))
assert.Equal(t, netip.AddrFrom4([4]byte{192, 168, 0, 5}), pool.Lookup("3.com"))
foo := pool.Lookup("foo.com")
bar := pool.Lookup("bar.com")
for i := 0; i < 9; i++ {
pool.Lookup(fmt.Sprintf("%d.com", i))
}
baz := pool.Lookup("baz.com")
next := pool.Lookup("foo.com")
assert.Equal(t, foo, baz)
assert.Equal(t, next, bar)
}
}

func TestPool_Skip(t *testing.T) {
ipnet := netip.MustParsePrefix("192.168.0.1/30")
ipnet := netip.MustParsePrefix("192.168.0.1/29")
tree := trie.New[struct{}]()
assert.NoError(t, tree.Insert("example.com", struct{}{}))
assert.False(t, tree.IsEmpty())
Expand Down Expand Up @@ -169,7 +201,7 @@ func TestPool_MaxCacheSize(t *testing.T) {
pool.Lookup("baz.com")
next := pool.Lookup("foo.com")

assert.False(t, first == next)
assert.NotEqual(t, first, next)
}

func TestPool_DoubleMapping(t *testing.T) {
Expand Down Expand Up @@ -199,7 +231,7 @@ func TestPool_DoubleMapping(t *testing.T) {
assert.False(t, bazExist)
assert.True(t, barExist)

assert.False(t, bazIP == newBazIP)
assert.NotEqual(t, bazIP, newBazIP)
}

func TestPool_Clone(t *testing.T) {
Expand Down Expand Up @@ -257,13 +289,13 @@ func TestPool_FlushFileCache(t *testing.T) {
baz := pool.Lookup("foo.com")
nero := pool.Lookup("foo.com")

assert.True(t, foo == fox)
assert.True(t, foo == next)
assert.False(t, foo == baz)
assert.True(t, bar == bax)
assert.True(t, bar == baz)
assert.False(t, bar == next)
assert.True(t, baz == nero)
assert.Equal(t, foo, fox)
assert.Equal(t, foo, next)
assert.NotEqual(t, foo, baz)
assert.Equal(t, bar, bax)
assert.Equal(t, bar, baz)
assert.NotEqual(t, bar, next)
assert.Equal(t, baz, nero)
}
}

Expand All @@ -286,11 +318,11 @@ func TestPool_FlushMemoryCache(t *testing.T) {
baz := pool.Lookup("foo.com")
nero := pool.Lookup("foo.com")

assert.True(t, foo == fox)
assert.True(t, foo == next)
assert.False(t, foo == baz)
assert.True(t, bar == bax)
assert.True(t, bar == baz)
assert.False(t, bar == next)
assert.True(t, baz == nero)
assert.Equal(t, foo, fox)
assert.Equal(t, foo, next)
assert.NotEqual(t, foo, baz)
assert.Equal(t, bar, bax)
assert.Equal(t, bar, baz)
assert.NotEqual(t, bar, next)
assert.Equal(t, baz, nero)
}
3 changes: 2 additions & 1 deletion component/trie/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ func TestTrie_Foreach(t *testing.T) {
assert.NoError(t, tree.Insert(domain, localIP))
}
count := 0
tree.Foreach(func(domain string, data netip.Addr) {
tree.Foreach(func(domain string, data netip.Addr) bool {
count++
return true
})
assert.Equal(t, 7, count)
}

0 comments on commit 0f2d4c1

Please sign in to comment.