nameserver_tcp_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package dns_test
  2. import (
  3. "context"
  4. "net/url"
  5. "testing"
  6. "time"
  7. "github.com/google/go-cmp/cmp"
  8. . "github.com/xtls/xray-core/app/dns"
  9. "github.com/xtls/xray-core/common"
  10. "github.com/xtls/xray-core/common/net"
  11. dns_feature "github.com/xtls/xray-core/features/dns"
  12. )
  13. func TestTCPLocalNameServer(t *testing.T) {
  14. url, err := url.Parse("tcp+local://8.8.8.8")
  15. common.Must(err)
  16. s, err := NewTCPLocalNameServer(url, QueryStrategy_USE_IP)
  17. common.Must(err)
  18. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  19. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
  20. IPv4Enable: true,
  21. IPv6Enable: true,
  22. }, false)
  23. cancel()
  24. common.Must(err)
  25. if len(ips) == 0 {
  26. t.Error("expect some ips, but got 0")
  27. }
  28. }
  29. func TestTCPLocalNameServerWithCache(t *testing.T) {
  30. url, err := url.Parse("tcp+local://8.8.8.8")
  31. common.Must(err)
  32. s, err := NewTCPLocalNameServer(url, QueryStrategy_USE_IP)
  33. common.Must(err)
  34. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  35. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
  36. IPv4Enable: true,
  37. IPv6Enable: true,
  38. }, false)
  39. cancel()
  40. common.Must(err)
  41. if len(ips) == 0 {
  42. t.Error("expect some ips, but got 0")
  43. }
  44. ctx2, cancel := context.WithTimeout(context.Background(), time.Second*5)
  45. ips2, err := s.QueryIP(ctx2, "google.com", net.IP(nil), dns_feature.IPOption{
  46. IPv4Enable: true,
  47. IPv6Enable: true,
  48. }, true)
  49. cancel()
  50. common.Must(err)
  51. if r := cmp.Diff(ips2, ips); r != "" {
  52. t.Fatal(r)
  53. }
  54. }
  55. func TestTCPLocalNameServerWithIPv4Override(t *testing.T) {
  56. url, err := url.Parse("tcp+local://8.8.8.8")
  57. common.Must(err)
  58. s, err := NewTCPLocalNameServer(url, QueryStrategy_USE_IP4)
  59. common.Must(err)
  60. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  61. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
  62. IPv4Enable: true,
  63. IPv6Enable: true,
  64. }, false)
  65. cancel()
  66. common.Must(err)
  67. if len(ips) == 0 {
  68. t.Error("expect some ips, but got 0")
  69. }
  70. for _, ip := range ips {
  71. if len(ip) != net.IPv4len {
  72. t.Error("expect only IPv4 response from DNS query")
  73. }
  74. }
  75. }
  76. func TestTCPLocalNameServerWithIPv6Override(t *testing.T) {
  77. url, err := url.Parse("tcp+local://8.8.8.8")
  78. common.Must(err)
  79. s, err := NewTCPLocalNameServer(url, QueryStrategy_USE_IP6)
  80. common.Must(err)
  81. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  82. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
  83. IPv4Enable: true,
  84. IPv6Enable: true,
  85. }, false)
  86. cancel()
  87. common.Must(err)
  88. if len(ips) == 0 {
  89. t.Error("expect some ips, but got 0")
  90. }
  91. for _, ip := range ips {
  92. if len(ip) != net.IPv6len {
  93. t.Error("expect only IPv6 response from DNS query")
  94. }
  95. }
  96. }