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

     1  package fastdns
     2  
     3  import (
     4  	"encoding/hex"
     5  	"net"
     6  	"net/netip"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestAppendHOSTRecord(t *testing.T) {
    12  	cases := []struct {
    13  		Hex string
    14  		IPs []netip.Addr
    15  		TTL uint32
    16  	}{
    17  		{
    18  			"c00c000100010000012c000401010101c00c000100010000012c000408080808c00c000100010000012c00047b2d064e",
    19  			[]netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1}), netip.AddrFrom4([4]byte{8, 8, 8, 8}), netip.AddrFrom4([4]byte{123, 45, 6, 78})},
    20  			300,
    21  		},
    22  		{
    23  			"c00c001c00010000012c001000000000000000000000000000000001c00c001c00010000012c001020014860486000000000000000008888",
    24  			[]netip.Addr{netip.MustParseAddr("::1"), netip.MustParseAddr("2001:4860:4860::8888")},
    25  			300,
    26  		},
    27  	}
    28  
    29  	req := new(Message)
    30  	req.Question.Class = ClassINET
    31  
    32  	for _, c := range cases {
    33  		if got, want := hex.EncodeToString(AppendHOSTRecord(nil, req, c.TTL, c.IPs)), c.Hex; got != want {
    34  			t.Errorf("AppendHOSTRecord(%v) error got=%#v want=%#v", c.IPs, got, want)
    35  		}
    36  	}
    37  
    38  }
    39  
    40  func TestAppendCNAMERecord(t *testing.T) {
    41  	cases := []struct {
    42  		Hex    string
    43  		CNAMEs []string
    44  		IPs    []netip.Addr
    45  		TTL    uint32
    46  	}{
    47  		{
    48  			"c00c000500010000012c00090470687573026c7500",
    49  			[]string{"phus.lu"},
    50  			nil,
    51  			300,
    52  		},
    53  		{
    54  			"c00c000500010000012c00090470687573026c7500c028001c00010000012c001020014860486000000000000000008888",
    55  			[]string{"phus.lu"},
    56  			[]netip.Addr{netip.MustParseAddr("2001:4860:4860::8888")},
    57  			300,
    58  		},
    59  		{
    60  			"c00c000500010000012c00090470687573026c7500c028000500010000012c000c02686b0470687573026c7500c040000100010000012c000401010101c040000100010000012c000408080808",
    61  			[]string{"phus.lu", "hk.phus.lu"},
    62  			[]netip.Addr{netip.AddrFrom4([4]byte{1, 1, 1, 1}), netip.AddrFrom4([4]byte{8, 8, 8, 8})},
    63  			300,
    64  		},
    65  	}
    66  
    67  	req := new(Message)
    68  	req.Question.Name = EncodeDomain(nil, "ip.phus.lu")
    69  	req.Question.Class = ClassINET
    70  
    71  	for _, c := range cases {
    72  		if got, want := hex.EncodeToString(AppendCNAMERecord(nil, req, c.TTL, c.CNAMEs, c.IPs)), c.Hex; got != want {
    73  			t.Errorf("AppendCNAMERecord(%v) error got=%#v want=%#v", c.IPs, got, want)
    74  		}
    75  	}
    76  
    77  }
    78  
    79  func TestAppendSRVRecord(t *testing.T) {
    80  	cases := []struct {
    81  		Hex string
    82  		TTL uint32
    83  		SRV net.SRV
    84  	}{
    85  		{
    86  			"c00c002100010000012c001203e803e8005002686b0470687573026c7500",
    87  			300,
    88  			net.SRV{Target: "hk.phus.lu", Port: 80, Priority: 1000, Weight: 1000},
    89  		},
    90  		{
    91  			"c00c002100010000012c00120400040001bb0273670470687573026c7500",
    92  			300,
    93  			net.SRV{Target: "sg.phus.lu", Port: 443, Priority: 1024, Weight: 1024},
    94  		},
    95  	}
    96  
    97  	req := new(Message)
    98  	req.Question.Class = ClassINET
    99  
   100  	for _, c := range cases {
   101  		if got, want := hex.EncodeToString(AppendSRVRecord(nil, req, c.TTL, []net.SRV{c.SRV})), c.Hex; got != want {
   102  			t.Errorf("AppendSRVRecord(%v) error got=%#v want=%#v", c.SRV, got, want)
   103  		}
   104  	}
   105  
   106  }
   107  
   108  func TestAppendNSRecord(t *testing.T) {
   109  	cases := []struct {
   110  		Hex         string
   111  		TTL         uint32
   112  		Nameservers []net.NS
   113  	}{
   114  		{
   115  			"c00c000200010000012c0010036e733106676f6f676c6503636f6d00",
   116  			300,
   117  			[]net.NS{{Host: "ns1.google.com"}},
   118  		},
   119  		{
   120  			"c00c000200010000012c0010036e733106676f6f676c6503636f6d00c00c000200010000012c0010036e733206676f6f676c6503636f6d00",
   121  			300,
   122  			[]net.NS{{Host: "ns1.google.com"}, {Host: "ns2.google.com"}},
   123  		},
   124  	}
   125  
   126  	req := new(Message)
   127  	req.Question.Name = EncodeDomain(nil, "ip.phus.lu")
   128  	req.Question.Class = ClassINET
   129  
   130  	for _, c := range cases {
   131  		if got, want := hex.EncodeToString(AppendNSRecord(nil, req, c.TTL, c.Nameservers)), c.Hex; got != want {
   132  			t.Errorf("AppendNSRecord(%v) error got=%#v want=%#v", c.Nameservers, got, want)
   133  		}
   134  	}
   135  
   136  }
   137  
   138  func TestAppendSOARecord(t *testing.T) {
   139  	cases := []struct {
   140  		Hex     string
   141  		TTL     uint32
   142  		MName   net.NS
   143  		RName   net.NS
   144  		Serial  uint32
   145  		Refresh uint32
   146  		Retry   uint32
   147  		Expire  uint32
   148  		Minimum uint32
   149  	}{
   150  		{
   151  			"c00c000600010000012c003a036e733106676f6f676c6503636f6d0009646e732d61646d696e06676f6f676c6503636f6d00400000000000038400000384000007080000003c",
   152  			300,
   153  			net.NS{Host: "ns1.google.com"},
   154  			net.NS{Host: "dns-admin.google.com"},
   155  			1073741824,
   156  			900,
   157  			900,
   158  			1800,
   159  			60,
   160  		},
   161  	}
   162  
   163  	req := new(Message)
   164  	req.Question.Name = EncodeDomain(nil, "www.google.com")
   165  	req.Question.Class = ClassINET
   166  
   167  	for _, c := range cases {
   168  		if got, want := hex.EncodeToString(AppendSOARecord(nil, req, c.TTL, c.MName, c.RName, c.Serial, c.Refresh, c.Retry, c.Expire, c.Minimum)), c.Hex; got != want {
   169  			t.Errorf("AppendSOARecord(%v) error got=%#v want=%#v", c.MName, got, want)
   170  		}
   171  	}
   172  
   173  }
   174  
   175  func TestAppendMXRecord(t *testing.T) {
   176  	cases := []struct {
   177  		Hex string
   178  		TTL uint32
   179  		MX  net.MX
   180  	}{
   181  		{
   182  			"c00c000f00010000012c000e000a02686b0470687573026c7500",
   183  			300,
   184  			net.MX{Host: "hk.phus.lu", Pref: 10},
   185  		},
   186  		{
   187  			"c00c000f00010000012c000e000a0273670470687573026c7500",
   188  			300,
   189  			net.MX{Host: "sg.phus.lu", Pref: 10},
   190  		},
   191  	}
   192  
   193  	req := new(Message)
   194  	req.Question.Class = ClassINET
   195  
   196  	for _, c := range cases {
   197  		if got, want := hex.EncodeToString(AppendMXRecord(nil, req, c.TTL, []net.MX{c.MX})), c.Hex; got != want {
   198  			t.Errorf("AppendMXRecord(%v) error got=%#v want=%#v", c.MX, got, want)
   199  		}
   200  	}
   201  
   202  }
   203  
   204  func TestAppendPTRRecord(t *testing.T) {
   205  	cases := []struct {
   206  		Hex string
   207  		PTR string
   208  		TTL uint32
   209  	}{
   210  		{
   211  			"c00c000c00010000012c000c02686b0470687573026c7500",
   212  			"hk.phus.lu",
   213  			300,
   214  		},
   215  		{
   216  			"c00c000c00010000012c000c0273670470687573026c7500",
   217  			"sg.phus.lu",
   218  			300,
   219  		},
   220  	}
   221  
   222  	req := new(Message)
   223  	req.Question.Class = ClassINET
   224  
   225  	for _, c := range cases {
   226  		if got, want := hex.EncodeToString(AppendPTRRecord(nil, req, c.TTL, c.PTR)), c.Hex; got != want {
   227  			t.Errorf("AppendPTRRecord(%v) error got=%#v want=%#v", c.PTR, got, want)
   228  		}
   229  	}
   230  
   231  }
   232  
   233  func TestAppendTXTRecord(t *testing.T) {
   234  	cases := []struct {
   235  		Hex string
   236  		TXT string
   237  		TTL uint32
   238  	}{
   239  		{
   240  			"c00c001000010000012c000e0d69616d617478747265636f7264",
   241  			"iamatxtrecord",
   242  			300,
   243  		},
   244  		{
   245  			"c00c001000010000012c010fff3030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030300e3069616d617478747265636f7264",
   246  			strings.Repeat("0", 256) + "iamatxtrecord",
   247  			300,
   248  		},
   249  	}
   250  
   251  	req := new(Message)
   252  	req.Question.Class = ClassINET
   253  
   254  	for _, c := range cases {
   255  		if got, want := hex.EncodeToString(AppendTXTRecord(nil, req, c.TTL, c.TXT)), c.Hex; got != want {
   256  			t.Errorf("AppendTXTRecord(%v) error got=%#v want=%#v", c.TXT, got, want)
   257  		}
   258  	}
   259  
   260  }
   261  
   262  func BenchmarkAppendHOSTRecord(b *testing.B) {
   263  	payload, _ := hex.DecodeString("00020100000100000000000002686b0470687573026c750000010001")
   264  	req := new(Message)
   265  
   266  	if err := ParseMessage(req, payload, false); err != nil {
   267  		b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
   268  	}
   269  
   270  	ips := []netip.Addr{netip.AddrFrom4([4]byte{8, 8, 8, 8})}
   271  	for i := 0; i < b.N; i++ {
   272  		payload = AppendHOSTRecord(payload[:0], req, 3000, ips)
   273  	}
   274  }
   275  
   276  func BenchmarkAppendCNAMERecord(b *testing.B) {
   277  	payload, _ := hex.DecodeString("00020100000100000000000002686b0470687573026c750000010001")
   278  	req := new(Message)
   279  
   280  	if err := ParseMessage(req, payload, false); err != nil {
   281  		b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
   282  	}
   283  
   284  	cnames := []string{"cname.example.org"}
   285  	for i := 0; i < b.N; i++ {
   286  		payload = AppendCNAMERecord(payload[:0], req, 3000, cnames, nil)
   287  	}
   288  }
   289  
   290  func BenchmarkAppendSRVRecord(b *testing.B) {
   291  	payload, _ := hex.DecodeString("00020100000100000000000002686b0470687573026c750000010001")
   292  	req := new(Message)
   293  
   294  	if err := ParseMessage(req, payload, false); err != nil {
   295  		b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
   296  	}
   297  
   298  	srv := net.SRV{Target: "service1.example.org", Port: 443, Priority: 100, Weight: 100}
   299  	for i := 0; i < b.N; i++ {
   300  		payload = AppendSRVRecord(payload[:0], req, 3000, []net.SRV{srv})
   301  	}
   302  }
   303  
   304  func BenchmarkAppendNSRecord(b *testing.B) {
   305  	payload, _ := hex.DecodeString("00020100000100000000000002686b0470687573026c750000010001")
   306  	req := new(Message)
   307  
   308  	if err := ParseMessage(req, payload, false); err != nil {
   309  		b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
   310  	}
   311  
   312  	nameservers := []net.NS{{Host: "ns1.google.com"}, {Host: "ns2.google.com"}}
   313  	for i := 0; i < b.N; i++ {
   314  		payload = AppendNSRecord(payload[:0], req, 300, nameservers)
   315  	}
   316  }
   317  
   318  func BenchmarkAppendSOARecord(b *testing.B) {
   319  	payload, _ := hex.DecodeString("00020100000100000000000002686b0470687573026c750000010001")
   320  	req := new(Message)
   321  
   322  	if err := ParseMessage(req, payload, false); err != nil {
   323  		b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
   324  	}
   325  
   326  	for i := 0; i < b.N; i++ {
   327  		payload = AppendSOARecord(payload[:0], req, 300, net.NS{Host: "ns1.google.com"}, net.NS{Host: "dns-admin.google.com"}, 1073741824, 900, 900, 1800, 60)
   328  	}
   329  }
   330  
   331  func BenchmarkAppendPTRRecord(b *testing.B) {
   332  	payload, _ := hex.DecodeString("00020100000100000000000002686b0470687573026c750000010001")
   333  	req := new(Message)
   334  
   335  	if err := ParseMessage(req, payload, false); err != nil {
   336  		b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
   337  	}
   338  
   339  	ptr := "ptr.example.org"
   340  	for i := 0; i < b.N; i++ {
   341  		payload = AppendPTRRecord(payload[:0], req, 3000, ptr)
   342  	}
   343  }
   344  
   345  func BenchmarkAppendMXRecord(b *testing.B) {
   346  	payload, _ := hex.DecodeString("00020100000100000000000002686b0470687573026c750000010001")
   347  	req := new(Message)
   348  
   349  	if err := ParseMessage(req, payload, false); err != nil {
   350  		b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
   351  	}
   352  
   353  	mx := net.MX{Host: "mail.google.com", Pref: 100}
   354  	for i := 0; i < b.N; i++ {
   355  		payload = AppendMXRecord(payload[:0], req, 3000, []net.MX{mx})
   356  	}
   357  }
   358  
   359  func BenchmarkAppendTXTRecord(b *testing.B) {
   360  	payload, _ := hex.DecodeString("00020100000100000000000002686b0470687573026c750000010001")
   361  	req := new(Message)
   362  
   363  	if err := ParseMessage(req, payload, false); err != nil {
   364  		b.Errorf("ParseMessage(%+v) error: %+v", payload, err)
   365  	}
   366  
   367  	txt := "iamatxtrecord"
   368  	for i := 0; i < b.N; i++ {
   369  		payload = AppendTXTRecord(payload[:0], req, 3000, txt)
   370  	}
   371  }