Debian Codegraph @

dirsrvtests/tests/suites/indexes/substring_index_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2026 Red Hat, Inc.
3 # All rights reserved.
4 #
5 # License: GPL (version 3 or any later version).
6 # See LICENSE for details.
7 # --- END COPYRIGHT BLOCK ---
8 #
9 import ldap
10 import logging
11 import os
12 import pytest
13
14 from lib389._constants import DEFAULT_BENAME, DEFAULT_SUFFIX
15 from lib389.idm.user import UserAccounts
16 from lib389.index import Index
17 from lib389.properties import TASK_WAIT
18 from lib389.tasks import Tasks
19 from test389.topologies import topology_st
20
21 pytestmark = pytest.mark.tier1
22
23 logging.getLogger(__name__).setLevel(logging.DEBUG)
24 log = logging.getLogger(__name__)
25
26 UID_INDEX_DN = f'cn=uid,cn=index,cn={DEFAULT_BENAME},cn=ldbm database,cn=plugins,cn=config'
27 UID_PREFIX = 'zzsubtest'
28
29
30 @pytest.fixture
31 def uid_index(topology_st):
32 """Get the uid index object and ensure clean state before/after each test."""
33 inst = topology_st.standalone
34 if not inst.status():
35 inst.start()
36 index = Index(inst, UID_INDEX_DN)
37
38 def _cleanup():
39 """Remove substring index config, ignore errors if attrs not present."""
40 if not inst.status():
41 inst.start()
42 for attr in ['nsSubStrBegin', 'nsSubStrMiddle', 'nsSubStrEnd']:
43 try:
44 index.remove_all(attr)
45 except (ldap.NO_SUCH_ATTRIBUTE, ldap.SERVER_DOWN):
46 pass
47
48 # Remove extensibleObject if present
49 try:
50 index.set('objectClass', 'extensibleObject', action=ldap.MOD_DELETE)
51 except (ldap.NO_SUCH_ATTRIBUTE, ldap.OBJECT_CLASS_VIOLATION,
52 ldap.SERVER_DOWN):
53 pass
54
55 # Remove nssubstr matching rules from nsMatchingRule
56 for rule in ['nssubstrbegin', 'nssubstrmiddle', 'nssubstrend']:
57 try:
58 mrs = index.get_attr_vals_utf8('nsMatchingRule')
59 for mr in mrs:
60 if mr.lower().startswith(rule):
61 index.set('nsMatchingRule', mr, action=ldap.MOD_DELETE)
62 except (ldap.NO_SUCH_ATTRIBUTE, ldap.OBJECT_CLASS_VIOLATION,
63 ldap.SERVER_DOWN):
64 pass
65
66 # Remove 'sub' from nsIndexType
67 try:
68 index.set('nsIndexType', 'sub', action=ldap.MOD_DELETE)
69 except (ldap.NO_SUCH_ATTRIBUTE, ldap.SERVER_DOWN):
70 pass
71
72 yield index
73 _cleanup()
74
75
76 @pytest.fixture
77 def create_user(topology_st):
78 """Create test users."""
79 inst = topology_st.standalone
80 users = UserAccounts(inst, DEFAULT_SUFFIX)
81 created = []
82
83 def _create_user(uid, cn, sn):
84 user = users.create(properties={
85 'uid': uid,
86 'cn': cn,
87 'sn': sn,
88 'uidNumber': str(1000 + len(created)),
89 'gidNumber': '1000',
90 'homeDirectory': f'/home/{uid}',
91 })
92 created.append(user)
93 return user
94
95 yield _create_user
96
97
98 def _reindex_uid(topology_st):
99 """Reindex the uid attribute and wait for completion."""
100 tasks = Tasks(topology_st.standalone)
101 assert tasks.reindex(
102 suffix=DEFAULT_SUFFIX,
103 attrname='uid',
104 args={TASK_WAIT: True}
105 ) == 0
106
107
108 def _search_and_assert(topology_st, filt, expected_count, msg=""):
109 """Search with filter and assert expected number of results."""
110 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
111 entries = users.filter(filt)
112 assert len(entries) == expected_count, \
113 f'{msg}: filter "{filt}" returned {len(entries)} entries, expected {expected_count}'
114 return entries
115
116
117 def test_substr_direct_attrs(topology_st, uid_index, create_user):
118 """Test substring index with nsSubStrBegin/nsSubStrEnd direct attributes.
119
120 :id: c670a123-71f6-4577-b3f0-7c938153903f
121 :customerscenario: True
122 :setup: Standalone instance
123 :steps:
124 1. Configure uid index with nsSubStrBegin=2, nsSubStrEnd=2
125 using extensibleObject direct attributes
126 2. Reindex uid attribute
127 3. Add a test user
128 4. Search with short initial substring
129 5. Search with 2-char initial
130 6. Search with 2-char final
131 :expectedresults:
132 1. Success
133 2. Success
134 3. Success
135 4. Returns the user
136 5. Returns the user
137 6. Returns the user
138 """
139 uid_index.add_many(
140 ('objectClass', 'extensibleObject'),
141 ('nsIndexType', 'sub'),
142 ('nsSubStrBegin', '2'),
143 ('nsSubStrEnd', '2'),
144 )
145
146 uid = f'{UID_PREFIX}a0'
147 create_user(uid, 'a user0', 'user0')
148
149 _reindex_uid(topology_st)
150
151 _search_and_assert(topology_st, f'(uid={uid[:len(UID_PREFIX)+1]}*)', 1,
152 'begin=2 initial search')
153 _search_and_assert(topology_st, f'(uid={uid[:len(UID_PREFIX)+2]}*)', 1,
154 'begin=2 two-char initial')
155 _search_and_assert(topology_st, f'(uid=*{uid[-2:]})', 1,
156 'end=2 final search')
157
158
159 def test_substr_matching_rule(topology_st, uid_index, create_user):
160 """Test substring index with nsMatchingRule format for substr lengths.
161
162 :id: 58eebbc5-2571-43ef-9b97-151b11c02eb5
163 :customerscenario: True
164 :setup: Standalone instance
165 :steps:
166 1. Configure uid index with nsMatchingRule: nssubstrbegin=2
167 and nsMatchingRule: nssubstrend=2
168 2. Reindex uid attribute
169 3. Add a test user
170 4. Search with short initial substring
171 :expectedresults:
172 1. Success
173 2. Success
174 3. Success
175 4. Returns the user
176 """
177 uid_index.add_many(
178 ('nsIndexType', 'sub'),
179 ('nsMatchingRule', 'nssubstrbegin=2'),
180 ('nsMatchingRule', 'nssubstrend=2'),
181 )
182
183 uid = f'{UID_PREFIX}b1'
184 create_user(uid, 'b user1', 'user1')
185
186 _reindex_uid(topology_st)
187
188 _search_and_assert(topology_st, f'(uid={uid[:len(UID_PREFIX)+1]}*)', 1,
189 'nsMatchingRule begin=2')
190 _search_and_assert(topology_st, f'(uid={uid[:len(UID_PREFIX)+2]}*)', 1,
191 'nsMatchingRule begin=2 two-char')
192
193
194 def test_substr_direct_attrs_override_matching_rule(topology_st, uid_index, create_user):
195 """Test that nsSubStr direct attributes take precedence over nsMatchingRule.
196
197 :id: 649051f7-97d6-4f85-bfeb-c94e4be49ede
198 :customerscenario: True
199 :setup: Standalone instance
200 :steps:
201 1. Configure uid index with BOTH formats:
202 - nsMatchingRule: nssubstrbegin=3 (would require 3-char initial)
203 - nsMatchingRule: nssubstrend=3
204 - nsSubStrBegin: 2 (direct attr, should override to 2)
205 - nsSubStrEnd: 2
206 2. Reindex uid attribute
207 3. Add a test user
208 4. Search with 1-char initial - only works if begin=2 (direct) is used
209 5. Search with 1-char final - only works if end=2 (direct) is used
210 :expectedresults:
211 1. Success
212 2. Success
213 3. Success
214 4. Returns the user - proves direct attr (2) overrides matching rule (3)
215 5. Returns the user
216 """
217 uid_index.add_many(
218 ('nsIndexType', 'sub'),
219 ('nsMatchingRule', 'nssubstrbegin=3'),
220 ('nsMatchingRule', 'nssubstrend=3'),
221 ('objectClass', 'extensibleObject'),
222 ('nsSubStrBegin', '2'),
223 ('nsSubStrEnd', '2'),
224 )
225
226 uid = f'{UID_PREFIX}c2'
227 create_user(uid, 'c user2', 'user2')
228
229 _reindex_uid(topology_st)
230
231 _search_and_assert(topology_st, f'(uid={uid[:len(UID_PREFIX)+1]}*)', 1,
232 'direct attr begin=2 overrides matching rule begin=3')
233 _search_and_assert(topology_st, f'(uid=*{uid[-1:]})', 1,
234 'direct attr end=2 overrides matching rule end=3')
235
236
237 def test_substr_mixed_values(topology_st, uid_index, create_user):
238 """Test substring index with different values for begin, middle and end.
239
240 :id: 6c0bb8a2-234b-4daa-bf9c-07e8d03b08b0
241 :customerscenario: True
242 :setup: Standalone instance
243 :steps:
244 1. Configure uid index with begin=2, middle=3, end=2
245 2. Reindex uid attribute
246 3. Add two test users
247 4. Search with short initial
248 5. Search with 3-char any
249 6. Search with 2-char final
250 7. Search with longer any (unique to one user)
251 :expectedresults:
252 1. Success
253 2. Success
254 3. Success
255 4. Returns 1 user
256 5. Returns 2 users
257 6. Returns 2 users
258 7. Returns 1 user
259 """
260 uid_index.add_many(
261 ('objectClass', 'extensibleObject'),
262 ('nsIndexType', 'sub'),
263 ('nsSubStrBegin', '2'),
264 ('nsSubStrMiddle', '3'),
265 ('nsSubStrEnd', '2'),
266 )
267
268 uid1 = f'{UID_PREFIX}d3mixed'
269 uid2 = f'{UID_PREFIX}e4mixed'
270 create_user(uid1, 'd user3mixed', 'user3')
271 create_user(uid2, 'e user4mixed', 'user4')
272
273 _reindex_uid(topology_st)
274
275 _search_and_assert(topology_st, f'(uid={uid1})', 1, 'exact match sanity check')
276 _search_and_assert(topology_st, f'(uid={uid1[:len(UID_PREFIX)+1]}*)', 1,
277 'mixed: begin=2 initial search')
278 _search_and_assert(topology_st, '(uid=*mix*)', 2, 'mixed: middle=3 any search')
279 _search_and_assert(topology_st, '(uid=*ed)', 2, 'mixed: end=2 final search')
280 _search_and_assert(topology_st, f'(uid=*d3m*)', 1, 'mixed: longer any search unique')
281
282
283 def test_substr_low_values(topology_st, uid_index, create_user):
284 """Test substring index with all values set to 2.
285
286 :id: db9dd1cc-f76d-4ea6-8c56-4238d3f323a7
287 :customerscenario: True
288 :setup: Standalone instance
289 :steps:
290 1. Configure uid index with all substr lengths set to 2
291 2. Reindex uid attribute
292 3. Add test users
293 4. Search with short initial
294 5. Search with 2-char any
295 6. Search with multi-char final
296 7. Search with longer any
297 :expectedresults:
298 1. Success
299 2. Success
300 3. Success
301 4. Returns 1 user
302 5. Returns 2 users
303 6. Returns 1 user
304 7. Returns 1 user
305 """
306 uid_index.add_many(
307 ('objectClass', 'extensibleObject'),
308 ('nsIndexType', 'sub'),
309 ('nsSubStrBegin', '2'),
310 ('nsSubStrMiddle', '2'),
311 ('nsSubStrEnd', '2'),
312 )
313
314 uid1 = f'{UID_PREFIX}f5low'
315 uid2 = f'{UID_PREFIX}g6low'
316 create_user(uid1, 'f user5low', 'user5')
317 create_user(uid2, 'g user6low', 'user6')
318
319 _reindex_uid(topology_st)
320
321 _search_and_assert(topology_st, f'(uid={uid1[:len(UID_PREFIX)+1]}*)', 1,
322 'low: begin=2 single char initial')
323 _search_and_assert(topology_st, '(uid=*5lo*)', 1, 'low: middle=2 any search unique')
324 _search_and_assert(topology_st, f'(uid=*{uid1[-4:]})', 1, 'low: end=2 final search')
325 _search_and_assert(topology_st, '(uid=*low)', 2, 'low: final matching both')
326
327
328 def test_substr_begin_1_middle_3_end_3(topology_st, uid_index, create_user):
329 """Test substring index with nsSubStrBegin=1, nsSubStrMiddle=3, nsSubStrEnd=3.
330
331 :id: c28166e9-41aa-44ee-ba66-3300ea3ea4d1
332 :customerscenario: True
333 :setup: Standalone instance
334 :steps:
335 1. Configure uid index with begin=1, middle=3, end=3
336 2. Reindex uid attribute
337 3. Add two test users
338 4. Search with 1-char initial
339 5. Search with 3-char initial
340 6. Search with 3-char any
341 7. Search with 3-char final
342 8. Search with 2-char any (shorter than middle=3)
343 :expectedresults:
344 1. Success
345 2. Success
346 3. Success
347 4. Returns 1 user
348 5. Returns 1 user
349 6. Returns 2 users
350 7. Returns 2 users
351 8. Returns 1 user
352 """
353 uid_index.add_many(
354 ('objectClass', 'extensibleObject'),
355 ('nsIndexType', 'sub'),
356 ('nsSubStrBegin', '1'),
357 ('nsSubStrMiddle', '3'),
358 ('nsSubStrEnd', '3'),
359 )
360
361 uid1 = f'{UID_PREFIX}h7degen'
362 uid2 = f'{UID_PREFIX}i8degen'
363 create_user(uid1, 'h user7', 'user7')
364 create_user(uid2, 'i user8', 'user8')
365
366 _reindex_uid(topology_st)
367
368 _search_and_assert(topology_st, f'(uid={uid1})', 1, 'exact match sanity check')
369 _search_and_assert(topology_st, f'(uid={uid1[:len(UID_PREFIX)+2]}*)', 1,
370 'begin=1 two-char initial')
371 _search_and_assert(topology_st, '(uid=*deg*)', 2, 'middle=3 any search')
372 _search_and_assert(topology_st, '(uid=*gen)', 2, 'end=3 final search')
373 _search_and_assert(topology_st, '(uid=*7d*)', 1, 'short any, post-filtered')
374
375
376 def test_substr_matching_rule_mixed(topology_st, uid_index, create_user):
377 """Test mixed substring lengths via nsMatchingRule format.
378
379 Uses begin=1, middle=3, end=3 via nsMatchingRule format.
380
381 :id: 0ef6d87e-5ce2-4ea7-a8b7-9cb09f0fcf53
382 :customerscenario: True
383 :setup: Standalone instance
384 :steps:
385 1. Configure uid index with nsMatchingRule format:
386 nssubstrbegin=1, nssubstrmiddle=3, nssubstrend=3
387 2. Reindex uid attribute
388 3. Add test users
389 4. Search with various filters to verify functionality
390 :expectedresults:
391 1. Success
392 2. Success
393 3. Success
394 4. All searches return correct results
395 """
396 uid_index.add_many(
397 ('nsIndexType', 'sub'),
398 ('nsMatchingRule', 'nssubstrbegin=1'),
399 ('nsMatchingRule', 'nssubstrmiddle=3'),
400 ('nsMatchingRule', 'nssubstrend=3'),
401 )
402
403 uid1 = f'{UID_PREFIX}j9mrule'
404 uid2 = f'{UID_PREFIX}k10mrule'
405 create_user(uid1, 'j user9', 'user9')
406 create_user(uid2, 'k user10', 'user10')
407
408 _reindex_uid(topology_st)
409
410 _search_and_assert(topology_st, f'(uid={uid1[:len(UID_PREFIX)+2]}*)', 1,
411 'matching rule begin=1')
412 _search_and_assert(topology_st, '(uid=*rul*)', 2, 'matching rule middle=3')
413 _search_and_assert(topology_st, f'(uid=*{uid1[-7:]})', 1, 'matching rule end=3')
414
415
416 def test_substr_mixed_end_gt_middle(topology_st, uid_index, create_user):
417 """Test mixed values where begin+end > middle*2 works correctly.
418
419 :id: 33deb10a-beb1-45c3-8a4a-89ad2166a7df
420 :customerscenario: True
421 :setup: Standalone instance
422 :steps:
423 1. Configure uid index with begin=2, middle=2, end=3
424 (begin+end > middle*2)
425 2. Add test users
426 3. Reindex uid
427 4. Search with various substring filters
428 :expectedresults:
429 1. Success
430 2. Success
431 3. Success - no crash
432 4. All searches return correct results
433 """
434 inst = topology_st.standalone
435
436 uid_index.add_many(
437 ('objectClass', 'extensibleObject'),
438 ('nsIndexType', 'sub'),
439 ('nsSubStrBegin', '2'),
440 ('nsSubStrMiddle', '2'),
441 ('nsSubStrEnd', '3'),
442 )
443
444 uid1 = f'{UID_PREFIX}l11endgt'
445 uid2 = f'{UID_PREFIX}m12endgt'
446 create_user(uid1, 'l user11', 'user11')
447 create_user(uid2, 'm user12', 'user12')
448
449 _reindex_uid(topology_st)
450
451 # Server must still be alive after reindex
452 assert inst.status(), 'Server should not crash with begin+end > middle*2'
453
454 _search_and_assert(topology_st, f'(uid={uid1})', 1, 'exact match sanity')
455 _search_and_assert(topology_st, f'(uid={uid1[:len(UID_PREFIX)+1]}*)', 1,
456 'begin=2 initial search')
457 _search_and_assert(topology_st, '(uid=*endgt)', 2, 'end=3 final search')
458 _search_and_assert(topology_st, '(uid=*nd*)', 2, 'middle=2 any search')
459
460
461 if __name__ == '__main__':
462 CURRENT_FILE = os.path.realpath(__file__)
463 pytest.main("-s %s" % CURRENT_FILE)