| 1 |
""" |
| 2 |
""" |
| 3 |
|
| 4 |
# Created on 2013.06.06 |
| 5 |
# |
| 6 |
# Author: Giovanni Cannata |
| 7 |
# |
| 8 |
# Copyright 2013 - 2020 Giovanni Cannata |
| 9 |
# |
| 10 |
# This file is part of ldap3. |
| 11 |
# |
| 12 |
# ldap3 is free software: you can redistribute it and/or modify |
| 13 |
# it under the terms of the GNU Lesser General Public License as published |
| 14 |
# by the Free Software Foundation, either version 3 of the License, or |
| 15 |
# (at your option) any later version. |
| 16 |
# |
| 17 |
# ldap3 is distributed in the hope that it will be useful, |
| 18 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 |
# GNU Lesser General Public License for more details. |
| 21 |
# |
| 22 |
# You should have received a copy of the GNU Lesser General Public License |
| 23 |
# along with ldap3 in the COPYING and COPYING.LESSER files. |
| 24 |
# If not, see <http://www.gnu.org/licenses/>. |
| 25 |
|
| 26 |
import unittest |
| 27 |
|
| 28 |
from ldap3 import ANONYMOUS, SASL, NTLM, Server, Connection, EXTERNAL, DIGEST_MD5, MOCK_SYNC, MOCK_ASYNC |
| 29 |
from ldap3.core.exceptions import LDAPSocketOpenError |
| 30 |
from test.config import test_sasl_user, test_sasl_password, random_id, get_connection, drop_connection, test_sasl_realm, test_server_type, \ |
| 31 |
test_ntlm_user, test_ntlm_password, test_sasl_user_dn, test_strategy |
| 32 |
|
| 33 |
|
| 34 |
class Test(unittest.TestCase): |
| 35 |
def test_bind_cleartext(self): |
| 36 |
connection = get_connection(bind=False) |
| 37 |
connection.open() |
| 38 |
connection.bind() |
| 39 |
self.assertTrue(connection.bound) |
| 40 |
drop_connection(connection) |
| 41 |
self.assertFalse(connection.bound) |
| 42 |
|
| 43 |
def test_bind_ssl_cert_none(self): |
| 44 |
connection = get_connection(bind=False, use_ssl=True) |
| 45 |
connection.open() |
| 46 |
connection.bind() |
| 47 |
self.assertTrue(connection.bound) |
| 48 |
drop_connection(connection) |
| 49 |
self.assertFalse(connection.bound) |
| 50 |
|
| 51 |
def test_bind_anonymous(self): |
| 52 |
connection = get_connection(bind=True, lazy_connection=False, authentication=ANONYMOUS) |
| 53 |
self.assertTrue(connection.bound) |
| 54 |
drop_connection(connection) |
| 55 |
self.assertFalse(connection.bound) |
| 56 |
|
| 57 |
def test_bind_sasl_digest_md5(self): |
| 58 |
if test_server_type not in ['AD', 'SLAPD'] and test_strategy not in [MOCK_SYNC, MOCK_ASYNC]: |
| 59 |
connection = get_connection(bind=False, authentication=SASL, sasl_mechanism=DIGEST_MD5, sasl_credentials=(test_sasl_realm, test_sasl_user, test_sasl_password, None)) |
| 60 |
connection.open() |
| 61 |
connection.bind() |
| 62 |
self.assertTrue(connection.bound) |
| 63 |
if not connection.strategy.pooled: |
| 64 |
if test_server_type == 'EDIR': |
| 65 |
connected_user = connection.extend.novell.get_bind_dn() |
| 66 |
else: |
| 67 |
connected_user = str(connection.extend.standard.who_am_i()) |
| 68 |
self.assertEqual(connected_user, test_sasl_user_dn) |
| 69 |
drop_connection(connection) |
| 70 |
self.assertFalse(connection.bound) |
| 71 |
|
| 72 |
def test_ntlm(self): |
| 73 |
if test_server_type == 'AD': |
| 74 |
connection = get_connection(bind=False, authentication=NTLM, ntlm_credentials=(test_ntlm_user, test_ntlm_password)) |
| 75 |
connection.open() |
| 76 |
connection.bind() |
| 77 |
self.assertTrue(connection.bound) |
| 78 |
connected_user = str(connection.extend.standard.who_am_i())[2:] |
| 79 |
self.assertEqual(connected_user, test_ntlm_user) |
| 80 |
drop_connection(connection) |
| 81 |
self.assertFalse(connection.bound) |
| 82 |
|
| 83 |
def test_ldapi(self): |
| 84 |
if test_server_type == 'SLAPD': |
| 85 |
try: |
| 86 |
server = Server('ldapi:///var/run/slapd/ldapi') |
| 87 |
connection = Connection(server, authentication=SASL, sasl_mechanism=EXTERNAL, sasl_credentials=('',)) |
| 88 |
connection.open() |
| 89 |
connection.bind() |
| 90 |
self.assertTrue(connection.bound) |
| 91 |
except LDAPSocketOpenError: |
| 92 |
return |
| 93 |
|
| 94 |
self.assertTrue(False) |
| 95 |
|
| 96 |
def test_ldapi_encoded_url(self): |
| 97 |
if test_server_type == 'SLAPD': |
| 98 |
try: |
| 99 |
server = Server('ldapi://%2Fvar%2Frun%2Fslapd%2Fldapi') |
| 100 |
connection = Connection(server, authentication=SASL, sasl_mechanism=EXTERNAL, sasl_credentials=('',)) |
| 101 |
connection.open() |
| 102 |
connection.bind() |
| 103 |
self.assertTrue(connection.bound) |
| 104 |
except LDAPSocketOpenError: |
| 105 |
return |
| 106 |
|
| 107 |
self.assertTrue(False) |