github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/dns/dns_test.go (about)

     1  package dns
     2  
     3  import (
     4  	"context"
     5  	"net/netip"
     6  	"testing"
     7  
     8  	"github.com/database64128/shadowsocks-go/conn"
     9  	"github.com/database64128/shadowsocks-go/direct"
    10  	"github.com/database64128/shadowsocks-go/zerocopy"
    11  	"go.uber.org/zap"
    12  	"go.uber.org/zap/zaptest"
    13  )
    14  
    15  func testResolver(t *testing.T, ctx context.Context, name string, serverAddrPort netip.AddrPort, tcpClient zerocopy.TCPClient, udpClient zerocopy.UDPClient, logger *zap.Logger) {
    16  	r := NewResolver(name, serverAddrPort, tcpClient, udpClient, logger)
    17  
    18  	// Uncached lookup.
    19  	uncachedResult, err := r.Lookup(ctx, "example.com")
    20  	if err != nil {
    21  		t.Fatal(err)
    22  	}
    23  	if len(uncachedResult.IPv4) == 0 {
    24  		t.Error("Expected at least one IPv4 address")
    25  	}
    26  	if len(uncachedResult.IPv6) == 0 {
    27  		t.Error("Expected at least one IPv6 address")
    28  	}
    29  
    30  	// Cached lookup.
    31  	cachedResult, err := r.Lookup(ctx, "example.com")
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  
    36  	if uncachedResult.TTL != cachedResult.TTL {
    37  		t.Error("TTL mismatch")
    38  	}
    39  }
    40  
    41  func TestResolver(t *testing.T) {
    42  	logger := zaptest.NewLogger(t)
    43  	defer logger.Sync()
    44  
    45  	ctx := context.Background()
    46  	serverAddrPort := netip.AddrPortFrom(netip.AddrFrom4([4]byte{1, 1, 1, 1}), 53)
    47  	tcpClient := direct.NewTCPClient("direct", "tcp", conn.DefaultTCPDialer)
    48  	udpClient := direct.NewDirectUDPClient("direct", "ip", 1500, conn.DefaultUDPClientListenConfig)
    49  
    50  	t.Run("UDP", func(t *testing.T) {
    51  		testResolver(t, ctx, "UDP", serverAddrPort, nil, udpClient, logger)
    52  	})
    53  
    54  	t.Run("TCP", func(t *testing.T) {
    55  		testResolver(t, ctx, "TCP", serverAddrPort, tcpClient, nil, logger)
    56  	})
    57  }