hosts_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package dns_test
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. . "github.com/xtls/xray-core/app/dns"
  6. "github.com/xtls/xray-core/common"
  7. "github.com/xtls/xray-core/common/net"
  8. "github.com/xtls/xray-core/features/dns"
  9. )
  10. func TestStaticHosts(t *testing.T) {
  11. pb := []*Config_HostMapping{
  12. {
  13. Type: DomainMatchingType_Full,
  14. Domain: "example.com",
  15. Ip: [][]byte{
  16. {1, 1, 1, 1},
  17. },
  18. },
  19. {
  20. Type: DomainMatchingType_Full,
  21. Domain: "proxy.xray.com",
  22. Ip: [][]byte{
  23. {1, 2, 3, 4},
  24. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  25. },
  26. ProxiedDomain: "another-proxy.xray.com",
  27. },
  28. {
  29. Type: DomainMatchingType_Full,
  30. Domain: "proxy2.xray.com",
  31. ProxiedDomain: "proxy.xray.com",
  32. },
  33. {
  34. Type: DomainMatchingType_Subdomain,
  35. Domain: "example.cn",
  36. Ip: [][]byte{
  37. {2, 2, 2, 2},
  38. },
  39. },
  40. {
  41. Type: DomainMatchingType_Subdomain,
  42. Domain: "baidu.com",
  43. Ip: [][]byte{
  44. {127, 0, 0, 1},
  45. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  46. },
  47. },
  48. }
  49. hosts, err := NewStaticHosts(pb)
  50. common.Must(err)
  51. {
  52. ips := hosts.Lookup("example.com", dns.IPOption{
  53. IPv4Enable: true,
  54. IPv6Enable: true,
  55. })
  56. if len(ips) != 1 {
  57. t.Error("expect 1 IP, but got ", len(ips))
  58. }
  59. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
  60. t.Error(diff)
  61. }
  62. }
  63. {
  64. domain := hosts.Lookup("proxy.xray.com", dns.IPOption{
  65. IPv4Enable: true,
  66. IPv6Enable: false,
  67. })
  68. if len(domain) != 1 {
  69. t.Error("expect 1 domain, but got ", len(domain))
  70. }
  71. if diff := cmp.Diff(domain[0].Domain(), "another-proxy.xray.com"); diff != "" {
  72. t.Error(diff)
  73. }
  74. }
  75. {
  76. domain := hosts.Lookup("proxy2.xray.com", dns.IPOption{
  77. IPv4Enable: true,
  78. IPv6Enable: false,
  79. })
  80. if len(domain) != 1 {
  81. t.Error("expect 1 domain, but got ", len(domain))
  82. }
  83. if diff := cmp.Diff(domain[0].Domain(), "another-proxy.xray.com"); diff != "" {
  84. t.Error(diff)
  85. }
  86. }
  87. {
  88. ips := hosts.Lookup("www.example.cn", dns.IPOption{
  89. IPv4Enable: true,
  90. IPv6Enable: true,
  91. })
  92. if len(ips) != 1 {
  93. t.Error("expect 1 IP, but got ", len(ips))
  94. }
  95. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
  96. t.Error(diff)
  97. }
  98. }
  99. {
  100. ips := hosts.Lookup("baidu.com", dns.IPOption{
  101. IPv4Enable: false,
  102. IPv6Enable: true,
  103. })
  104. if len(ips) != 1 {
  105. t.Error("expect 1 IP, but got ", len(ips))
  106. }
  107. if diff := cmp.Diff([]byte(ips[0].IP()), []byte(net.LocalHostIPv6.IP())); diff != "" {
  108. t.Error(diff)
  109. }
  110. }
  111. }