nameserver_quic_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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, QueryStrategy_USE_IP)
  17. common.Must(err)
  18. ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
  19. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns.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. ctx2, cancel := context.WithTimeout(context.Background(), time.Second*5)
  29. ips2, err := s.QueryIP(ctx2, "google.com", net.IP(nil), dns.IPOption{
  30. IPv4Enable: true,
  31. IPv6Enable: true,
  32. }, true)
  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, QueryStrategy_USE_IP4)
  43. common.Must(err)
  44. ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
  45. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns.IPOption{
  46. IPv4Enable: true,
  47. IPv6Enable: true,
  48. }, false)
  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, QueryStrategy_USE_IP6)
  64. common.Must(err)
  65. ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
  66. ips, err := s.QueryIP(ctx, "google.com", net.IP(nil), dns.IPOption{
  67. IPv4Enable: true,
  68. IPv6Enable: true,
  69. }, false)
  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. }