hosts_test.go 1.5 KB

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