Debian Codegraph @

dirsrvtests/tests/suites/schema/schema_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2016 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 '''
10 Created on Dec 18, 2013
11
12 @author: rmeggins
13 '''
14 import logging
15
16 import ldap
17 import pytest
18 from ldap.cidict import cidict
19 from ldap.schema import SubSchema
20 from lib389.schema import SchemaLegacy
21 from lib389._constants import *
22 from test389.topologies import topology_st, topology_m2 as topo_m2
23 from lib389.idm.user import UserAccounts, UserAccount
24 from lib389.replica import ReplicationManager
25 from lib389.utils import ensure_bytes
26
27 pytestmark = pytest.mark.tier1
28
29 logging.getLogger(__name__).setLevel(logging.DEBUG)
30 log = logging.getLogger(__name__)
31
32 attrclass = ldap.schema.models.AttributeType
33 occlass = ldap.schema.models.ObjectClass
34 syntax_len_supported = False
35
36
37 def ochasattr(subschema, oc, mustormay, attr, key):
38 """See if the oc and any of its parents and ancestors have the
39 given attr"""
40 rc = False
41 if not key in oc.__dict__:
42 dd = cidict()
43 for ii in oc.__dict__[mustormay]:
44 dd[ii] = ii
45 oc.__dict__[key] = dd
46 if attr in oc.__dict__[key]:
47 rc = True
48 else:
49 # look in parents
50 for noroid in oc.sup:
51 ocpar = subschema.get_obj(occlass, noroid)
52 assert (ocpar)
53 rc = ochasattr(subschema, ocpar, mustormay, attr, key)
54 if rc:
55 break
56 return rc
57
58
59 def ochasattrs(subschema, oc, mustormay, attrs):
60 key = mustormay + "dict"
61 ret = []
62 for attr in attrs:
63 if not ochasattr(subschema, oc, mustormay, attr, key):
64 ret.append(attr)
65 return ret
66
67
68 def mycmp(v1, v2):
69 v1ary, v2ary = [v1], [v2]
70 if isinstance(v1, list) or isinstance(v1, tuple):
71 v1ary, v2ary = list(set([x.lower() for x in v1])), list(set([x.lower() for x in v2]))
72 if not len(v1ary) == len(v2ary):
73 return False
74 for v1, v2 in zip(v1ary, v2ary):
75 if isinstance(v1, str):
76 if not len(v1) == len(v2):
77 return False
78 if not v1 == v2:
79 return False
80 return True
81
82
83 def ocgetdiffs(ldschema, oc1, oc2):
84 fields = ['obsolete', 'names', 'desc', 'must', 'may', 'kind', 'sup']
85 ret = ''
86 for field in fields:
87 v1, v2 = oc1.__dict__[field], oc2.__dict__[field]
88 if field == 'may' or field == 'must':
89 missing = ochasattrs(ldschema, oc1, field, oc2.__dict__[field])
90 if missing:
91 ret = ret + '\t%s is missing %s\n' % (field, missing)
92 missing = ochasattrs(ldschema, oc2, field, oc1.__dict__[field])
93 if missing:
94 ret = ret + '\t%s is missing %s\n' % (field, missing)
95 elif not mycmp(v1, v2):
96 ret = ret + '\t%s differs: [%s] vs. [%s]\n' % (field, oc1.__dict__[field], oc2.__dict__[field])
97 return ret
98
99
100 def atgetparfield(subschema, at, field):
101 v = None
102 for nameoroid in at.sup:
103 atpar = subschema.get_obj(attrclass, nameoroid)
104 assert (atpar)
105 v = atpar.__dict__.get(field, atgetparfield(subschema, atpar, field))
106 if v is not None:
107 break
108 return v
109
110
111 def atgetdiffs(ldschema, at1, at2):
112 fields = ['names', 'desc', 'obsolete', 'sup', 'equality', 'ordering', 'substr', 'syntax',
113 'single_value', 'collective', 'no_user_mod', 'usage']
114 if syntax_len_supported:
115 fields.append('syntax_len')
116 ret = ''
117 for field in fields:
118 v1 = at1.__dict__.get(field) or atgetparfield(ldschema, at1, field)
119 v2 = at2.__dict__.get(field) or atgetparfield(ldschema, at2, field)
120 if not mycmp(v1, v2):
121 ret = ret + '\t%s differs: [%s] vs. [%s]\n' % (field, at1.__dict__[field], at2.__dict__[field])
122 return ret
123
124
125 def test_schema_comparewithfiles(topology_st):
126 """Compare the schema from ldap cn=schema with the schema files
127
128 :id: 31f0233e-a7e6-442f-8b05-c0b095e93387
129 :setup: Standalone instance
130 :steps:
131 1. Compare the schema from ldap cn=schema with the schema from files
132
133 :expectedresults:
134 1. Schema in cn=schema and files should be the same
135 """
136
137 log.info('Running test_schema_comparewithfiles...')
138
139 retval = True
140 schemainst = topology_st.standalone
141 ldschema = schemainst.schema.get_subschema()
142 assert ldschema
143 for fn in schemainst.schema.list_files():
144 try:
145 fschema = schemainst.schema.file_to_subschema(fn)
146 if fschema is None:
147 raise Exception("Empty schema file %s" % fn)
148 except:
149 log.warning("Unable to parse %s as a schema file - skipping" % fn)
150 continue
151 log.info("Parsed %s as a schema file - checking" % fn)
152 for oid in fschema.listall(occlass):
153 se = fschema.get_obj(occlass, oid)
154 assert se
155 ldse = ldschema.get_obj(occlass, oid)
156 if not ldse:
157 log.error("objectclass in %s but not in %s: %s" % (fn, DN_SCHEMA, se))
158 retval = False
159 continue
160 ret = ocgetdiffs(ldschema, ldse, se)
161 if ret:
162 log.error("name %s oid %s\n%s" % (se.names[0], oid, ret))
163 retval = False
164 for oid in fschema.listall(attrclass):
165 se = fschema.get_obj(attrclass, oid)
166 assert se
167 ldse = ldschema.get_obj(attrclass, oid)
168 if not ldse:
169 log.error("attributetype in %s but not in %s: %s" % (fn, DN_SCHEMA, se))
170 retval = False
171 continue
172 ret = atgetdiffs(ldschema, ldse, se)
173 if ret:
174 log.error("name %s oid %s\n%s" % (se.names[0], oid, ret))
175 retval = False
176 assert retval
177
178 log.info('test_schema_comparewithfiles: PASSED')
179
180 def test_gecos_directoryString(topology_st):
181 """Check that gecos supports directoryString value
182
183 :id: aee422bb-6299-4124-b5cd-d7393dac19d3
184
185 :setup: Standalone instance
186
187 :steps:
188 1. Add a common user
189 2. replace gecos with a direstoryString value
190
191 :expectedresults:
192 1. Success
193 2. Success
194 """
195
196 users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
197
198 user_properties = {
199 'uid': 'testuser',
200 'cn' : 'testuser',
201 'sn' : 'user',
202 'uidNumber' : '1000',
203 'gidNumber' : '2000',
204 'homeDirectory' : '/home/testuser',
205 }
206 testuser = users.create(properties=user_properties)
207
208 # Add a gecos UTF value
209 testuser.replace('gecos', 'Hélène')
210
211 def test_gecos_mixed_definition_topo(topo_m2, request):
212 """Check that replication is still working if schema contains
213 definitions that does not conform with a replicated entry
214
215 :id: 0a1b4f8c-e84a-4cc4-82c8-2561b1126786
216 :setup: Two suppliers replication setup
217 :steps:
218 1. Create a testuser on M1
219 2 Stop M1 and M2
220 3 Change gecos def on M2 to be IA5
221 4 Update testuser with gecos directoryString value
222 5 Check replication is still working
223 :expectedresults:
224 1. success
225 2. success
226 3. success
227 4. success
228 5. success
229
230 """
231
232 repl = ReplicationManager(DEFAULT_SUFFIX)
233 m1 = topo_m2.ms["supplier1"]
234 m2 = topo_m2.ms["supplier2"]
235
236
237 # create a test user
238 testuser_dn = 'uid={},{}'.format('testuser', DEFAULT_SUFFIX)
239 testuser = UserAccount(m1, testuser_dn)
240 try:
241 testuser.create(properties={
242 'uid': 'testuser',
243 'cn': 'testuser',
244 'sn': 'testuser',
245 'uidNumber' : '1000',
246 'gidNumber' : '2000',
247 'homeDirectory' : '/home/testuser',
248 })
249 except ldap.ALREADY_EXISTS:
250 pass
251 repl.wait_for_replication(m1, m2)
252
253 # Stop suppliers to update the schema
254 m1.stop()
255 m2.stop()
256
257 # on M1: gecos is DirectoryString (default)
258 # on M2: gecos is IA5
259 schema_filename = (m2.schemadir + "/99user.ldif")
260 try:
261 with open(schema_filename, 'w') as schema_file:
262 schema_file.write("dn: cn=schema\n")
263 schema_file.write("attributetypes: ( 1.3.6.1.1.1.1.2 NAME " +
264 "'gecos' DESC 'The GECOS field; the common name' " +
265 "EQUALITY caseIgnoreIA5Match " +
266 "SUBSTR caseIgnoreIA5SubstringsMatch " +
267 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 " +
268 "SINGLE-VALUE )\n")
269 os.chmod(schema_filename, 0o777)
270 except OSError as e:
271 log.fatal("Failed to update schema file: " +
272 "{} Error: {}".format(schema_filename, str(e)))
273
274 # start the instances
275 m1.start()
276 m2.start()
277
278 # Check that gecos is IA5 on M2
279 schema = SchemaLegacy(m2)
280 attributetypes = schema.query_attributetype('gecos')
281 assert attributetypes[0].syntax == "1.3.6.1.4.1.1466.115.121.1.26"
282
283
284 # Add a gecos UTF value on M1
285 testuser.replace('gecos', 'Hélène')
286
287 # Check replication is still working
288 testuser.replace('displayName', 'ascii value')
289 repl.wait_for_replication(m1, m2)
290 testuser_m2 = UserAccount(m2, testuser_dn)
291 assert testuser_m2.exists()
292 assert testuser_m2.get_attr_val_utf8('displayName') == 'ascii value'
293
294 def fin():
295 m1.start()
296 m2.start()
297 testuser.delete()
298 repl.wait_for_replication(m1, m2)
299
300 # on M2 restore a default 99user.ldif
301 m2.stop()
302 os.remove(m2.schemadir + "/99user.ldif")
303 schema_filename = (m2.schemadir + "/99user.ldif")
304 try:
305 with open(schema_filename, 'w') as schema_file:
306 schema_file.write("dn: cn=schema\n")
307 os.chmod(schema_filename, 0o777)
308 except OSError as e:
309 log.fatal("Failed to update schema file: " +
310 "{} Error: {}".format(schema_filename, str(e)))
311 m2.start()
312 m1.start()
313
314 request.addfinalizer(fin)
315
316 def test_gecos_directoryString_wins_M1(topo_m2, request):
317 """Check that if inital syntax are IA5(M2) and DirectoryString(M1)
318 Then directoryString wins when nsSchemaCSN M1 is the greatest
319
320 :id: ad119fa5-7671-45c8-b2ef-0b28ffb68fdb
321 :setup: Two suppliers replication setup
322 :steps:
323 1. Create a testuser on M1
324 2 Stop M1 and M2
325 3 Change gecos def on M2 to be IA5
326 4 Start M1 and M2
327 5 Update M1 schema so that M1 has greatest nsSchemaCSN
328 6 Update testuser with gecos directoryString value
329 7 Check replication is still working
330 8 Check gecos is DirectoryString on M1 and M2
331 :expectedresults:
332 1. success
333 2. success
334 3. success
335 4. success
336 5. success
337 6. success
338 7. success
339 8. success
340
341 """
342
343 repl = ReplicationManager(DEFAULT_SUFFIX)
344 m1 = topo_m2.ms["supplier1"]
345 m2 = topo_m2.ms["supplier2"]
346
347
348 # create a test user
349 testuser_dn = 'uid={},{}'.format('testuser', DEFAULT_SUFFIX)
350 testuser = UserAccount(m1, testuser_dn)
351 try:
352 testuser.create(properties={
353 'uid': 'testuser',
354 'cn': 'testuser',
355 'sn': 'testuser',
356 'uidNumber' : '1000',
357 'gidNumber' : '2000',
358 'homeDirectory' : '/home/testuser',
359 })
360 except ldap.ALREADY_EXISTS:
361 pass
362 repl.wait_for_replication(m1, m2)
363
364 # Stop suppliers to update the schema
365 m1.stop()
366 m2.stop()
367
368 # on M1: gecos is DirectoryString (default)
369 # on M2: gecos is IA5
370 schema_filename = (m2.schemadir + "/99user.ldif")
371 try:
372 with open(schema_filename, 'w') as schema_file:
373 schema_file.write("dn: cn=schema\n")
374 schema_file.write("attributetypes: ( 1.3.6.1.1.1.1.2 NAME " +
375 "'gecos' DESC 'The GECOS field; the common name' " +
376 "EQUALITY caseIgnoreIA5Match " +
377 "SUBSTR caseIgnoreIA5SubstringsMatch " +
378 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 " +
379 "SINGLE-VALUE )\n")
380 os.chmod(schema_filename, 0o777)
381 except OSError as e:
382 log.fatal("Failed to update schema file: " +
383 "{} Error: {}".format(schema_filename, str(e)))
384
385 # start the instances
386 m1.start()
387 m2.start()
388
389 # Check that gecos is IA5 on M2
390 schema = SchemaLegacy(m2)
391 attributetypes = schema.query_attributetype('gecos')
392 assert attributetypes[0].syntax == "1.3.6.1.4.1.1466.115.121.1.26"
393
394
395 # update M1 schema to increase its nsschemaCSN
396 new_at = "( dummy-oid NAME 'dummy' DESC 'dummy attribute' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 SINGLE-VALUE X-ORIGIN 'RFC 2307' )"
397 m1.schema.add_schema('attributetypes', ensure_bytes(new_at))
398
399 # Add a gecos UTF value on M1
400 testuser.replace('gecos', 'Hélène')
401
402 # Check replication is still working
403 testuser.replace('displayName', 'ascii value')
404 repl.wait_for_replication(m1, m2)
405 testuser_m2 = UserAccount(m2, testuser_dn)
406 assert testuser_m2.exists()
407 assert testuser_m2.get_attr_val_utf8('displayName') == 'ascii value'
408
409 # Check that gecos is DirectoryString on M1
410 schema = SchemaLegacy(m1)
411 attributetypes = schema.query_attributetype('gecos')
412 assert attributetypes[0].syntax == "1.3.6.1.4.1.1466.115.121.1.15"
413
414 # Check that gecos is DirectoryString on M2
415 schema = SchemaLegacy(m2)
416 attributetypes = schema.query_attributetype('gecos')
417 assert attributetypes[0].syntax == "1.3.6.1.4.1.1466.115.121.1.15"
418
419 def fin():
420 m1.start()
421 m2.start()
422 testuser.delete()
423 m1.schema.del_schema('attributetypes', ensure_bytes(new_at))
424 repl.wait_for_replication(m1, m2)
425
426 # on M2 restore a default 99user.ldif
427 m2.stop()
428 os.remove(m2.schemadir + "/99user.ldif")
429 schema_filename = (m2.schemadir + "/99user.ldif")
430 try:
431 with open(schema_filename, 'w') as schema_file:
432 schema_file.write("dn: cn=schema\n")
433 os.chmod(schema_filename, 0o777)
434 except OSError as e:
435 log.fatal("Failed to update schema file: " +
436 "{} Error: {}".format(schema_filename, str(e)))
437 m2.start()
438 m1.start()
439
440 request.addfinalizer(fin)
441
442 def test_gecos_directoryString_wins_M2(topo_m2, request):
443 """Check that if inital syntax are IA5(M2) and DirectoryString(M1)
444 Then directoryString wins when nsSchemaCSN M2 is the greatest
445
446 :id: 2da7f1b1-f86d-4072-a940-ba56d4bc8348
447 :setup: Two suppliers replication setup
448 :steps:
449 1. Create a testuser on M1
450 2 Stop M1 and M2
451 3 Change gecos def on M2 to be IA5
452 4 Start M1 and M2
453 5 Update M2 schema so that M2 has greatest nsSchemaCSN
454 6 Update testuser on M2 and trigger replication to M1
455 7 Update testuser on M2 with gecos directoryString value
456 8 Check replication is still working
457 9 Check gecos is DirectoryString on M1 and M2
458 :expectedresults:
459 1. success
460 2. success
461 3. success
462 4. success
463 5. success
464 6. success
465 7. success
466 8. success
467 9. success
468
469 """
470
471 repl = ReplicationManager(DEFAULT_SUFFIX)
472 m1 = topo_m2.ms["supplier1"]
473 m2 = topo_m2.ms["supplier2"]
474
475
476 # create a test user
477 testuser_dn = 'uid={},{}'.format('testuser', DEFAULT_SUFFIX)
478 testuser = UserAccount(m1, testuser_dn)
479 try:
480 testuser.create(properties={
481 'uid': 'testuser',
482 'cn': 'testuser',
483 'sn': 'testuser',
484 'uidNumber' : '1000',
485 'gidNumber' : '2000',
486 'homeDirectory' : '/home/testuser',
487 })
488 except ldap.ALREADY_EXISTS:
489 pass
490 testuser.replace('displayName', 'to trigger replication M1-> M2')
491 repl.wait_for_replication(m1, m2)
492
493 # Stop suppliers to update the schema
494 m1.stop()
495 m2.stop()
496
497 # on M1: gecos is DirectoryString (default)
498 # on M2: gecos is IA5
499 schema_filename = (m2.schemadir + "/99user.ldif")
500 try:
501 with open(schema_filename, 'w') as schema_file:
502 schema_file.write("dn: cn=schema\n")
503 schema_file.write("attributetypes: ( 1.3.6.1.1.1.1.2 NAME " +
504 "'gecos' DESC 'The GECOS field; the common name' " +
505 "EQUALITY caseIgnoreIA5Match " +
506 "SUBSTR caseIgnoreIA5SubstringsMatch " +
507 "SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 " +
508 "SINGLE-VALUE )\n")
509 os.chmod(schema_filename, 0o777)
510 except OSError as e:
511 log.fatal("Failed to update schema file: " +
512 "{} Error: {}".format(schema_filename, str(e)))
513
514 # start the instances
515 m1.start()
516 m2.start()
517
518 # Check that gecos is IA5 on M2
519 schema = SchemaLegacy(m2)
520 attributetypes = schema.query_attributetype('gecos')
521 assert attributetypes[0].syntax == "1.3.6.1.4.1.1466.115.121.1.26"
522
523 # update M2 schema to increase its nsschemaCSN
524 new_at = "( dummy-oid NAME 'dummy' DESC 'dummy attribute' SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 SINGLE-VALUE X-ORIGIN 'RFC 2307' )"
525 m2.schema.add_schema('attributetypes', ensure_bytes(new_at))
526
527 # update just to trigger replication M2->M1
528 # and update of M2 schema
529 testuser_m2 = UserAccount(m2, testuser_dn)
530 testuser_m2.replace('displayName', 'to trigger replication M2-> M1')
531
532 # Add a gecos UTF value on M1
533 testuser.replace('gecos', 'Hélène')
534
535 # Check replication is still working
536 testuser.replace('displayName', 'ascii value')
537 repl.wait_for_replication(m1, m2)
538 assert testuser_m2.exists()
539 assert testuser_m2.get_attr_val_utf8('displayName') == 'ascii value'
540
541 # Check that gecos is DirectoryString on M1
542 schema = SchemaLegacy(m1)
543 attributetypes = schema.query_attributetype('gecos')
544 assert attributetypes[0].syntax == "1.3.6.1.4.1.1466.115.121.1.15"
545
546 # Check that gecos is DirectoryString on M2
547 schema = SchemaLegacy(m2)
548 attributetypes = schema.query_attributetype('gecos')
549 assert attributetypes[0].syntax == "1.3.6.1.4.1.1466.115.121.1.15"
550
551 def fin():
552 m1.start()
553 m2.start()
554 testuser.delete()
555 m1.schema.del_schema('attributetypes', ensure_bytes(new_at))
556 repl.wait_for_replication(m1, m2)
557
558 # on M2 restore a default 99user.ldif
559 m2.stop()
560 os.remove(m2.schemadir + "/99user.ldif")
561 schema_filename = (m2.schemadir + "/99user.ldif")
562 try:
563 with open(schema_filename, 'w') as schema_file:
564 schema_file.write("dn: cn=schema\n")
565 os.chmod(schema_filename, 0o777)
566 except OSError as e:
567 log.fatal("Failed to update schema file: " +
568 "{} Error: {}".format(schema_filename, str(e)))
569 m2.start()
570
571 request.addfinalizer(fin)
572
573 def test_definition_with_sharp(topology_st, request):
574 """Check that replication is still working if schema contains
575 definitions that does not conform with a replicated entry
576
577 :id: 94aa18ca-752f-11ec-a5ad-482ae39447e5
578 :setup: A single instance
579 :steps:
580 1 Stop instance
581 2 Add schema definition with a line starting with " #"
582 3 Start instance
583 4 Check that nstance is really running.
584 :expectedresults:
585 1. success
586 2. success
587 3. success
588 4. success
589
590 """
591
592 inst = topology_st.standalone
593 inst.stop()
594
595 def fin():
596 # restore a default 99user.ldif
597 inst.stop()
598 os.remove(inst.schemadir + "/99user.ldif")
599 schema_filename = (inst.schemadir + "/99user.ldif")
600 try:
601 with open(schema_filename, 'w') as schema_file:
602 schema_file.write("dn: cn=schema\n")
603 os.chmod(schema_filename, 0o777)
604 except OSError as e:
605 log.fatal("Failed to update schema file: " +
606 "{} Error: {}".format(schema_filename, str(e)))
607 inst.start()
608
609 request.addfinalizer(fin)
610
611 schema_filename = (inst.schemadir + "/99user.ldif")
612 try:
613 with open(schema_filename, 'w') as schema_file:
614 schema_file.write("dn: cn=schema\n")
615 schema_file.write("attributeTypes: ( 2.16.840.1.113730.3.8.11.61 NAME 'ipaWrappingKey' DESC 'PKCS\n")
616 schema_file.write(" #11 URI of the wrapping key' EQUALITY caseExactMatch SYNTAX 1.3.6.1.4.1.1466.\n")
617 schema_file.write(" 115.121.1.15 SINGLE-VALUE X-ORIGIN ( 'IPA v4.1' 'user defined' ) )\n")
618 os.chmod(schema_filename, 0o777)
619 except OSError as e:
620 log.fatal("Failed to update schema file: " +
621 "{} Error: {}".format(schema_filename, str(e)))
622
623 # start the instances
624 inst.start()
625
626 # Check that server is really running.
627 assert inst.status()
628
629
630 if __name__ == '__main__':
631 # Run isolated
632 # -s for DEBUG mode
633 CURRENT_FILE = os.path.realpath(__file__)
634 pytest.main("-s %s" % CURRENT_FILE)