github.com/fumiama/terasu@v0.0.0-20240507144117-547a591149c0/dns/dns_test.go (about)

     1  package dns
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"fmt"
     7  	"net"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/fumiama/terasu"
    12  	"github.com/fumiama/terasu/ip"
    13  )
    14  
    15  func TestResolver(t *testing.T) {
    16  	t.Log("IsIPv6Available:", ip.IsIPv6Available.Get())
    17  	addrs, err := DefaultResolver.LookupHost(context.TODO(), "huggingface.co")
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	t.Log(addrs)
    22  	if len(addrs) == 0 {
    23  		t.Fail()
    24  	}
    25  }
    26  
    27  func TestResolverFallback(t *testing.T) {
    28  	t.Log("IsIPv6Available:", ip.IsIPv6Available.Get())
    29  
    30  	if ip.IsIPv6Available.Get() {
    31  		addrs, err := IPv6Servers.LookupHostFallback(context.TODO(), "huggingface.co")
    32  		if err != nil {
    33  			t.Fatal(err)
    34  		}
    35  		t.Log(addrs)
    36  		if len(addrs) == 0 {
    37  			t.Fail()
    38  		}
    39  	}
    40  	addrs, err := IPv4Servers.LookupHostFallback(context.TODO(), "huggingface.co")
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  	t.Log(addrs)
    45  	if len(addrs) == 0 {
    46  		t.Fail()
    47  	}
    48  }
    49  
    50  func TestDNS(t *testing.T) {
    51  	if ip.IsIPv6Available.Get() {
    52  		IPv6Servers.test()
    53  	}
    54  	IPv4Servers.test()
    55  	for i := 0; i < 10; i++ {
    56  		addrs, err := DefaultResolver.LookupHost(context.TODO(), "huggingface.co")
    57  		if err != nil {
    58  			t.Fatal(err)
    59  		}
    60  		t.Log(addrs)
    61  		if len(addrs) == 0 {
    62  			t.Fail()
    63  		}
    64  		time.Sleep(time.Millisecond * 50)
    65  	}
    66  }
    67  
    68  func TestBadDNS(t *testing.T) {
    69  	dotv6serversbak := IPv6Servers.m
    70  	dotv4serversbak := IPv4Servers.m
    71  	defer func() {
    72  		IPv6Servers.m = dotv6serversbak
    73  		IPv4Servers.m = dotv4serversbak
    74  	}()
    75  	if ip.IsIPv6Available.Get() {
    76  		IPv6Servers = DNSList{
    77  			m: map[string][]*dnsstat{},
    78  		}
    79  		IPv6Servers.Add(&DNSConfig{
    80  			Servers: map[string][]string{"test.bad.host": {"169.254.122.111"}},
    81  		})
    82  	} else {
    83  		IPv4Servers = DNSList{
    84  			m: map[string][]*dnsstat{},
    85  		}
    86  		IPv4Servers.Add(&DNSConfig{
    87  			Servers: map[string][]string{"test.bad.host": {"169.254.122.111:853"}},
    88  		})
    89  	}
    90  	for i := 0; i < 10; i++ {
    91  		addrs, err := DefaultResolver.LookupHost(context.TODO(), "api.mangacopy.com")
    92  		t.Log(err)
    93  		if err == nil && len(addrs) > 0 {
    94  			t.Fatal("unexpected")
    95  		}
    96  		time.Sleep(time.Millisecond * 50)
    97  	}
    98  }
    99  
   100  func (ds *DNSList) test() {
   101  	ds.RLock()
   102  	defer ds.RUnlock()
   103  	for host, addrs := range ds.m {
   104  		for _, addr := range addrs {
   105  			if !addr.e {
   106  				continue
   107  			}
   108  			fmt.Println("dial:", host, addr.a)
   109  			conn, err := net.Dial("tcp", addr.a)
   110  			if err != nil {
   111  				continue
   112  			}
   113  			tlsConn := tls.Client(conn, &tls.Config{ServerName: host})
   114  			err = terasu.Use(tlsConn).Handshake(4)
   115  			_ = tlsConn.Close()
   116  			if err == nil {
   117  				fmt.Println("succ:", host, addr.a)
   118  				continue
   119  			}
   120  			fmt.Println("fail:", host, addr.a)
   121  		}
   122  	}
   123  }