hosts_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_Subdomain,
  14. Domain: "lan",
  15. ProxiedDomain: "#3",
  16. },
  17. {
  18. Type: DomainMatchingType_Full,
  19. Domain: "example.com",
  20. Ip: [][]byte{
  21. {1, 1, 1, 1},
  22. },
  23. },
  24. {
  25. Type: DomainMatchingType_Full,
  26. Domain: "proxy.xray.com",
  27. Ip: [][]byte{
  28. {1, 2, 3, 4},
  29. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  30. },
  31. ProxiedDomain: "another-proxy.xray.com",
  32. },
  33. {
  34. Type: DomainMatchingType_Full,
  35. Domain: "proxy2.xray.com",
  36. ProxiedDomain: "proxy.xray.com",
  37. },
  38. {
  39. Type: DomainMatchingType_Subdomain,
  40. Domain: "example.cn",
  41. Ip: [][]byte{
  42. {2, 2, 2, 2},
  43. },
  44. },
  45. {
  46. Type: DomainMatchingType_Subdomain,
  47. Domain: "baidu.com",
  48. Ip: [][]byte{
  49. {127, 0, 0, 1},
  50. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
  51. },
  52. },
  53. }
  54. hosts, err := NewStaticHosts(pb)
  55. common.Must(err)
  56. {
  57. _, err := hosts.Lookup("example.com.lan", dns.IPOption{})
  58. if dns.RCodeFromError(err) != 3 {
  59. t.Error(err)
  60. }
  61. }
  62. {
  63. ips, _ := hosts.Lookup("example.com", dns.IPOption{
  64. IPv4Enable: true,
  65. IPv6Enable: true,
  66. })
  67. if len(ips) != 1 {
  68. t.Error("expect 1 IP, but got ", len(ips))
  69. }
  70. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
  71. t.Error(diff)
  72. }
  73. }
  74. {
  75. domain, _ := hosts.Lookup("proxy.xray.com", dns.IPOption{
  76. IPv4Enable: true,
  77. IPv6Enable: false,
  78. })
  79. if len(domain) != 1 {
  80. t.Error("expect 1 domain, but got ", len(domain))
  81. }
  82. if diff := cmp.Diff(domain[0].Domain(), "another-proxy.xray.com"); diff != "" {
  83. t.Error(diff)
  84. }
  85. }
  86. {
  87. domain, _ := hosts.Lookup("proxy2.xray.com", dns.IPOption{
  88. IPv4Enable: true,
  89. IPv6Enable: false,
  90. })
  91. if len(domain) != 1 {
  92. t.Error("expect 1 domain, but got ", len(domain))
  93. }
  94. if diff := cmp.Diff(domain[0].Domain(), "another-proxy.xray.com"); diff != "" {
  95. t.Error(diff)
  96. }
  97. }
  98. {
  99. ips, _ := hosts.Lookup("www.example.cn", dns.IPOption{
  100. IPv4Enable: true,
  101. IPv6Enable: true,
  102. })
  103. if len(ips) != 1 {
  104. t.Error("expect 1 IP, but got ", len(ips))
  105. }
  106. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
  107. t.Error(diff)
  108. }
  109. }
  110. {
  111. ips, _ := hosts.Lookup("baidu.com", dns.IPOption{
  112. IPv4Enable: false,
  113. IPv6Enable: true,
  114. })
  115. if len(ips) != 1 {
  116. t.Error("expect 1 IP, but got ", len(ips))
  117. }
  118. if diff := cmp.Diff([]byte(ips[0].IP()), []byte(net.LocalHostIPv6.IP())); diff != "" {
  119. t.Error(diff)
  120. }
  121. }
  122. }