Skip to content

Commit 5f69cb5

Browse files
committed
test(ldap.dn): Add test cases for ldap.dn.dn2str() with different format flags
1 parent e75c24d commit 5f69cb5

File tree

1 file changed

+137
-4
lines changed

1 file changed

+137
-4
lines changed

Tests/t_ldap_dn.py

Lines changed: 137 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ def test_str2dn(self):
5757
test function str2dn()
5858
"""
5959
self.assertEqual(ldap.dn.str2dn(''), [])
60+
self.assertEqual(ldap.dn.str2dn(None), [])
6061
self.assertEqual(
6162
ldap.dn.str2dn('uid=test42,ou=Testing,dc=example,dc=com'),
6263
[
@@ -105,15 +106,24 @@ def test_str2dn(self):
105106
self.assertEqual(
106107
ldap.dn.str2dn('cn=äöüÄÖÜß,dc=example,dc=com', flags=0),
107108
[
108-
[('cn', 'äöüÄÖÜß', 4)],
109+
[('cn', 'äöüÄÖÜß', ldap.AVA_NONPRINTABLE)],
109110
[('dc', 'example', 1)],
110111
[('dc', 'com', 1)]
111112
]
112113
)
113114
self.assertEqual(
114115
ldap.dn.str2dn('cn=\\c3\\a4\\c3\\b6\\c3\\bc\\c3\\84\\c3\\96\\c3\\9c\\c3\\9f,dc=example,dc=com', flags=0),
115116
[
116-
[('cn', 'äöüÄÖÜß', 4)],
117+
[('cn', 'äöüÄÖÜß', ldap.AVA_NONPRINTABLE)],
118+
[('dc', 'example', 1)],
119+
[('dc', 'com', 1)]
120+
]
121+
)
122+
self.assertEqual(
123+
ldap.dn.str2dn('/dc=com/dc=example/ou=Testing/uid=test42', flags=ldap.DN_FORMAT_DCE),
124+
[
125+
[('uid', 'test42', 1)],
126+
[('ou', 'Testing', 1)],
117127
[('dc', 'example', 1)],
118128
[('dc', 'com', 1)]
119129
]
@@ -123,7 +133,7 @@ def test_dn2str(self):
123133
"""
124134
test function dn2str()
125135
"""
126-
self.assertEqual(ldap.dn.str2dn(''), [])
136+
self.assertEqual(ldap.dn.dn2str([]), '')
127137
self.assertEqual(
128138
ldap.dn.dn2str([
129139
[('uid', 'test42', 1)],
@@ -162,12 +172,135 @@ def test_dn2str(self):
162172
)
163173
self.assertEqual(
164174
ldap.dn.dn2str([
165-
[('cn', 'äöüÄÖÜß', 4)],
175+
[('uid', 'test, 42', 1)],
176+
[('ou', 'Testing', 1)],
177+
[('dc', 'example', 1)],
178+
[('dc', 'com', 1)]
179+
], ldap.DN_FORMAT_LDAPV3),
180+
r'uid=test\2C 42,ou=Testing,dc=example,dc=com'
181+
)
182+
self.assertEqual(
183+
ldap.dn.dn2str([
184+
[('cn', 'äöüÄÖÜß', ldap.AVA_NONPRINTABLE)],
166185
[('dc', 'example', 1)],
167186
[('dc', 'com', 1)]
168187
]),
169188
'cn=äöüÄÖÜß,dc=example,dc=com'
170189
)
190+
self.assertEqual(
191+
ldap.dn.dn2str([
192+
[('cn', 'äöüÄÖÜß', ldap.AVA_NONPRINTABLE)],
193+
[('dc', 'example', 1)],
194+
[('dc', 'com', 1)]
195+
], ldap.DN_FORMAT_LDAPV3),
196+
r'cn=\C3\A4\C3\B6\C3\BC\C3\84\C3\96\C3\9C\C3\9F,dc=example,dc=com'
197+
)
198+
self.assertEqual(
199+
ldap.dn.dn2str([
200+
[('uid', 'test42', 1), ('cn', 'test42', 1)],
201+
[('ou', 'Testing', 1)],
202+
[('dc', 'example', 1)],
203+
[('dc', 'com', 1)]
204+
], ldap.DN_FORMAT_AD_CANONICAL),
205+
'example.com/Testing/test42,test42'
206+
)
207+
self.assertEqual(
208+
ldap.dn.dn2str([
209+
[('uid', 'test42', 1), ('cn', 'test42', 1)],
210+
[('ou', 'Testing', 1)],
211+
[('dc', 'example', 1)],
212+
[('dc', 'com', 1)]
213+
], ldap.DN_FORMAT_UFN),
214+
'test42 + test42, Testing, example.com'
215+
)
216+
self.assertEqual(
217+
ldap.dn.dn2str([
218+
[('uid', 'test42', 1), ('cn', 'test42', 1)],
219+
[('ou', 'Testing', 1)],
220+
[('dc', 'example', 1)],
221+
[('dc', 'com', 1)]
222+
], ldap.DN_FORMAT_DCE),
223+
'/dc=com/dc=example/ou=Testing/uid=test42,cn=test42'
224+
)
225+
226+
self.assertEqual(
227+
ldap.dn.dn2str([
228+
[('cn', 'äöüÄÖÜß', ldap.AVA_BINARY)],
229+
[('dc', 'example', 1)],
230+
[('dc', 'com', 1)]
231+
], ldap.DN_FORMAT_LDAPV3),
232+
'cn=#C3A4C3B6C3BCC384C396C39CC39F,dc=example,dc=com'
233+
)
234+
self.assertEqual(
235+
ldap.dn.dn2str([
236+
[('cn', 'äöüÄÖÜß', ldap.AVA_NULL)],
237+
[('dc', 'example', 1)],
238+
[('dc', 'com', 1)]
239+
], ldap.DN_FORMAT_LDAPV3),
240+
r'cn=\C3\A4\C3\B6\C3\BC\C3\84\C3\96\C3\9C\C3\9F,dc=example,dc=com'
241+
)
242+
self.assertEqual(
243+
ldap.dn.dn2str([
244+
[('cn', 'äöüÄÖÜß', ldap.AVA_STRING)],
245+
[('dc', 'example', 1)],
246+
[('dc', 'com', 1)]
247+
], ldap.DN_FORMAT_LDAPV3),
248+
r'cn=\C3\A4\C3\B6\C3\BC\C3\84\C3\96\C3\9C\C3\9F,dc=example,dc=com'
249+
)
250+
self.assertEqual(
251+
ldap.dn.dn2str([
252+
[('cn', 'äöüÄÖÜß', ldap.AVA_NONPRINTABLE)],
253+
[('dc', 'example', 1)],
254+
[('dc', 'com', 1)]
255+
], ldap.DN_FORMAT_LDAPV3),
256+
r'cn=\C3\A4\C3\B6\C3\BC\C3\84\C3\96\C3\9C\C3\9F,dc=example,dc=com'
257+
)
258+
259+
def test_dn2str_errors(self):
260+
"""
261+
test error handling of function dn2str()
262+
"""
263+
with self.assertRaises(RuntimeError):
264+
ldap.dn.dn2str([[('uid', 'test42', 1)]], 142)
265+
266+
ldap_format = ldap.DN_FORMAT_LDAPV3
267+
268+
with self.assertRaises(TypeError):
269+
ldap.dn.dn2str(None)
270+
271+
with self.assertRaises(TypeError):
272+
ldap.dn.dn2str(None, ldap_format)
273+
274+
with self.assertRaises(TypeError):
275+
ldap.dn.dn2str([1], ldap_format)
276+
277+
with self.assertRaises(TypeError):
278+
ldap.dn.dn2str([[1]], ldap_format)
279+
280+
with self.assertRaises(TypeError):
281+
ldap.dn.dn2str([[('uid', 'test42', '1')]], ldap_format)
282+
283+
with self.assertRaises(TypeError):
284+
ldap.dn.dn2str([[('uid', 'test42', 1.0)]], ldap_format)
285+
286+
with self.assertRaises(TypeError):
287+
ldap.dn.dn2str([[['uid', 'test42', 1]]], ldap_format)
288+
289+
with self.assertRaises(TypeError):
290+
ldap.dn.dn2str([
291+
[('uid', 'test42', 1), ('cn', 'test42', 1)],
292+
[('ou', 'Testing', 1)],
293+
[('dc', 'example', '1')],
294+
[('dc', 'com', 1)]
295+
], ldap_format),
296+
297+
with self.assertRaises(TypeError):
298+
ldap.dn.dn2str([
299+
[('ou', 'Testing', 1)],
300+
[('dc', 'example', 1)],
301+
[('uid', 'test42', 1), ('cn', 'test42', '1')],
302+
[('dc', 'com', 1)]
303+
], ldap_format),
171304

172305
def test_explode_dn(self):
173306
"""

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy