hosts_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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_Subdomain,
  21. Domain: "example.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. },
  32. },
  33. }
  34. hosts, err := NewStaticHosts(pb, nil)
  35. common.Must(err)
  36. {
  37. ips := hosts.LookupIP("example.com", dns.IPOption{
  38. IPv4Enable: true,
  39. IPv6Enable: true,
  40. })
  41. if len(ips) != 1 {
  42. t.Error("expect 1 IP, but got ", len(ips))
  43. }
  44. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{1, 1, 1, 1}); diff != "" {
  45. t.Error(diff)
  46. }
  47. }
  48. {
  49. ips := hosts.LookupIP("www.example.cn", dns.IPOption{
  50. IPv4Enable: true,
  51. IPv6Enable: true,
  52. })
  53. if len(ips) != 1 {
  54. t.Error("expect 1 IP, but got ", len(ips))
  55. }
  56. if diff := cmp.Diff([]byte(ips[0].IP()), []byte{2, 2, 2, 2}); diff != "" {
  57. t.Error(diff)
  58. }
  59. }
  60. {
  61. ips := hosts.LookupIP("baidu.com", dns.IPOption{
  62. IPv4Enable: false,
  63. IPv6Enable: true,
  64. })
  65. if len(ips) != 1 {
  66. t.Error("expect 1 IP, but got ", len(ips))
  67. }
  68. if diff := cmp.Diff([]byte(ips[0].IP()), []byte(net.LocalHostIPv6.IP())); diff != "" {
  69. t.Error(diff)
  70. }
  71. }
  72. }