github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/app/dns/hosts_test.go (about)

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