hosts_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package dns_test
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. . "github.com/v2fly/v2ray-core/v4/app/dns"
  6. "github.com/v2fly/v2ray-core/v4/common"
  7. "github.com/v2fly/v2ray-core/v4/common/net"
  8. "github.com/v2fly/v2ray-core/v4/features/dns"
  9. )
  10. func TestStaticHosts(t *testing.T) {
  11. pb := []*Config_HostMapping{
  12. {
  13. Type: DomainMatchingType_Full,
  14. Domain: "v2fly.org",
  15. Ip: [][]byte{
  16. {1, 1, 1, 1},
  17. },
  18. },
  19. {
  20. Type: DomainMatchingType_Subdomain,
  21. Domain: "v2ray.cn",
  22. Ip: [][]byte{
  23. {2, 2, 2, 2},
  24. },
  25. },
  26. {
  27. Type: DomainMatchingType_Subdomain,
  28. Domain: "baidu.com",
  29. Ip: [][]byte{
  30. {127, 0, 0, 1},
  31. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  32. },
  33. },
  34. }
  35. hosts, err := NewStaticHosts(pb, nil)
  36. common.Must(err)
  37. {
  38. ips := hosts.Lookup("v2fly.org", dns.IPOption{
  39. IPv4Enable: true,
  40. IPv6Enable: true,
  41. })
  42. if len(ips) != 1 {
  43. t.Error("expect 1 IP, but got ", len(ips))
  44. }
  45. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
  46. t.Error(diff)
  47. }
  48. }
  49. {
  50. ips := hosts.Lookup("www.v2ray.cn", dns.IPOption{
  51. IPv4Enable: true,
  52. IPv6Enable: true,
  53. })
  54. if len(ips) != 1 {
  55. t.Error("expect 1 IP, but got ", len(ips))
  56. }
  57. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
  58. t.Error(diff)
  59. }
  60. }
  61. {
  62. ips := hosts.Lookup("baidu.com", dns.IPOption{
  63. IPv4Enable: false,
  64. IPv6Enable: true,
  65. })
  66. if len(ips) != 1 {
  67. t.Error("expect 1 IP, but got ", len(ips))
  68. }
  69. if diff := cmp.Diff([]byte(ips[0].IP()), []byte(net.LocalHostIPv6.IP())); diff != "" {
  70. t.Error(diff)
  71. }
  72. }
  73. }