github.com/xmplusdev/xray-core@v1.8.10/app/dns/nameserver_tcp_test.go (about)

     1  package dns_test
     2  
     3  import (
     4  	"context"
     5  	"net/url"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/google/go-cmp/cmp"
    10  	. "github.com/xmplusdev/xray-core/app/dns"
    11  	"github.com/xmplusdev/xray-core/common"
    12  	"github.com/xmplusdev/xray-core/common/net"
    13  	dns_feature "github.com/xmplusdev/xray-core/features/dns"
    14  )
    15  
    16  func TestTCPLocalNameServer(t *testing.T) {
    17  	url, err := url.Parse("tcp+local://8.8.8.8")
    18  	common.Must(err)
    19  	s, err := NewTCPLocalNameServer(url, QueryStrategy_USE_IP)
    20  	common.Must(err)
    21  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    22  	ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
    23  		IPv4Enable: true,
    24  		IPv6Enable: true,
    25  	}, false)
    26  	cancel()
    27  	common.Must(err)
    28  	if len(ips) == 0 {
    29  		t.Error("expect some ips, but got 0")
    30  	}
    31  }
    32  
    33  func TestTCPLocalNameServerWithCache(t *testing.T) {
    34  	url, err := url.Parse("tcp+local://8.8.8.8")
    35  	common.Must(err)
    36  	s, err := NewTCPLocalNameServer(url, QueryStrategy_USE_IP)
    37  	common.Must(err)
    38  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    39  	ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
    40  		IPv4Enable: true,
    41  		IPv6Enable: true,
    42  	}, false)
    43  	cancel()
    44  	common.Must(err)
    45  	if len(ips) == 0 {
    46  		t.Error("expect some ips, but got 0")
    47  	}
    48  
    49  	ctx2, cancel := context.WithTimeout(context.Background(), time.Second*5)
    50  	ips2, err := s.QueryIP(ctx2, "google.com", net.IP(nil), dns_feature.IPOption{
    51  		IPv4Enable: true,
    52  		IPv6Enable: true,
    53  	}, true)
    54  	cancel()
    55  	common.Must(err)
    56  	if r := cmp.Diff(ips2, ips); r != "" {
    57  		t.Fatal(r)
    58  	}
    59  }
    60  
    61  func TestTCPLocalNameServerWithIPv4Override(t *testing.T) {
    62  	url, err := url.Parse("tcp+local://8.8.8.8")
    63  	common.Must(err)
    64  	s, err := NewTCPLocalNameServer(url, QueryStrategy_USE_IP4)
    65  	common.Must(err)
    66  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    67  	ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
    68  		IPv4Enable: true,
    69  		IPv6Enable: true,
    70  	}, false)
    71  	cancel()
    72  	common.Must(err)
    73  
    74  	if len(ips) == 0 {
    75  		t.Error("expect some ips, but got 0")
    76  	}
    77  
    78  	for _, ip := range ips {
    79  		if len(ip) != net.IPv4len {
    80  			t.Error("expect only IPv4 response from DNS query")
    81  		}
    82  	}
    83  }
    84  
    85  func TestTCPLocalNameServerWithIPv6Override(t *testing.T) {
    86  	url, err := url.Parse("tcp+local://8.8.8.8")
    87  	common.Must(err)
    88  	s, err := NewTCPLocalNameServer(url, QueryStrategy_USE_IP6)
    89  	common.Must(err)
    90  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
    91  	ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
    92  		IPv4Enable: true,
    93  		IPv6Enable: true,
    94  	}, false)
    95  	cancel()
    96  	common.Must(err)
    97  
    98  	if len(ips) == 0 {
    99  		t.Error("expect some ips, but got 0")
   100  	}
   101  
   102  	for _, ip := range ips {
   103  		if len(ip) != net.IPv6len {
   104  			t.Error("expect only IPv6 response from DNS query")
   105  		}
   106  	}
   107  }