Debian Codegraph @

test/testRdns.py

raw
1 """ Unit tests for utilities around reverse dns lookup """
2
3 # Created on 2020.09.16
4 #
5 # Author: Azaria Zornberg
6 #
7 # Copyright 2020 Giovanni Cannata
8 # Copyright 2020 Azaria Zornberg
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 from socket import gaierror
28
29 from ldap3.core.rdns import ReverseDnsSetting, get_hostname_by_addr, is_ip_addr
30
31
32 class Test(unittest.TestCase):
33
34 def test_no_broken_backwards_compat_in_enum(self):
35 fail_msg = ('Changing the mapping of reverse dns enum types to numeric values will break backwards '
36 'compatibility for existing clients on older versions of the ldap3 package. If new enum types '
37 'are added or functionally changed, please use new values')
38 self.assertEqual(ReverseDnsSetting.OFF, (0,), fail_msg)
39 self.assertEqual(ReverseDnsSetting.REQUIRE_RESOLVE_ALL_ADDRESSES, (1,), fail_msg)
40 self.assertEqual(ReverseDnsSetting.REQUIRE_RESOLVE_IP_ADDRESSES_ONLY, (2,), fail_msg)
41 self.assertEqual(ReverseDnsSetting.OPTIONAL_RESOLVE_ALL_ADDRESSES, (3,), fail_msg)
42 self.assertEqual(ReverseDnsSetting.OPTIONAL_RESOLVE_IP_ADDRESSES_ONLY, (4,), fail_msg)
43
44 fail_msg = ('Removing values from the set of supported values for reverse dns settings will break '
45 'backwards compatibility for existing clients on older versions of the ldap3 package. '
46 'Please do not remove values.')
47 self.assertTrue(ReverseDnsSetting.OFF in ReverseDnsSetting.SUPPORTED_VALUES, fail_msg)
48 self.assertTrue(ReverseDnsSetting.REQUIRE_RESOLVE_ALL_ADDRESSES in ReverseDnsSetting.SUPPORTED_VALUES, fail_msg)
49 self.assertTrue(ReverseDnsSetting.REQUIRE_RESOLVE_IP_ADDRESSES_ONLY in ReverseDnsSetting.SUPPORTED_VALUES, fail_msg)
50 self.assertTrue(ReverseDnsSetting.OPTIONAL_RESOLVE_ALL_ADDRESSES in ReverseDnsSetting.SUPPORTED_VALUES, fail_msg)
51 self.assertTrue(ReverseDnsSetting.OPTIONAL_RESOLVE_IP_ADDRESSES_ONLY in ReverseDnsSetting.SUPPORTED_VALUES, fail_msg)
52
53 def test_ipv4_ip_addr_checking(self):
54 valid = is_ip_addr('10.254.76.5')
55 self.assertTrue(valid, 'IPv4 addresses should be identified as ip addresses')
56
57 def test_ipv6_ip_addr_checking(self):
58 full_addr = '2001:0db8:0000:0000:0000:ff00:0042:8329'
59 zeros_reduced_addr = '2001:db8:0:0:0:ff00:42:8329' # equivalent to above
60 consecutive_zeros_removed_addr = '2001:db8::ff00:42:8329' # equivalent to above
61
62 for valid_ipv6_addr in [full_addr, zeros_reduced_addr, consecutive_zeros_removed_addr]:
63 valid = is_ip_addr(valid_ipv6_addr)
64 self.assertTrue(valid, 'IPv6 addresses should be identified as ip addresses regardless of format. {}'.format(valid_ipv6_addr))
65
66 def test_hostname_ip_addr_checking(self):
67 valid = is_ip_addr('ldap.example.com')
68 self.assertFalse(valid, 'Hostnames should not be identified as ip addresses')
69
70 def test_success_required_option_for_lookup(self):
71 addr_that_fails_to_resolve = '10.254.76.09212' # this address isn't valid so it'll fail to resolve
72 # try:
73 with self.assertRaises(gaierror, msg='An exception should be raised when invoking get_hostname_by_addr with default options for an address that cannot resolve'):
74 host = get_hostname_by_addr(addr_that_fails_to_resolve)
75
76 with self.assertRaises(gaierror, msg='An exception should be raised when invoking get_hostname_by_addr with success_required set to True for an address that cannot resolve.'):
77 host = get_hostname_by_addr(addr_that_fails_to_resolve, success_required=True)
78
79 host = get_hostname_by_addr(addr_that_fails_to_resolve, success_required=False)
80 self.assertIsNone(host, 'An null host should be returned when invoking get_hostname_by_addr with success_required set to False for an address that cannot resolve.')