1 """
2 Test cases for ldaptor.protocols.ldap.ldapserver module.
3 """
4
5 from twisted.trial import unittest
6 from ldaptor import inmemory
7 from ldaptor.protocols import pureldap, pureber
8 from ldaptor.protocols.ldap import ldapsyntax
9
10 -class TestEntryMatch(unittest.TestCase):
11 - def test_matchAll(self):
12 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
13 attributes={
14 'objectClass': ['a', 'b'],
15 'aValue': ['a'],
16 'bValue': ['b'],
17 })
18 result = o.match(pureldap.LDAPFilterMatchAll)
19 self.assertEquals(result, True)
20
22 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
23 attributes={
24 'objectClass': ['a', 'b'],
25 'aValue': ['a'],
26 'bValue': ['b'],
27 })
28 result = o.match(pureldap.LDAPFilter_present('aValue'))
29 self.assertEquals(result, True)
30
31
33 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
34 attributes={
35 'objectClass': ['a', 'b'],
36 'aValue': ['a'],
37 'bValue': ['b'],
38 })
39 result = o.match(pureldap.LDAPFilter_present('noSuchValue'))
40 self.assertEquals(result, False)
41
42 - def test_and_match(self):
43 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
44 attributes={
45 'objectClass': ['a', 'b'],
46 'aValue': ['a'],
47 'bValue': ['b'],
48 })
49 result = o.match(
50 pureldap.LDAPFilter_and([
51 pureldap.LDAPFilter_present('aValue'),
52 pureldap.LDAPFilter_present('bValue'),
53 ]))
54 self.assertEquals(result, True)
55
57 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
58 attributes={
59 'objectClass': ['a', 'b'],
60 'aValue': ['a'],
61 'bValue': ['b'],
62 })
63 result = o.match(
64 pureldap.LDAPFilter_and([
65 pureldap.LDAPFilter_present('cValue'),
66 pureldap.LDAPFilter_present('dValue'),
67 ]))
68 self.assertEquals(result, False)
69
70 - def test_or_match(self):
71 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
72 attributes={
73 'objectClass': ['a', 'b'],
74 'aValue': ['a'],
75 'bValue': ['b'],
76 })
77 result = o.match(
78 pureldap.LDAPFilter_or([
79 pureldap.LDAPFilter_present('cValue'),
80 pureldap.LDAPFilter_present('bValue'),
81 ]))
82 self.assertEquals(result, True)
83
85 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
86 attributes={
87 'objectClass': ['a', 'b'],
88 'aValue': ['a'],
89 'bValue': ['b'],
90 })
91 result = o.match(
92 pureldap.LDAPFilter_or([
93 pureldap.LDAPFilter_present('cValue'),
94 pureldap.LDAPFilter_present('dValue'),
95 ]))
96 self.assertEquals(result, False)
97
99 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
100 attributes={
101 'objectClass': ['a', 'b'],
102 'aValue': ['a'],
103 'bValue': ['b'],
104 })
105 result = o.match(
106 pureldap.LDAPFilter_not(
107 pureldap.LDAPFilter_or([
108 pureldap.LDAPFilter_present('cValue'),
109 pureldap.LDAPFilter_present('dValue'),
110 ])))
111 self.assertEquals(result, True)
112
114 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
115 attributes={
116 'objectClass': ['a', 'b'],
117 'aValue': ['a'],
118 'bValue': ['b'],
119 })
120 result = o.match(pureldap.LDAPFilter_equalityMatch(
121 attributeDesc=pureber.BEROctetString('aValue'),
122 assertionValue=pureber.BEROctetString('a')))
123 self.assertEquals(result, True)
124
126 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
127 attributes={
128 'objectClass': ['a', 'b'],
129 'aValue': ['a'],
130 'bValue': ['b'],
131 })
132 result = o.match(pureldap.LDAPFilter_equalityMatch(
133 attributeDesc=pureber.BEROctetString('avaLUe'),
134 assertionValue=pureber.BEROctetString('A')))
135 self.assertEquals(result, True)
136
137
139 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
140 attributes={
141 'objectClass': ['a', 'b'],
142 'aValue': ['a'],
143 'bValue': ['b'],
144 })
145 result = o.match(pureldap.LDAPFilter_equalityMatch(
146 attributeDesc=pureber.BEROctetString('aValue'),
147 assertionValue=pureber.BEROctetString('b')))
148 self.assertEquals(result, False)
149
151 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
152 attributes={
153 'objectClass': ['a', 'b'],
154 'aValue': ['a'],
155 'bValue': ['b'],
156 })
157 result = o.match(pureldap.LDAPFilter_substrings(
158 type='aValue',
159 substrings=[
160 pureldap.LDAPFilter_substrings_initial('a'),
161 ]))
162 self.assertEquals(result, True)
163
165 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
166 attributes={
167 'objectClass': ['a', 'b'],
168 'aValue': ['abcde'],
169 'bValue': ['b'],
170 })
171 result = o.match(pureldap.LDAPFilter_substrings(
172 type='aValue',
173 substrings=[
174 pureldap.LDAPFilter_substrings_initial('a'),
175 pureldap.LDAPFilter_substrings_final('e'),
176 ]))
177 self.assertEquals(result, True)
178
180 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
181 attributes={
182 'objectClass': ['a', 'b'],
183 'aValue': ['abcde'],
184 'bValue': ['b'],
185 })
186 result = o.match(pureldap.LDAPFilter_substrings(
187 type='aValue',
188 substrings=[
189 pureldap.LDAPFilter_substrings_initial('a'),
190 pureldap.LDAPFilter_substrings_any('c'),
191 pureldap.LDAPFilter_substrings_final('e'),
192 ]))
193 self.assertEquals(result, True)
194
196 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
197 attributes={
198 'objectClass': ['a', 'b'],
199 'aValue': ['abcde'],
200 'bValue': ['b'],
201 })
202 result = o.match(pureldap.LDAPFilter_substrings(
203 type='aValue',
204 substrings=[
205 pureldap.LDAPFilter_substrings_initial('a'),
206 pureldap.LDAPFilter_substrings_any('b'),
207 pureldap.LDAPFilter_substrings_any('c'),
208 pureldap.LDAPFilter_substrings_any('d'),
209 pureldap.LDAPFilter_substrings_final('e'),
210 ]))
211 self.assertEquals(result, True)
212
214 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
215 attributes={
216 'objectClass': ['a', 'b'],
217 'aValue': ['aoeuboeucoeudoeue'],
218 'bValue': ['b'],
219 })
220 result = o.match(pureldap.LDAPFilter_substrings(
221 type='aValue',
222 substrings=[
223 pureldap.LDAPFilter_substrings_initial('a'),
224 pureldap.LDAPFilter_substrings_any('b'),
225 pureldap.LDAPFilter_substrings_any('c'),
226 pureldap.LDAPFilter_substrings_any('d'),
227 pureldap.LDAPFilter_substrings_final('e'),
228 ]))
229 self.assertEquals(result, True)
230
232 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
233 attributes={
234 'objectClass': ['a', 'b'],
235 'aValue': ['aBCdE'],
236 'bValue': ['b'],
237 })
238 result = o.match(pureldap.LDAPFilter_substrings(
239 type='aValue',
240 substrings=[
241 pureldap.LDAPFilter_substrings_initial('A'),
242 pureldap.LDAPFilter_substrings_any('b'),
243 pureldap.LDAPFilter_substrings_any('C'),
244 pureldap.LDAPFilter_substrings_any('D'),
245 pureldap.LDAPFilter_substrings_final('e'),
246 ]))
247 self.assertEquals(result, True)
248
250 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
251 attributes={
252 'objectClass': ['a', 'b'],
253 'aValue': ['Foo'],
254 })
255 result = o.match(pureldap.LDAPFilter_substrings(
256 type='aValue',
257 substrings=[
258 pureldap.LDAPFilter_substrings_initial('f'),
259 ]))
260 self.assertEquals(result, True)
261
263 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
264 attributes={
265 'objectClass': ['a', 'b'],
266 'aValue': ['a'],
267 'bValue': ['b'],
268 })
269 result = o.match(pureldap.LDAPFilter_substrings(
270 type='aValue',
271 substrings=[
272 pureldap.LDAPFilter_substrings_initial('bad'),
273 pureldap.LDAPFilter_substrings_any('dog'),
274 pureldap.LDAPFilter_substrings_any('no'),
275 pureldap.LDAPFilter_substrings_final('bone'),
276 ]))
277 self.assertEquals(result, False)
278
280 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
281 attributes={
282 'objectClass': ['a', 'b'],
283 'aValue': ['aoeuboeucoeudoeue'],
284 'bValue': ['b'],
285 })
286 result = o.match(pureldap.LDAPFilter_substrings(
287 type='aValue',
288 substrings=[
289 pureldap.LDAPFilter_substrings_initial('a'),
290 pureldap.LDAPFilter_substrings_any('b'),
291 pureldap.LDAPFilter_substrings_any('Z'),
292 pureldap.LDAPFilter_substrings_any('d'),
293 pureldap.LDAPFilter_substrings_final('e'),
294 ]))
295 self.assertEquals(result, False)
296
298 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
299 attributes={
300 'objectClass': ['a', 'b'],
301 'aValue': ['b'],
302 'num': [4],
303 })
304 result = o.match(pureldap.LDAPFilter_greaterOrEqual('foo',
305 42))
306 self.assertEquals(result, False)
307
309 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
310 attributes={
311 'objectClass': ['a', 'b'],
312 'aValue': ['b'],
313 'num': [4],
314 })
315 result = o.match(pureldap.LDAPFilter_greaterOrEqual('num',
316 3))
317 self.assertEquals(result, True)
318
320 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
321 attributes={
322 'objectClass': ['a', 'b'],
323 'aValue': ['b'],
324 'num': [4],
325 })
326 result = o.match(pureldap.LDAPFilter_greaterOrEqual('num',
327 4))
328 self.assertEquals(result, True)
329
331 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
332 attributes={
333 'objectClass': ['a', 'b'],
334 'aValue': ['b'],
335 'bValue': [4],
336 })
337 result = o.match(pureldap.LDAPFilter_greaterOrEqual('num',
338 5))
339 self.assertEquals(result, False)
340
341
343 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
344 attributes={
345 'objectClass': ['a', 'b'],
346 'aValue': ['b'],
347 'num': [4],
348 })
349 result = o.match(pureldap.LDAPFilter_lessOrEqual('foo',
350 42))
351 self.assertEquals(result, False)
352
354 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
355 attributes={
356 'objectClass': ['a', 'b'],
357 'aValue': ['b'],
358 'num': [4],
359 })
360 result = o.match(pureldap.LDAPFilter_lessOrEqual('num',
361 5))
362 self.assertEquals(result, True)
363
365 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
366 attributes={
367 'objectClass': ['a', 'b'],
368 'aValue': ['b'],
369 'num': [4],
370 })
371 result = o.match(pureldap.LDAPFilter_lessOrEqual('num',
372 4))
373 self.assertEquals(result, True)
374
376 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
377 attributes={
378 'objectClass': ['a', 'b'],
379 'aValue': ['b'],
380 'num': [4],
381 })
382 result = o.match(pureldap.LDAPFilter_lessOrEqual('num',
383 3))
384 self.assertEquals(result, False)
385
387 o=inmemory.ReadOnlyInMemoryLDAPEntry(dn='cn=foo,dc=example,dc=com',
388 attributes={
389 'objectClass': ['a', 'b'],
390 'aValue': ['b'],
391 'num': [4],
392 })
393 class UnknownMatch(object): pass
394 unknownMatch = UnknownMatch()
395 self.assertRaises(ldapsyntax.MatchNotImplemented,
396 o.match, unknownMatch)
397
398
399
400
401