matchers_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package strmatcher_test
  2. import (
  3. "reflect"
  4. "testing"
  5. "unsafe"
  6. "github.com/v2fly/v2ray-core/v5/common"
  7. . "github.com/v2fly/v2ray-core/v5/common/strmatcher"
  8. )
  9. func TestMatcher(t *testing.T) {
  10. cases := []struct {
  11. pattern string
  12. mType Type
  13. input string
  14. output bool
  15. }{
  16. {
  17. pattern: "v2fly.org",
  18. mType: Domain,
  19. input: "www.v2fly.org",
  20. output: true,
  21. },
  22. {
  23. pattern: "v2fly.org",
  24. mType: Domain,
  25. input: "v2fly.org",
  26. output: true,
  27. },
  28. {
  29. pattern: "v2fly.org",
  30. mType: Domain,
  31. input: "www.v3fly.org",
  32. output: false,
  33. },
  34. {
  35. pattern: "v2fly.org",
  36. mType: Domain,
  37. input: "2fly.org",
  38. output: false,
  39. },
  40. {
  41. pattern: "v2fly.org",
  42. mType: Domain,
  43. input: "xv2fly.org",
  44. output: false,
  45. },
  46. {
  47. pattern: "v2fly.org",
  48. mType: Full,
  49. input: "v2fly.org",
  50. output: true,
  51. },
  52. {
  53. pattern: "v2fly.org",
  54. mType: Full,
  55. input: "xv2fly.org",
  56. output: false,
  57. },
  58. {
  59. pattern: "v2fly.org",
  60. mType: Regex,
  61. input: "v2flyxorg",
  62. output: true,
  63. },
  64. }
  65. for _, test := range cases {
  66. matcher, err := test.mType.New(test.pattern)
  67. common.Must(err)
  68. if m := matcher.Match(test.input); m != test.output {
  69. t.Error("unexpected output: ", m, " for test case ", test)
  70. }
  71. }
  72. }
  73. func TestToDomain(t *testing.T) {
  74. { // Test normal ASCII domain, which should not trigger new string data allocation
  75. input := "v2fly.org"
  76. domain, err := ToDomain(input)
  77. if err != nil {
  78. t.Error("unexpected error: ", err)
  79. }
  80. if domain != input {
  81. t.Error("unexpected output: ", domain, " for test case ", input)
  82. }
  83. if (*reflect.StringHeader)(unsafe.Pointer(&input)).Data != (*reflect.StringHeader)(unsafe.Pointer(&domain)).Data {
  84. t.Error("different string data of output: ", domain, " and test case ", input)
  85. }
  86. }
  87. { // Test ASCII domain containing upper case letter, which should be converted to lower case
  88. input := "v2FLY.oRg"
  89. domain, err := ToDomain(input)
  90. if err != nil {
  91. t.Error("unexpected error: ", err)
  92. }
  93. if domain != "v2fly.org" {
  94. t.Error("unexpected output: ", domain, " for test case ", input)
  95. }
  96. }
  97. { // Test internationalized domain, which should be translated to ASCII punycode
  98. input := "v2fly.公益"
  99. domain, err := ToDomain(input)
  100. if err != nil {
  101. t.Error("unexpected error: ", err)
  102. }
  103. if domain != "v2fly.xn--55qw42g" {
  104. t.Error("unexpected output: ", domain, " for test case ", input)
  105. }
  106. }
  107. { // Test internationalized domain containing upper case letter
  108. input := "v2FLY.公益"
  109. domain, err := ToDomain(input)
  110. if err != nil {
  111. t.Error("unexpected error: ", err)
  112. }
  113. if domain != "v2fly.xn--55qw42g" {
  114. t.Error("unexpected output: ", domain, " for test case ", input)
  115. }
  116. }
  117. { // Test domain name of invalid character, which should return with error
  118. input := "{"
  119. _, err := ToDomain(input)
  120. if err == nil {
  121. t.Error("unexpected non error for test case ", input)
  122. }
  123. }
  124. { // Test domain name containing a space, which should return with error
  125. input := "Mijia Cloud"
  126. _, err := ToDomain(input)
  127. if err == nil {
  128. t.Error("unexpected non error for test case ", input)
  129. }
  130. }
  131. { // Test domain name containing an underscore, which should return with error
  132. input := "Mijia_Cloud.com"
  133. _, err := ToDomain(input)
  134. if err == nil {
  135. t.Error("unexpected non error for test case ", input)
  136. }
  137. }
  138. { // Test internationalized domain containing invalid character
  139. input := "Mijia Cloud.公司"
  140. _, err := ToDomain(input)
  141. if err == nil {
  142. t.Error("unexpected non error for test case ", input)
  143. }
  144. }
  145. }