github.com/eagleql/xray-core@v1.4.4/app/dns/fakedns/fakedns_test.go (about)

     1  package fakedns
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/eagleql/xray-core/common"
     9  	"github.com/eagleql/xray-core/common/net"
    10  	"github.com/eagleql/xray-core/common/uuid"
    11  	"github.com/eagleql/xray-core/features/dns"
    12  )
    13  
    14  var (
    15  	ipPrefix = "198.18."
    16  )
    17  
    18  func TestNewFakeDnsHolder(_ *testing.T) {
    19  	_, err := NewFakeDNSHolder()
    20  	common.Must(err)
    21  }
    22  
    23  func TestFakeDnsHolderCreateMapping(t *testing.T) {
    24  	fkdns, err := NewFakeDNSHolder()
    25  	common.Must(err)
    26  
    27  	addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
    28  	assert.Equal(t, ipPrefix, addr[0].IP().String()[0:len(ipPrefix)])
    29  }
    30  
    31  func TestFakeDnsHolderCreateMappingMany(t *testing.T) {
    32  	fkdns, err := NewFakeDNSHolder()
    33  	common.Must(err)
    34  
    35  	addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
    36  	assert.Equal(t, ipPrefix, addr[0].IP().String()[0:len(ipPrefix)])
    37  
    38  	addr2 := fkdns.GetFakeIPForDomain("fakednstest2.example.com")
    39  	assert.Equal(t, ipPrefix, addr2[0].IP().String()[0:len(ipPrefix)])
    40  	assert.NotEqual(t, addr[0].IP().String(), addr2[0].IP().String())
    41  }
    42  
    43  func TestFakeDnsHolderCreateMappingManyAndResolve(t *testing.T) {
    44  	fkdns, err := NewFakeDNSHolder()
    45  	common.Must(err)
    46  
    47  	addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
    48  	addr2 := fkdns.GetFakeIPForDomain("fakednstest2.example.com")
    49  
    50  	{
    51  		result := fkdns.GetDomainFromFakeDNS(addr[0])
    52  		assert.Equal(t, "fakednstest.example.com", result)
    53  	}
    54  
    55  	{
    56  		result := fkdns.GetDomainFromFakeDNS(addr2[0])
    57  		assert.Equal(t, "fakednstest2.example.com", result)
    58  	}
    59  }
    60  
    61  func TestFakeDnsHolderCreateMappingManySingleDomain(t *testing.T) {
    62  	fkdns, err := NewFakeDNSHolder()
    63  	common.Must(err)
    64  
    65  	addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
    66  	addr2 := fkdns.GetFakeIPForDomain("fakednstest.example.com")
    67  	assert.Equal(t, addr[0].IP().String(), addr2[0].IP().String())
    68  }
    69  
    70  func TestFakeDnsHolderCreateMappingAndRollOver(t *testing.T) {
    71  	fkdns, err := NewFakeDNSHolderConfigOnly(&FakeDnsPool{
    72  		IpPool:  dns.FakeIPPool,
    73  		LruSize: 256,
    74  	})
    75  	common.Must(err)
    76  
    77  	err = fkdns.Start()
    78  
    79  	common.Must(err)
    80  
    81  	addr := fkdns.GetFakeIPForDomain("fakednstest.example.com")
    82  	addr2 := fkdns.GetFakeIPForDomain("fakednstest2.example.com")
    83  
    84  	for i := 0; i <= 8192; i++ {
    85  		{
    86  			result := fkdns.GetDomainFromFakeDNS(addr[0])
    87  			assert.Equal(t, "fakednstest.example.com", result)
    88  		}
    89  
    90  		{
    91  			result := fkdns.GetDomainFromFakeDNS(addr2[0])
    92  			assert.Equal(t, "fakednstest2.example.com", result)
    93  		}
    94  
    95  		{
    96  			uuid := uuid.New()
    97  			domain := uuid.String() + ".fakednstest.example.com"
    98  			tempAddr := fkdns.GetFakeIPForDomain(domain)
    99  			rsaddr := tempAddr[0].IP().String()
   100  
   101  			result := fkdns.GetDomainFromFakeDNS(net.ParseAddress(rsaddr))
   102  			assert.Equal(t, domain, result)
   103  		}
   104  	}
   105  }