| 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 logging |
| 10 |
import os |
| 11 |
import pytest |
| 12 |
from test389.topologies import topology_st as topo |
| 13 |
from lib389.backend import Backends |
| 14 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 15 |
from lib389._constants import DEFAULT_SUFFIX |
| 16 |
|
| 17 |
pytestmark = pytest.mark.tier1 |
| 18 |
|
| 19 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 20 |
log = logging.getLogger(__name__) |
| 21 |
|
| 22 |
|
| 23 |
@pytest.fixture(scope="function") |
| 24 |
def setup_subsuffix_with_entry_parent(topo, request): |
| 25 |
"""Setup a sub-suffix whose parent-suffix points to an entry, not a suffix.""" |
| 26 |
inst = topo.standalone |
| 27 |
|
| 28 |
# Create ou=people entry under the root suffix |
| 29 |
log.info("Creating ou=people,dc=example,dc=com entry") |
| 30 |
ous = OrganizationalUnits(inst, DEFAULT_SUFFIX) |
| 31 |
ou_people = ous.get('people') |
| 32 |
|
| 33 |
# Create sub-suffix with parent-suffix pointing to the entry |
| 34 |
log.info("Creating sub-suffix ou=foo,ou=people,dc=example,dc=com") |
| 35 |
backends = Backends(inst) |
| 36 |
subsuffix_dn = 'ou=foo,ou=people,dc=example,dc=com' |
| 37 |
parent_suffix_dn = 'ou=people,dc=example,dc=com' |
| 38 |
|
| 39 |
foo_backend = backends.create(properties={ |
| 40 |
'cn': 'foo', |
| 41 |
'nsslapd-suffix': subsuffix_dn, |
| 42 |
'parent': parent_suffix_dn, |
| 43 |
}) |
| 44 |
|
| 45 |
# Create the suffix entry |
| 46 |
foo_ous = OrganizationalUnits(inst, parent_suffix_dn) |
| 47 |
foo_ou = foo_ous.create(properties={'ou': 'foo'}) |
| 48 |
|
| 49 |
def cleanup(): |
| 50 |
log.info("Cleaning up test backends and entries") |
| 51 |
try: |
| 52 |
foo_ou.delete() |
| 53 |
except Exception as e: |
| 54 |
log.warning(f"Failed to delete foo_ou: {e}") |
| 55 |
try: |
| 56 |
foo_backend.delete() |
| 57 |
except Exception as e: |
| 58 |
log.warning(f"Failed to delete foo_backend: {e}") |
| 59 |
|
| 60 |
request.addfinalizer(cleanup) |
| 61 |
|
| 62 |
return { |
| 63 |
'instance': inst, |
| 64 |
'backends': backends, |
| 65 |
'foo_backend': foo_backend, |
| 66 |
'ou_people': ou_people, |
| 67 |
'subsuffix_dn': subsuffix_dn, |
| 68 |
'parent_suffix_dn': parent_suffix_dn, |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
def test_subsuffix_with_entry_parent_in_tree(topo, setup_subsuffix_with_entry_parent): |
| 73 |
"""Test that a sub-suffix with parent pointing to an entry is visible in the tree. |
| 74 |
|
| 75 |
:id: 256f36f5-76ad-4043-ad8d-1f9e2afc4e1d |
| 76 |
:setup: Standalone instance with sub-suffix whose parent is an entry |
| 77 |
:steps: |
| 78 |
1. Verify the sub-suffix backend exists |
| 79 |
2. Get sub-suffixes of the root backend |
| 80 |
3. Verify the sub-suffix appears in the list |
| 81 |
:expectedresults: |
| 82 |
1. Backend should exist |
| 83 |
2. Sub-suffixes should be retrievable |
| 84 |
3. Sub-suffix should be visible (this is where the bug manifested) |
| 85 |
""" |
| 86 |
backends = setup_subsuffix_with_entry_parent['backends'] |
| 87 |
foo_backend = setup_subsuffix_with_entry_parent['foo_backend'] |
| 88 |
subsuffix_dn = setup_subsuffix_with_entry_parent['subsuffix_dn'] |
| 89 |
|
| 90 |
# Step 1: Verify the sub-suffix backend exists |
| 91 |
assert foo_backend.exists(), "The foo backend should exist" |
| 92 |
|
| 93 |
# Step 2: Get sub-suffixes of the root backend |
| 94 |
root_backend = backends.get(DEFAULT_SUFFIX) |
| 95 |
sub_suffixes = root_backend.get_sub_suffixes() |
| 96 |
log.info(f"Sub-suffixes found: {[s.get_attr_val_utf8('nsslapd-suffix') for s in sub_suffixes]}") |
| 97 |
|
| 98 |
# Step 3: Verify sub-suffix is in the list |
| 99 |
sub_suffix_found = any( |
| 100 |
s.get_attr_val_utf8_l('nsslapd-suffix') == subsuffix_dn.lower() |
| 101 |
for s in sub_suffixes |
| 102 |
) |
| 103 |
|
| 104 |
assert sub_suffix_found, ( |
| 105 |
f"Sub-suffix {subsuffix_dn} should be visible in get_sub_suffixes(). " |
| 106 |
"The parent-suffix points to an entry, not a backend suffix." |
| 107 |
) |
| 108 |
|
| 109 |
|
| 110 |
def test_subsuffix_in_backend_list(topo, setup_subsuffix_with_entry_parent): |
| 111 |
"""Test that the sub-suffix appears in the backend list. |
| 112 |
|
| 113 |
:id: 0ccc49af-91bb-4e8f-b0e1-1bd0b75c041b |
| 114 |
:setup: Standalone instance with sub-suffix configuration |
| 115 |
:steps: |
| 116 |
1. Get all backends |
| 117 |
2. Verify both root suffix and sub-suffix are present |
| 118 |
:expectedresults: |
| 119 |
1. Should retrieve all backends |
| 120 |
2. Both suffixes should be listed |
| 121 |
""" |
| 122 |
backends = setup_subsuffix_with_entry_parent['backends'] |
| 123 |
subsuffix_dn = setup_subsuffix_with_entry_parent['subsuffix_dn'] |
| 124 |
|
| 125 |
be_list = backends.list() |
| 126 |
suffixes = [be.get_attr_val_utf8_l('nsslapd-suffix') for be in be_list] |
| 127 |
|
| 128 |
assert DEFAULT_SUFFIX.lower() in suffixes, \ |
| 129 |
f"Root suffix {DEFAULT_SUFFIX} should be in the list" |
| 130 |
assert subsuffix_dn.lower() in suffixes, \ |
| 131 |
f"Sub-suffix {subsuffix_dn} should be in the list" |
| 132 |
|
| 133 |
|
| 134 |
def test_subsuffix_dn_boundary_matching(): |
| 135 |
"""Test that suffix matching respects DN component boundaries. |
| 136 |
|
| 137 |
:id: 0b856e26-c394-4c36-b9ba-d7894aa2ed11 |
| 138 |
:setup: None (unit test) |
| 139 |
:steps: |
| 140 |
1. Test exact suffix match |
| 141 |
2. Test proper DN ancestor match (ends with ,suffix) |
| 142 |
3. Test that partial string matches are rejected |
| 143 |
:expectedresults: |
| 144 |
1. Exact match should return True |
| 145 |
2. Proper ancestor should return True |
| 146 |
3. Partial string match should return False |
| 147 |
""" |
| 148 |
from lib389.backend import is_subsuffix_of |
| 149 |
|
| 150 |
all_suffixes = {'dc=com', 'dc=example,dc=com', 'ou=dept,dc=example,dc=com'} |
| 151 |
|
| 152 |
# Test 1: Exact match |
| 153 |
assert is_subsuffix_of('dc=example,dc=com', 'dc=example,dc=com', all_suffixes), \ |
| 154 |
"Exact match should return True" |
| 155 |
|
| 156 |
# Test 2: Parent is an entry under the suffix (not itself a suffix) |
| 157 |
assert is_subsuffix_of('ou=people,dc=example,dc=com', 'dc=example,dc=com', all_suffixes), \ |
| 158 |
"Parent entry under suffix should return True" |
| 159 |
|
| 160 |
# Test 3: Parent IS a suffix - should return False (handled separately) |
| 161 |
assert not is_subsuffix_of('ou=dept,dc=example,dc=com', 'dc=example,dc=com', all_suffixes), \ |
| 162 |
"Parent that is itself a suffix should return False" |
| 163 |
|
| 164 |
# Test 4: Edge case - wrong DN boundary (string ends with suffix but wrong boundary) |
| 165 |
edge_suffixes = {'dc=com', 'st,dc=com'} |
| 166 |
assert is_subsuffix_of('dc=test,dc=com', 'dc=com', edge_suffixes), \ |
| 167 |
"dc=test,dc=com should match dc=com" |
| 168 |
assert not is_subsuffix_of('dc=test,dc=com', 'st,dc=com', edge_suffixes), \ |
| 169 |
"dc=test,dc=com should NOT match st,dc=com (wrong DN boundary)" |
| 170 |
|
| 171 |
# Test 5: None input |
| 172 |
assert not is_subsuffix_of(None, 'dc=com', all_suffixes), \ |
| 173 |
"None parent should return False" |
| 174 |
|
| 175 |
# Test 6: Closest ancestor - should only match the nearest suffix |
| 176 |
# Hierarchy: dc=com -> dc=example,dc=com -> ou=branch,dc=example,dc=com (suffix) |
| 177 |
# -> ou=dept,ou=branch,dc=example,dc=com (entry) -> subsuffix |
| 178 |
# The subsuffix should only appear under ou=branch, not under dc=example,dc=com |
| 179 |
nested_suffixes = {'dc=com', 'dc=example,dc=com', 'ou=branch,dc=example,dc=com'} |
| 180 |
entry_parent = 'ou=dept,ou=branch,dc=example,dc=com' |
| 181 |
# Should match ou=branch (closest) |
| 182 |
assert is_subsuffix_of(entry_parent, 'ou=branch,dc=example,dc=com', nested_suffixes), \ |
| 183 |
"Should match closest ancestor suffix (ou=branch)" |
| 184 |
# Should NOT match dc=example,dc=com (not closest) |
| 185 |
assert not is_subsuffix_of(entry_parent, 'dc=example,dc=com', nested_suffixes), \ |
| 186 |
"Should NOT match distant ancestor (dc=example) - ou=branch is closer" |
| 187 |
# Should NOT match dc=com (not closest) |
| 188 |
assert not is_subsuffix_of(entry_parent, 'dc=com', nested_suffixes), \ |
| 189 |
"Should NOT match distant ancestor (dc=com) - ou=branch is closer" |
| 190 |
|
| 191 |
log.info("All DN boundary edge cases passed") |
| 192 |
|
| 193 |
|
| 194 |
def test_deep_suffix_hierarchy(topo, request): |
| 195 |
"""Test complex hierarchy: suffix -> suffix -> entry -> suffix -> suffix. |
| 196 |
|
| 197 |
:id: fd06491a-defa-4780-8472-78c077febdfb |
| 198 |
:setup: Standalone instance |
| 199 |
:steps: |
| 200 |
1. Create sub-suffix ou=branch (parent=dc=example,dc=com - a suffix) |
| 201 |
2. Create entry ou=dept,ou=branch (not a suffix) |
| 202 |
3. Create sub-suffix ou=team,ou=dept,ou=branch (parent=ou=dept - an entry) |
| 203 |
4. Create sub-suffix ou=sub,ou=team,ou=dept,ou=branch (parent=ou=team - a suffix) |
| 204 |
5. Verify all sub-suffixes are correctly placed in the tree |
| 205 |
:expectedresults: |
| 206 |
1. Sub-suffix created successfully |
| 207 |
2. Entry created successfully |
| 208 |
3. Sub-suffix with entry parent created successfully |
| 209 |
4. Sub-suffix with suffix parent created successfully |
| 210 |
5. Tree hierarchy is correct |
| 211 |
""" |
| 212 |
inst = topo.standalone |
| 213 |
backends = Backends(inst) |
| 214 |
|
| 215 |
# Define the hierarchy |
| 216 |
branch_suffix = f'ou=branch,{DEFAULT_SUFFIX}' |
| 217 |
dept_entry = f'ou=dept,{branch_suffix}' # This is an ENTRY, not a suffix |
| 218 |
team_suffix = f'ou=team,{dept_entry}' |
| 219 |
sub_suffix = f'ou=sub,{team_suffix}' |
| 220 |
|
| 221 |
created_backends = [] |
| 222 |
created_entries = [] |
| 223 |
|
| 224 |
def cleanup(): |
| 225 |
log.info("Cleaning up deep hierarchy test") |
| 226 |
for entry in reversed(created_entries): |
| 227 |
try: |
| 228 |
entry.delete() |
| 229 |
except Exception as e: |
| 230 |
log.warning(f"Failed to delete entry: {e}") |
| 231 |
for be in reversed(created_backends): |
| 232 |
try: |
| 233 |
be.delete() |
| 234 |
except Exception as e: |
| 235 |
log.warning(f"Failed to delete backend: {e}") |
| 236 |
|
| 237 |
request.addfinalizer(cleanup) |
| 238 |
|
| 239 |
# Step 1: Create ou=branch sub-suffix (parent is root suffix) |
| 240 |
log.info(f"Creating sub-suffix {branch_suffix}") |
| 241 |
branch_be = backends.create(properties={ |
| 242 |
'cn': 'branch', |
| 243 |
'nsslapd-suffix': branch_suffix, |
| 244 |
'parent': DEFAULT_SUFFIX, |
| 245 |
}) |
| 246 |
created_backends.append(branch_be) |
| 247 |
branch_ous = OrganizationalUnits(inst, DEFAULT_SUFFIX) |
| 248 |
branch_ou = branch_ous.create(properties={'ou': 'branch'}) |
| 249 |
created_entries.append(branch_ou) |
| 250 |
|
| 251 |
# Step 2: Create ou=dept entry under branch (NOT a suffix) |
| 252 |
log.info(f"Creating entry {dept_entry}") |
| 253 |
dept_ous = OrganizationalUnits(inst, branch_suffix) |
| 254 |
dept_ou = dept_ous.create(properties={'ou': 'dept'}) |
| 255 |
created_entries.append(dept_ou) |
| 256 |
|
| 257 |
# Step 3: Create ou=team sub-suffix (parent is dept ENTRY, not a suffix) |
| 258 |
log.info(f"Creating sub-suffix {team_suffix} with entry parent {dept_entry}") |
| 259 |
team_be = backends.create(properties={ |
| 260 |
'cn': 'team', |
| 261 |
'nsslapd-suffix': team_suffix, |
| 262 |
'parent': dept_entry, # Parent is an ENTRY! |
| 263 |
}) |
| 264 |
created_backends.append(team_be) |
| 265 |
team_ous = OrganizationalUnits(inst, dept_entry) |
| 266 |
team_ou = team_ous.create(properties={'ou': 'team'}) |
| 267 |
created_entries.append(team_ou) |
| 268 |
|
| 269 |
# Step 4: Create ou=sub sub-suffix (parent is team suffix) |
| 270 |
log.info(f"Creating sub-suffix {sub_suffix} with suffix parent {team_suffix}") |
| 271 |
sub_be = backends.create(properties={ |
| 272 |
'cn': 'sub', |
| 273 |
'nsslapd-suffix': sub_suffix, |
| 274 |
'parent': team_suffix, # Parent is a SUFFIX |
| 275 |
}) |
| 276 |
created_backends.append(sub_be) |
| 277 |
sub_ous = OrganizationalUnits(inst, team_suffix) |
| 278 |
sub_ou = sub_ous.create(properties={'ou': 'sub'}) |
| 279 |
created_entries.append(sub_ou) |
| 280 |
|
| 281 |
# Step 5: Verify the tree hierarchy |
| 282 |
log.info("Verifying tree hierarchy...") |
| 283 |
|
| 284 |
# Root should have branch as sub-suffix |
| 285 |
root_be = backends.get(DEFAULT_SUFFIX) |
| 286 |
root_subs = root_be.get_sub_suffixes() |
| 287 |
root_sub_suffixes = [s.get_attr_val_utf8_l('nsslapd-suffix') for s in root_subs] |
| 288 |
log.info(f"Root sub-suffixes: {root_sub_suffixes}") |
| 289 |
assert branch_suffix.lower() in root_sub_suffixes, \ |
| 290 |
f"branch should be under root suffix" |
| 291 |
|
| 292 |
# Branch should have team as sub-suffix (even though team's parent is an entry) |
| 293 |
branch_be_obj = backends.get(branch_suffix) |
| 294 |
branch_subs = branch_be_obj.get_sub_suffixes() |
| 295 |
branch_sub_suffixes = [s.get_attr_val_utf8_l('nsslapd-suffix') for s in branch_subs] |
| 296 |
log.info(f"Branch sub-suffixes: {branch_sub_suffixes}") |
| 297 |
assert team_suffix.lower() in branch_sub_suffixes, \ |
| 298 |
f"team should be under branch suffix (parent is entry under branch)" |
| 299 |
|
| 300 |
# Team should have sub as sub-suffix |
| 301 |
team_be_obj = backends.get(team_suffix) |
| 302 |
team_subs = team_be_obj.get_sub_suffixes() |
| 303 |
team_sub_suffixes = [s.get_attr_val_utf8_l('nsslapd-suffix') for s in team_subs] |
| 304 |
log.info(f"Team sub-suffixes: {team_sub_suffixes}") |
| 305 |
assert sub_suffix.lower() in team_sub_suffixes, \ |
| 306 |
f"sub should be under team suffix" |
| 307 |
|
| 308 |
log.info("Deep hierarchy test passed!") |
| 309 |
|
| 310 |
|
| 311 |
if __name__ == '__main__': |
| 312 |
CURRENT_FILE = os.path.realpath(__file__) |
| 313 |
pytest.main(["-s", CURRENT_FILE]) |