1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 14:26:26 -05:00
v2fly/app/dns/fakedns/fakedns_test.go
2021-02-17 04:31:50 +08:00

116 lines
2.8 KiB
Go

package fakedns
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/net"
"github.com/v2fly/v2ray-core/v4/common/uuid"
)
func TestNewFakeDnsHolder(_ *testing.T) {
_, err := NewFakeDNSHolder()
common.Must(err)
}
func TestFakeDnsHolderCreateMapping(t *testing.T) {
fkdns, err := NewFakeDNSHolder()
common.Must(err)
addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
assert.Equal(t, "240.0.0.0", addr[0].IP().String())
}
func TestFakeDnsHolderCreateMappingMany(t *testing.T) {
fkdns, err := NewFakeDNSHolder()
common.Must(err)
addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
assert.Equal(t, "240.0.0.0", addr[0].IP().String())
addr2 := fkdns.GetFakeIPForDomain("fakednstest2.v2fly.org")
assert.Equal(t, "240.0.0.1", addr2[0].IP().String())
}
func TestFakeDnsHolderCreateMappingManyAndResolve(t *testing.T) {
fkdns, err := NewFakeDNSHolder()
common.Must(err)
{
addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
assert.Equal(t, "240.0.0.0", addr[0].IP().String())
}
{
addr2 := fkdns.GetFakeIPForDomain("fakednstest2.v2fly.org")
assert.Equal(t, "240.0.0.1", addr2[0].IP().String())
}
{
result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("240.0.0.0"))
assert.Equal(t, "fakednstest.v2fly.org", result)
}
{
result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("240.0.0.1"))
assert.Equal(t, "fakednstest2.v2fly.org", result)
}
}
func TestFakeDnsHolderCreateMappingManySingleDomain(t *testing.T) {
fkdns, err := NewFakeDNSHolder()
common.Must(err)
addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
assert.Equal(t, "240.0.0.0", addr[0].IP().String())
addr2 := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
assert.Equal(t, "240.0.0.0", addr2[0].IP().String())
}
func TestFakeDnsHolderCreateMappingAndRollOver(t *testing.T) {
fkdns, err := NewFakeDNSHolderConfigOnly(&FakeDnsPool{
IpPool: "240.0.0.0/12",
LruSize: 256,
})
common.Must(err)
err = fkdns.Start()
common.Must(err)
{
addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
assert.Equal(t, "240.0.0.0", addr[0].IP().String())
}
{
addr2 := fkdns.GetFakeIPForDomain("fakednstest2.v2fly.org")
assert.Equal(t, "240.0.0.1", addr2[0].IP().String())
}
for i := 0; i <= 8192; i++ {
{
result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("240.0.0.0"))
assert.Equal(t, "fakednstest.v2fly.org", result)
}
{
result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("240.0.0.1"))
assert.Equal(t, "fakednstest2.v2fly.org", result)
}
{
uuid := uuid.New()
domain := uuid.String() + ".fakednstest.v2fly.org"
addr := fkdns.GetFakeIPForDomain(domain)
rsaddr := addr[0].IP().String()
result := fkdns.GetDomainFromFakeDNS(net.ParseAddress(rsaddr))
assert.Equal(t, domain, result)
}
}
}