8
8
from ldap .controls import RequestControlTuples
9
9
from ldap .controls .pagedresults import SimplePagedResultsControl
10
10
from ldap .controls .openldap import SearchNoOpControl
11
- from slapdtest import requires_tls
12
-
11
+ from ldap . ldapobject import SimpleLDAPObject
12
+ from slapdtest import SlapdTestCase , requires_tls
13
13
14
14
SENTINEL = object ()
15
15
26
26
]
27
27
28
28
29
- class TestGlobalOptions (unittest .TestCase ):
29
+ class BaseTestOptions (object ):
30
+ """Common tests for getting/setting options
31
+
32
+ Used in subclasses below
33
+ """
34
+
35
+ def get_option (self , option ):
36
+ raise NotImplementedError ()
37
+
38
+ def set_option (self , option , value ):
39
+ raise NotImplementedError ()
40
+
30
41
def _check_option (self , option , value , expected = SENTINEL ,
31
42
nonevalue = None ):
32
- old = ldap .get_option (option )
43
+ old = self .get_option (option )
33
44
try :
34
- ldap .set_option (option , value )
35
- new = ldap .get_option (option )
45
+ self .set_option (option , value )
46
+ new = self .get_option (option )
36
47
if expected is SENTINEL :
37
48
self .assertEqual (new , value )
38
49
else :
39
50
self .assertEqual (new , expected )
40
51
finally :
41
- ldap .set_option (option , old if old is not None else nonevalue )
42
- self .assertEqual (ldap .get_option (option ), old )
52
+ self .set_option (
53
+ option ,
54
+ old if old is not None else nonevalue
55
+ )
56
+ self .assertEqual (self .get_option (option ), old )
43
57
44
58
def test_invalid (self ):
45
59
with self .assertRaises (ValueError ):
46
- ldap .get_option (- 1 )
60
+ self .get_option (- 1 )
47
61
with self .assertRaises (ValueError ):
48
- ldap .set_option (- 1 , '' )
62
+ self .set_option (- 1 , '' )
49
63
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 )
64
+ def _test_timeout (self , option ):
65
+ self ._check_option (option , 10.5 , nonevalue = - 1 )
66
+ self ._check_option (option , 0 , nonevalue = - 1 )
53
67
with self .assertRaises (ValueError ):
54
- self ._check_option (ldap . OPT_TIMEOUT , - 5 , nonevalue = - 1 )
68
+ self ._check_option (option , - 5 , nonevalue = - 1 )
55
69
with self .assertRaises (TypeError ):
56
- ldap .set_option (ldap .OPT_TIMEOUT , object )
70
+ self .set_option (option , object )
71
+
72
+ def test_timeout (self ):
73
+ self ._test_timeout (ldap .OPT_TIMEOUT )
57
74
58
75
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 )
76
+ self ._test_timeout (ldap .OPT_NETWORK_TIMEOUT )
63
77
64
78
def _test_controls (self , option ):
65
79
self ._check_option (option , [])
66
80
self ._check_option (option , TEST_CTRL , TEST_CTRL_EXPECTED )
67
81
self ._check_option (option , tuple (TEST_CTRL ), TEST_CTRL_EXPECTED )
68
82
with self .assertRaises (TypeError ):
69
- ldap .set_option (option , object )
83
+ self .set_option (option , object )
70
84
71
85
with self .assertRaises (TypeError ):
72
86
# must contain a tuple
73
- ldap .set_option (option , [list (TEST_CTRL [0 ])])
87
+ self .set_option (option , [list (TEST_CTRL [0 ])])
74
88
with self .assertRaises (TypeError ):
75
89
# data must be bytes or None
76
- ldap .set_option (
90
+ self .set_option (
77
91
option ,
78
92
[TEST_CTRL [0 ][0 ], TEST_CTRL [0 ][1 ], u'data' ]
79
93
)
@@ -87,20 +101,64 @@ def test_server_controls(self):
87
101
def test_uri (self ):
88
102
self ._check_option (ldap .OPT_URI , "ldapi:///path/to/socket" )
89
103
with self .assertRaises (TypeError ):
90
- ldap .set_option (ldap .OPT_URI , object )
104
+ self .set_option (ldap .OPT_URI , object )
91
105
92
106
@requires_tls ()
93
107
def test_cafile (self ):
94
108
# None or a distribution or OS-specific path
95
- ldap .get_option (ldap .OPT_X_TLS_CACERTFILE )
109
+ self .get_option (ldap .OPT_X_TLS_CACERTFILE )
96
110
97
111
def test_readonly (self ):
98
- value = ldap .get_option (ldap .OPT_API_INFO )
112
+ value = self .get_option (ldap .OPT_API_INFO )
99
113
self .assertIsInstance (value , dict )
100
114
with self .assertRaises (ValueError ) as e :
101
- ldap .set_option (ldap .OPT_API_INFO , value )
115
+ self .set_option (ldap .OPT_API_INFO , value )
102
116
self .assertIn ('read-only' , str (e .exception ))
103
117
104
118
119
+ class TestGlobalOptions (BaseTestOptions , unittest .TestCase ):
120
+ """Test setting/getting options globally
121
+ """
122
+
123
+ def get_option (self , option ):
124
+ return ldap .get_option (option )
125
+
126
+ def set_option (self , option , value ):
127
+ return ldap .set_option (option , value )
128
+
129
+
130
+ class TestLDAPObjectOptions (BaseTestOptions , SlapdTestCase ):
131
+ """Test setting/getting connection-specific options
132
+ """
133
+
134
+ ldap_object_class = SimpleLDAPObject
135
+
136
+ def setUp (self ):
137
+ self .conn = self ._open_ldap_conn (
138
+ who = self .server .root_dn ,
139
+ cred = self .server .root_pw
140
+ )
141
+
142
+ def tearDown (self ):
143
+ self .conn .unbind_s ()
144
+ self .conn = None
145
+
146
+ def get_option (self , option ):
147
+ return self .conn .get_option (option )
148
+
149
+ def set_option (self , option , value ):
150
+ return self .conn .set_option (option , value )
151
+
152
+ # test is failing with:
153
+ # pyasn1.error.SubstrateUnderrunError: Short octet stream on tag decoding
154
+ @unittest .expectedFailure
155
+ def test_client_controls (self ):
156
+ self ._test_controls (ldap .OPT_CLIENT_CONTROLS )
157
+
158
+ @unittest .expectedFailure
159
+ def test_server_controls (self ):
160
+ self ._test_controls (ldap .OPT_SERVER_CONTROLS )
161
+
162
+
105
163
if __name__ == '__main__' :
106
164
unittest .main ()
0 commit comments