| 1 |
""" |
| 2 |
""" |
| 3 |
|
| 4 |
# Created on 2014.09.15 |
| 5 |
# |
| 6 |
# Author: Giovanni Cannata |
| 7 |
# |
| 8 |
# Copyright 2014 - 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.utils.dn import parse_dn as p |
| 29 |
|
| 30 |
|
| 31 |
class Test(unittest.TestCase): |
| 32 |
def test_parse_dn_single(self): |
| 33 |
parsed = p('cn=admin') |
| 34 |
self.assertEqual(len(parsed), 1) |
| 35 |
self.assertEqual(parsed[0], ('cn', 'admin', '')) |
| 36 |
|
| 37 |
def test_parse_dn_single_multi_rdn(self): |
| 38 |
parsed = p('[email protected]') |
| 39 |
self.assertEqual(len(parsed), 2) |
| 40 |
self.assertEqual(parsed[0], ('cn', 'admin', '+')) |
| 41 |
self.assertEqual(parsed[1], ('email', '[email protected]', '')) |
| 42 |
|
| 43 |
def test_parse_dn_escaped_single_multi_rdn(self): |
| 44 |
parsed = p('cn=\\\\\\[email protected]') |
| 45 |
self.assertEqual(len(parsed), 2) |
| 46 |
self.assertEqual(parsed[0], ('cn', '\\\\\\+admin', '+')) |
| 47 |
self.assertEqual(parsed[1], ('email', '[email protected]', '')) |
| 48 |
|
| 49 |
def test_parse_dn_double(self): |
| 50 |
parsed = p('cn=user1,o=users') |
| 51 |
self.assertEqual(len(parsed), 2) |
| 52 |
self.assertEqual(parsed[0], ('cn', 'user1', ',')) |
| 53 |
self.assertEqual(parsed[1], ('o', 'users', '')) |
| 54 |
|
| 55 |
def test_parse_dn_multi(self): |
| 56 |
parsed = p('cn=user1,ou=users,dc=branch,dc=company,c=IT') |
| 57 |
self.assertEqual(len(parsed), 5) |
| 58 |
self.assertEqual(parsed[0], ('cn', 'user1', ',')) |
| 59 |
self.assertEqual(parsed[1], ('ou', 'users', ',')) |
| 60 |
self.assertEqual(parsed[2], ('dc', 'branch', ',')) |
| 61 |
self.assertEqual(parsed[3], ('dc', 'company', ',')) |
| 62 |
self.assertEqual(parsed[4], ('c', 'IT', '')) |
| 63 |
|
| 64 |
def test_parse_dn_multi_type(self): |
| 65 |
parsed = p('cn=user1+sn=surname1,o=users') |
| 66 |
self.assertEqual(len(parsed), 3) |
| 67 |
self.assertEqual(parsed[0], ('cn', 'user1', '+')) |
| 68 |
self.assertEqual(parsed[1], ('sn', 'surname1', ',')) |
| 69 |
self.assertEqual(parsed[2], ('o', 'users', '')) |
| 70 |
|
| 71 |
def test_parse_dn_escaped_single(self): |
| 72 |
parsed = p('cn=admi\\,n') |
| 73 |
self.assertEqual(len(parsed), 1) |
| 74 |
self.assertEqual(parsed[0], ('cn', 'admi\\,n', '')) |
| 75 |
|
| 76 |
def test_parse_dn_escaped_double(self): |
| 77 |
parsed = p('cn=us\\=er1,o=us\\,ers') |
| 78 |
self.assertEqual(len(parsed), 2) |
| 79 |
self.assertEqual(parsed[0], ('cn', 'us\\=er1', ',')) |
| 80 |
self.assertEqual(parsed[1], ('o', 'us\\,ers', '')) |
| 81 |
|
| 82 |
def test_parse_dn_escaped_double_1(self): |
| 83 |
parsed = p('cn=\\\\,o=\\\\') |
| 84 |
self.assertEqual(len(parsed), 2) |
| 85 |
self.assertEqual(parsed[0], ('cn', '\\\\', ',')) |
| 86 |
self.assertEqual(parsed[1], ('o', '\\\\', '')) |
| 87 |
|
| 88 |
def test_parse_dn_escaped_multi(self): |
| 89 |
parsed = p('cn=us\\,er1,ou=us\\08ers,dc=br\\,anch,dc=company,c=IT') |
| 90 |
self.assertEqual(len(parsed), 5) |
| 91 |
self.assertEqual(parsed[0], ('cn', 'us\\,er1', ',')) |
| 92 |
self.assertEqual(parsed[1], ('ou', 'us\\08ers', ',')) |
| 93 |
self.assertEqual(parsed[2], ('dc', 'br\\,anch', ',')) |
| 94 |
self.assertEqual(parsed[3], ('dc', 'company', ',')) |
| 95 |
self.assertEqual(parsed[4], ('c', 'IT', '')) |
| 96 |
|
| 97 |
def test_parse_dn_escaped_multi_type(self): |
| 98 |
parsed = p('cn=us\\+er1+sn=su\\,rname1,o=users') |
| 99 |
self.assertEqual(len(parsed), 3) |
| 100 |
self.assertEqual(parsed[0], ('cn', 'us\\+er1', '+')) |
| 101 |
self.assertEqual(parsed[1], ('sn', 'su\\,rname1', ',')) |
| 102 |
self.assertEqual(parsed[2], ('o', 'users', '')) |
| 103 |
|
| 104 |
def test_parse_dn_unescaped_single(self): |
| 105 |
parsed = p('cn=admi,n', escape=True) |
| 106 |
self.assertEqual(len(parsed), 1) |
| 107 |
self.assertEqual(parsed[0], ('cn', 'admi\\,n', '')) |
| 108 |
|
| 109 |
def test_parse_dn_unescaped_double(self): |
| 110 |
parsed = p('cn=us=er1,o=us,ers', escape=True) |
| 111 |
self.assertEqual(len(parsed), 2) |
| 112 |
self.assertEqual(parsed[0], ('cn', 'us\\=er1', ',')) |
| 113 |
self.assertEqual(parsed[1], ('o', 'us\\,ers', '')) |
| 114 |
|
| 115 |
def test_parse_dn_unescaped_multi(self): |
| 116 |
parsed = p('cn=us,er1,ou=use<rs,dc=br+anch,dc=company,c=IT', escape=True) |
| 117 |
self.assertEqual(len(parsed), 5) |
| 118 |
self.assertEqual(parsed[0], ('cn', 'us\\,er1', ',')) |
| 119 |
self.assertEqual(parsed[1], ('ou', 'use\\<rs', ',')) |
| 120 |
self.assertEqual(parsed[2], ('dc', 'br\\+anch', ',')) |
| 121 |
self.assertEqual(parsed[3], ('dc', 'company', ',')) |
| 122 |
self.assertEqual(parsed[4], ('c', 'IT', '')) |
| 123 |
|
| 124 |
def test_parse_dn_unescaped_multi_type(self): |
| 125 |
parsed = p('cn=us+er1+sn=su,rname1,o=users', escape=True) |
| 126 |
self.assertEqual(len(parsed), 3) |
| 127 |
self.assertEqual(parsed[0], ('cn', 'us\\+er1', '+')) |
| 128 |
self.assertEqual(parsed[1], ('sn', 'su\\,rname1', ',')) |
| 129 |
self.assertEqual(parsed[2], ('o', 'users', '')) |
| 130 |
|
| 131 |
def pair_generator(self): |
| 132 |
# escaped = DQUOTE / PLUS / COMMA / SEMI / LANGLE / RANGLE |
| 133 |
escaped = ['"', '+', ',', ';', '<', '>'] |
| 134 |
# special = escaped / SPACE / SHARP / EQUALS |
| 135 |
special = escaped + [' ', '#', '='] |
| 136 |
ESC = ['\\'] |
| 137 |
# hexpair = HEX HEX |
| 138 |
hexpairs = ['00', '99', 'aa', 'AA', 'ff', 'FF', 'aF', 'Af'] |
| 139 |
# pair = ESC ( ESC / special / hexpair ) |
| 140 |
pair = self.combine_strings(ESC, ESC + special + hexpairs) |
| 141 |
return pair |
| 142 |
|
| 143 |
def test_parse_dn_pair(self): |
| 144 |
if hasattr(self, 'subTest'): # python3 only |
| 145 |
for onepair in self.pair_generator(): |
| 146 |
for attributeValue, mode in [ |
| 147 |
(onepair, "alone"), |
| 148 |
("a" + onepair + "b", "between"), |
| 149 |
("a" + onepair, "after"), |
| 150 |
(onepair + "b", "before") |
| 151 |
]: |
| 152 |
for separator in [None, '+', ',']: |
| 153 |
|
| 154 |
if not separator: |
| 155 |
dn = r'cn={0}'.format(attributeValue) |
| 156 |
expected = [('cn', attributeValue, '')] |
| 157 |
else: |
| 158 |
dn = r'cn={0}{1}ou={0}'.format(attributeValue, separator) |
| 159 |
expected = [('cn', attributeValue, separator), ('ou', attributeValue, '')] |
| 160 |
|
| 161 |
with self.subTest(pair=onepair, separator=separator, mode=mode, input=dn): |
| 162 |
self._test_parse_dn( |
| 163 |
dn, |
| 164 |
expected |
| 165 |
) |
| 166 |
|
| 167 |
|
| 168 |
def combine_strings(self, *args): |
| 169 |
if len(args) == 0: raise Exception("Invalid parameter") |
| 170 |
if len(args) == 1: |
| 171 |
for variant in args[0]: |
| 172 |
yield variant |
| 173 |
else: |
| 174 |
for head in args[0]: |
| 175 |
for tail in self.combine_strings(*args[1:]): |
| 176 |
variant = head + tail |
| 177 |
yield variant |
| 178 |
|
| 179 |
def test_combine_strings(self): |
| 180 |
variants = set(self.combine_strings(['a', 'b'], ['x', 'y'])) |
| 181 |
self.assertEqual(variants, {'ax', 'ay', 'bx', 'by'}) |
| 182 |
|
| 183 |
def test_combine_strings_empty1(self): |
| 184 |
variants = set(self.combine_strings([], ['x', 'y'])) |
| 185 |
self.assertEqual(len(variants), 0) |
| 186 |
|
| 187 |
def test_combine_strings_empty2(self): |
| 188 |
variants = set(self.combine_strings(['a', 'b'], [])) |
| 189 |
self.assertEqual(len(variants), 0) |
| 190 |
|
| 191 |
def _test_parse_dn(self, input, expected): |
| 192 |
parsed = p(input, escape=False) |
| 193 |
self.assertEqual(parsed, expected) |
| 194 |
|
| 195 |
def test_unit_test_deep_equality(self): |
| 196 |
self.assertEqual([], []) |
| 197 |
self.assertNotEqual([], [()]) |
| 198 |
self.assertEqual([()], [()]) |
| 199 |
self.assertNotEqual([()], [(), ()]) |
| 200 |
self.assertNotEqual([()], [('b')]) |
| 201 |
self.assertEqual([('a')], [('a')]) |
| 202 |
self.assertNotEqual([('a')], [('b')]) |
| 203 |
self.assertEqual([('a', 'b', 'x'), ('a', 'b', 'x')], [('a', 'b', 'x'), ('a', 'b', 'x')]) |
| 204 |
self.assertNotEqual([('a', 'b', 'x'), ('a', 'b', 'x')], [('a', 'b', 'x'), ('a', 'b', 'y')]) |
| 205 |
self.assertNotEqual([('1')], [(1)]) |