From 6349bd430ef84e01b87acf3642bd77500edb24cf Mon Sep 17 00:00:00 2001 From: TheTechromancer Date: Wed, 1 May 2024 16:48:52 -0400 Subject: [PATCH] stricter type handling --- pyproject.toml | 4 ++-- radixtarget/radixtarget.py | 9 +++++++-- radixtarget/test/test_pyradix.py | 7 +++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6d4d8d7..6ef5c28 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "radixtarget" -version = "0.0.9" +version = "1.1.0" description = "Check whether an IP address belongs to a cloud provider" authors = ["TheTechromancer"] license = "GPL-3.0" @@ -23,7 +23,7 @@ pytest-cov = "^5.0.0" [tool.poetry-dynamic-versioning] enable = true metadata = false -format-jinja = 'v1.0.0.{{ distance }}' +format-jinja = 'v1.1.0.{{ distance }}' [build-system] requires = ["poetry-core>=1.0.0", "poetry-dynamic-versioning>=1.0.0,<2.0.0"] diff --git a/radixtarget/radixtarget.py b/radixtarget/radixtarget.py index febc6a5..26b621d 100644 --- a/radixtarget/radixtarget.py +++ b/radixtarget/radixtarget.py @@ -24,10 +24,15 @@ def search(self, host): return self.dns_tree.search(host) def make_ip(self, host): + if not isinstance(host, str): + if not self.is_ip(host): + raise ValueError( + f'Host "{host}" must be of str or ipaddress type, not "{type(host)}"' + ) try: - return ipaddress.ip_network(host) + return ipaddress.ip_network(host, strict=False) except Exception: - return str(host).lower() + return host.lower() def is_ip(self, host): return ipaddress._IPAddressBase in host.__class__.__mro__ diff --git a/radixtarget/test/test_pyradix.py b/radixtarget/test/test_pyradix.py index 03df68c..a67ae7c 100644 --- a/radixtarget/test/test_pyradix.py +++ b/radixtarget/test/test_pyradix.py @@ -1,4 +1,5 @@ import time +import pytest import random import logging import ipaddress @@ -66,6 +67,12 @@ def test_radixtarget(): rt.insert("evilcorp.co.uk", "custom_data") assert rt.search("www.evilcorp.co.uk") == "custom_data" + with pytest.raises(ValueError, match=".*must be of str or ipaddress type.*"): + rt.insert(b"asdf") + + with pytest.raises(ValueError, match=".*must be of str or ipaddress type.*"): + rt.search(b"asdf") + assert "net" in rt.dns_tree.root.children assert "com" in rt.dns_tree.root.children