-
Notifications
You must be signed in to change notification settings - Fork 27
/
filter_test.go
142 lines (127 loc) · 4.17 KB
/
filter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package ldap
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/mavricknz/asn1-ber"
"testing"
)
type compile_test struct {
filter_str string
filter_type int
}
var test_filters = []compile_test{
compile_test{filter_str: "(&(sn=Miller)(givenName=Bob))", filter_type: FilterAnd},
compile_test{filter_str: "(|(sn=Miller)(givenName=Bob))", filter_type: FilterOr},
compile_test{filter_str: "(!(sn=Miller))", filter_type: FilterNot},
compile_test{filter_str: "(sn=Miller)", filter_type: FilterEqualityMatch},
compile_test{filter_str: "(sn=Mill*)", filter_type: FilterSubstrings},
compile_test{filter_str: "(sn=*Mill)", filter_type: FilterSubstrings},
compile_test{filter_str: "(sn=*Mill*)", filter_type: FilterSubstrings},
compile_test{filter_str: "(sn>=Miller)", filter_type: FilterGreaterOrEqual},
compile_test{filter_str: "(sn<=Miller)", filter_type: FilterLessOrEqual},
compile_test{filter_str: "(sn=*)", filter_type: FilterPresent},
compile_test{filter_str: "(sn~=Miller)", filter_type: FilterApproxMatch},
// compile_test{ filter_str: "(cn:dn:=People)", filter_type: FilterExtensibleMatch },
}
type encoded_test struct {
filter_str string
// reference good values
filter_encoded string
}
var encode_filters = []encoded_test{
encoded_test{
"(|(cn:dn:=people)(cn=xxx*yyy*zzz)(cn=*)(phones>=1))",
"a139a90f8202636e830670656f706c658401ffa4150402636e300f8003787878810379797982037a7a7a8702636ea50b040670686f6e6573040131",
},
}
func TestFilter(t *testing.T) {
// Test Compiler and Decompiler
for _, i := range test_filters {
filter, err := CompileFilter(i.filter_str)
if err != nil {
t.Errorf("Problem compiling %s - %s", i.filter_str, err)
} else if filter.Tag != uint8(i.filter_type) {
t.Errorf("%q Expected %q got %q", i.filter_str, FilterMap[uint64(i.filter_type)], FilterMap[uint64(filter.Tag)])
} else {
o, err := DecompileFilter(filter)
if err != nil {
t.Errorf("Problem DecompileCompiling %s - %s", i, err)
} else if i.filter_str != o {
t.Errorf("%q expected, got %q", i.filter_str, o)
}
}
}
}
func TestFilterEncode(t *testing.T) {
for _, i := range encode_filters {
p, err := CompileFilter(i.filter_str)
if err != nil {
t.Errorf("Problem compiling %s - %s\n", i.filter_str, err)
}
fBytes, error := hex.DecodeString(i.filter_encoded)
if error != nil {
t.Errorf("Error decoding byte string: %s\n", i.filter_encoded)
}
if !bytes.Equal(p.Bytes(), fBytes) {
l := len(fBytes)
pBytes := p.Bytes()
if l > len(pBytes) {
l = len(pBytes)
}
for i := 0; i < l; i++ {
if pBytes[i] != fBytes[i] {
l = i
break
}
}
t.Errorf("Filter does not match ref bytes (first difference at byte %d) %s\n\n%s\n%s",
l, i.filter_str, hex.Dump(p.Bytes()), hex.Dump(fBytes))
}
}
}
func TestFilterValueUnescape(t *testing.T) {
filter := `cn=abc \(123\) \28bob\29 \\\\ \*`
filterStandard := `cn=abc (123) (bob) \\ *`
filterUnescaped := UnescapeFilterValue(filter)
if filterUnescaped != filterStandard {
t.Errorf("Standard and Unescaped filter do not match [%s] != [%s]\n", filterStandard, filterUnescaped)
}
fmt.Printf("filter : %s\n", filter)
fmt.Printf("filter Standard : %s\n", filterStandard)
fmt.Printf("filter Unescaped : %s\n", UnescapeFilterValue(filter))
}
func TestFilterValueEscape(t *testing.T) {
filter := "£¥©" + `(*\)`
filterStandard := `\a3\a5\a9\28\2a\5c\29`
filterEscaped := EscapeFilterValue(filter)
if filterEscaped != filterStandard {
t.Errorf("Standard and Escaped filter do not match [%s] != [%s]\n", filterStandard, filterEscaped)
}
}
func BenchmarkFilterCompile(b *testing.B) {
b.StopTimer()
filters := make([]string, len(test_filters))
// Test Compiler and Decompiler
for idx, i := range test_filters {
filters[idx] = i.filter_str
}
max_idx := len(filters)
b.StartTimer()
for i := 0; i < b.N; i++ {
CompileFilter(filters[i%max_idx])
}
}
func BenchmarkFilterDecompile(b *testing.B) {
b.StopTimer()
filters := make([]*ber.Packet, len(test_filters))
// Test Compiler and Decompiler
for idx, i := range test_filters {
filters[idx], _ = CompileFilter(i.filter_str)
}
max_idx := len(filters)
b.StartTimer()
for i := 0; i < b.N; i++ {
DecompileFilter(filters[i%max_idx])
}
}