github.com/v2fly/v2ray-core/v4@v4.45.2/app/dns/fakedns/fakedns_test.go (about)

     1  package fakedns
     2  
     3  import (
     4  	gonet "net"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/v2fly/v2ray-core/v4/common"
    10  	"github.com/v2fly/v2ray-core/v4/common/net"
    11  	"github.com/v2fly/v2ray-core/v4/common/uuid"
    12  )
    13  
    14  func TestNewFakeDnsHolder(_ *testing.T) {
    15  	_, err := NewFakeDNSHolder()
    16  	common.Must(err)
    17  }
    18  
    19  func TestFakeDnsHolderCreateMapping(t *testing.T) {
    20  	fkdns, err := NewFakeDNSHolder()
    21  	common.Must(err)
    22  
    23  	addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    24  	assert.Equal(t, "198.18.0.0", addr[0].IP().String())
    25  }
    26  
    27  func TestFakeDnsHolderCreateMappingMany(t *testing.T) {
    28  	fkdns, err := NewFakeDNSHolder()
    29  	common.Must(err)
    30  
    31  	addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    32  	assert.Equal(t, "198.18.0.0", addr[0].IP().String())
    33  
    34  	addr2 := fkdns.GetFakeIPForDomain("fakednstest2.v2fly.org")
    35  	assert.Equal(t, "198.18.0.1", addr2[0].IP().String())
    36  }
    37  
    38  func TestFakeDnsHolderCreateMappingManyAndResolve(t *testing.T) {
    39  	fkdns, err := NewFakeDNSHolder()
    40  	common.Must(err)
    41  
    42  	{
    43  		addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    44  		assert.Equal(t, "198.18.0.0", addr[0].IP().String())
    45  	}
    46  
    47  	{
    48  		addr2 := fkdns.GetFakeIPForDomain("fakednstest2.v2fly.org")
    49  		assert.Equal(t, "198.18.0.1", addr2[0].IP().String())
    50  	}
    51  
    52  	{
    53  		result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("198.18.0.0"))
    54  		assert.Equal(t, "fakednstest.v2fly.org", result)
    55  	}
    56  
    57  	{
    58  		result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("198.18.0.1"))
    59  		assert.Equal(t, "fakednstest2.v2fly.org", result)
    60  	}
    61  }
    62  
    63  func TestFakeDnsHolderCreateMappingManySingleDomain(t *testing.T) {
    64  	fkdns, err := NewFakeDNSHolder()
    65  	common.Must(err)
    66  
    67  	addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    68  	assert.Equal(t, "198.18.0.0", addr[0].IP().String())
    69  
    70  	addr2 := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    71  	assert.Equal(t, "198.18.0.0", addr2[0].IP().String())
    72  }
    73  
    74  func TestFakeDnsHolderCreateMappingAndRollOver(t *testing.T) {
    75  	fkdns, err := NewFakeDNSHolderConfigOnly(&FakeDnsPool{
    76  		IpPool:  "240.0.0.0/12",
    77  		LruSize: 256,
    78  	})
    79  	common.Must(err)
    80  
    81  	err = fkdns.Start()
    82  
    83  	common.Must(err)
    84  
    85  	{
    86  		addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    87  		assert.Equal(t, "240.0.0.0", addr[0].IP().String())
    88  	}
    89  
    90  	{
    91  		addr2 := fkdns.GetFakeIPForDomain("fakednstest2.v2fly.org")
    92  		assert.Equal(t, "240.0.0.1", addr2[0].IP().String())
    93  	}
    94  
    95  	for i := 0; i <= 8192; i++ {
    96  		{
    97  			result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("240.0.0.0"))
    98  			assert.Equal(t, "fakednstest.v2fly.org", result)
    99  		}
   100  
   101  		{
   102  			result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("240.0.0.1"))
   103  			assert.Equal(t, "fakednstest2.v2fly.org", result)
   104  		}
   105  
   106  		{
   107  			uuid := uuid.New()
   108  			domain := uuid.String() + ".fakednstest.v2fly.org"
   109  			addr := fkdns.GetFakeIPForDomain(domain)
   110  			rsaddr := addr[0].IP().String()
   111  
   112  			result := fkdns.GetDomainFromFakeDNS(net.ParseAddress(rsaddr))
   113  			assert.Equal(t, domain, result)
   114  		}
   115  	}
   116  }
   117  
   118  func TestFakeDNSMulti(t *testing.T) {
   119  	fakeMulti, err := NewFakeDNSHolderMulti(&FakeDnsPoolMulti{
   120  		Pools: []*FakeDnsPool{{
   121  			IpPool:  "240.0.0.0/12",
   122  			LruSize: 256,
   123  		}, {
   124  			IpPool:  "fddd:c5b4:ff5f:f4f0::/64",
   125  			LruSize: 256,
   126  		}},
   127  	},
   128  	)
   129  	common.Must(err)
   130  
   131  	err = fakeMulti.Start()
   132  
   133  	common.Must(err)
   134  
   135  	assert.Nil(t, err, "Should not throw error")
   136  	_ = fakeMulti
   137  
   138  	t.Run("checkInRange", func(t *testing.T) {
   139  		t.Run("ipv4", func(t *testing.T) {
   140  			inPool := fakeMulti.IsIPInIPPool(net.IPAddress([]byte{240, 0, 0, 5}))
   141  			assert.True(t, inPool)
   142  		})
   143  		t.Run("ipv6", func(t *testing.T) {
   144  			ip, err := gonet.ResolveIPAddr("ip", "fddd:c5b4:ff5f:f4f0::5")
   145  			assert.Nil(t, err)
   146  			inPool := fakeMulti.IsIPInIPPool(net.IPAddress(ip.IP))
   147  			assert.True(t, inPool)
   148  		})
   149  		t.Run("ipv4_inverse", func(t *testing.T) {
   150  			inPool := fakeMulti.IsIPInIPPool(net.IPAddress([]byte{241, 0, 0, 5}))
   151  			assert.False(t, inPool)
   152  		})
   153  		t.Run("ipv6_inverse", func(t *testing.T) {
   154  			ip, err := gonet.ResolveIPAddr("ip", "fcdd:c5b4:ff5f:f4f0::5")
   155  			assert.Nil(t, err)
   156  			inPool := fakeMulti.IsIPInIPPool(net.IPAddress(ip.IP))
   157  			assert.False(t, inPool)
   158  		})
   159  	})
   160  
   161  	t.Run("allocateTwoAddressForTwoPool", func(t *testing.T) {
   162  		address := fakeMulti.GetFakeIPForDomain("fakednstest.v2fly.org")
   163  		assert.Len(t, address, 2, "should be 2 address one for each pool")
   164  		t.Run("eachOfThemShouldResolve:0", func(t *testing.T) {
   165  			domain := fakeMulti.GetDomainFromFakeDNS(address[0])
   166  			assert.Equal(t, "fakednstest.v2fly.org", domain)
   167  		})
   168  		t.Run("eachOfThemShouldResolve:1", func(t *testing.T) {
   169  			domain := fakeMulti.GetDomainFromFakeDNS(address[1])
   170  			assert.Equal(t, "fakednstest.v2fly.org", domain)
   171  		})
   172  	})
   173  
   174  	t.Run("understandIPTypeSelector", func(t *testing.T) {
   175  		t.Run("ipv4", func(t *testing.T) {
   176  			address := fakeMulti.GetFakeIPForDomain3("fakednstestipv4.v2fly.org", true, false)
   177  			assert.Len(t, address, 1, "should be 1 address")
   178  			assert.True(t, address[0].Family().IsIPv4())
   179  		})
   180  		t.Run("ipv6", func(t *testing.T) {
   181  			address := fakeMulti.GetFakeIPForDomain3("fakednstestipv6.v2fly.org", false, true)
   182  			assert.Len(t, address, 1, "should be 1 address")
   183  			assert.True(t, address[0].Family().IsIPv6())
   184  		})
   185  		t.Run("ipv46", func(t *testing.T) {
   186  			address := fakeMulti.GetFakeIPForDomain3("fakednstestipv46.v2fly.org", true, true)
   187  			assert.Len(t, address, 2, "should be 2 address")
   188  			assert.True(t, address[0].Family().IsIPv4())
   189  			assert.True(t, address[1].Family().IsIPv6())
   190  		})
   191  	})
   192  }