convertor_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package adguard
  2. import (
  3. "context"
  4. "strings"
  5. "testing"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/route/rule"
  8. "github.com/sagernet/sing/common/logger"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestConverter(t *testing.T) {
  12. t.Parallel()
  13. ruleString := `||sagernet.org^$important
  14. @@|sing-box.sagernet.org^$important
  15. ||example.org^
  16. |example.com^
  17. example.net^
  18. ||example.edu
  19. ||example.edu.tw^
  20. |example.gov
  21. example.arpa
  22. @@|sagernet.example.org^
  23. `
  24. rules, err := ToOptions(strings.NewReader(ruleString), logger.NOP())
  25. require.NoError(t, err)
  26. require.Len(t, rules, 1)
  27. rule, err := rule.NewHeadlessRule(context.Background(), rules[0])
  28. require.NoError(t, err)
  29. matchDomain := []string{
  30. "example.org",
  31. "www.example.org",
  32. "example.com",
  33. "example.net",
  34. "isexample.net",
  35. "www.example.net",
  36. "example.edu",
  37. "example.edu.cn",
  38. "example.edu.tw",
  39. "www.example.edu",
  40. "www.example.edu.cn",
  41. "example.gov",
  42. "example.gov.cn",
  43. "example.arpa",
  44. "www.example.arpa",
  45. "isexample.arpa",
  46. "example.arpa.cn",
  47. "www.example.arpa.cn",
  48. "isexample.arpa.cn",
  49. "sagernet.org",
  50. "www.sagernet.org",
  51. }
  52. notMatchDomain := []string{
  53. "example.org.cn",
  54. "notexample.org",
  55. "example.com.cn",
  56. "www.example.com.cn",
  57. "example.net.cn",
  58. "notexample.edu",
  59. "notexample.edu.cn",
  60. "www.example.gov",
  61. "notexample.gov",
  62. "sagernet.example.org",
  63. "sing-box.sagernet.org",
  64. }
  65. for _, domain := range matchDomain {
  66. require.True(t, rule.Match(&adapter.InboundContext{
  67. Domain: domain,
  68. }), domain)
  69. }
  70. for _, domain := range notMatchDomain {
  71. require.False(t, rule.Match(&adapter.InboundContext{
  72. Domain: domain,
  73. }), domain)
  74. }
  75. ruleFromOptions, err := FromOptions(rules)
  76. require.NoError(t, err)
  77. require.Equal(t, ruleString, string(ruleFromOptions))
  78. }
  79. func TestHosts(t *testing.T) {
  80. t.Parallel()
  81. rules, err := ToOptions(strings.NewReader(`
  82. 127.0.0.1 localhost
  83. ::1 localhost #[IPv6]
  84. 0.0.0.0 google.com
  85. `), logger.NOP())
  86. require.NoError(t, err)
  87. require.Len(t, rules, 1)
  88. rule, err := rule.NewHeadlessRule(context.Background(), rules[0])
  89. require.NoError(t, err)
  90. matchDomain := []string{
  91. "google.com",
  92. }
  93. notMatchDomain := []string{
  94. "www.google.com",
  95. "notgoogle.com",
  96. "localhost",
  97. }
  98. for _, domain := range matchDomain {
  99. require.True(t, rule.Match(&adapter.InboundContext{
  100. Domain: domain,
  101. }), domain)
  102. }
  103. for _, domain := range notMatchDomain {
  104. require.False(t, rule.Match(&adapter.InboundContext{
  105. Domain: domain,
  106. }), domain)
  107. }
  108. }
  109. func TestSimpleHosts(t *testing.T) {
  110. t.Parallel()
  111. rules, err := ToOptions(strings.NewReader(`
  112. example.com
  113. www.example.org
  114. `), logger.NOP())
  115. require.NoError(t, err)
  116. require.Len(t, rules, 1)
  117. rule, err := rule.NewHeadlessRule(context.Background(), rules[0])
  118. require.NoError(t, err)
  119. matchDomain := []string{
  120. "example.com",
  121. "www.example.org",
  122. }
  123. notMatchDomain := []string{
  124. "example.com.cn",
  125. "www.example.com",
  126. "notexample.com",
  127. "example.org",
  128. }
  129. for _, domain := range matchDomain {
  130. require.True(t, rule.Match(&adapter.InboundContext{
  131. Domain: domain,
  132. }), domain)
  133. }
  134. for _, domain := range notMatchDomain {
  135. require.False(t, rule.Match(&adapter.InboundContext{
  136. Domain: domain,
  137. }), domain)
  138. }
  139. }