nameserver_doh_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 TestDOHNameServer(t *testing.T) {
  14. url, err := url.Parse("https+local://1.1.1.1/dns-query")
  15. common.Must(err)
  16. s := NewDoHLocalNameServer(url, QueryStrategy_USE_IP)
  17. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  18. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
  19. IPv4Enable: true,
  20. IPv6Enable: true,
  21. }, false)
  22. cancel()
  23. common.Must(err)
  24. if len(ips) == 0 {
  25. t.Error("expect some ips, but got 0")
  26. }
  27. }
  28. func TestDOHNameServerWithCache(t *testing.T) {
  29. url, err := url.Parse("https+local://1.1.1.1/dns-query")
  30. common.Must(err)
  31. s := NewDoHLocalNameServer(url, QueryStrategy_USE_IP)
  32. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  33. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
  34. IPv4Enable: true,
  35. IPv6Enable: true,
  36. }, false)
  37. cancel()
  38. common.Must(err)
  39. if len(ips) == 0 {
  40. t.Error("expect some ips, but got 0")
  41. }
  42. ctx2, cancel := context.WithTimeout(context.Background(), time.Second*5)
  43. ips2, err := s.QueryIP(ctx2, "google.com", net.IP(nil), dns_feature.IPOption{
  44. IPv4Enable: true,
  45. IPv6Enable: true,
  46. }, true)
  47. cancel()
  48. common.Must(err)
  49. if r := cmp.Diff(ips2, ips); r != "" {
  50. t.Fatal(r)
  51. }
  52. }
  53. func TestDOHNameServerWithIPv4Override(t *testing.T) {
  54. url, err := url.Parse("https+local://1.1.1.1/dns-query")
  55. common.Must(err)
  56. s := NewDoHLocalNameServer(url, QueryStrategy_USE_IP4)
  57. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  58. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
  59. IPv4Enable: true,
  60. IPv6Enable: true,
  61. }, false)
  62. cancel()
  63. common.Must(err)
  64. if len(ips) == 0 {
  65. t.Error("expect some ips, but got 0")
  66. }
  67. for _, ip := range ips {
  68. if len(ip) != net.IPv4len {
  69. t.Error("expect only IPv4 response from DNS query")
  70. }
  71. }
  72. }
  73. func TestDOHNameServerWithIPv6Override(t *testing.T) {
  74. url, err := url.Parse("https+local://1.1.1.1/dns-query")
  75. common.Must(err)
  76. s := NewDoHLocalNameServer(url, QueryStrategy_USE_IP6)
  77. ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
  78. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns_feature.IPOption{
  79. IPv4Enable: true,
  80. IPv6Enable: true,
  81. }, false)
  82. cancel()
  83. common.Must(err)
  84. if len(ips) == 0 {
  85. t.Error("expect some ips, but got 0")
  86. }
  87. for _, ip := range ips {
  88. if len(ip) != net.IPv6len {
  89. t.Error("expect only IPv6 response from DNS query")
  90. }
  91. }
  92. }