hosts_test.go 901 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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")
  29. if len(ips) != 1 {
  30. t.Error("expect 1 IP, but got ", len(ips))
  31. }
  32. if diff := cmp.Diff([]byte(ips[0]), []byte{1, 1, 1, 1}); diff != "" {
  33. t.Error(diff)
  34. }
  35. }
  36. {
  37. ips := hosts.LookupIP("www.v2ray.cn")
  38. if len(ips) != 1 {
  39. t.Error("expect 1 IP, but got ", len(ips))
  40. }
  41. if diff := cmp.Diff([]byte(ips[0]), []byte{2, 2, 2, 2}); diff != "" {
  42. t.Error(diff)
  43. }
  44. }
  45. }