dns_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "domain:example.com": "google.com"
  46. },
  47. "clientIp": "10.0.0.1"
  48. }`,
  49. Parser: parserCreator(),
  50. Output: &dns.Config{
  51. NameServer: []*dns.NameServer{
  52. {
  53. Address: &net.Endpoint{
  54. Address: &net.IPOrDomain{
  55. Address: &net.IPOrDomain_Ip{
  56. Ip: []byte{8, 8, 8, 8},
  57. },
  58. },
  59. Network: net.Network_UDP,
  60. Port: 5353,
  61. },
  62. PrioritizedDomain: []*dns.NameServer_PriorityDomain{
  63. {
  64. Type: dns.DomainMatchingType_Subdomain,
  65. Domain: "v2ray.com",
  66. },
  67. },
  68. },
  69. },
  70. StaticHosts: []*dns.Config_HostMapping{
  71. {
  72. Type: dns.DomainMatchingType_Subdomain,
  73. Domain: "example.com",
  74. ProxiedDomain: "google.com",
  75. },
  76. {
  77. Type: dns.DomainMatchingType_Full,
  78. Domain: "v2ray.com",
  79. Ip: [][]byte{{127, 0, 0, 1}},
  80. },
  81. },
  82. ClientIp: []byte{10, 0, 0, 1},
  83. },
  84. },
  85. })
  86. }