github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/app/dns/nameserver_quic_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/xtls/xray-core/app/dns"
    11  	"github.com/xtls/xray-core/common"
    12  	"github.com/xtls/xray-core/common/net"
    13  	"github.com/xtls/xray-core/features/dns"
    14  )
    15  
    16  func TestQUICNameServer(t *testing.T) {
    17  	url, err := url.Parse("quic://dns.adguard.com")
    18  	common.Must(err)
    19  	s, err := NewQUICNameServer(url, QueryStrategy_USE_IP)
    20  	common.Must(err)
    21  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
    22  	ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns.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  	ctx2, cancel := context.WithTimeout(context.Background(), time.Second*5)
    33  	ips2, err := s.QueryIP(ctx2, "google.com", net.IP(nil), dns.IPOption{
    34  		IPv4Enable: true,
    35  		IPv6Enable: true,
    36  	}, true)
    37  	cancel()
    38  	common.Must(err)
    39  	if r := cmp.Diff(ips2, ips); r != "" {
    40  		t.Fatal(r)
    41  	}
    42  }
    43  
    44  func TestQUICNameServerWithIPv4Override(t *testing.T) {
    45  	url, err := url.Parse("quic://dns.adguard.com")
    46  	common.Must(err)
    47  	s, err := NewQUICNameServer(url, QueryStrategy_USE_IP4)
    48  	common.Must(err)
    49  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
    50  	ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns.IPOption{
    51  		IPv4Enable: true,
    52  		IPv6Enable: true,
    53  	}, false)
    54  	cancel()
    55  	common.Must(err)
    56  	if len(ips) == 0 {
    57  		t.Error("expect some ips, but got 0")
    58  	}
    59  
    60  	for _, ip := range ips {
    61  		if len(ip) != net.IPv4len {
    62  			t.Error("expect only IPv4 response from DNS query")
    63  		}
    64  	}
    65  }
    66  
    67  func TestQUICNameServerWithIPv6Override(t *testing.T) {
    68  	url, err := url.Parse("quic://dns.adguard.com")
    69  	common.Must(err)
    70  	s, err := NewQUICNameServer(url, QueryStrategy_USE_IP6)
    71  	common.Must(err)
    72  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
    73  	ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns.IPOption{
    74  		IPv4Enable: true,
    75  		IPv6Enable: true,
    76  	}, false)
    77  	cancel()
    78  	common.Must(err)
    79  	if len(ips) == 0 {
    80  		t.Error("expect some ips, but got 0")
    81  	}
    82  
    83  	for _, ip := range ips {
    84  		if len(ip) != net.IPv6len {
    85  			t.Error("expect only IPv6 response from DNS query")
    86  		}
    87  	}
    88  }