nameserver_doh_test.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/v2fly/v2ray-core/v5/app/dns"
  9. "github.com/v2fly/v2ray-core/v5/common"
  10. "github.com/v2fly/v2ray-core/v5/common/net"
  11. dns_feature "github.com/v2fly/v2ray-core/v5/features/dns"
  12. )
  13. func TestDoHLocalNameServer(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 TestDoHLocalNameServerWithCache(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. }