simplify network/boardcast validation
This commit is contained in:
parent
c56cdcfa02
commit
5e0bf84e50
4 changed files with 18 additions and 34 deletions
|
|
@ -21,6 +21,7 @@ def test_ip(config_type):
|
|||
cfg.option('a').value.set('192.168.1.0')
|
||||
cfg.option('a').value.set('88.88.88.88')
|
||||
cfg.option('a').value.set('0.0.0.0')
|
||||
cfg.option('a').value.set('2001:db8::1')
|
||||
if config_type != 'tiramisu-api':
|
||||
# FIXME
|
||||
with pytest.raises(ValueError):
|
||||
|
|
@ -148,8 +149,7 @@ def test_network_cidr(config_type):
|
|||
cfg.option('a').value.set('192.168.1.1')
|
||||
with pytest.raises(ValueError):
|
||||
cfg.option('a').value.set('192.168.1.1/24')
|
||||
with pytest.raises(ValueError):
|
||||
cfg.option('a').value.set('2001:db00::0/24')
|
||||
cfg.option('a').value.set('2001:db00::0/24')
|
||||
# assert not list_sessions()
|
||||
|
||||
|
||||
|
|
@ -197,8 +197,7 @@ def test_broadcast(config_type):
|
|||
cfg.option('a').value.set(1)
|
||||
with pytest.raises(ValueError):
|
||||
cfg.option('a').value.set(2)
|
||||
with pytest.raises(ValueError):
|
||||
cfg.option('a').value.set('2001:db8::1')
|
||||
cfg.option('a').value.set('2001:db8::1')
|
||||
cfg.option('a').value.set('0.0.0.0')
|
||||
cfg.option('a').value.set('255.255.255.0')
|
||||
# assert not list_sessions()
|
||||
|
|
|
|||
|
|
@ -23,27 +23,22 @@
|
|||
from ipaddress import ip_address
|
||||
|
||||
from ..i18n import _
|
||||
from .option import Option
|
||||
from .stroption import StrOption
|
||||
|
||||
|
||||
class BroadcastOption(Option):
|
||||
class BroadcastOption(StrOption):
|
||||
"""represents the choice of a broadcast"""
|
||||
|
||||
__slots__ = tuple()
|
||||
_type = "broadcast address"
|
||||
_t_type = _("broadcast address")
|
||||
|
||||
def validate(
|
||||
self,
|
||||
value: str,
|
||||
) -> None:
|
||||
"""validate"""
|
||||
if not isinstance(value, str):
|
||||
raise ValueError(_("invalid string"))
|
||||
if value.count(".") != 3:
|
||||
raise ValueError()
|
||||
for val in value.split("."):
|
||||
if val.startswith("0") and len(val) > 1:
|
||||
raise ValueError()
|
||||
super().validate(value)
|
||||
try:
|
||||
ip_address(value)
|
||||
except ValueError as err:
|
||||
|
|
|
|||
|
|
@ -31,6 +31,7 @@ class IPOption(StrOption):
|
|||
|
||||
__slots__ = tuple()
|
||||
_type = "IP"
|
||||
_t_type = _("IP")
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
|
|
@ -53,6 +54,9 @@ class IPOption(StrOption):
|
|||
ip_obj = ip_interface(value)
|
||||
except ValueError as err:
|
||||
raise ValueError() from err
|
||||
self._second_level_cidr(ip_obj)
|
||||
|
||||
def _second_level_cidr(self, ip_obj):
|
||||
if ip_obj.ip == ip_obj.network.network_address:
|
||||
raise ValueError(_("it's in fact a network address"))
|
||||
if ip_obj.ip == ip_obj.network.broadcast_address:
|
||||
|
|
|
|||
|
|
@ -23,36 +23,22 @@
|
|||
from ipaddress import ip_network
|
||||
|
||||
from ..i18n import _
|
||||
from .ipoption import IPOption
|
||||
from .stroption import StrOption
|
||||
|
||||
|
||||
class NetworkOption(StrOption):
|
||||
class NetworkOption(IPOption):
|
||||
"represents the choice of a network"
|
||||
__slots__ = tuple()
|
||||
_type = "network address"
|
||||
_t_type = _("network address")
|
||||
|
||||
def __init__(self, *args, cidr=False, **kwargs):
|
||||
extra = {"cidr": cidr}
|
||||
super().__init__(*args, extra=extra, **kwargs)
|
||||
super().__init__(*args, cidr=cidr, **kwargs)
|
||||
|
||||
def validate(self, value: str) -> None:
|
||||
super().validate(value)
|
||||
if value.count(".") != 3:
|
||||
raise ValueError()
|
||||
cidr = self.impl_get_extra("cidr")
|
||||
if cidr:
|
||||
if "/" not in value:
|
||||
raise ValueError(_("must use CIDR notation"))
|
||||
value_ = value.split("/")[0]
|
||||
else:
|
||||
value_ = value
|
||||
for val in value_.split("."):
|
||||
if val.startswith("0") and len(val) > 1:
|
||||
raise ValueError()
|
||||
try:
|
||||
ip_network(value)
|
||||
except ValueError as err:
|
||||
raise ValueError() from err
|
||||
def _second_level_cidr(self, ip_obj):
|
||||
if ip_obj.ip != ip_obj.network.network_address:
|
||||
raise ValueError(_("it's not a network address"))
|
||||
|
||||
def second_level_validation(self, value: str, warnings_only: bool) -> None:
|
||||
if ip_network(value).network_address.is_reserved:
|
||||
|
|
|
|||
Loading…
Reference in a new issue