Debian Codegraph @

dirsrvtests/tests/suites/plugins/entryusn_overflow_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2025 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 os
10 import ldap
11 import logging
12 import pytest
13 import time
14 import random
15 from lib389._constants import DEFAULT_SUFFIX
16 from lib389.config import Config
17 from lib389.plugins import USNPlugin
18 from lib389.idm.user import UserAccounts
19 from test389.topologies import topology_st
20 from lib389.rootdse import RootDSE
21
22 pytestmark = pytest.mark.tier2
23
24 log = logging.getLogger(__name__)
25
26 # Test constants
27 DEMO_USER_BASE_DN = "uid=demo_user,ou=people," + DEFAULT_SUFFIX
28 TEST_USER_PREFIX = "Demo User"
29 MAX_USN_64BIT = 18446744073709551615 # 2^64 - 1
30 ITERATIONS = 10
31 ADD_EXISTING_ENTRY_MAX_ATTEMPTS = 5
32
33
34 @pytest.fixture(scope="module")
35 def setup_usn_test(topology_st, request):
36 """Setup USN plugin and test data for entryUSN overflow testing"""
37
38 inst = topology_st.standalone
39
40 log.info("Enable the USN plugin...")
41 plugin = USNPlugin(inst)
42 plugin.enable()
43 plugin.enable_global_mode()
44
45 inst.restart()
46
47 # Create initial test users
48 users = UserAccounts(inst, DEFAULT_SUFFIX)
49 created_users = []
50
51 log.info("Creating initial test users...")
52 for i in range(3):
53 user_props = {
54 'uid': f'{TEST_USER_PREFIX}-{i}',
55 'cn': f'{TEST_USER_PREFIX}-{i}',
56 'sn': f'User{i}',
57 'uidNumber': str(1000 + i),
58 'gidNumber': str(1000 + i),
59 'homeDirectory': f'/home/{TEST_USER_PREFIX}-{i}',
60 'userPassword': 'password123'
61 }
62 try:
63 user = users.create(properties=user_props)
64 created_users.append(user)
65 log.info(f"Created user: {user.dn}")
66 except ldap.ALREADY_EXISTS:
67 log.info(f"User {user_props['uid']} already exists, skipping creation")
68 user = users.get(user_props['uid'])
69 created_users.append(user)
70
71 def fin():
72 log.info("Cleaning up test users...")
73 for user in created_users:
74 try:
75 user.delete()
76 except ldap.NO_SUCH_OBJECT:
77 pass
78
79 request.addfinalizer(fin)
80
81 return created_users
82
83
84 @pytest.mark.xfail(reason="DS6250")
85 def test_entryusn_overflow_on_add_existing_entries(topology_st, setup_usn_test):
86 """Test that reproduces entryUSN overflow when adding existing entries
87
88 :id: a5a8c33d-82f3-4113-be2b-027de51791c8
89 :setup: Standalone instance with USN plugin enabled and test users
90 :steps:
91 1. Record initial entryUSN values for existing users
92 2. Attempt to add existing entries multiple times (should fail)
93 3. Perform modify operations on the entries
94 4. Check that entryUSN values increment correctly without overflow
95 5. Verify lastusn values are consistent
96 :expectedresults:
97 1. Initial entryUSN values are recorded successfully
98 2. Add operations fail with ALREADY_EXISTS error
99 3. Modify operations succeed
100 4. EntryUSN values increment properly without underflow/overflow
101 5. LastUSN values are consistent and increasing
102 """
103
104 inst = topology_st.standalone
105 users = setup_usn_test
106
107 # Enable detailed logging for debugging
108 config = Config(inst)
109 config.replace('nsslapd-accesslog-level', '260') # Internal op logging
110 config.replace('nsslapd-errorlog-level', '65536')
111 config.replace('nsslapd-plugin-logging', 'on')
112
113 root_dse = RootDSE(inst)
114
115 log.info("Starting entryUSN overflow reproduction test")
116
117 # Record initial state
118 initial_usn_values = {}
119 for user in users:
120 initial_usn = user.get_attr_val_int('entryusn')
121 initial_usn_values[user.dn] = initial_usn
122 log.info(f"Initial entryUSN for {user.get_attr_val_utf8('cn')}: {initial_usn}")
123
124 initial_lastusn = root_dse.get_attr_val_int("lastusn")
125 log.info(f"Initial lastUSN: {initial_lastusn}")
126
127 # Perform test iterations
128 for iteration in range(1, ITERATIONS + 1):
129 log.info(f"\n--- Iteration {iteration} ---")
130
131 # Step 1: Try to add existing entries multiple times
132 selected_user = random.choice(users)
133 cn_value = selected_user.get_attr_val_utf8('cn')
134 attempts = random.randint(1, ADD_EXISTING_ENTRY_MAX_ATTEMPTS)
135
136 log.info(f"Attempting to add existing entry '{cn_value}' {attempts} times")
137
138 # Get user attributes for recreation attempt
139 user_attrs = {
140 'uid': selected_user.get_attr_val_utf8('uid'),
141 'cn': selected_user.get_attr_val_utf8('cn'),
142 'sn': selected_user.get_attr_val_utf8('sn'),
143 'uidNumber': selected_user.get_attr_val_utf8('uidNumber'),
144 'gidNumber': selected_user.get_attr_val_utf8('gidNumber'),
145 'homeDirectory': selected_user.get_attr_val_utf8('homeDirectory'),
146 'userPassword': 'password123'
147 }
148
149 users_collection = UserAccounts(inst, DEFAULT_SUFFIX)
150
151 # Try to add the existing user multiple times
152 for attempt in range(attempts):
153 try:
154 users_collection.create(properties=user_attrs)
155 log.error(f"ERROR: Add operation should have failed but succeeded on attempt {attempt + 1}")
156 assert False, "Add operation should have failed with ALREADY_EXISTS"
157 except ldap.ALREADY_EXISTS:
158 log.info(f"Attempt {attempt + 1}: Got expected ALREADY_EXISTS error")
159 except Exception as e:
160 log.error(f"Unexpected error on attempt {attempt + 1}: {e}")
161 raise
162
163 # Step 2: Perform modify operation
164 target_user = random.choice(users)
165 cn_value = target_user.get_attr_val_utf8('cn')
166 old_usn = target_user.get_attr_val_int('entryusn')
167
168 # Modify the user entry
169 new_description = f"Modified in iteration {iteration} - {time.time()}"
170 target_user.replace('description', new_description)
171
172 # Get new USN value
173 new_usn = target_user.get_attr_val_int('entryusn')
174
175 log.info(f"Modified entry '{cn_value}': old USN = {old_usn}, new USN = {new_usn}")
176
177 # Step 3: Validate USN values
178 # Check for overflow/underflow conditions
179 assert new_usn > 0, f"EntryUSN should be positive, got {new_usn}"
180 assert new_usn < MAX_USN_64BIT, f"EntryUSN overflow detected: {new_usn} >= {MAX_USN_64BIT}"
181
182 # Check that USN didn't wrap around (underflow detection)
183 usn_diff = new_usn - old_usn
184 assert usn_diff < 1000, f"USN increment too large, possible overflow: {usn_diff}"
185
186 # Verify lastUSN is also reasonable
187 current_lastusn = root_dse.get_attr_val_int("lastusn")
188 assert current_lastusn >= new_usn, f"LastUSN ({current_lastusn}) should be >= entryUSN ({new_usn})"
189 assert current_lastusn < MAX_USN_64BIT, f"LastUSN overflow detected: {current_lastusn}"
190
191 log.info(f"USN validation passed for iteration {iteration}")
192
193 # Add a new entry occasionally to increase USN diversity
194 if iteration % 3 == 0:
195 new_user_props = {
196 'uid': f'{TEST_USER_PREFIX}-new-{iteration}',
197 'cn': f'{TEST_USER_PREFIX}-new-{iteration}',
198 'sn': f'NewUser{iteration}',
199 'uidNumber': str(2000 + iteration),
200 'gidNumber': str(2000 + iteration),
201 'homeDirectory': f'/home/{TEST_USER_PREFIX}-new-{iteration}',
202 'userPassword': 'newpassword123'
203 }
204 try:
205 new_user = users_collection.create(properties=new_user_props)
206 new_user_usn = new_user.get_attr_val_int('entryusn')
207 log.info(f"Created new entry '{new_user.get_attr_val_utf8('cn')}' with USN: {new_user_usn}")
208 users.append(new_user) # Add to cleanup list
209 except Exception as e:
210 log.warning(f"Failed to create new user in iteration {iteration}: {e}")
211
212 # Final validation: Check all USN values are reasonable
213 log.info("\nFinal USN validation")
214 final_lastusn = root_dse.get_attr_val_int("lastusn")
215
216 for user in users:
217 try:
218 final_usn = user.get_attr_val_int('entryusn')
219 cn_value = user.get_attr_val_utf8('cn')
220 log.info(f"Final entryUSN for '{cn_value}': {final_usn}")
221
222 # Ensure no overflow occurred
223 assert final_usn > 0, f"Final entryUSN should be positive for {cn_value}: {final_usn}"
224 assert final_usn < MAX_USN_64BIT, f"EntryUSN overflow for {cn_value}: {final_usn}"
225
226 except ldap.NO_SUCH_OBJECT:
227 log.info(f"User {user.dn} was deleted during test")
228
229 log.info(f"Final lastUSN: {final_lastusn}")
230 assert final_lastusn > initial_lastusn, "LastUSN should have increased during test"
231 assert final_lastusn < MAX_USN_64BIT, f"LastUSN overflow detected: {final_lastusn}"
232
233 log.info("EntryUSN overflow test completed successfully")
234
235
236 @pytest.mark.xfail(reason="DS6250")
237 def test_entryusn_consistency_after_failed_adds(topology_st, setup_usn_test):
238 """Test that entryUSN remains consistent after failed add operations
239
240 :id: e380ccad-527b-427e-a331-df5c41badbed
241 :setup: Standalone instance with USN plugin enabled and test users
242 :steps:
243 1. Record entryUSN values before failed add attempts
244 2. Attempt to add existing entries (should fail)
245 3. Verify entryUSN values haven't changed due to failed operations
246 4. Perform successful modify operations
247 5. Verify entryUSN increments correctly
248 :expectedresults:
249 1. Initial entryUSN values recorded
250 2. Add operations fail as expected
251 3. EntryUSN values unchanged after failed adds
252 4. Modify operations succeed
253 5. EntryUSN values increment correctly without overflow
254 """
255
256 inst = topology_st.standalone
257 users = setup_usn_test
258
259 log.info("Testing entryUSN consistency after failed adds")
260
261 # Record USN values before any operations
262 pre_operation_usns = {}
263 for user in users:
264 usn = user.get_attr_val_int('entryusn')
265 pre_operation_usns[user.dn] = usn
266 log.info(f"Pre-operation entryUSN for {user.get_attr_val_utf8('cn')}: {usn}")
267
268 # Attempt to add existing entries - these should fail
269 users_collection = UserAccounts(inst, DEFAULT_SUFFIX)
270
271 for user in users:
272 cn_value = user.get_attr_val_utf8('cn')
273 log.info(f"Attempting to add existing user: {cn_value}")
274
275 user_attrs = {
276 'uid': user.get_attr_val_utf8('uid'),
277 'cn': cn_value,
278 'sn': user.get_attr_val_utf8('sn'),
279 'uidNumber': user.get_attr_val_utf8('uidNumber'),
280 'gidNumber': user.get_attr_val_utf8('gidNumber'),
281 'homeDirectory': user.get_attr_val_utf8('homeDirectory'),
282 'userPassword': 'password123'
283 }
284
285 try:
286 users_collection.create(properties=user_attrs)
287 assert False, f"Add operation should have failed for existing user {cn_value}"
288 except ldap.ALREADY_EXISTS:
289 log.info(f"Got expected ALREADY_EXISTS for {cn_value}")
290
291 # Verify USN values haven't changed after failed adds
292 log.info("Verifying entryUSN values after failed add operations...")
293 for user in users:
294 current_usn = user.get_attr_val_int('entryusn')
295 expected_usn = pre_operation_usns[user.dn]
296 cn_value = user.get_attr_val_utf8('cn')
297
298 assert current_usn == expected_usn, \
299 f"EntryUSN changed after failed add for {cn_value}: was {expected_usn}, now {current_usn}"
300 log.info(f"EntryUSN unchanged for {cn_value}: {current_usn}")
301
302 # Now perform successful modify operations
303 log.info("Performing successful modify operations...")
304 for i, user in enumerate(users):
305 cn_value = user.get_attr_val_utf8('cn')
306 old_usn = user.get_attr_val_int('entryusn')
307
308 # Modify the user
309 user.replace('description', f'Consistency test modification {i + 1}')
310
311 new_usn = user.get_attr_val_int('entryusn')
312 log.info(f"Modified {cn_value}: USN {old_usn} -> {new_usn}")
313
314 # Verify proper increment
315 assert (new_usn - old_usn) == 1, f"EntryUSN should increment by 1 for {cn_value}: {old_usn} -> {new_usn}"
316 assert new_usn < MAX_USN_64BIT, f"EntryUSN overflow for {cn_value}: {new_usn}"
317
318 log.info("EntryUSN consistency test completed successfully")
319
320
321 if __name__ == '__main__':
322 # Run isolated
323 # -s for DEBUG mode
324 CURRENT_FILE = os.path.realpath(__file__)
325 pytest.main("-s %s" % CURRENT_FILE)