-
Notifications
You must be signed in to change notification settings - Fork 80
/
basic_test.py
1455 lines (1192 loc) · 50.8 KB
/
basic_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# --- BEGIN COPYRIGHT BLOCK ---
# Copyright (C) 2016 Red Hat, Inc.
# All rights reserved.
#
# License: GPL (version 3 or any later version).
# See LICENSE for details.
# --- END COPYRIGHT BLOCK ---
#
"""
:Requirement: Basic Directory Server Operations
"""
from subprocess import check_output, Popen
from lib389 import DirSrv
from lib389.idm.user import UserAccount, UserAccounts
import pytest
from lib389.tasks import *
from lib389.utils import *
from lib389.topologies import topology_st
from lib389.dbgen import dbgen
from lib389.idm.organizationalunit import OrganizationalUnits
from lib389._constants import DN_DM, PASSWORD, PW_DM
from lib389.topologies import topology_st
from lib389.paths import Paths
from lib389.idm.directorymanager import DirectoryManager
from lib389.config import LDBMConfig
from lib389.dseldif import DSEldif
from lib389.rootdse import RootDSE
pytestmark = pytest.mark.tier0
default_paths = Paths()
log = logging.getLogger(__name__)
# Globals
USER1_DN = 'uid=user1,' + DEFAULT_SUFFIX
USER2_DN = 'uid=user2,' + DEFAULT_SUFFIX
USER3_DN = 'uid=user3,' + DEFAULT_SUFFIX
USER4_DN = 'uid=user4,' + DEFAULT_SUFFIX
ROOTDSE_DEF_ATTR_LIST = ('namingContexts',
'supportedLDAPVersion',
'supportedControl',
'supportedExtension',
'supportedSASLMechanisms',
'vendorName',
'vendorVersion')
@pytest.fixture(scope="module")
def import_example_ldif(topology_st):
"""Import the Example LDIF for the tests in this suite"""
log.info('Initializing the "basic" test suite')
ldif = '%s/dirsrv/data/Example.ldif' % topology_st.standalone.get_data_dir()
import_ldif = topology_st.standalone.get_ldif_dir() + "/Example.ldif"
shutil.copy(ldif, import_ldif)
import_task = ImportTask(topology_st.standalone)
import_task.import_suffix_from_ldif(ldiffile=import_ldif, suffix=DEFAULT_SUFFIX)
import_task.wait()
@pytest.fixture(params=ROOTDSE_DEF_ATTR_LIST)
def rootdse_attr(topology_st, request):
"""Adds an attr from the list
as the default attr to the rootDSE
"""
# Ensure the server is started and connected
topology_st.standalone.start()
RETURN_DEFAULT_OPATTR = "nsslapd-return-default-opattr"
rootdse_attr_name = ensure_bytes(request.param)
log.info(" Add the %s: %s to rootdse" % (RETURN_DEFAULT_OPATTR,
rootdse_attr_name))
mod = [(ldap.MOD_ADD, RETURN_DEFAULT_OPATTR, rootdse_attr_name)]
try:
topology_st.standalone.modify_s("", mod)
except ldap.LDAPError as e:
log.fatal('Failed to add attr: error (%s)' % (e.args[0]['desc']))
assert False
def fin():
log.info(" Delete the %s: %s from rootdse" % (RETURN_DEFAULT_OPATTR,
rootdse_attr_name))
mod = [(ldap.MOD_DELETE, RETURN_DEFAULT_OPATTR, rootdse_attr_name)]
try:
topology_st.standalone.modify_s("", mod)
except ldap.LDAPError as e:
log.fatal('Failed to delete attr: error (%s)' % (e.args[0]['desc']))
assert False
request.addfinalizer(fin)
return rootdse_attr_name
def test_basic_ops(topology_st, import_example_ldif):
"""Tests adds, mods, modrdns, and deletes operations
:id: 33f97f55-60bf-46c7-b880-6c488517ae19
:setup: Standalone instance
:steps:
1. Add 3 test users USER1, USER2 and USER3 to database
2. Modify (ADD, REPLACE and DELETE) description for USER1 in database
3. Rename USER1, USER2 and USER3 using Modrds
4. Delete test entries USER1, USER2 and USER3
:expectedresults:
1. Add operation should PASS.
2. Modify operations should PASS.
3. Rename operations should PASS.
4. Delete operations should PASS.
"""
log.info('Running test_basic_ops...')
USER1_NEWDN = 'cn=user1'
USER2_NEWDN = 'cn=user2'
USER3_NEWDN = 'cn=user3'
NEW_SUPERIOR = 'ou=people,' + DEFAULT_SUFFIX
USER1_RDN_DN = 'cn=user1,' + DEFAULT_SUFFIX
USER2_RDN_DN = 'cn=user2,' + DEFAULT_SUFFIX
USER3_RDN_DN = 'cn=user3,' + NEW_SUPERIOR # New superior test
#
# Adds#
try:
topology_st.standalone.add_s(Entry((USER1_DN,
{'objectclass': "top extensibleObject".split(),
'sn': '1',
'cn': 'user1',
'uid': 'user1',
'userpassword': 'password'})))
except ldap.LDAPError as e:
log.error('Failed to add test user' + USER1_DN + ': error ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.add_s(Entry((USER2_DN,
{'objectclass': "top extensibleObject".split(),
'sn': '2',
'cn': 'user2',
'uid': 'user2',
'userpassword': 'password'})))
except ldap.LDAPError as e:
log.error('Failed to add test user' + USER2_DN + ': error ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.add_s(Entry((USER3_DN,
{'objectclass': "top extensibleObject".split(),
'sn': '3',
'cn': 'user3',
'uid': 'user3',
'userpassword': 'password'})))
except ldap.LDAPError as e:
log.error('Failed to add test user' + USER3_DN + ': error ' + e.args[0]['desc'])
assert False
#
# Mods
#
try:
topology_st.standalone.modify_s(USER1_DN, [(ldap.MOD_ADD, 'description',
b'New description')])
except ldap.LDAPError as e:
log.error('Failed to add description: error ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.modify_s(USER1_DN, [(ldap.MOD_REPLACE, 'description',
b'Modified description')])
except ldap.LDAPError as e:
log.error('Failed to modify description: error ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.modify_s(USER1_DN, [(ldap.MOD_DELETE, 'description',
None)])
except ldap.LDAPError as e:
log.error('Failed to delete description: error ' + e.args[0]['desc'])
assert False
#
# Modrdns
#
try:
topology_st.standalone.rename_s(USER1_DN, USER1_NEWDN, delold=1)
except ldap.LDAPError as e:
log.error('Failed to modrdn user1: error ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.rename_s(USER2_DN, USER2_NEWDN, delold=0)
except ldap.LDAPError as e:
log.error('Failed to modrdn user2: error ' + e.args[0]['desc'])
assert False # Modrdn - New superior
try:
topology_st.standalone.rename_s(USER3_DN, USER3_NEWDN,
newsuperior=NEW_SUPERIOR, delold=1)
except ldap.LDAPError as e:
log.error('Failed to modrdn(new superior) user3: error ' + e.args[0]['desc'])
assert False
#
# Deletes
#
try:
topology_st.standalone.delete_s(USER1_RDN_DN)
except ldap.LDAPError as e:
log.error('Failed to delete test entry1: ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.delete_s(USER2_RDN_DN)
except ldap.LDAPError as e:
log.error('Failed to delete test entry2: ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.delete_s(USER3_RDN_DN)
except ldap.LDAPError as e:
log.error('Failed to delete test entry3: ' + e.args[0]['desc'])
assert False
log.info('test_basic_ops: PASSED')
def test_basic_import_export(topology_st, import_example_ldif):
"""Test online and offline LDIF import & export
:id: 3ceeea11-9235-4e20-b80e-7203b2c6e149
:setup: Standalone instance
:steps:
1. Generate a test ldif (50k entries)
2. Import test ldif file using Online import.
3. Import test ldif file using Offline import (ldif2db).
4. Export test ldif file using Online export.
5. Export test ldif file using Offline export (db2ldif).
6. Cleanup - Import the Example LDIF for the other tests in this suite
:expectedresults:
1. Test ldif file creation should PASS.
2. Online import should PASS.
3. Offline import should PASS.
4. Online export should PASS.
5. Offline export should PASS.
6. Cleanup should PASS.
"""
log.info('Running test_basic_import_export...')
#
# Test online/offline LDIF imports
#
topology_st.standalone.start()
# Generate a test ldif (50k entries)
log.info("Generating LDIF...")
ldif_dir = topology_st.standalone.get_ldif_dir()
import_ldif = ldif_dir + '/basic_import.ldif'
dbgen(topology_st.standalone, 50000, import_ldif, DEFAULT_SUFFIX)
# Online
log.info("Importing LDIF online...")
import_task = ImportTask(topology_st.standalone)
import_task.import_suffix_from_ldif(ldiffile=import_ldif, suffix=DEFAULT_SUFFIX)
# Wait a bit till the task is created and available for searching
time.sleep(0.5)
# Good as place as any to quick test the task has some expected attributes
if ds_is_newer('1.4.1.2'):
assert import_task.present('nstaskcreated')
assert import_task.present('nstasklog')
assert import_task.present('nstaskcurrentitem')
assert import_task.present('nstasktotalitems')
assert import_task.present('ttl')
import_task.wait()
# Offline
log.info("Importing LDIF offline...")
topology_st.standalone.stop()
if not topology_st.standalone.ldif2db(DEFAULT_BENAME, None, None, None, import_ldif):
log.fatal('test_basic_import_export: Offline import failed')
assert False
topology_st.standalone.start()
#
# Test online and offline LDIF export
#
# Online export
log.info("Exporting LDIF online...")
export_ldif = ldif_dir + '/export.ldif'
export_task = ExportTask(topology_st.standalone)
export_task.export_suffix_to_ldif(ldiffile=export_ldif, suffix=DEFAULT_SUFFIX)
export_task.wait()
# Offline export
log.info("Exporting LDIF offline...")
topology_st.standalone.stop()
if not topology_st.standalone.db2ldif(DEFAULT_BENAME, (DEFAULT_SUFFIX,),
None, None, None, export_ldif):
log.fatal('test_basic_import_export: Failed to run offline db2ldif')
assert False
topology_st.standalone.start()
#
# Cleanup - Import the Example LDIF for the other tests in this suite
#
log.info("Restore datrabase, import initial LDIF...")
ldif = '%s/dirsrv/data/Example.ldif' % topology_st.standalone.get_data_dir()
import_ldif = topology_st.standalone.get_ldif_dir() + "/Example.ldif"
shutil.copyfile(ldif, import_ldif)
import_task = ImportTask(topology_st.standalone)
import_task.import_suffix_from_ldif(ldiffile=import_ldif, suffix=DEFAULT_SUFFIX)
import_task.wait()
log.info('test_basic_import_export: PASSED')
def test_basic_backup(topology_st, import_example_ldif):
"""Tests online and offline backup and restore
:id: 0e9d91f8-8748-40b6-ab03-fbd1998eb985
:setup: Standalone instance and import example.ldif
:steps:
1. Test online backup using db2bak.
2. Test online restore using bak2db.
3. Test offline backup using db2bak.
4. Test offline restore using bak2db.
:expectedresults:
1. Online backup should PASS.
2. Online restore should PASS.
3. Offline backup should PASS.
4. Offline restore should PASS.
"""
log.info('Running test_basic_backup...')
backup_dir = topology_st.standalone.get_bak_dir() + '/backup_test'
# Test online backup
try:
topology_st.standalone.tasks.db2bak(backup_dir=backup_dir,
args={TASK_WAIT: True})
except ValueError:
log.fatal('test_basic_backup: Online backup failed')
assert False
# Test online restore
try:
topology_st.standalone.tasks.bak2db(backup_dir=backup_dir,
args={TASK_WAIT: True})
except ValueError:
log.fatal('test_basic_backup: Online restore failed')
assert False
# Test offline backup
topology_st.standalone.stop()
if not topology_st.standalone.db2bak(backup_dir):
log.fatal('test_basic_backup: Offline backup failed')
assert False
# Test offline restore
if not topology_st.standalone.bak2db(backup_dir):
log.fatal('test_basic_backup: Offline backup failed')
assert False
topology_st.standalone.start()
log.info('test_basic_backup: PASSED')
def test_basic_db2index(topology_st, import_example_ldif):
"""Assert db2index can operate correctly.
:id: 191fc0fd-9722-46b5-a7c3-e8760effe119
:setup: Standalone instance
:steps:
1: call db2index
:expectedresults:
1: Index succeeds.
"""
topology_st.standalone.stop()
topology_st.standalone.db2index()
topology_st.standalone.db2index(suffixes=[DEFAULT_SUFFIX], attrs=['uid'])
topology_st.standalone.start()
def test_basic_acl(topology_st, import_example_ldif):
"""Run some basic access control (ACL) tests
:id: 4f4e705f-32f4-4065-b3a8-2b0c2525798b
:setup: Standalone instance
:steps:
1. Add two test users USER1_DN and USER2_DN.
2. Add an aci that denies USER1 from doing anything.
3. Set the default anonymous access for USER2.
4. Try searching entries using USER1.
5. Try searching entries using USER2.
6. Try searching entries using root dn.
7. Cleanup - delete test users and test ACI.
:expectedresults:
1. Test Users should be added.
2. ACI should be added.
3. This operation should PASS.
4. USER1 should not be able to search anything.
5. USER2 should be able to search everything except password.
6. RootDN should be allowed to search everything.
7. Cleanup should PASS.
"""
"""Run some basic access control(ACL) tests"""
log.info('Running test_basic_acl...')
DENY_ACI = ensure_bytes('(targetattr = "*")(version 3.0;acl "deny user";deny (all)(userdn = "ldap:///%s");)' % USER1_DN)
#
# Add two users
#
try:
topology_st.standalone.add_s(Entry((USER1_DN,
{'objectclass': "top extensibleObject".split(),
'sn': '1',
'cn': 'user 1',
'uid': 'user1',
'userpassword': PASSWORD})))
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN +
': error ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.add_s(Entry((USER2_DN,
{'objectclass': "top extensibleObject".split(),
'sn': '2',
'cn': 'user 2',
'uid': 'user2',
'userpassword': PASSWORD})))
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Failed to add test user ' + USER1_DN +
': error ' + e.args[0]['desc'])
assert False
#
# Add an aci that denies USER1 from doing anything,
# and also set the default anonymous access
#
try:
topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_ADD, 'aci', DENY_ACI)])
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Failed to add DENY ACI: error ' + e.args[0]['desc'])
assert False
#
# Make sure USER1_DN can not search anything, but USER2_dn can...
#
try:
topology_st.standalone.simple_bind_s(USER1_DN, PASSWORD)
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Failed to bind as user1, error: ' + e.args[0]['desc'])
assert False
try:
entries = topology_st.standalone.search_s(DEFAULT_SUFFIX,
ldap.SCOPE_SUBTREE,
'(uid=*)')
if entries:
log.fatal('test_basic_acl: User1 was incorrectly able to search the suffix!')
assert False
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Search suffix failed(as user1): ' + e.args[0]['desc'])
assert False
# Now try user2... Also check that userpassword is stripped out
try:
topology_st.standalone.simple_bind_s(USER2_DN, PASSWORD)
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Failed to bind as user2, error: ' + e.args[0]['desc'])
assert False
try:
entries = topology_st.standalone.search_s(DEFAULT_SUFFIX,
ldap.SCOPE_SUBTREE,
'(uid=user1)')
if not entries:
log.fatal('test_basic_acl: User1 incorrectly not able to search the suffix')
assert False
if entries[0].hasAttr('userpassword'):
# The default anonymous access aci should have stripped out userpassword
log.fatal('test_basic_acl: User2 was incorrectly able to see userpassword')
assert False
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Search for user1 failed(as user2): ' + e.args[0]['desc'])
assert False
# Make sure RootDN can also search (this also resets the bind dn to the
# Root DN for future operations)
try:
topology_st.standalone.simple_bind_s(DN_DM, PW_DM)
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Failed to bind as ROotDN, error: ' + e.args[0]['desc'])
assert False
try:
entries = topology_st.standalone.search_s(DEFAULT_SUFFIX,
ldap.SCOPE_SUBTREE,
'(uid=*)')
if not entries:
log.fatal('test_basic_acl: Root DN incorrectly not able to search the suffix')
assert False
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Search for user1 failed(as user2): ' + e.args[0]['desc'])
assert False
#
# Cleanup
#
try:
topology_st.standalone.modify_s(DEFAULT_SUFFIX, [(ldap.MOD_DELETE, 'aci', DENY_ACI)])
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Failed to delete DENY ACI: error ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.delete_s(USER1_DN)
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Failed to delete test entry1: ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.delete_s(USER2_DN)
except ldap.LDAPError as e:
log.fatal('test_basic_acl: Failed to delete test entry2: ' + e.args[0]['desc'])
assert False
log.info('test_basic_acl: PASSED')
def test_basic_searches(topology_st, import_example_ldif):
"""Tests basic search operations with filters.
:id: 426a59ff-49b8-4a70-b377-0c0634a29b6f
:setup: Standalone instance, add example.ldif to the database
:steps:
1. Execute search command while using different filters.
2. Check number of entries returned by search filters.
:expectedresults:
1. Search command should PASS.
2. Number of result entries returned should match number of the database entries according to the search filter.
"""
log.info('Running test_basic_searches...')
filters = (('(uid=scarter)', 1),
('(uid=tmorris*)', 1),
('(uid=*hunt*)', 4),
('(uid=*cope)', 2),
('(mail=*)', 150),
('(roomnumber>=4000)', 35),
('(roomnumber<=4000)', 115),
('(&(roomnumber>=4000)(roomnumber<=4500))', 18),
('(!(l=sunnyvale))', 120),
('(&(uid=t*)(l=santa clara))', 7),
('(|(uid=k*)(uid=r*))', 18),
('(|(uid=t*)(l=sunnyvale))', 50),
('(&(!(uid=r*))(ou=people))', 139),
('(&(uid=m*)(l=sunnyvale)(ou=people)(mail=*example*)(roomNumber=*))', 3),
('(&(|(uid=m*)(l=santa clara))(roomNumber=22*))', 5),
('(&(|(uid=m*)(l=santa clara))(roomNumber=22*)(!(roomnumber=2254)))', 4),)
for (search_filter, search_result) in filters:
try:
entries = topology_st.standalone.search_s(DEFAULT_SUFFIX,
ldap.SCOPE_SUBTREE,
search_filter)
if len(entries) != search_result:
log.fatal('test_basic_searches: An incorrect number of entries\
was returned from filter (%s): (%d) expected (%d)' %
(search_filter, len(entries), search_result))
assert False
except ldap.LDAPError as e:
log.fatal('Search failed: ' + e.args[0]['desc'])
assert False
log.info('test_basic_searches: PASSED')
@pytest.fixture(scope="module")
def add_test_entry(topology_st, request):
# Add test entry
topology_st.standalone.add_s(Entry((USER4_DN,
{'objectclass': "top extensibleObject".split(),
'cn': 'user1', 'uid': 'user1'})))
search_params = [(['1.1'], 'cn', False),
(['1.1', 'cn'], 'cn', True),
(['+'], 'nsUniqueId', True),
(['*'], 'cn', True),
(['cn'], 'cn', True)]
@pytest.mark.skipif(ds_is_older("1.4.2.0"), reason="Not implemented")
@pytest.mark.parametrize("attrs, attr, present", search_params)
def test_search_req_attrs(topology_st, add_test_entry, attrs, attr, present):
"""Test requested attributes in search operations.
:id: 426a59ff-49b8-4a70-b377-0c0634a29b6e
:parametrized: yes
:setup: Standalone instance
:steps:
1. Test "1.1" does not return any attributes.
2. Test "1.1" is ignored if there are other requested attributes
3. Test "+" returns all operational attributes
4. Test "*" returns all attributes
5. Test requested attributes
:expectedresults:
1. Success
2. Success
3. Success
4. Success
5. Success
"""
log.info("Testing attrs: {} attr: {} present: {}".format(attrs, attr, present))
entry = topology_st.standalone.search_s(USER4_DN,
ldap.SCOPE_BASE,
'objectclass=top',
attrs)
if present:
assert entry[0].hasAttr(attr)
else:
assert not entry[0].hasAttr(attr)
def test_basic_referrals(topology_st, import_example_ldif):
"""Test LDAP server in referral mode.
:id: c586aede-7ac3-4e8d-a1cf-bfa8b8d78cc2
:setup: Standalone instance
:steps:
1. Set the referral and the backend state
2. Set backend state to referral mode.
3. Set server to not follow referral.
4. Search using referral.
5. Make sure server can restart in referral mode.
6. Cleanup - Delete referral.
:expectedresults:
1. Set the referral, and the backend state should PASS.
2. Set backend state to referral mode should PASS.
3. Set server to not follow referral should PASS.
4. referral error(10) should occur.
5. Restart should PASS.
6. Cleanup should PASS.
"""
log.info('Running test_basic_referrals...')
SUFFIX_CONFIG = 'cn="dc=example,dc=com",cn=mapping tree,cn=config'
#
# Set the referral, and the backend state
#
try:
topology_st.standalone.modify_s(SUFFIX_CONFIG,
[(ldap.MOD_REPLACE,
'nsslapd-referral',
b'ldap://localhost.localdomain:389/o%3dnetscaperoot')])
except ldap.LDAPError as e:
log.fatal('test_basic_referrals: Failed to set referral: error ' + e.args[0]['desc'])
assert False
try:
topology_st.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE,
'nsslapd-state', b'Referral')])
except ldap.LDAPError as e:
log.fatal('test_basic_referrals: Failed to set backend state: error '
+ e.args[0]['desc'])
assert False
#
# Test that a referral error is returned
#
topology_st.standalone.set_option(ldap.OPT_REFERRALS, 0) # Do not follow referral
try:
topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=top')
except ldap.REFERRAL:
pass
except ldap.LDAPError as e:
log.fatal('test_basic_referrals: Search failed: ' + e.args[0]['desc'])
assert False
#
# Make sure server can restart in referral mode
#
topology_st.standalone.restart(timeout=10)
#
# Cleanup
#
try:
topology_st.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_REPLACE,
'nsslapd-state', b'Backend')])
except ldap.LDAPError as e:
log.fatal('test_basic_referrals: Failed to set backend state: error '
+ e.args[0]['desc'])
assert False
try:
topology_st.standalone.modify_s(SUFFIX_CONFIG, [(ldap.MOD_DELETE,
'nsslapd-referral', None)])
except ldap.LDAPError as e:
log.fatal('test_basic_referrals: Failed to delete referral: error '
+ e.args[0]['desc'])
assert False
topology_st.standalone.set_option(ldap.OPT_REFERRALS, 1)
log.info('test_basic_referrals: PASSED')
def test_basic_systemctl(topology_st, import_example_ldif):
"""Tests systemctl/lib389 can stop and start the server.
:id: a92a7438-ecfa-4583-a89c-5fbfc0220b69
:setup: Standalone instance
:steps:
1. Stop the server.
2. Start the server.
3. Stop the server, break the dse.ldif and dse.ldif.bak, so a start fails.
4. Verify that systemctl detects the failed start.
5. Fix the dse.ldif, and make sure the server starts up.
6. Verify systemctl correctly identifies the successful start.
:expectedresults:
1. Server should be stopped.
2. Server should start
3. Stop should work but start after breaking dse.ldif should fail.
4. Systemctl should be able to detect the failed start.
5. Server should start.
6. Systemctl should be able to detect the successful start.
"""
log.info('Running test_basic_systemctl...')
config_dir = topology_st.standalone.get_config_dir()
#
# Stop the server
#
log.info('Stopping the server...')
topology_st.standalone.stop()
log.info('Stopped the server.')
#
# Start the server
#
log.info('Starting the server...')
topology_st.standalone.start()
log.info('Started the server.')
#
# Stop the server, break the dse.ldif so a start fails,
# and verify that systemctl detects the failed start
#
log.info('Stopping the server...')
topology_st.standalone.stop()
log.info('Stopped the server before breaking the dse.ldif.')
shutil.copy(config_dir + '/dse.ldif', config_dir + '/dse.ldif.correct')
open(config_dir + '/dse.ldif', 'w').close()
# We need to kill the .bak file too, DS is just too smart!
open(config_dir + '/dse.ldif.bak', 'w').close()
log.info('Attempting to start the server with broken dse.ldif...')
try:
topology_st.standalone.start()
except Exception as e:
log.info('Server failed to start as expected: ' + str(e))
log.info('Check the status...')
assert (not topology_st.standalone.status())
log.info('Server failed to start as expected')
time.sleep(5)
#
# Fix the dse.ldif, and make sure the server starts up,
# and systemctl correctly identifies the successful start
#
shutil.copy(config_dir + '/dse.ldif.correct', config_dir + '/dse.ldif')
log.info('Starting the server with good dse.ldif...')
topology_st.standalone.start()
log.info('Check the status...')
assert (topology_st.standalone.status())
log.info('Server started after fixing dse.ldif.')
log.info('test_basic_systemctl: PASSED')
def test_basic_ldapagent(topology_st, import_example_ldif):
"""Tests that the ldap agent starts
:id: da1d1846-8fc4-4b8c-8e53-4c9c16eff1ba
:setup: Standalone instance
:steps:
1. Start SNMP ldap agent using command.
2. Cleanup - Kill SNMP agent process.
:expectedresults:
1. SNMP agent should start.
2. SNMP agent process should be successfully killed.
"""
log.info('Running test_basic_ldapagent...')
var_dir = topology_st.standalone.get_local_state_dir()
config_file = os.path.join(topology_st.standalone.get_sysconf_dir(), 'dirsrv/config/agent.conf')
agent_config_file = open(config_file, 'w')
agent_config_file.write('agentx-master ' + var_dir + '/agentx/master\n')
agent_config_file.write('agent-logdir ' + var_dir + '/log/dirsrv\n')
agent_config_file.write('server slapd-' + topology_st.standalone.serverid + '\n')
agent_config_file.close()
# Remember, this is *forking*
check_output([os.path.join(topology_st.standalone.get_sbin_dir(), 'ldap-agent'), config_file])
# First kill any previous agents ....
pidpath = os.path.join(var_dir, 'run/ldap-agent.pid')
pid = None
with open(pidpath, 'r') as pf:
pid = pf.readlines()[0].strip()
if pid:
log.debug('test_basic_ldapagent: Terminating agent %s', pid)
check_output(['kill', pid])
log.info('test_basic_ldapagent: PASSED')
@pytest.mark.skipif(not get_user_is_ds_owner(),
reason="process ownership permission is required")
def test_basic_dse_survives_kill9(topology_st, import_example_ldif):
"""Tests that the dse.ldif is not wiped out after the process is killed (bug 910581)
:id: 10f141da-9b22-443a-885c-87271dcd7a59
:setup: Standalone instance
:steps:
1. Check out pid of ns-slapd process and Kill ns-slapd process.
2. Check the contents of dse.ldif file.
3. Start server.
:expectedresults:
1. ns-slapd process should be killed.
2. dse.ldif should not be corrupted.
3. Server should start successfully.
"""
log.info('Running test_basic_dse...')
dse_file = topology_st.standalone.confdir + '/dse.ldif'
pid = check_output(['pidof', '-s', 'ns-slapd']).strip()
# We can't guarantee we have access to sudo in any environment ... Either
# run py.test with sudo, or as the same user as the dirsrv.
check_output(['kill', '-9', ensure_str(pid)])
if os.path.getsize(dse_file) == 0:
log.fatal('test_basic_dse: dse.ldif\'s content was incorrectly removed!')
assert False
topology_st.standalone.start(timeout=60)
log.info('dse.ldif was not corrupted, and the server was restarted')
log.info('test_basic_dse: PASSED')
# Give the server time to startup, in some conditions this can be racey without systemd notification. Only affects this one test though...
time.sleep(10)
@pytest.mark.parametrize("rootdse_attr_name", ROOTDSE_DEF_ATTR_LIST)
def test_def_rootdse_attr(topology_st, import_example_ldif, rootdse_attr_name):
"""Tests that operational attributes are not returned by default in rootDSE searches
:id: 4fee33cc-4019-4c27-89e8-998e6c770dc0
:parametrized: yes
:setup: Standalone instance
:steps:
1. Make an ldapsearch for rootdse attribute
2. Check the returned entries.
:expectedresults:
1. Search should not fail
2. Operational attributes should not be returned.
"""
topology_st.standalone.start()
log.info(" Assert rootdse search hasn't %s attr" % rootdse_attr_name)
try:
entry = topology_st.standalone.search_s("", ldap.SCOPE_BASE)[0]
assert not entry.hasAttr(rootdse_attr_name)
except ldap.LDAPError as e:
log.fatal('Search failed, error: ' + e.args[0]['desc'])
assert False
def test_mod_def_rootdse_attr(topology_st, import_example_ldif, rootdse_attr):
"""Tests that operational attributes are returned by default in rootDSE searches after config modification
:id: c7831e04-f458-4e23-83c7-b6f66109f639
:parametrized: yes
:setup: Standalone instance and we are using rootdse_attr fixture which
adds nsslapd-return-default-opattr attr with value of one operation attribute.
:steps:
1. Make an ldapsearch for rootdse attribute
2. Check the returned entries.
:expectedresults:
1. Search should not fail
2. Operational attributes should be returned after the config modification
"""
log.info(" Assert rootdse search has %s attr" % rootdse_attr)
try:
entry = topology_st.standalone.search_s("", ldap.SCOPE_BASE)[0]
assert entry.hasAttr(rootdse_attr)
except ldap.LDAPError as e:
log.fatal('Search failed, error: ' + e.args[0]['desc'])
assert False
@pytest.fixture(scope="module")
def create_users(topology_st):
"""Add users to the default suffix
"""
users = UserAccounts(topology_st.standalone, DEFAULT_SUFFIX)
user_names = ["Directory", "Server", "389", "lib389", "pytest"]
log.info('Adding 5 test users')
for name in user_names:
users.create(properties={
'uid': name,
'sn': name,
'cn': name,
'uidNumber': '1000',
'gidNumber': '1000',
'homeDirectory': '/home/%s' % name,
'mail': '%s@example.com' % name,
'userpassword': 'pass%s' % name,
})
def test_basic_anonymous_search(topology_st, create_users):
"""Tests basic anonymous search operations
:id: c7831e04-f458-4e50-83c7-b6f77109f639
:setup: Standalone instance
Add 5 test users with different user names
:steps:
1. Execute anonymous search with different filters
:expectedresults:
1. Search should be successful
"""
filters = ["uid=Directory", "(|(uid=S*)(uid=3*))", "(&(uid=l*)(mail=l*))", "(&(!(uid=D*))(ou=People))"]
log.info("Execute anonymous search with different filters")
for filtr in filters:
entries = topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, filtr)
assert len(entries) != 0