dnscommon_test.go 4.0 KB

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