dnscommon_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package dns
  2. import (
  3. "math/rand"
  4. "testing"
  5. "time"
  6. "github.com/google/go-cmp/cmp"
  7. "github.com/miekg/dns"
  8. "github.com/xtls/xray-core/common"
  9. "github.com/xtls/xray-core/common/net"
  10. "golang.org/x/net/dns/dnsmessage"
  11. )
  12. func Test_parseResponse(t *testing.T) {
  13. var p [][]byte
  14. ans := new(dns.Msg)
  15. ans.Id = 0
  16. p = append(p, common.Must2(ans.Pack()).([]byte))
  17. p = append(p, []byte{})
  18. ans = new(dns.Msg)
  19. ans.Id = 1
  20. ans.Answer = append(ans.Answer,
  21. common.Must2(dns.NewRR("google.com. IN CNAME m.test.google.com")).(dns.RR),
  22. common.Must2(dns.NewRR("google.com. IN CNAME fake.google.com")).(dns.RR),
  23. common.Must2(dns.NewRR("google.com. IN A 8.8.8.8")).(dns.RR),
  24. common.Must2(dns.NewRR("google.com. IN A 8.8.4.4")).(dns.RR),
  25. )
  26. p = append(p, common.Must2(ans.Pack()).([]byte))
  27. ans = new(dns.Msg)
  28. ans.Id = 2
  29. ans.Answer = append(ans.Answer,
  30. common.Must2(dns.NewRR("google.com. IN CNAME m.test.google.com")).(dns.RR),
  31. common.Must2(dns.NewRR("google.com. IN CNAME fake.google.com")).(dns.RR),
  32. common.Must2(dns.NewRR("google.com. IN CNAME m.test.google.com")).(dns.RR),
  33. common.Must2(dns.NewRR("google.com. IN CNAME test.google.com")).(dns.RR),
  34. common.Must2(dns.NewRR("google.com. IN AAAA 2001::123:8888")).(dns.RR),
  35. common.Must2(dns.NewRR("google.com. IN AAAA 2001::123:8844")).(dns.RR),
  36. )
  37. p = append(p, common.Must2(ans.Pack()).([]byte))
  38. tests := []struct {
  39. name string
  40. want *IPRecord
  41. wantErr bool
  42. }{
  43. {"empty",
  44. &IPRecord{0, []net.Address(nil), time.Time{}, dnsmessage.RCodeSuccess},
  45. false,
  46. },
  47. {"error",
  48. nil,
  49. true,
  50. },
  51. {"a record",
  52. &IPRecord{1, []net.Address{net.ParseAddress("8.8.8.8"), net.ParseAddress("8.8.4.4")},
  53. time.Time{}, dnsmessage.RCodeSuccess},
  54. false,
  55. },
  56. {"aaaa record",
  57. &IPRecord{2, []net.Address{net.ParseAddress("2001::123:8888"), net.ParseAddress("2001::123:8844")}, time.Time{}, dnsmessage.RCodeSuccess},
  58. false,
  59. },
  60. }
  61. for i, tt := range tests {
  62. t.Run(tt.name, func(t *testing.T) {
  63. got, err := parseResponse(p[i])
  64. if (err != nil) != tt.wantErr {
  65. t.Errorf("handleResponse() error = %v, wantErr %v", err, tt.wantErr)
  66. return
  67. }
  68. if got != nil {
  69. // reset the time
  70. got.Expire = time.Time{}
  71. }
  72. if cmp.Diff(got, tt.want) != "" {
  73. t.Errorf(cmp.Diff(got, tt.want))
  74. // t.Errorf("handleResponse() = %#v, want %#v", got, tt.want)
  75. }
  76. })
  77. }
  78. }
  79. func Test_buildReqMsgs(t *testing.T) {
  80. stubID := func() uint16 {
  81. return uint16(rand.Uint32())
  82. }
  83. type args struct {
  84. domain string
  85. option IPOption
  86. reqOpts *dnsmessage.Resource
  87. }
  88. tests := []struct {
  89. name string
  90. args args
  91. want int
  92. }{
  93. {"dual stack", args{"test.com", IPOption{true, true}, nil}, 2},
  94. {"ipv4 only", args{"test.com", IPOption{true, false}, nil}, 1},
  95. {"ipv6 only", args{"test.com", IPOption{false, true}, nil}, 1},
  96. {"none/error", args{"test.com", IPOption{false, false}, nil}, 0},
  97. }
  98. for _, tt := range tests {
  99. t.Run(tt.name, func(t *testing.T) {
  100. if got := buildReqMsgs(tt.args.domain, tt.args.option, stubID, tt.args.reqOpts); !(len(got) == tt.want) {
  101. t.Errorf("buildReqMsgs() = %v, want %v", got, tt.want)
  102. }
  103. })
  104. }
  105. }
  106. func Test_genEDNS0Options(t *testing.T) {
  107. type args struct {
  108. clientIP net.IP
  109. }
  110. tests := []struct {
  111. name string
  112. args args
  113. want *dnsmessage.Resource
  114. }{
  115. // TODO: Add test cases.
  116. {"ipv4", args{net.ParseIP("4.3.2.1")}, nil},
  117. {"ipv6", args{net.ParseIP("2001::4321")}, nil},
  118. }
  119. for _, tt := range tests {
  120. t.Run(tt.name, func(t *testing.T) {
  121. if got := genEDNS0Options(tt.args.clientIP); got == nil {
  122. t.Errorf("genEDNS0Options() = %v, want %v", got, tt.want)
  123. }
  124. })
  125. }
  126. }
  127. func TestFqdn(t *testing.T) {
  128. type args struct {
  129. domain string
  130. }
  131. tests := []struct {
  132. name string
  133. args args
  134. want string
  135. }{
  136. {"with fqdn", args{"www.example.com."}, "www.example.com."},
  137. {"without fqdn", args{"www.example.com"}, "www.example.com."},
  138. }
  139. for _, tt := range tests {
  140. t.Run(tt.name, func(t *testing.T) {
  141. if got := Fqdn(tt.args.domain); got != tt.want {
  142. t.Errorf("Fqdn() = %v, want %v", got, tt.want)
  143. }
  144. })
  145. }
  146. }