github.com/Uhtred009/v2ray-core-1@v4.31.2+incompatible/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  	. "v2ray.com/core/app/dns"
     9  	"v2ray.com/core/common"
    10  	"v2ray.com/core/common/net"
    11  )
    12  
    13  func TestStaticHosts(t *testing.T) {
    14  	pb := []*Config_HostMapping{
    15  		{
    16  			Type:   DomainMatchingType_Full,
    17  			Domain: "v2ray.com",
    18  			Ip: [][]byte{
    19  				{1, 1, 1, 1},
    20  			},
    21  		},
    22  		{
    23  			Type:   DomainMatchingType_Subdomain,
    24  			Domain: "v2ray.cn",
    25  			Ip: [][]byte{
    26  				{2, 2, 2, 2},
    27  			},
    28  		},
    29  		{
    30  			Type:   DomainMatchingType_Subdomain,
    31  			Domain: "baidu.com",
    32  			Ip: [][]byte{
    33  				{127, 0, 0, 1},
    34  			},
    35  		},
    36  	}
    37  
    38  	hosts, err := NewStaticHosts(pb, nil)
    39  	common.Must(err)
    40  
    41  	{
    42  		ips := hosts.LookupIP("v2ray.com", IPOption{
    43  			IPv4Enable: true,
    44  			IPv6Enable: true,
    45  		})
    46  		if len(ips) != 1 {
    47  			t.Error("expect 1 IP, but got ", len(ips))
    48  		}
    49  		if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
    50  			t.Error(diff)
    51  		}
    52  	}
    53  
    54  	{
    55  		ips := hosts.LookupIP("www.v2ray.cn", IPOption{
    56  			IPv4Enable: true,
    57  			IPv6Enable: true,
    58  		})
    59  		if len(ips) != 1 {
    60  			t.Error("expect 1 IP, but got ", len(ips))
    61  		}
    62  		if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
    63  			t.Error(diff)
    64  		}
    65  	}
    66  
    67  	{
    68  		ips := hosts.LookupIP("baidu.com", IPOption{
    69  			IPv4Enable: false,
    70  			IPv6Enable: true,
    71  		})
    72  		if len(ips) != 1 {
    73  			t.Error("expect 1 IP, but got ", len(ips))
    74  		}
    75  		if diff := cmp.Diff([]byte(ips[0].IP()), []byte(net.LocalHostIPv6.IP())); diff != "" {
    76  			t.Error(diff)
    77  		}
    78  	}
    79  }