github.com/phuslu/fastdns@v0.8.3-0.20240310041952-69506fc67dd1/client_test.go (about)

     1  package fastdns
     2  
     3  import (
     4  	"net/netip"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestClientExchange(t *testing.T) {
    10  	var cases = []struct {
    11  		Domain string
    12  		Class  Class
    13  		Type   Type
    14  	}{
    15  		{"hk2cn.flyspace.top", ClassINET, TypeA},
    16  	}
    17  
    18  	client := &Client{
    19  		AddrPort:    netip.AddrPortFrom(netip.AddrFrom4([4]byte{8, 8, 8, 8}), 53),
    20  		ReadTimeout: 1 * time.Second,
    21  		MaxConns:    1000,
    22  	}
    23  
    24  	for _, c := range cases {
    25  		req, resp := AcquireMessage(), AcquireMessage()
    26  		req.SetRequestQuestion(c.Domain, c.Type, c.Class)
    27  		err := client.Exchange(req, resp)
    28  		if err != nil {
    29  			t.Errorf("client=%+v exchange(%v) error: %+v\n", client, c.Domain, err)
    30  		}
    31  		t.Logf("%s: CLASS %s TYPE %s\n", resp.Domain, resp.Question.Class, resp.Question.Type)
    32  		_ = resp.Walk(func(name []byte, typ Type, class Class, ttl uint32, data []byte) bool {
    33  			switch typ {
    34  			case TypeCNAME:
    35  				t.Logf("%s.\t%d\t%s\t%s\t%s.\n", resp.DecodeName(nil, name), ttl, class, typ, resp.DecodeName(nil, data))
    36  			case TypeA:
    37  				t.Logf("%s.\t%d\t%s\t%s\t%s\n", resp.DecodeName(nil, name), ttl, class, typ, netip.AddrFrom4(*(*[4]byte)(data)))
    38  			case TypeAAAA:
    39  				t.Logf("%s.\t%d\t%s\t%s\t%s\n", resp.DecodeName(nil, name), ttl, class, typ, netip.AddrFrom16(*(*[16]byte)(data)))
    40  			}
    41  			return true
    42  		})
    43  	}
    44  }