Debian Codegraph @

dirsrvtests/tests/suites/subentries/subentries_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2021 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 # Author: Anton Bobrov <[email protected]>
10
11 import logging
12 import pytest
13 import os
14 import ldap
15 from ldap.controls import LDAPControl
16 from lib389 import DirSrv
17 from lib389.rootdse import RootDSE
18 from lib389.utils import *
19 from lib389._constants import *
20 from test389.topologies import create_topology
21 from test389.topologies import topology_st as topo
22 from lib389.idm.user import UserAccount, UserAccounts
23
24 log = logging.getLogger(__name__)
25
26 pytestmark = pytest.mark.tier0
27
28 """
29 This BooleanControl class is taken from python-ldap,
30 see https://www.python-ldap.org/ for details.
31 The reason is python-ldap standard class has not been
32 updated for Python 3 properly and behaves incorrectly.
33 When python-ldap is fixed this class can be removed in
34 exchange for from ldap.controls import BooleanControl
35 """
36 class BooleanControl(LDAPControl):
37 """
38 Base class for simple request controls with boolean control value.
39 Constructor argument and class attribute:
40 booleanValue
41 Boolean (True/False or 1/0) which is the boolean controlValue.
42 """
43 boolean2ber = { 1:b'\x01\x01\xFF', 0:b'\x01\x01\x00' }
44 ber2boolean = { b'\x01\x01\xFF':1, b'\x01\x01\x00':0 }
45
46 def __init__(self,controlType=None,criticality=False,booleanValue=False):
47 self.controlType = controlType
48 self.criticality = criticality
49 self.booleanValue = booleanValue
50
51 def encodeControlValue(self):
52 return self.boolean2ber[int(self.booleanValue)]
53
54 def decodeControlValue(self,encodedControlValue):
55 self.booleanValue = self.ber2boolean[encodedControlValue]
56
57 def has_subentries_control(topo):
58 """
59 Checks if server supports LDAP Subentries control
60 """
61 rdse = RootDSE(topo.standalone)
62 return "1.3.6.1.4.1.4203.1.10.1" in rdse.get_supported_ctrls()
63
64
65 @pytest.fixture(scope="module")
66 def setup_test_entries(topo, request):
67 """
68 Add nentries entries and nentries subentries.
69 """
70 users = []
71 # Add normal entries.
72 user = UserAccounts(topo.standalone, DEFAULT_SUFFIX)
73 for i in range(0, nentries):
74 user1 = user.create_test_user(uid=i)
75 users.append(user1)
76
77 # Add subentries.
78 for i in range(nentries, nentries * 2):
79 user1 = user.create_test_user(uid=i)
80 user1.add("objectclass", "ldapsubentry")
81 users.append(user1)
82
83 def fin():
84 for user in users:
85 user.delete()
86
87
88 request.addfinalizer(fin)
89
90
91 nentries = 5
92 search_entries = '(objectclass=inetorgperson)'
93 search_subentries = '(objectclass=ldapsubentry)'
94
95 # Test matrix parameters
96 searches = [
97 # Search with subentries control visibility TRUE
98 (search_entries, True, True, nentries, True),
99 # Search with subentries control visibility FALSE
100 (search_entries, True, False, nentries, False),
101 # Search for normal entries
102 (search_entries, False, None, nentries, False),
103 # Search for subentries
104 (search_subentries, False, None, nentries, True),
105 # Search for normal entries and subentries
106 (f'(|{search_entries}{search_subentries})', False, None,
107 nentries * 2, None)
108 ]
109
110 @pytest.mark.parametrize('search_filter, use_control, controlValue,'\
111 'expected_nentries, expected_subentries', searches)
112 def test_subentries(topo, setup_test_entries, search_filter, use_control,
113 controlValue, expected_nentries, expected_subentries):
114 """Test LDAP Subentries control (RFC 3672)
115
116 :id: 5cdb72eb-d227-49c8-9f7a-89314c717a85
117 :setup: Standalone Instance
118 :parametrized: yes
119 :steps:
120 1. Add test entries and subentries
121 2. Search with subentries control visibility TRUE
122 3. Search with subentries control visibility FALSE
123 4. Search for normal entries
124 5. Search for subentries
125 6. Search for normal entries and subentries
126 :expectedresults:
127 1. Entries and subentries should be added
128 2. Only subentries are visible
129 3. Only normal entries are visible
130 4. Only normal entries are visible
131 5. Only subentries are visible
132 6. Both normal entries and subentries are visible
133 """
134 if use_control and not has_subentries_control(topo):
135 pytest.skip("This test is only required when LDAP Subentries "\
136 "control is supported.")
137
138 request_control = BooleanControl(
139 controlType="1.3.6.1.4.1.4203.1.10.1",
140 criticality=True, booleanValue=controlValue)
141
142 if use_control:
143 request_ctrl = [request_control]
144 else:
145 request_ctrl = None
146
147 entries = topo.standalone.search_ext_s("ou=people," +
148 DEFAULT_SUFFIX,
149 ldap.SCOPE_SUBTREE,
150 search_filter,
151 serverctrls=request_ctrl)
152
153 assert len(entries) == expected_nentries
154 if expected_subentries is not None:
155 if expected_subentries:
156 for entry in entries:
157 assert ensure_bytes("ldapsubentry") in \
158 entry.getValues("objectclass")
159 else:
160 for entry in entries:
161 assert ensure_bytes("ldapsubentry") not in \
162 entry.getValues("objectclass")
163
164
165 if __name__ == '__main__':
166 # Run isolated
167 # -s for DEBUG mode
168 CURRENT_FILE = os.path.realpath(__file__)
169 pytest.main(["-s", CURRENT_FILE])