dnscommon_test.go 4.1 KB

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