matchergroup_full_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package strmatcher_test
  2. import (
  3. "reflect"
  4. "testing"
  5. . "github.com/v2fly/v2ray-core/v5/common/strmatcher"
  6. )
  7. func TestFullMatcherGroup(t *testing.T) {
  8. patterns := []struct {
  9. Pattern string
  10. Value uint32
  11. }{
  12. {
  13. Pattern: "v2fly.org",
  14. Value: 1,
  15. },
  16. {
  17. Pattern: "google.com",
  18. Value: 2,
  19. },
  20. {
  21. Pattern: "x.a.com",
  22. Value: 3,
  23. },
  24. {
  25. Pattern: "x.y.com",
  26. Value: 4,
  27. },
  28. {
  29. Pattern: "x.y.com",
  30. Value: 6,
  31. },
  32. }
  33. testCases := []struct {
  34. Domain string
  35. Result []uint32
  36. }{
  37. {
  38. Domain: "v2fly.org",
  39. Result: []uint32{1},
  40. },
  41. {
  42. Domain: "y.com",
  43. Result: nil,
  44. },
  45. {
  46. Domain: "x.y.com",
  47. Result: []uint32{4, 6},
  48. },
  49. }
  50. g := new(FullMatcherGroup)
  51. for _, pattern := range patterns {
  52. AddMatcherToGroup(g, FullMatcher(pattern.Pattern), pattern.Value)
  53. }
  54. for _, testCase := range testCases {
  55. r := g.Match(testCase.Domain)
  56. if !reflect.DeepEqual(r, testCase.Result) {
  57. t.Error("Failed to match domain: ", testCase.Domain, ", expect ", testCase.Result, ", but got ", r)
  58. }
  59. }
  60. }
  61. func TestEmptyFullMatcherGroup(t *testing.T) {
  62. g := new(FullMatcherGroup)
  63. r := g.Match("v2fly.org")
  64. if len(r) != 0 {
  65. t.Error("Expect [], but ", r)
  66. }
  67. }