2021-03-07 07:39:50 +03:00
|
|
|
package fakedns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-10 17:55:51 +03:00
|
|
|
|
2021-03-07 07:39:50 +03:00
|
|
|
"github.com/xtls/xray-core/common"
|
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/common/uuid"
|
2021-03-14 18:58:27 +03:00
|
|
|
"github.com/xtls/xray-core/features/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ipPrefix = "198.18."
|
2021-03-07 07:39:50 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewFakeDnsHolder(_ *testing.T) {
|
|
|
|
_, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFakeDnsHolderCreateMapping(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-03-10 17:55:51 +03:00
|
|
|
addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
2021-03-14 18:58:27 +03:00
|
|
|
assert.Equal(t, ipPrefix, addr[0].IP().String()[0:len(ipPrefix)])
|
2021-03-07 07:39:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestFakeDnsHolderCreateMappingMany(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-03-10 17:55:51 +03:00
|
|
|
addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
2021-03-14 18:58:27 +03:00
|
|
|
assert.Equal(t, ipPrefix, addr[0].IP().String()[0:len(ipPrefix)])
|
2021-03-07 07:39:50 +03:00
|
|
|
|
2021-03-10 17:55:51 +03:00
|
|
|
addr2 := fkdns.GetFakeIPForDomain("fakednstest2.example.com")
|
2021-03-14 18:58:27 +03:00
|
|
|
assert.Equal(t, ipPrefix, addr2[0].IP().String()[0:len(ipPrefix)])
|
2021-03-07 07:39:50 +03:00
|
|
|
assert.NotEqual(t, addr[0].IP().String(), addr2[0].IP().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFakeDnsHolderCreateMappingManyAndResolve(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-03-10 17:55:51 +03:00
|
|
|
addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
|
|
|
addr2 := fkdns.GetFakeIPForDomain("fakednstest2.example.com")
|
2021-03-07 07:39:50 +03:00
|
|
|
|
|
|
|
{
|
|
|
|
result := fkdns.GetDomainFromFakeDNS(addr[0])
|
2021-03-10 17:55:51 +03:00
|
|
|
assert.Equal(t, "fakednstest.example.com", result)
|
2021-03-07 07:39:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
result := fkdns.GetDomainFromFakeDNS(addr2[0])
|
2021-03-10 17:55:51 +03:00
|
|
|
assert.Equal(t, "fakednstest2.example.com", result)
|
2021-03-07 07:39:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFakeDnsHolderCreateMappingManySingleDomain(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolder()
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-03-10 17:55:51 +03:00
|
|
|
addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
|
|
|
addr2 := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
2021-03-07 07:39:50 +03:00
|
|
|
assert.Equal(t, addr[0].IP().String(), addr2[0].IP().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFakeDnsHolderCreateMappingAndRollOver(t *testing.T) {
|
|
|
|
fkdns, err := NewFakeDNSHolderConfigOnly(&FakeDnsPool{
|
2021-03-14 18:58:27 +03:00
|
|
|
IpPool: dns.FakeIPPool,
|
2021-03-07 07:39:50 +03:00
|
|
|
LruSize: 256,
|
|
|
|
})
|
|
|
|
common.Must(err)
|
|
|
|
|
|
|
|
err = fkdns.Start()
|
|
|
|
|
|
|
|
common.Must(err)
|
|
|
|
|
2021-03-10 17:55:51 +03:00
|
|
|
addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
|
|
|
|
addr2 := fkdns.GetFakeIPForDomain("fakednstest2.example.com")
|
2021-03-07 07:39:50 +03:00
|
|
|
|
|
|
|
for i := 0; i <= 8192; i++ {
|
|
|
|
{
|
|
|
|
result := fkdns.GetDomainFromFakeDNS(addr[0])
|
2021-03-10 17:55:51 +03:00
|
|
|
assert.Equal(t, "fakednstest.example.com", result)
|
2021-03-07 07:39:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
result := fkdns.GetDomainFromFakeDNS(addr2[0])
|
2021-03-10 17:55:51 +03:00
|
|
|
assert.Equal(t, "fakednstest2.example.com", result)
|
2021-03-07 07:39:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
uuid := uuid.New()
|
2021-03-10 17:55:51 +03:00
|
|
|
domain := uuid.String() + ".fakednstest.example.com"
|
2021-03-07 07:39:50 +03:00
|
|
|
tempAddr := fkdns.GetFakeIPForDomain(domain)
|
|
|
|
rsaddr := tempAddr[0].IP().String()
|
|
|
|
|
|
|
|
result := fkdns.GetDomainFromFakeDNS(net.ParseAddress(rsaddr))
|
|
|
|
assert.Equal(t, domain, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|