github.com/moqsien/xraycore@v1.8.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 "github.com/moqsien/xraycore/common" 10 "github.com/moqsien/xraycore/common/net" 11 "github.com/moqsien/xraycore/common/uuid" 12 "github.com/moqsien/xraycore/features/dns" 13 "golang.org/x/sync/errgroup" 14 ) 15 16 var ipPrefix = "198.1" 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 TestGetFakeIPForDomainConcurrently(t *testing.T) { 71 fkdns, err := NewFakeDNSHolder() 72 common.Must(err) 73 74 total := 200 75 addr := make([][]net.Address, total) 76 var errg errgroup.Group 77 for i := 0; i < total; i++ { 78 errg.Go(testGetFakeIP(i, addr, fkdns)) 79 } 80 errg.Wait() 81 for i := 0; i < total; i++ { 82 for j := i + 1; j < total; j++ { 83 assert.NotEqual(t, addr[i][0].IP().String(), addr[j][0].IP().String()) 84 } 85 } 86 } 87 88 func testGetFakeIP(index int, addr [][]net.Address, fkdns *Holder) func() error { 89 return func() error { 90 addr[index] = fkdns.GetFakeIPForDomain("fakednstest" + strconv.Itoa(index) + ".example.com") 91 return nil 92 } 93 } 94 95 func TestFakeDnsHolderCreateMappingAndRollOver(t *testing.T) { 96 fkdns, err := NewFakeDNSHolderConfigOnly(&FakeDnsPool{ 97 IpPool: dns.FakeIPv4Pool, 98 LruSize: 256, 99 }) 100 common.Must(err) 101 102 err = fkdns.Start() 103 104 common.Must(err) 105 106 addr := fkdns.GetFakeIPForDomain("fakednstest.example.com") 107 addr2 := fkdns.GetFakeIPForDomain("fakednstest2.example.com") 108 109 for i := 0; i <= 8192; i++ { 110 { 111 result := fkdns.GetDomainFromFakeDNS(addr[0]) 112 assert.Equal(t, "fakednstest.example.com", result) 113 } 114 115 { 116 result := fkdns.GetDomainFromFakeDNS(addr2[0]) 117 assert.Equal(t, "fakednstest2.example.com", result) 118 } 119 120 { 121 uuid := uuid.New() 122 domain := uuid.String() + ".fakednstest.example.com" 123 tempAddr := fkdns.GetFakeIPForDomain(domain) 124 rsaddr := tempAddr[0].IP().String() 125 126 result := fkdns.GetDomainFromFakeDNS(net.ParseAddress(rsaddr)) 127 assert.Equal(t, domain, result) 128 } 129 } 130 } 131 132 func TestFakeDNSMulti(t *testing.T) { 133 fakeMulti, err := NewFakeDNSHolderMulti(&FakeDnsPoolMulti{ 134 Pools: []*FakeDnsPool{{ 135 IpPool: "240.0.0.0/12", 136 LruSize: 256, 137 }, { 138 IpPool: "fddd:c5b4:ff5f:f4f0::/64", 139 LruSize: 256, 140 }}, 141 }, 142 ) 143 common.Must(err) 144 145 err = fakeMulti.Start() 146 147 common.Must(err) 148 149 assert.Nil(t, err, "Should not throw error") 150 _ = fakeMulti 151 152 t.Run("checkInRange", func(t *testing.T) { 153 t.Run("ipv4", func(t *testing.T) { 154 inPool := fakeMulti.IsIPInIPPool(net.IPAddress([]byte{240, 0, 0, 5})) 155 assert.True(t, inPool) 156 }) 157 t.Run("ipv6", func(t *testing.T) { 158 ip, err := gonet.ResolveIPAddr("ip", "fddd:c5b4:ff5f:f4f0::5") 159 assert.Nil(t, err) 160 inPool := fakeMulti.IsIPInIPPool(net.IPAddress(ip.IP)) 161 assert.True(t, inPool) 162 }) 163 t.Run("ipv4_inverse", func(t *testing.T) { 164 inPool := fakeMulti.IsIPInIPPool(net.IPAddress([]byte{241, 0, 0, 5})) 165 assert.False(t, inPool) 166 }) 167 t.Run("ipv6_inverse", func(t *testing.T) { 168 ip, err := gonet.ResolveIPAddr("ip", "fcdd:c5b4:ff5f:f4f0::5") 169 assert.Nil(t, err) 170 inPool := fakeMulti.IsIPInIPPool(net.IPAddress(ip.IP)) 171 assert.False(t, inPool) 172 }) 173 }) 174 175 t.Run("allocateTwoAddressForTwoPool", func(t *testing.T) { 176 address := fakeMulti.GetFakeIPForDomain("fakednstest.example.com") 177 assert.Len(t, address, 2, "should be 2 address one for each pool") 178 t.Run("eachOfThemShouldResolve:0", func(t *testing.T) { 179 domain := fakeMulti.GetDomainFromFakeDNS(address[0]) 180 assert.Equal(t, "fakednstest.example.com", domain) 181 }) 182 t.Run("eachOfThemShouldResolve:1", func(t *testing.T) { 183 domain := fakeMulti.GetDomainFromFakeDNS(address[1]) 184 assert.Equal(t, "fakednstest.example.com", domain) 185 }) 186 }) 187 188 t.Run("understandIPTypeSelector", func(t *testing.T) { 189 t.Run("ipv4", func(t *testing.T) { 190 address := fakeMulti.GetFakeIPForDomain3("fakednstestipv4.example.com", true, false) 191 assert.Len(t, address, 1, "should be 1 address") 192 assert.True(t, address[0].Family().IsIPv4()) 193 }) 194 t.Run("ipv6", func(t *testing.T) { 195 address := fakeMulti.GetFakeIPForDomain3("fakednstestipv6.example.com", false, true) 196 assert.Len(t, address, 1, "should be 1 address") 197 assert.True(t, address[0].Family().IsIPv6()) 198 }) 199 t.Run("ipv46", func(t *testing.T) { 200 address := fakeMulti.GetFakeIPForDomain3("fakednstestipv46.example.com", true, true) 201 assert.Len(t, address, 2, "should be 2 address") 202 assert.True(t, address[0].Family().IsIPv4()) 203 assert.True(t, address[1].Family().IsIPv6()) 204 }) 205 }) 206 }