hosts_test.go 1015 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package dns_test
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. . "v2ray.com/core/app/dns"
  6. "v2ray.com/core/common"
  7. )
  8. func TestStaticHosts(t *testing.T) {
  9. pb := []*Config_HostMapping{
  10. {
  11. Type: DomainMatchingType_Full,
  12. Domain: "v2ray.com",
  13. Ip: [][]byte{
  14. {1, 1, 1, 1},
  15. },
  16. },
  17. {
  18. Type: DomainMatchingType_Subdomain,
  19. Domain: "v2ray.cn",
  20. Ip: [][]byte{
  21. {2, 2, 2, 2},
  22. },
  23. },
  24. }
  25. hosts, err := NewStaticHosts(pb, nil)
  26. common.Must(err)
  27. {
  28. ips := hosts.LookupIP("v2ray.com", IPOption{
  29. IPv4Enable: true,
  30. IPv6Enable: true,
  31. })
  32. if len(ips) != 1 {
  33. t.Error("expect 1 IP, but got ", len(ips))
  34. }
  35. if diff := cmp.Diff([]byte(ips[0]), []byte{1, 1, 1, 1}); diff != "" {
  36. t.Error(diff)
  37. }
  38. }
  39. {
  40. ips := hosts.LookupIP("www.v2ray.cn", IPOption{
  41. IPv4Enable: true,
  42. IPv6Enable: true,
  43. })
  44. if len(ips) != 1 {
  45. t.Error("expect 1 IP, but got ", len(ips))
  46. }
  47. if diff := cmp.Diff([]byte(ips[0]), []byte{2, 2, 2, 2}); diff != "" {
  48. t.Error(diff)
  49. }
  50. }
  51. }