Skip to content

Commit 2aa8bca

Browse files
tiranencukou
authored andcommitted
Add tests for get/set options
Signed-off-by: Christian Heimes <cheimes@redhat.com>
1 parent dcb5c00 commit 2aa8bca

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

Tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
from . import t_ldap_schema_subentry
2323
from . import t_untested_mods
2424
from . import t_ldap_controls_libldap
25+
from . import t_ldap_options

Tests/t_ldap_options.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import os
2+
import unittest
3+
4+
# Switch off processing .ldaprc or ldap.conf before importing _ldap
5+
os.environ['LDAPNOINIT'] = '1'
6+
7+
import ldap
8+
from ldap.controls import RequestControlTuples
9+
from ldap.controls.pagedresults import SimplePagedResultsControl
10+
from ldap.controls.openldap import SearchNoOpControl
11+
from slapdtest import requires_tls
12+
13+
14+
SENTINEL = object()
15+
16+
TEST_CTRL = RequestControlTuples([
17+
# with BER data
18+
SimplePagedResultsControl(criticality=0, size=5, cookie=b'cookie'),
19+
# value-less
20+
SearchNoOpControl(criticality=1),
21+
])
22+
TEST_CTRL_EXPECTED = [
23+
TEST_CTRL[0],
24+
# get_option returns empty bytes
25+
(TEST_CTRL[1][0], TEST_CTRL[1][1], b''),
26+
]
27+
28+
29+
class TestGlobalOptions(unittest.TestCase):
30+
def _check_option(self, option, value, expected=SENTINEL,
31+
nonevalue=None):
32+
old = ldap.get_option(option)
33+
try:
34+
ldap.set_option(option, value)
35+
new = ldap.get_option(option)
36+
if expected is SENTINEL:
37+
self.assertEqual(new, value)
38+
else:
39+
self.assertEqual(new, expected)
40+
finally:
41+
ldap.set_option(option, old if old is not None else nonevalue)
42+
self.assertEqual(ldap.get_option(option), old)
43+
44+
def test_invalid(self):
45+
with self.assertRaises(ValueError):
46+
ldap.get_option(-1)
47+
with self.assertRaises(ValueError):
48+
ldap.set_option(-1, '')
49+
50+
def test_timeout(self):
51+
self._check_option(ldap.OPT_TIMEOUT, 0, nonevalue=-1)
52+
self._check_option(ldap.OPT_TIMEOUT, 10.5, nonevalue=-1)
53+
with self.assertRaises(ValueError):
54+
self._check_option(ldap.OPT_TIMEOUT, -5, nonevalue=-1)
55+
with self.assertRaises(TypeError):
56+
ldap.set_option(ldap.OPT_TIMEOUT, object)
57+
58+
def test_network_timeout(self):
59+
self._check_option(ldap.OPT_NETWORK_TIMEOUT, 0, nonevalue=-1)
60+
self._check_option(ldap.OPT_NETWORK_TIMEOUT, 10.5, nonevalue=-1)
61+
with self.assertRaises(ValueError):
62+
self._check_option(ldap.OPT_NETWORK_TIMEOUT, -5, nonevalue=-1)
63+
64+
def _test_controls(self, option):
65+
self._check_option(option, [])
66+
self._check_option(option, TEST_CTRL, TEST_CTRL_EXPECTED)
67+
self._check_option(option, tuple(TEST_CTRL), TEST_CTRL_EXPECTED)
68+
with self.assertRaises(TypeError):
69+
ldap.set_option(option, object)
70+
71+
with self.assertRaises(TypeError):
72+
# must contain a tuple
73+
ldap.set_option(option, [list(TEST_CTRL[0])])
74+
with self.assertRaises(TypeError):
75+
# data must be bytes or None
76+
ldap.set_option(
77+
option,
78+
[TEST_CTRL[0][0], TEST_CTRL[0][1], u'data']
79+
)
80+
81+
def test_client_controls(self):
82+
self._test_controls(ldap.OPT_CLIENT_CONTROLS)
83+
84+
def test_server_controls(self):
85+
self._test_controls(ldap.OPT_SERVER_CONTROLS)
86+
87+
def test_uri(self):
88+
self._check_option(ldap.OPT_URI, "ldapi:///path/to/socket")
89+
with self.assertRaises(TypeError):
90+
ldap.set_option(ldap.OPT_URI, object)
91+
92+
@requires_tls()
93+
def test_cafile(self):
94+
# None or a distribution or OS-specific path
95+
ldap.get_option(ldap.OPT_X_TLS_CACERTFILE)
96+
97+
def test_readonly(self):
98+
value = ldap.get_option(ldap.OPT_API_INFO)
99+
self.assertIsInstance(value, dict)
100+
with self.assertRaises(ValueError) as e:
101+
ldap.set_option(ldap.OPT_API_INFO, value)
102+
self.assertIn('read-only', str(e.exception))
103+
104+
105+
if __name__ == '__main__':
106+
unittest.main()

0 commit comments

Comments
 (0)