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

     1  package fastdns
     2  
     3  import (
     4  	"runtime"
     5  	"testing"
     6  )
     7  
     8  func TestEncodeDomain(t *testing.T) {
     9  	var cases = []struct {
    10  		Domain string
    11  		QName  string
    12  	}{
    13  		{"phus.lu", "\x04phus\x02lu\x00"},
    14  		{"splunk.phus.lu", "\x06splunk\x04phus\x02lu\x00"},
    15  	}
    16  
    17  	for _, c := range cases {
    18  		if got, want := string(EncodeDomain(nil, c.Domain)), c.QName; got != want {
    19  			t.Errorf("EncodeDomain(%v) error got=%#v want=%#v", c.Domain, got, want)
    20  		}
    21  	}
    22  }
    23  
    24  func TestListen(t *testing.T) {
    25  	if runtime.GOOS != "linux" {
    26  		return
    27  	}
    28  
    29  	_, err := listen("udp", ":65537")
    30  	if err == nil {
    31  		t.Errorf("listen(:65537) at shall return error but empty")
    32  	}
    33  
    34  	var addr = ":19841"
    35  	for i := 1; i <= 64; i++ {
    36  		_, err := listen("udp", addr)
    37  		if err != nil {
    38  			t.Errorf("listen(%+v) at %d times got error: %+v", addr, i, err)
    39  		}
    40  	}
    41  }
    42  
    43  func TestTastset(t *testing.T) {
    44  	if runtime.GOOS != "linux" {
    45  		return
    46  	}
    47  
    48  	err := taskset(1023)
    49  	if err == nil {
    50  		t.Errorf("taskset(65537) shall return error but empty")
    51  	}
    52  
    53  	err = taskset(0)
    54  	if err != nil {
    55  		t.Errorf("taskset(0) error: %+v", err)
    56  	}
    57  }
    58  
    59  func BenchmarkEncodeDomain(b *testing.B) {
    60  	dst := make([]byte, 0, 256)
    61  	for i := 0; i < b.N; i++ {
    62  		dst = EncodeDomain(dst[:0], "hk.phus.lu")
    63  	}
    64  }