|
| 1 | +from __future__ import unicode_literals |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +if sys.version_info[0] <= 2: |
| 6 | + PY2 = True |
| 7 | + text_type = unicode |
| 8 | +else: |
| 9 | + PY2 = False |
| 10 | + text_type = str |
| 11 | + |
| 12 | +import ldap, unittest |
| 13 | +from slapdtest import SlapdObject |
| 14 | +from ldap.ldapobject import LDAPObject |
| 15 | + |
| 16 | +server = None |
| 17 | + |
| 18 | + |
| 19 | +class TestBinds(unittest.TestCase): |
| 20 | + |
| 21 | + def setUp(self): |
| 22 | + global server |
| 23 | + if server is None: |
| 24 | + server = SlapdObject() |
| 25 | + server.start() |
| 26 | + |
| 27 | + self.server = server |
| 28 | + self.unicode_val = "abc\U0001f498def" |
| 29 | + self.unicode_val_bytes = self.unicode_val.encode('utf-8') |
| 30 | + |
| 31 | + self.dn_unicode = "CN=" + self.unicode_val |
| 32 | + self.dn_bytes = self.dn_unicode.encode('utf-8') |
| 33 | + |
| 34 | + def _get_ldapobject(self, bytes_mode=None): |
| 35 | + l = LDAPObject(self.server.ldap_uri, bytes_mode=bytes_mode) |
| 36 | + l.protocol_version = 3 |
| 37 | + l.set_option(ldap.OPT_REFERRALS,0) |
| 38 | + return l |
| 39 | + |
| 40 | + def test_simple_bind(self): |
| 41 | + l = self._get_ldapobject(False) |
| 42 | + with self.assertRaises(ldap.INVALID_CREDENTIALS): |
| 43 | + l.simple_bind_s(self.dn_unicode, self.unicode_val) |
| 44 | + |
| 45 | + def test_unicode_bind(self): |
| 46 | + l = self._get_ldapobject(False) |
| 47 | + l.simple_bind(self.dn_unicode, "ascii") |
| 48 | + |
| 49 | + l = self._get_ldapobject(False) |
| 50 | + l.simple_bind("CN=user", self.unicode_val) |
| 51 | + |
| 52 | + @unittest.skipUnless(PY2, "no bytes_mode under Py3") |
| 53 | + def test_unicode_bind_bytesmode(self): |
| 54 | + l = self._get_ldapobject(True) |
| 55 | + with self.assertRaises(TypeError): |
| 56 | + l.simple_bind_s(self.dn_unicode, self.unicode_val_bytes) |
| 57 | + |
| 58 | + with self.assertRaises(TypeError): |
| 59 | + l.simple_bind_s(self.dn_bytes, self.unicode_val) |
| 60 | + |
| 61 | + # Works when encoded to UTF-8 |
| 62 | + with self.assertRaises(ldap.INVALID_CREDENTIALS): |
| 63 | + l.simple_bind_s(self.dn_bytes, self.unicode_val_bytes) |
| 64 | + |
| 65 | + def test_unicode_bind_no_bytesmode(self): |
| 66 | + l = self._get_ldapobject(False) |
| 67 | + with self.assertRaises(TypeError): |
| 68 | + l.simple_bind_s(self.dn_bytes, self.unicode_val) |
| 69 | + |
| 70 | + # Works fine in Python 3 because 'cred' (the password) is read in |
| 71 | + # using the "s#" format which, unlike "s", accepts either a str |
| 72 | + # (unicode) *or* bytes. |
| 73 | + # |
| 74 | + # with self.assertRaises(TypeError): |
| 75 | + # l.simple_bind_s(self.dn_unicode, self.unicode_val_bytes) |
| 76 | + |
| 77 | + with self.assertRaises(ldap.INVALID_CREDENTIALS): |
| 78 | + l.simple_bind_s(self.dn_unicode, self.unicode_val) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == '__main__': |
| 82 | + unittest.main() |
0 commit comments