From 3a7c273218d071304b2f77835cafe752180faf06 Mon Sep 17 00:00:00 2001 From: rs Date: Tue, 12 Feb 2019 07:37:50 +0000 Subject: [PATCH] Add support for dashes --- nipio/backend.py | 11 +++++++---- nipio_tests/backend_test.py | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/nipio/backend.py b/nipio/backend.py index 7219414..e20a906 100755 --- a/nipio/backend.py +++ b/nipio/backend.py @@ -141,7 +141,7 @@ def handle_self(self, name): def handle_subdomains(self, qname): subdomain = qname[0:qname.find(self.domain) - 1] - subparts = subdomain.split('.') + subparts = self._split_subdomain(subdomain) if len(subparts) < 4: if _is_debug(): _log('subparts less than 4') @@ -157,10 +157,10 @@ def handle_subdomains(self, qname): _log('%s is not a number' % part) self.handle_self(qname) return - parti = int(part) - if parti < 0 or parti > 255: + part_int = int(part) + if part_int < 0 or part_int > 255: if _is_debug(): - _log('%d is too big/small' % parti) + _log('%d is too big/small' % part_int) self.handle_self(qname) return @@ -197,6 +197,9 @@ def handle_blacklisted(self, ip_address): def _get_config_filename(self): return os.path.join(os.path.dirname(os.path.realpath(__file__)), 'backend.conf') + def _split_subdomain(self, subdomain): + return re.split("[.-]", subdomain) + if __name__ == '__main__': backend = DynamicBackend() diff --git a/nipio_tests/backend_test.py b/nipio_tests/backend_test.py index 0843faf..920f58f 100644 --- a/nipio_tests/backend_test.py +++ b/nipio_tests/backend_test.py @@ -76,6 +76,28 @@ def test_backend_responds_to_A_request_with_valid_ip(self): ["DATA", "subdomain.127.0.0.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"], ) + def test_backend_responds_to_ANY_request_with_valid_ip_separated_by_dashes(self): + self._send_commands(["Q", "subdomain-127-0-0-1.lcl.io", "IN", "ANY", "1", "127.0.0.1"]) + + self._run_backend() + + self._assert_expected_responses( + ["DATA", "subdomain-127-0-0-1.lcl.io", "IN", "A", "200", "22", "127.0.0.1"], + ["DATA", "subdomain-127-0-0-1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"], + ["DATA", "subdomain-127-0-0-1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"], + ) + + def test_backend_responds_to_A_request_with_valid_ip_separated_by_dashes(self): + self._send_commands(["Q", "subdomain-127-0-0-1.lcl.io", "IN", "A", "1", "127.0.0.1"]) + + self._run_backend() + + self._assert_expected_responses( + ["DATA", "subdomain-127-0-0-1.lcl.io", "IN", "A", "200", "22", "127.0.0.1"], + ["DATA", "subdomain-127-0-0-1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"], + ["DATA", "subdomain-127-0-0-1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"], + ) + def test_backend_responds_to_invalid_ip_in_ANY_request_with_self_ip(self): self._send_commands(["Q", "subdomain.127.0.1.lcl.io", "IN", "ANY", "1", "127.0.0.1"])