matchers_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package strmatcher_test
  2. import (
  3. "testing"
  4. "github.com/xtls/xray-core/common"
  5. . "github.com/xtls/xray-core/common/strmatcher"
  6. )
  7. func TestMatcher(t *testing.T) {
  8. cases := []struct {
  9. pattern string
  10. mType Type
  11. input string
  12. output bool
  13. }{
  14. {
  15. pattern: "example.com",
  16. mType: Domain,
  17. input: "www.example.com",
  18. output: true,
  19. },
  20. {
  21. pattern: "example.com",
  22. mType: Domain,
  23. input: "example.com",
  24. output: true,
  25. },
  26. {
  27. pattern: "example.com",
  28. mType: Domain,
  29. input: "www.fxample.com",
  30. output: false,
  31. },
  32. {
  33. pattern: "example.com",
  34. mType: Domain,
  35. input: "xample.com",
  36. output: false,
  37. },
  38. {
  39. pattern: "example.com",
  40. mType: Domain,
  41. input: "xexample.com",
  42. output: false,
  43. },
  44. {
  45. pattern: "example.com",
  46. mType: Full,
  47. input: "example.com",
  48. output: true,
  49. },
  50. {
  51. pattern: "example.com",
  52. mType: Full,
  53. input: "xexample.com",
  54. output: false,
  55. },
  56. {
  57. pattern: "example.com",
  58. mType: Regex,
  59. input: "examplexcom",
  60. output: true,
  61. },
  62. }
  63. for _, test := range cases {
  64. matcher, err := test.mType.New(test.pattern)
  65. common.Must(err)
  66. if m := matcher.Match(test.input); m != test.output {
  67. t.Error("unexpected output: ", m, " for test case ", test)
  68. }
  69. }
  70. }