dnscommon_test.go 4.0 KB

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