diff --git a/common/dns/fakedns/cache.go b/common/cache/cache.go old mode 100644 new mode 100755 similarity index 96% rename from common/dns/fakedns/cache.go rename to common/cache/cache.go index a68f5be..6b252c2 --- a/common/dns/fakedns/cache.go +++ b/common/cache/cache.go @@ -1,4 +1,4 @@ -package fakedns +package cache import ( "runtime" @@ -93,7 +93,7 @@ func stopJanitor(c *Cache) { } // New return *Cache -func NewCache(interval time.Duration) *Cache { +func New(interval time.Duration) *Cache { j := &janitor{ interval: interval, stop: make(chan struct{}), diff --git a/common/cache/cache_test.go b/common/cache/cache_test.go new file mode 100755 index 0000000..101ca86 --- /dev/null +++ b/common/cache/cache_test.go @@ -0,0 +1,70 @@ +package cache + +import ( + "runtime" + "testing" + "time" +) + +func TestCache_Basic(t *testing.T) { + interval := 200 * time.Millisecond + ttl := 20 * time.Millisecond + c := New(interval) + c.Put("int", 1, ttl) + c.Put("string", "a", ttl) + + i := c.Get("int") + if i.(int) != 1 { + t.Error("should recv 1") + } + + s := c.Get("string") + if s.(string) != "a" { + t.Error("should recv 'a'") + } +} + +func TestCache_TTL(t *testing.T) { + interval := 200 * time.Millisecond + ttl := 20 * time.Millisecond + c := New(interval) + c.Put("int", 1, ttl) + + i := c.Get("int") + if i.(int) != 1 { + t.Error("should recv 1") + } + + time.Sleep(ttl * 2) + i = c.Get("int") + if i != nil { + t.Error("should recv nil") + } +} + +func TestCache_AutoCleanup(t *testing.T) { + interval := 10 * time.Millisecond + ttl := 15 * time.Millisecond + c := New(interval) + c.Put("int", 1, ttl) + + time.Sleep(ttl * 2) + i := c.Get("int") + if i != nil { + t.Error("should recv nil") + } +} + +func TestCache_AutoGC(t *testing.T) { + sign := make(chan struct{}) + go func() { + interval := 10 * time.Millisecond + ttl := 15 * time.Millisecond + c := New(interval) + c.Put("int", 1, ttl) + sign <- struct{}{} + }() + + <-sign + runtime.GC() +} diff --git a/common/dns/domain-trie/node.go b/common/domain-trie/node.go similarity index 100% rename from common/dns/domain-trie/node.go rename to common/domain-trie/node.go diff --git a/common/dns/domain-trie/tire.go b/common/domain-trie/tire.go similarity index 100% rename from common/dns/domain-trie/tire.go rename to common/domain-trie/tire.go diff --git a/common/domain-trie/trie_test.go b/common/domain-trie/trie_test.go new file mode 100755 index 0000000..5303596 --- /dev/null +++ b/common/domain-trie/trie_test.go @@ -0,0 +1,83 @@ +package trie + +import ( + "net" + "testing" +) + +var localIP = net.IP{127, 0, 0, 1} + +func TestTrie_Basic(t *testing.T) { + tree := New() + domains := []string{ + "example.com", + "google.com", + } + + for _, domain := range domains { + tree.Insert(domain, localIP) + } + + node := tree.Search("example.com") + if node == nil { + t.Error("should not recv nil") + } + + if !node.Data.(net.IP).Equal(localIP) { + t.Error("should equal 127.0.0.1") + } +} + +func TestTrie_Wildcard(t *testing.T) { + tree := New() + domains := []string{ + "*.example.com", + "sub.*.example.com", + "*.dev", + } + + for _, domain := range domains { + tree.Insert(domain, localIP) + } + + if tree.Search("sub.example.com") == nil { + t.Error("should not recv nil") + } + + if tree.Search("sub.foo.example.com") == nil { + t.Error("should not recv nil") + } + + if tree.Search("foo.sub.example.com") != nil { + t.Error("should recv nil") + } + + if tree.Search("foo.example.dev") != nil { + t.Error("should recv nil") + } + + if tree.Search("example.com") != nil { + t.Error("should recv nil") + } +} + +func TestTrie_Boundary(t *testing.T) { + tree := New() + tree.Insert("*.dev", localIP) + + if err := tree.Insert(".", localIP); err == nil { + t.Error("should recv err") + } + + if err := tree.Insert(".com", localIP); err == nil { + t.Error("should recv err") + } + + if tree.Search("dev") != nil { + t.Error("should recv nil") + } + + if tree.Search(".dev") != nil { + t.Error("should recv nil") + } +} diff --git a/common/dns/fakedns/pool.go b/common/fakeip/pool.go old mode 100644 new mode 100755 similarity index 93% rename from common/dns/fakedns/pool.go rename to common/fakeip/pool.go index aea9ec8..32d5d57 --- a/common/dns/fakedns/pool.go +++ b/common/fakeip/pool.go @@ -1,4 +1,4 @@ -package fakedns +package fakeip import ( "errors" @@ -36,7 +36,7 @@ func uintToIP(v uint32) net.IP { } // New return Pool instance -func NewPool(ipnet *net.IPNet) (*Pool, error) { +func New(ipnet *net.IPNet) (*Pool, error) { min := ipToUint(ipnet.IP) + 1 ones, bits := ipnet.Mask.Size() diff --git a/common/fakeip/pool_test.go b/common/fakeip/pool_test.go new file mode 100755 index 0000000..ee816c6 --- /dev/null +++ b/common/fakeip/pool_test.go @@ -0,0 +1,44 @@ +package fakeip + +import ( + "net" + "testing" +) + +func TestPool_Basic(t *testing.T) { + _, ipnet, _ := net.ParseCIDR("192.168.0.1/30") + pool, _ := New(ipnet) + + first := pool.Get() + last := pool.Get() + + if !first.Equal(net.IP{192, 168, 0, 1}) { + t.Error("should get right first ip, instead of", first.String()) + } + + if !last.Equal(net.IP{192, 168, 0, 2}) { + t.Error("should get right last ip, instead of", first.String()) + } +} + +func TestPool_Cycle(t *testing.T) { + _, ipnet, _ := net.ParseCIDR("192.168.0.1/30") + pool, _ := New(ipnet) + + first := pool.Get() + pool.Get() + same := pool.Get() + + if !first.Equal(same) { + t.Error("should return same ip", first.String()) + } +} + +func TestPool_Error(t *testing.T) { + _, ipnet, _ := net.ParseCIDR("192.168.0.1/31") + _, err := New(ipnet) + + if err == nil { + t.Error("should return err") + } +}