nameserver_doh_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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)
  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)
  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. }