filter_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package ldap
  2. import (
  3. "testing"
  4. "github.com/gogits/gogs/modules/asn1-ber"
  5. )
  6. type compileTest struct {
  7. filterStr string
  8. filterType int
  9. }
  10. var testFilters = []compileTest{
  11. compileTest{filterStr: "(&(sn=Miller)(givenName=Bob))", filterType: FilterAnd},
  12. compileTest{filterStr: "(|(sn=Miller)(givenName=Bob))", filterType: FilterOr},
  13. compileTest{filterStr: "(!(sn=Miller))", filterType: FilterNot},
  14. compileTest{filterStr: "(sn=Miller)", filterType: FilterEqualityMatch},
  15. compileTest{filterStr: "(sn=Mill*)", filterType: FilterSubstrings},
  16. compileTest{filterStr: "(sn=*Mill)", filterType: FilterSubstrings},
  17. compileTest{filterStr: "(sn=*Mill*)", filterType: FilterSubstrings},
  18. compileTest{filterStr: "(sn>=Miller)", filterType: FilterGreaterOrEqual},
  19. compileTest{filterStr: "(sn<=Miller)", filterType: FilterLessOrEqual},
  20. compileTest{filterStr: "(sn=*)", filterType: FilterPresent},
  21. compileTest{filterStr: "(sn~=Miller)", filterType: FilterApproxMatch},
  22. // compileTest{ filterStr: "()", filterType: FilterExtensibleMatch },
  23. }
  24. func TestFilter(t *testing.T) {
  25. // Test Compiler and Decompiler
  26. for _, i := range testFilters {
  27. filter, err := CompileFilter(i.filterStr)
  28. if err != nil {
  29. t.Errorf("Problem compiling %s - %s", i.filterStr, err.Error())
  30. } else if filter.Tag != uint8(i.filterType) {
  31. t.Errorf("%q Expected %q got %q", i.filterStr, FilterMap[uint64(i.filterType)], FilterMap[uint64(filter.Tag)])
  32. } else {
  33. o, err := DecompileFilter(filter)
  34. if err != nil {
  35. t.Errorf("Problem compiling %s - %s", i.filterStr, err.Error())
  36. } else if i.filterStr != o {
  37. t.Errorf("%q expected, got %q", i.filterStr, o)
  38. }
  39. }
  40. }
  41. }
  42. func BenchmarkFilterCompile(b *testing.B) {
  43. b.StopTimer()
  44. filters := make([]string, len(testFilters))
  45. // Test Compiler and Decompiler
  46. for idx, i := range testFilters {
  47. filters[idx] = i.filterStr
  48. }
  49. maxIdx := len(filters)
  50. b.StartTimer()
  51. for i := 0; i < b.N; i++ {
  52. CompileFilter(filters[i%maxIdx])
  53. }
  54. }
  55. func BenchmarkFilterDecompile(b *testing.B) {
  56. b.StopTimer()
  57. filters := make([]*ber.Packet, len(testFilters))
  58. // Test Compiler and Decompiler
  59. for idx, i := range testFilters {
  60. filters[idx], _ = CompileFilter(i.filterStr)
  61. }
  62. maxIdx := len(filters)
  63. b.StartTimer()
  64. for i := 0; i < b.N; i++ {
  65. DecompileFilter(filters[i%maxIdx])
  66. }
  67. }