Debian Codegraph @

test/testParseSearchFilter.py

raw
1 """
2 """
3
4 # Created on 2013.06.04
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.operation.search import parse_filter, MATCH_EQUAL, MATCH_EXTENSIBLE
29 from ldap3.utils.conv import escape_filter_chars
30 from ldap3.protocol.schemas.edir914 import edir_9_1_4_schema, edir_9_1_4_dsa_info
31 from ldap3.protocol.rfc4512 import SchemaInfo, DsaInfo
32 from ldap3.core.exceptions import LDAPAttributeError, LDAPObjectClassError
33 from test.config import test_auto_escape, test_auto_encode, test_validator, test_check_names
34
35
36 class Test(unittest.TestCase):
37 def test_parse_search_filter_equality(self):
38 f = parse_filter('(cn=admin)', None, test_auto_escape, test_auto_encode, test_validator, test_check_names)
39 self.assertEqual(f.elements[0].tag, MATCH_EQUAL)
40 self.assertEqual(f.elements[0].assertion['attr'], 'cn')
41 self.assertEqual(f.elements[0].assertion['value'], b'admin')
42
43 def test_parse_search_filter_equality_2(self):
44 f = parse_filter('(cn=a<=b=>c)', None, test_auto_escape, test_auto_encode, test_validator, test_check_names)
45 self.assertEqual(f.elements[0].tag, MATCH_EQUAL)
46 self.assertEqual(f.elements[0].assertion['attr'], 'cn')
47 self.assertEqual(f.elements[0].assertion['value'], b'a<=b=>c')
48
49 def test_parse_search_filter_extensible_syntax_1(self):
50 f = parse_filter('(cn:caseExactMatch:=Fred Flintstone)', None, test_auto_escape, test_auto_encode, test_validator, test_check_names)
51 self.assertEqual(f.elements[0].tag, MATCH_EXTENSIBLE)
52 self.assertEqual(f.elements[0].assertion['attr'], 'cn')
53 self.assertEqual(f.elements[0].assertion['value'], b'Fred Flintstone')
54 self.assertEqual(f.elements[0].assertion['matchingRule'], 'caseExactMatch')
55 self.assertEqual(f.elements[0].assertion['dnAttributes'], False)
56
57 def test_parse_search_filter_extensible_syntax_2(self):
58 f = parse_filter('(cn:=Betty Rubble)', None, test_auto_escape, test_auto_encode, test_validator, test_check_names)
59 self.assertEqual(f.elements[0].tag, MATCH_EXTENSIBLE)
60 self.assertEqual(f.elements[0].assertion['attr'], 'cn')
61 self.assertEqual(f.elements[0].assertion['value'], b'Betty Rubble')
62 self.assertEqual(f.elements[0].assertion['matchingRule'], False)
63 self.assertEqual(f.elements[0].assertion['dnAttributes'], False)
64
65 def test_parse_search_filter_extensible_syntax_3(self):
66 f = parse_filter('(sn:dn:2.4.6.8.10:=Barney Rubble)', None, test_auto_escape, test_auto_encode, test_validator, test_check_names)
67 self.assertEqual(f.elements[0].tag, MATCH_EXTENSIBLE)
68 self.assertEqual(f.elements[0].assertion['attr'], 'sn')
69 self.assertEqual(f.elements[0].assertion['value'], b'Barney Rubble')
70 self.assertEqual(f.elements[0].assertion['matchingRule'], '2.4.6.8.10')
71 self.assertEqual(f.elements[0].assertion['dnAttributes'], True)
72
73 def test_parse_search_filter_extensible_syntax_4(self):
74 f = parse_filter('(o:dn:=Ace Industry)', None, test_auto_escape, test_auto_encode, test_validator, test_check_names)
75 self.assertEqual(f.elements[0].tag, MATCH_EXTENSIBLE)
76 self.assertEqual(f.elements[0].assertion['attr'], 'o')
77 self.assertEqual(f.elements[0].assertion['value'], b'Ace Industry')
78 self.assertEqual(f.elements[0].assertion['matchingRule'], False)
79 self.assertEqual(f.elements[0].assertion['dnAttributes'], True)
80
81 def test_parse_search_filter_extensible_syntax_5(self):
82 f = parse_filter('(:1.2.3:=Wilma Flintstone)', None, test_auto_escape, test_auto_encode, test_validator, test_check_names)
83 self.assertEqual(f.elements[0].tag, MATCH_EXTENSIBLE)
84 self.assertEqual(f.elements[0].assertion['attr'], False)
85 self.assertEqual(f.elements[0].assertion['value'], b'Wilma Flintstone')
86 self.assertEqual(f.elements[0].assertion['matchingRule'], '1.2.3')
87 self.assertEqual(f.elements[0].assertion['dnAttributes'], False)
88
89 def test_parse_search_filter_extensible_syntax_6(self):
90 f = parse_filter('(:DN:2.4.6.8.10:=Dino)', None, test_auto_escape, test_auto_encode, test_validator, test_check_names)
91 self.assertEqual(f.elements[0].tag, MATCH_EXTENSIBLE)
92 self.assertEqual(f.elements[0].assertion['attr'], False)
93 self.assertEqual(f.elements[0].assertion['value'], b'Dino')
94 self.assertEqual(f.elements[0].assertion['matchingRule'], '2.4.6.8.''10')
95 self.assertEqual(f.elements[0].assertion['dnAttributes'], True)
96
97 def test_parse_search_filter_parenteses(self):
98 f = parse_filter('(cn=' + escape_filter_chars('Doe (Missing Inc)') + ')', None, test_auto_escape, test_auto_encode, test_validator, test_check_names)
99 self.assertEqual(f.elements[0].tag, MATCH_EQUAL)
100 self.assertEqual(f.elements[0].assertion['attr'], 'cn')
101 self.assertEqual(f.elements[0].assertion['value'], b'Doe \\28Missing Inc\\29')
102
103 def test_parse_search_filter_bad_attribute_type_check_true(self):
104 self.assertRaises(LDAPAttributeError, parse_filter, '(bad=admin)', SchemaInfo.from_json(edir_9_1_4_schema), test_auto_escape, test_auto_encode, test_validator, check_names=True)
105
106 def test_parse_search_filter_bad_attribute_type_check_false(self):
107 f = parse_filter('(bad=admin)', SchemaInfo.from_json(edir_9_1_4_schema), test_auto_escape, test_auto_encode, test_validator, check_names=False)
108 self.assertEqual(f.elements[0].tag, MATCH_EQUAL)
109 self.assertEqual(f.elements[0].assertion['attr'], 'bad')
110 self.assertEqual(f.elements[0].assertion['value'], b'admin')
111
112 def test_parse_search_filter_bad_object_class_type_check_true(self):
113 self.assertRaises(LDAPObjectClassError, parse_filter, '(objectClass=bad)', SchemaInfo.from_json(edir_9_1_4_schema), test_auto_escape, test_auto_encode, test_validator, check_names=True)
114
115 def test_parse_search_filter_bad_object_class_type_check_false(self):
116 f = parse_filter('(objectClass=bad)', SchemaInfo.from_json(edir_9_1_4_schema), test_auto_escape, test_auto_encode, test_validator, check_names=False)
117 self.assertEqual(f.elements[0].tag, MATCH_EQUAL)
118 self.assertEqual(f.elements[0].assertion['attr'], 'objectClass')
119 self.assertEqual(f.elements[0].assertion['value'], b'bad')