matchergroup_substr_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package strmatcher_test
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/v2fly/v2ray-core/v4/common"
  6. . "github.com/v2fly/v2ray-core/v4/common/strmatcher"
  7. )
  8. func TestSubstrMatcherGroup(t *testing.T) {
  9. patterns := []struct {
  10. pattern string
  11. mType Type
  12. }{
  13. {
  14. pattern: "apis",
  15. mType: Substr,
  16. },
  17. {
  18. pattern: "google",
  19. mType: Substr,
  20. },
  21. {
  22. pattern: "apis",
  23. mType: Substr,
  24. },
  25. }
  26. cases := []struct {
  27. input string
  28. output []uint32
  29. }{
  30. {
  31. input: "google.com",
  32. output: []uint32{1},
  33. },
  34. {
  35. input: "apis.com",
  36. output: []uint32{0, 2},
  37. },
  38. {
  39. input: "googleapis.com",
  40. output: []uint32{1, 0, 2},
  41. },
  42. {
  43. input: "fonts.googleapis.com",
  44. output: []uint32{1, 0, 2},
  45. },
  46. {
  47. input: "apis.googleapis.com",
  48. output: []uint32{0, 2, 1, 0, 2},
  49. },
  50. }
  51. matcherGroup := &SubstrMatcherGroup{}
  52. for id, entry := range patterns {
  53. matcher, err := entry.mType.New(entry.pattern)
  54. common.Must(err)
  55. common.Must(AddMatcherToGroup(matcherGroup, matcher, uint32(id)))
  56. }
  57. for _, test := range cases {
  58. if r := matcherGroup.Match(test.input); !reflect.DeepEqual(r, test.output) {
  59. t.Error("unexpected output: ", r, " for test case ", test)
  60. }
  61. }
  62. }