convertor_test.go 2.9 KB

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