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('192.168.1.0')
|
||||||
cfg.option('a').value.set('88.88.88.88')
|
cfg.option('a').value.set('88.88.88.88')
|
||||||
cfg.option('a').value.set('0.0.0.0')
|
cfg.option('a').value.set('0.0.0.0')
|
||||||
|
cfg.option('a').value.set('2001:db8::1')
|
||||||
if config_type != 'tiramisu-api':
|
if config_type != 'tiramisu-api':
|
||||||
# FIXME
|
# FIXME
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
|
|
@ -148,8 +149,7 @@ def test_network_cidr(config_type):
|
||||||
cfg.option('a').value.set('192.168.1.1')
|
cfg.option('a').value.set('192.168.1.1')
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
cfg.option('a').value.set('192.168.1.1/24')
|
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()
|
# assert not list_sessions()
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -197,8 +197,7 @@ def test_broadcast(config_type):
|
||||||
cfg.option('a').value.set(1)
|
cfg.option('a').value.set(1)
|
||||||
with pytest.raises(ValueError):
|
with pytest.raises(ValueError):
|
||||||
cfg.option('a').value.set(2)
|
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('0.0.0.0')
|
||||||
cfg.option('a').value.set('255.255.255.0')
|
cfg.option('a').value.set('255.255.255.0')
|
||||||
# assert not list_sessions()
|
# assert not list_sessions()
|
||||||
|
|
|
||||||
|
|
@ -23,27 +23,22 @@
|
||||||
from ipaddress import ip_address
|
from ipaddress import ip_address
|
||||||
|
|
||||||
from ..i18n import _
|
from ..i18n import _
|
||||||
from .option import Option
|
from .stroption import StrOption
|
||||||
|
|
||||||
|
|
||||||
class BroadcastOption(Option):
|
class BroadcastOption(StrOption):
|
||||||
"""represents the choice of a broadcast"""
|
"""represents the choice of a broadcast"""
|
||||||
|
|
||||||
__slots__ = tuple()
|
__slots__ = tuple()
|
||||||
_type = "broadcast address"
|
_type = "broadcast address"
|
||||||
|
_t_type = _("broadcast address")
|
||||||
|
|
||||||
def validate(
|
def validate(
|
||||||
self,
|
self,
|
||||||
value: str,
|
value: str,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""validate"""
|
"""validate"""
|
||||||
if not isinstance(value, str):
|
super().validate(value)
|
||||||
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()
|
|
||||||
try:
|
try:
|
||||||
ip_address(value)
|
ip_address(value)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,7 @@ class IPOption(StrOption):
|
||||||
|
|
||||||
__slots__ = tuple()
|
__slots__ = tuple()
|
||||||
_type = "IP"
|
_type = "IP"
|
||||||
|
_t_type = _("IP")
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
|
@ -53,6 +54,9 @@ class IPOption(StrOption):
|
||||||
ip_obj = ip_interface(value)
|
ip_obj = ip_interface(value)
|
||||||
except ValueError as err:
|
except ValueError as err:
|
||||||
raise ValueError() from 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:
|
if ip_obj.ip == ip_obj.network.network_address:
|
||||||
raise ValueError(_("it's in fact a network address"))
|
raise ValueError(_("it's in fact a network address"))
|
||||||
if ip_obj.ip == ip_obj.network.broadcast_address:
|
if ip_obj.ip == ip_obj.network.broadcast_address:
|
||||||
|
|
|
||||||
|
|
@ -23,36 +23,22 @@
|
||||||
from ipaddress import ip_network
|
from ipaddress import ip_network
|
||||||
|
|
||||||
from ..i18n import _
|
from ..i18n import _
|
||||||
|
from .ipoption import IPOption
|
||||||
from .stroption import StrOption
|
from .stroption import StrOption
|
||||||
|
|
||||||
|
|
||||||
class NetworkOption(StrOption):
|
class NetworkOption(IPOption):
|
||||||
"represents the choice of a network"
|
"represents the choice of a network"
|
||||||
__slots__ = tuple()
|
__slots__ = tuple()
|
||||||
_type = "network address"
|
_type = "network address"
|
||||||
|
_t_type = _("network address")
|
||||||
|
|
||||||
def __init__(self, *args, cidr=False, **kwargs):
|
def __init__(self, *args, cidr=False, **kwargs):
|
||||||
extra = {"cidr": cidr}
|
super().__init__(*args, cidr=cidr, **kwargs)
|
||||||
super().__init__(*args, extra=extra, **kwargs)
|
|
||||||
|
|
||||||
def validate(self, value: str) -> None:
|
def _second_level_cidr(self, ip_obj):
|
||||||
super().validate(value)
|
if ip_obj.ip != ip_obj.network.network_address:
|
||||||
if value.count(".") != 3:
|
raise ValueError(_("it's not a network address"))
|
||||||
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_validation(self, value: str, warnings_only: bool) -> None:
|
def second_level_validation(self, value: str, warnings_only: bool) -> None:
|
||||||
if ip_network(value).network_address.is_reserved:
|
if ip_network(value).network_address.is_reserved:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue