nameserver_quic_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "github.com/xtls/xray-core/features/dns"
  12. )
  13. func TestQUICNameServer(t *testing.T) {
  14. url, err := url.Parse("quic://dns.adguard-dns.com")
  15. common.Must(err)
  16. s, err := NewQUICNameServer(url, false, net.IP(nil))
  17. common.Must(err)
  18. ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
  19. ips, _, err := s.QueryIP(ctx, "google.com", dns.IPOption{
  20. IPv4Enable: true,
  21. IPv6Enable: true,
  22. })
  23. cancel()
  24. common.Must(err)
  25. if len(ips) == 0 {
  26. t.Error("expect some ips, but got 0")
  27. }
  28. ctx2, cancel := context.WithTimeout(context.Background(), time.Second*5)
  29. ips2, _, err := s.QueryIP(ctx2, "google.com", dns.IPOption{
  30. IPv4Enable: true,
  31. IPv6Enable: true,
  32. })
  33. cancel()
  34. common.Must(err)
  35. if r := cmp.Diff(ips2, ips); r != "" {
  36. t.Fatal(r)
  37. }
  38. }
  39. func TestQUICNameServerWithIPv4Override(t *testing.T) {
  40. url, err := url.Parse("quic://dns.adguard-dns.com")
  41. common.Must(err)
  42. s, err := NewQUICNameServer(url, false, net.IP(nil))
  43. common.Must(err)
  44. ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
  45. ips, _, err := s.QueryIP(ctx, "google.com", dns.IPOption{
  46. IPv4Enable: true,
  47. IPv6Enable: false,
  48. })
  49. cancel()
  50. common.Must(err)
  51. if len(ips) == 0 {
  52. t.Error("expect some ips, but got 0")
  53. }
  54. for _, ip := range ips {
  55. if len(ip) != net.IPv4len {
  56. t.Error("expect only IPv4 response from DNS query")
  57. }
  58. }
  59. }
  60. func TestQUICNameServerWithIPv6Override(t *testing.T) {
  61. url, err := url.Parse("quic://dns.adguard-dns.com")
  62. common.Must(err)
  63. s, err := NewQUICNameServer(url, false, net.IP(nil))
  64. common.Must(err)
  65. ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
  66. ips, _, err := s.QueryIP(ctx, "google.com", dns.IPOption{
  67. IPv4Enable: false,
  68. IPv6Enable: true,
  69. })
  70. cancel()
  71. common.Must(err)
  72. if len(ips) == 0 {
  73. t.Error("expect some ips, but got 0")
  74. }
  75. for _, ip := range ips {
  76. if len(ip) != net.IPv6len {
  77. t.Error("expect only IPv6 response from DNS query")
  78. }
  79. }
  80. }