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