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

     1  package dns_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/google/go-cmp/cmp"
     7  
     8  	. "github.com/imannamdari/v2ray-core/v5/app/dns"
     9  	"github.com/imannamdari/v2ray-core/v5/common"
    10  	"github.com/imannamdari/v2ray-core/v5/common/net"
    11  	"github.com/imannamdari/v2ray-core/v5/features/dns"
    12  )
    13  
    14  func TestStaticHosts(t *testing.T) {
    15  	pb := []*HostMapping{
    16  		{
    17  			Type:   DomainMatchingType_Full,
    18  			Domain: "v2fly.org",
    19  			Ip: [][]byte{
    20  				{1, 1, 1, 1},
    21  			},
    22  		},
    23  		{
    24  			Type:   DomainMatchingType_Full,
    25  			Domain: "proxy.v2fly.org",
    26  			Ip: [][]byte{
    27  				{1, 2, 3, 4},
    28  				{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    29  			},
    30  			ProxiedDomain: "another-proxy.v2fly.org",
    31  		},
    32  		{
    33  			Type:          DomainMatchingType_Full,
    34  			Domain:        "proxy2.v2fly.org",
    35  			ProxiedDomain: "proxy.v2fly.org",
    36  		},
    37  		{
    38  			Type:   DomainMatchingType_Subdomain,
    39  			Domain: "v2ray.cn",
    40  			Ip: [][]byte{
    41  				{2, 2, 2, 2},
    42  			},
    43  		},
    44  		{
    45  			Type:   DomainMatchingType_Subdomain,
    46  			Domain: "baidu.com",
    47  			Ip: [][]byte{
    48  				{127, 0, 0, 1},
    49  				{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
    50  			},
    51  		},
    52  	}
    53  
    54  	hosts, err := NewStaticHosts(pb, nil)
    55  	common.Must(err)
    56  
    57  	{
    58  		ips := hosts.Lookup("v2fly.org", dns.IPOption{
    59  			IPv4Enable: true,
    60  			IPv6Enable: true,
    61  		})
    62  		if len(ips) != 1 {
    63  			t.Error("expect 1 IP, but got ", len(ips))
    64  		}
    65  		if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
    66  			t.Error(diff)
    67  		}
    68  	}
    69  
    70  	{
    71  		domain := hosts.Lookup("proxy.v2fly.org", dns.IPOption{
    72  			IPv4Enable: true,
    73  			IPv6Enable: false,
    74  		})
    75  		if len(domain) != 1 {
    76  			t.Error("expect 1 domain, but got ", len(domain))
    77  		}
    78  		if diff := cmp.Diff(domain[0].Domain(), "another-proxy.v2fly.org"); diff != "" {
    79  			t.Error(diff)
    80  		}
    81  	}
    82  
    83  	{
    84  		domain := hosts.Lookup("proxy2.v2fly.org", dns.IPOption{
    85  			IPv4Enable: true,
    86  			IPv6Enable: false,
    87  		})
    88  		if len(domain) != 1 {
    89  			t.Error("expect 1 domain, but got ", len(domain))
    90  		}
    91  		if diff := cmp.Diff(domain[0].Domain(), "another-proxy.v2fly.org"); diff != "" {
    92  			t.Error(diff)
    93  		}
    94  	}
    95  
    96  	{
    97  		ips := hosts.Lookup("www.v2ray.cn", dns.IPOption{
    98  			IPv4Enable: true,
    99  			IPv6Enable: true,
   100  		})
   101  		if len(ips) != 1 {
   102  			t.Error("expect 1 IP, but got ", len(ips))
   103  		}
   104  		if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
   105  			t.Error(diff)
   106  		}
   107  	}
   108  
   109  	{
   110  		ips := hosts.Lookup("baidu.com", dns.IPOption{
   111  			IPv4Enable: false,
   112  			IPv6Enable: true,
   113  		})
   114  		if len(ips) != 1 {
   115  			t.Error("expect 1 IP, but got ", len(ips))
   116  		}
   117  		if diff := cmp.Diff([]byte(ips[0].IP()), []byte(net.LocalHostIPv6.IP())); diff != "" {
   118  			t.Error(diff)
   119  		}
   120  	}
   121  }