dns_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package conf_test
  2. import (
  3. "encoding/json"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/golang/protobuf/proto"
  8. "v2ray.com/core/app/dns"
  9. "v2ray.com/core/common"
  10. "v2ray.com/core/common/net"
  11. "v2ray.com/core/common/platform"
  12. "v2ray.com/core/common/platform/filesystem"
  13. . "v2ray.com/core/infra/conf"
  14. )
  15. func init() {
  16. wd, err := os.Getwd()
  17. common.Must(err)
  18. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geoip.dat"), filepath.Join(wd, "..", "..", "release", "config", "geoip.dat")))
  19. common.Must(filesystem.CopyFile(platform.GetAssetLocation("geosite.dat"), filepath.Join(wd, "..", "..", "release", "config", "geosite.dat")))
  20. }
  21. func TestDnsConfigParsing(t *testing.T) {
  22. geositePath := platform.GetAssetLocation("geosite.dat")
  23. defer func() {
  24. os.Remove(geositePath)
  25. }()
  26. parserCreator := func() func(string) (proto.Message, error) {
  27. return func(s string) (proto.Message, error) {
  28. config := new(DnsConfig)
  29. if err := json.Unmarshal([]byte(s), config); err != nil {
  30. return nil, err
  31. }
  32. return config.Build()
  33. }
  34. }
  35. runMultiTestCase(t, []TestCase{
  36. {
  37. Input: `{
  38. "servers": [{
  39. "address": "8.8.8.8",
  40. "port": 5353,
  41. "domains": ["domain:v2ray.com"]
  42. }],
  43. "hosts": {
  44. "v2ray.com": "127.0.0.1",
  45. "geosite:tld-cn": "10.0.0.1",
  46. "domain:example.com": "google.com"
  47. },
  48. "clientIp": "10.0.0.1"
  49. }`,
  50. Parser: parserCreator(),
  51. Output: &dns.Config{
  52. NameServer: []*dns.NameServer{
  53. {
  54. Address: &net.Endpoint{
  55. Address: &net.IPOrDomain{
  56. Address: &net.IPOrDomain_Ip{
  57. Ip: []byte{8, 8, 8, 8},
  58. },
  59. },
  60. Network: net.Network_UDP,
  61. Port: 5353,
  62. },
  63. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  64. {
  65. Type: dns.DomainMatchingType_Subdomain,
  66. Domain: "v2ray.com",
  67. },
  68. },
  69. },
  70. },
  71. StaticHosts: []*dns.Config_HostMapping{
  72. {
  73. Type: dns.DomainMatchingType_Subdomain,
  74. Domain: "example.com",
  75. ProxiedDomain: "google.com",
  76. },
  77. {
  78. Type: dns.DomainMatchingType_Subdomain,
  79. Domain: "cn",
  80. Ip: [][]byte{{10, 0, 0, 1}},
  81. },
  82. {
  83. Type: dns.DomainMatchingType_Subdomain,
  84. Domain: "xn--fiqs8s",
  85. Ip: [][]byte{{10, 0, 0, 1}},
  86. },
  87. {
  88. Type: dns.DomainMatchingType_Full,
  89. Domain: "v2ray.com",
  90. Ip: [][]byte{{127, 0, 0, 1}},
  91. },
  92. },
  93. ClientIp: []byte{10, 0, 0, 1},
  94. },
  95. },
  96. })
  97. }