dnscommon_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. "github.com/v2fly/v2ray-core/v4/common"
  11. "github.com/v2fly/v2ray-core/v4/common/net"
  12. dns_feature "github.com/v2fly/v2ray-core/v4/features/dns"
  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, []net.Address(nil), time.Time{}, dnsmessage.RCodeSuccess},
  47. false,
  48. },
  49. {"error",
  50. nil,
  51. true,
  52. },
  53. {"a record",
  54. &IPRecord{1, []net.Address{net.ParseAddress("8.8.8.8"), net.ParseAddress("8.8.4.4")},
  55. time.Time{}, dnsmessage.RCodeSuccess},
  56. false,
  57. },
  58. {"aaaa record",
  59. &IPRecord{2, []net.Address{net.ParseAddress("2001::123:8888"), net.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 dns_feature.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", dns_feature.IPOption{
  96. IPv4Enable: true,
  97. IPv6Enable: true,
  98. FakeEnable: false,
  99. }, nil}, 2},
  100. {"ipv4 only", args{"test.com", dns_feature.IPOption{
  101. IPv4Enable: true,
  102. IPv6Enable: false,
  103. FakeEnable: false,
  104. }, nil}, 1},
  105. {"ipv6 only", args{"test.com", dns_feature.IPOption{
  106. IPv4Enable: false,
  107. IPv6Enable: true,
  108. FakeEnable: false,
  109. }, nil}, 1},
  110. {"none/error", args{"test.com", dns_feature.IPOption{
  111. IPv4Enable: false,
  112. IPv6Enable: false,
  113. FakeEnable: false,
  114. }, nil}, 0},
  115. }
  116. for _, tt := range tests {
  117. t.Run(tt.name, func(t *testing.T) {
  118. if got := buildReqMsgs(tt.args.domain, tt.args.option, stubID, tt.args.reqOpts); !(len(got) == tt.want) {
  119. t.Errorf("buildReqMsgs() = %v, want %v", got, tt.want)
  120. }
  121. })
  122. }
  123. }
  124. func Test_genEDNS0Options(t *testing.T) {
  125. type args struct {
  126. clientIP net.IP
  127. }
  128. tests := []struct {
  129. name string
  130. args args
  131. want *dnsmessage.Resource
  132. }{
  133. // TODO: Add test cases.
  134. {"ipv4", args{net.ParseIP("4.3.2.1")}, nil},
  135. {"ipv6", args{net.ParseIP("2001::4321")}, nil},
  136. }
  137. for _, tt := range tests {
  138. t.Run(tt.name, func(t *testing.T) {
  139. if got := genEDNS0Options(tt.args.clientIP); got == nil {
  140. t.Errorf("genEDNS0Options() = %v, want %v", got, tt.want)
  141. }
  142. })
  143. }
  144. }
  145. func TestFqdn(t *testing.T) {
  146. type args struct {
  147. domain string
  148. }
  149. tests := []struct {
  150. name string
  151. args args
  152. want string
  153. }{
  154. {"with fqdn", args{"www.v2fly.org."}, "www.v2fly.org."},
  155. {"without fqdn", args{"www.v2fly.org"}, "www.v2fly.org."},
  156. }
  157. for _, tt := range tests {
  158. t.Run(tt.name, func(t *testing.T) {
  159. if got := Fqdn(tt.args.domain); got != tt.want {
  160. t.Errorf("Fqdn() = %v, want %v", got, tt.want)
  161. }
  162. })
  163. }
  164. }