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