Skip to content

Commit

Permalink
Add support for dashes
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Feb 12, 2019
1 parent 0041cce commit 3a7c273
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
11 changes: 7 additions & 4 deletions nipio/backend.py
Expand Up @@ -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')
Expand All @@ -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

Expand Down Expand Up @@ -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()
Expand Down
22 changes: 22 additions & 0 deletions nipio_tests/backend_test.py
Expand Up @@ -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"])

Expand Down

0 comments on commit 3a7c273

Please sign in to comment.