condition_serialize_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package router_test
  2. import (
  3. "bytes"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/require"
  8. "github.com/xtls/xray-core/app/router"
  9. "github.com/xtls/xray-core/common/platform/filesystem"
  10. )
  11. func TestDomainMatcherSerialization(t *testing.T) {
  12. domains := []*router.Domain{
  13. {Type: router.Domain_Domain, Value: "google.com"},
  14. {Type: router.Domain_Domain, Value: "v2ray.com"},
  15. {Type: router.Domain_Full, Value: "full.example.com"},
  16. }
  17. var buf bytes.Buffer
  18. if err := router.SerializeDomainMatcher(domains, &buf); err != nil {
  19. t.Fatalf("Serialize failed: %v", err)
  20. }
  21. matcher, err := router.NewDomainMatcherFromBuffer(buf.Bytes())
  22. if err != nil {
  23. t.Fatalf("Deserialize failed: %v", err)
  24. }
  25. dMatcher := &router.DomainMatcher{
  26. Matchers: matcher,
  27. }
  28. testCases := []struct {
  29. Input string
  30. Match bool
  31. }{
  32. {"google.com", true},
  33. {"maps.google.com", true},
  34. {"v2ray.com", true},
  35. {"full.example.com", true},
  36. {"example.com", false},
  37. }
  38. for _, tc := range testCases {
  39. if res := dMatcher.ApplyDomain(tc.Input); res != tc.Match {
  40. t.Errorf("Match(%s) = %v, want %v", tc.Input, res, tc.Match)
  41. }
  42. }
  43. }
  44. func TestGeoSiteSerialization(t *testing.T) {
  45. sites := []*router.GeoSite{
  46. {
  47. CountryCode: "CN",
  48. Domain: []*router.Domain{
  49. {Type: router.Domain_Domain, Value: "baidu.cn"},
  50. {Type: router.Domain_Domain, Value: "qq.com"},
  51. },
  52. },
  53. {
  54. CountryCode: "US",
  55. Domain: []*router.Domain{
  56. {Type: router.Domain_Domain, Value: "google.com"},
  57. {Type: router.Domain_Domain, Value: "facebook.com"},
  58. },
  59. },
  60. }
  61. var buf bytes.Buffer
  62. if err := router.SerializeGeoSiteList(sites, nil, nil, &buf); err != nil {
  63. t.Fatalf("SerializeGeoSiteList failed: %v", err)
  64. }
  65. tmp := t.TempDir()
  66. path := filepath.Join(tmp, "matcher.cache")
  67. f, err := os.Create(path)
  68. require.NoError(t, err)
  69. _, err = f.Write(buf.Bytes())
  70. require.NoError(t, err)
  71. f.Close()
  72. f, err = os.Open(path)
  73. require.NoError(t, err)
  74. defer f.Close()
  75. require.NoError(t, err)
  76. data, _ := filesystem.ReadFile(path)
  77. // cn
  78. gp, err := router.LoadGeoSiteMatcher(bytes.NewReader(data), "CN")
  79. if err != nil {
  80. t.Fatalf("LoadGeoSiteMatcher(CN) failed: %v", err)
  81. }
  82. cnMatcher := &router.DomainMatcher{
  83. Matchers: gp,
  84. }
  85. if !cnMatcher.ApplyDomain("baidu.cn") {
  86. t.Error("CN matcher should match baidu.cn")
  87. }
  88. if cnMatcher.ApplyDomain("google.com") {
  89. t.Error("CN matcher should NOT match google.com")
  90. }
  91. // us
  92. gp, err = router.LoadGeoSiteMatcher(bytes.NewReader(data), "US")
  93. if err != nil {
  94. t.Fatalf("LoadGeoSiteMatcher(US) failed: %v", err)
  95. }
  96. usMatcher := &router.DomainMatcher{
  97. Matchers: gp,
  98. }
  99. if !usMatcher.ApplyDomain("google.com") {
  100. t.Error("US matcher should match google.com")
  101. }
  102. if usMatcher.ApplyDomain("baidu.cn") {
  103. t.Error("US matcher should NOT match baidu.cn")
  104. }
  105. // unknown
  106. _, err = router.LoadGeoSiteMatcher(bytes.NewReader(data), "unknown")
  107. if err == nil {
  108. t.Error("LoadGeoSiteMatcher(unknown) should fail")
  109. }
  110. }
  111. func TestGeoSiteSerializationWithDeps(t *testing.T) {
  112. sites := []*router.GeoSite{
  113. {
  114. CountryCode: "geosite:cn",
  115. Domain: []*router.Domain{
  116. {Type: router.Domain_Domain, Value: "baidu.cn"},
  117. },
  118. },
  119. {
  120. CountryCode: "geosite:google@cn",
  121. Domain: []*router.Domain{
  122. {Type: router.Domain_Domain, Value: "google.cn"},
  123. },
  124. },
  125. {
  126. CountryCode: "rule-1",
  127. Domain: []*router.Domain{
  128. {Type: router.Domain_Domain, Value: "google.com"},
  129. },
  130. },
  131. }
  132. deps := map[string][]string{
  133. "rule-1": {"geosite:cn", "geosite:google@cn"},
  134. }
  135. var buf bytes.Buffer
  136. err := router.SerializeGeoSiteList(sites, deps, nil, &buf)
  137. require.NoError(t, err)
  138. matcher, err := router.LoadGeoSiteMatcher(bytes.NewReader(buf.Bytes()), "rule-1")
  139. require.NoError(t, err)
  140. require.True(t, matcher.Match("google.com") != nil)
  141. require.True(t, matcher.Match("baidu.cn") != nil)
  142. require.True(t, matcher.Match("google.cn") != nil)
  143. }