github.com/imannamdari/v2ray-core/v5@v5.0.5/app/dns/fakedns/fakedns_test.go (about)

     1  package fakedns
     2  
     3  import (
     4  	gonet "net"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"golang.org/x/sync/errgroup"
    10  
    11  	"github.com/imannamdari/v2ray-core/v5/common"
    12  	"github.com/imannamdari/v2ray-core/v5/common/net"
    13  	"github.com/imannamdari/v2ray-core/v5/common/uuid"
    14  )
    15  
    16  func TestNewFakeDnsHolder(_ *testing.T) {
    17  	_, err := NewFakeDNSHolder()
    18  	common.Must(err)
    19  }
    20  
    21  func TestFakeDnsHolderCreateMapping(t *testing.T) {
    22  	fkdns, err := NewFakeDNSHolder()
    23  	common.Must(err)
    24  
    25  	addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    26  	assert.Equal(t, "198.18.0.0", addr[0].IP().String())
    27  }
    28  
    29  func TestFakeDnsHolderCreateMappingMany(t *testing.T) {
    30  	fkdns, err := NewFakeDNSHolder()
    31  	common.Must(err)
    32  
    33  	addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    34  	assert.Equal(t, "198.18.0.0", addr[0].IP().String())
    35  
    36  	addr2 := fkdns.GetFakeIPForDomain("fakednstest2.v2fly.org")
    37  	assert.Equal(t, "198.18.0.1", addr2[0].IP().String())
    38  }
    39  
    40  func TestFakeDnsHolderCreateMappingManyAndResolve(t *testing.T) {
    41  	fkdns, err := NewFakeDNSHolder()
    42  	common.Must(err)
    43  
    44  	{
    45  		addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    46  		assert.Equal(t, "198.18.0.0", addr[0].IP().String())
    47  	}
    48  
    49  	{
    50  		addr2 := fkdns.GetFakeIPForDomain("fakednstest2.v2fly.org")
    51  		assert.Equal(t, "198.18.0.1", addr2[0].IP().String())
    52  	}
    53  
    54  	{
    55  		result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("198.18.0.0"))
    56  		assert.Equal(t, "fakednstest.v2fly.org", result)
    57  	}
    58  
    59  	{
    60  		result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("198.18.0.1"))
    61  		assert.Equal(t, "fakednstest2.v2fly.org", result)
    62  	}
    63  }
    64  
    65  func TestFakeDnsHolderCreateMappingManySingleDomain(t *testing.T) {
    66  	fkdns, err := NewFakeDNSHolder()
    67  	common.Must(err)
    68  
    69  	addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    70  	assert.Equal(t, "198.18.0.0", addr[0].IP().String())
    71  
    72  	addr2 := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
    73  	assert.Equal(t, "198.18.0.0", addr2[0].IP().String())
    74  }
    75  
    76  func TestGetFakeIPForDomainConcurrently(t *testing.T) {
    77  	fkdns, err := NewFakeDNSHolder()
    78  	common.Must(err)
    79  
    80  	total := 200
    81  	addr := make([][]net.Address, total+1)
    82  	var errg errgroup.Group
    83  	for i := 0; i < total; i++ {
    84  		errg.Go(testGetFakeIP(i, addr, fkdns))
    85  	}
    86  	errg.Wait()
    87  	for i := 0; i < total; i++ {
    88  		for j := i + 1; j < total; j++ {
    89  			assert.NotEqual(t, addr[i][0].IP().String(), addr[j][0].IP().String())
    90  		}
    91  	}
    92  }
    93  
    94  func testGetFakeIP(index int, addr [][]net.Address, fkdns *Holder) func() error {
    95  	return func() error {
    96  		addr[index] = fkdns.GetFakeIPForDomain("fakednstest" + strconv.Itoa(index) + ".example.com")
    97  		return nil
    98  	}
    99  }
   100  
   101  func TestFakeDnsHolderCreateMappingAndRollOver(t *testing.T) {
   102  	fkdns, err := NewFakeDNSHolderConfigOnly(&FakeDnsPool{
   103  		IpPool:  "240.0.0.0/12",
   104  		LruSize: 256,
   105  	})
   106  	common.Must(err)
   107  
   108  	err = fkdns.Start()
   109  
   110  	common.Must(err)
   111  
   112  	{
   113  		addr := fkdns.GetFakeIPForDomain("fakednstest.v2fly.org")
   114  		assert.Equal(t, "240.0.0.0", addr[0].IP().String())
   115  	}
   116  
   117  	{
   118  		addr2 := fkdns.GetFakeIPForDomain("fakednstest2.v2fly.org")
   119  		assert.Equal(t, "240.0.0.1", addr2[0].IP().String())
   120  	}
   121  
   122  	for i := 0; i <= 8192; i++ {
   123  		{
   124  			result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("240.0.0.0"))
   125  			assert.Equal(t, "fakednstest.v2fly.org", result)
   126  		}
   127  
   128  		{
   129  			result := fkdns.GetDomainFromFakeDNS(net.ParseAddress("240.0.0.1"))
   130  			assert.Equal(t, "fakednstest2.v2fly.org", result)
   131  		}
   132  
   133  		{
   134  			uuid := uuid.New()
   135  			domain := uuid.String() + ".fakednstest.v2fly.org"
   136  			addr := fkdns.GetFakeIPForDomain(domain)
   137  			rsaddr := addr[0].IP().String()
   138  
   139  			result := fkdns.GetDomainFromFakeDNS(net.ParseAddress(rsaddr))
   140  			assert.Equal(t, domain, result)
   141  		}
   142  	}
   143  }
   144  
   145  func TestFakeDNSMulti(t *testing.T) {
   146  	fakeMulti, err := NewFakeDNSHolderMulti(&FakeDnsPoolMulti{
   147  		Pools: []*FakeDnsPool{{
   148  			IpPool:  "240.0.0.0/12",
   149  			LruSize: 256,
   150  		}, {
   151  			IpPool:  "fddd:c5b4:ff5f:f4f0::/64",
   152  			LruSize: 256,
   153  		}},
   154  	},
   155  	)
   156  	common.Must(err)
   157  
   158  	err = fakeMulti.Start()
   159  
   160  	common.Must(err)
   161  
   162  	assert.Nil(t, err, "Should not throw error")
   163  	_ = fakeMulti
   164  
   165  	t.Run("checkInRange", func(t *testing.T) {
   166  		t.Run("ipv4", func(t *testing.T) {
   167  			inPool := fakeMulti.IsIPInIPPool(net.IPAddress([]byte{240, 0, 0, 5}))
   168  			assert.True(t, inPool)
   169  		})
   170  		t.Run("ipv6", func(t *testing.T) {
   171  			ip, err := gonet.ResolveIPAddr("ip", "fddd:c5b4:ff5f:f4f0::5")
   172  			assert.Nil(t, err)
   173  			inPool := fakeMulti.IsIPInIPPool(net.IPAddress(ip.IP))
   174  			assert.True(t, inPool)
   175  		})
   176  		t.Run("ipv4_inverse", func(t *testing.T) {
   177  			inPool := fakeMulti.IsIPInIPPool(net.IPAddress([]byte{241, 0, 0, 5}))
   178  			assert.False(t, inPool)
   179  		})
   180  		t.Run("ipv6_inverse", func(t *testing.T) {
   181  			ip, err := gonet.ResolveIPAddr("ip", "fcdd:c5b4:ff5f:f4f0::5")
   182  			assert.Nil(t, err)
   183  			inPool := fakeMulti.IsIPInIPPool(net.IPAddress(ip.IP))
   184  			assert.False(t, inPool)
   185  		})
   186  	})
   187  
   188  	t.Run("allocateTwoAddressForTwoPool", func(t *testing.T) {
   189  		address := fakeMulti.GetFakeIPForDomain("fakednstest.v2fly.org")
   190  		assert.Len(t, address, 2, "should be 2 address one for each pool")
   191  		t.Run("eachOfThemShouldResolve:0", func(t *testing.T) {
   192  			domain := fakeMulti.GetDomainFromFakeDNS(address[0])
   193  			assert.Equal(t, "fakednstest.v2fly.org", domain)
   194  		})
   195  		t.Run("eachOfThemShouldResolve:1", func(t *testing.T) {
   196  			domain := fakeMulti.GetDomainFromFakeDNS(address[1])
   197  			assert.Equal(t, "fakednstest.v2fly.org", domain)
   198  		})
   199  	})
   200  
   201  	t.Run("understandIPTypeSelector", func(t *testing.T) {
   202  		t.Run("ipv4", func(t *testing.T) {
   203  			address := fakeMulti.GetFakeIPForDomain3("fakednstestipv4.v2fly.org", true, false)
   204  			assert.Len(t, address, 1, "should be 1 address")
   205  			assert.True(t, address[0].Family().IsIPv4())
   206  		})
   207  		t.Run("ipv6", func(t *testing.T) {
   208  			address := fakeMulti.GetFakeIPForDomain3("fakednstestipv6.v2fly.org", false, true)
   209  			assert.Len(t, address, 1, "should be 1 address")
   210  			assert.True(t, address[0].Family().IsIPv6())
   211  		})
   212  		t.Run("ipv46", func(t *testing.T) {
   213  			address := fakeMulti.GetFakeIPForDomain3("fakednstestipv46.v2fly.org", true, true)
   214  			assert.Len(t, address, 2, "should be 2 address")
   215  			assert.True(t, address[0].Family().IsIPv4())
   216  			assert.True(t, address[1].Family().IsIPv6())
   217  		})
   218  	})
   219  }
   220  
   221  func TestFakeDNSMultiAddPool(t *testing.T) {
   222  	runTest := func(runTestBeforeStart bool) {
   223  		fakeMulti, err := NewFakeDNSHolderMulti(&FakeDnsPoolMulti{
   224  			Pools: []*FakeDnsPool{{
   225  				IpPool:  "240.0.0.0/12",
   226  				LruSize: 256,
   227  			}, {
   228  				IpPool:  "fddd:c5b4:ff5f:f4f0::/64",
   229  				LruSize: 256,
   230  			}},
   231  		})
   232  		common.Must(err)
   233  		if !runTestBeforeStart {
   234  			err = fakeMulti.Start()
   235  			common.Must(err)
   236  		}
   237  		t.Run("ipv4_return_existing", func(t *testing.T) {
   238  			pool, err := fakeMulti.AddPool(&FakeDnsPool{
   239  				IpPool:  "240.0.0.1/12",
   240  				LruSize: 256,
   241  			})
   242  			common.Must(err)
   243  			if pool != fakeMulti.holders[0] {
   244  				t.Error("HolderMulti.AddPool not returning same holder for existing IPv4 pool")
   245  			}
   246  		})
   247  		t.Run("ipv6_return_existing", func(t *testing.T) {
   248  			pool, err := fakeMulti.AddPool(&FakeDnsPool{
   249  				IpPool:  "fddd:c5b4:ff5f:f4f0::1/64",
   250  				LruSize: 256,
   251  			})
   252  			common.Must(err)
   253  			if pool != fakeMulti.holders[1] {
   254  				t.Error("HolderMulti.AddPool not returning same holder for existing IPv6 pool")
   255  			}
   256  		})
   257  		t.Run("ipv4_reject_overlap", func(t *testing.T) {
   258  			_, err := fakeMulti.AddPool(&FakeDnsPool{
   259  				IpPool:  "240.8.0.0/13",
   260  				LruSize: 256,
   261  			})
   262  			if err == nil {
   263  				t.Error("HolderMulti.AddPool not rejecting IPv4 pool that is subnet of existing ones")
   264  			}
   265  			_, err = fakeMulti.AddPool(&FakeDnsPool{
   266  				IpPool:  "240.0.0.0/11",
   267  				LruSize: 256,
   268  			})
   269  			if err == nil {
   270  				t.Error("HolderMulti.AddPool not rejecting IPv4 pool that contains existing ones")
   271  			}
   272  		})
   273  		t.Run("new_pool", func(t *testing.T) {
   274  			pool, err := fakeMulti.AddPool(&FakeDnsPool{
   275  				IpPool:  "192.168.168.0/16",
   276  				LruSize: 256,
   277  			})
   278  			common.Must(err)
   279  			if pool != fakeMulti.holders[2] {
   280  				t.Error("HolderMulti.AddPool not creating new holder for new IPv4 pool")
   281  			}
   282  		})
   283  		t.Run("add_pool_multi", func(t *testing.T) {
   284  			pools, err := fakeMulti.AddPoolMulti(&FakeDnsPoolMulti{
   285  				Pools: []*FakeDnsPool{{
   286  					IpPool:  "192.168.168.0/16",
   287  					LruSize: 256,
   288  				}, {
   289  					IpPool:  "2001:1111::/64",
   290  					LruSize: 256,
   291  				}},
   292  			})
   293  			common.Must(err)
   294  			if len(pools.holders) != 2 {
   295  				t.Error("HolderMulti.AddPoolMutli not returning holderMulti that has the same length as passed PoolMulti config")
   296  			}
   297  			if pools.holders[0] != fakeMulti.holders[2] {
   298  				t.Error("HolderMulti.AddPoolMulti not returning same holder for existing IPv4 pool 192.168.168.0/16")
   299  			}
   300  			if pools.holders[1] != fakeMulti.holders[3] {
   301  				t.Error("HolderMulti.AddPoolMulti not creating new holder for new IPv6 pool 2001:1111::/64")
   302  			}
   303  		})
   304  		if runTestBeforeStart {
   305  			err = fakeMulti.Start()
   306  			common.Must(err)
   307  		}
   308  	}
   309  	t.Run("addPoolBeforeStart", func(t *testing.T) {
   310  		runTest(true)
   311  	})
   312  	t.Run("addPoolAfterStart", func(t *testing.T) {
   313  		runTest(false)
   314  	})
   315  }