diff --git a/tests/test_option_validator.py b/tests/test_option_validator.py index 5ab0ab0..33c4153 100644 --- a/tests/test_option_validator.py +++ b/tests/test_option_validator.py @@ -598,6 +598,30 @@ async def test_validator_ip_in_network(config_type): assert not await list_sessions() +@pytest.mark.asyncio +async def test_validator_ip_in_network_incomplete(config_type): + a = NetworkOption('a', '') + b = NetmaskOption('b', '') + c = IPOption('c', '', validators=[Calculation(valid_in_network, Params((ParamSelfOption(), ParamOption(a, todict=True), ParamOption(b, todict=True))))]) + d = IPOption('d', '', validators=[Calculation(valid_in_network, Params((ParamSelfOption(), ParamOption(a, todict=True), ParamOption(b, todict=True))), warnings_only=True)]) + od = OptionDescription('od', '', [a, b, c, d]) + warnings.simplefilter("always", ValueWarning) + async with await Config(od) as cfg: + cfg = await get_config(cfg, config_type) + # + await cfg.option('c').value.set('192.168.1.1') + # + await cfg.option('a').value.set('192.168.1.0') + await cfg.option('c').value.set('192.168.1.2') + await cfg.option('c').value.set('192.168.2.1') + # + await cfg.option('b').value.set('255.255.255.0') + await cfg.option('c').value.set('192.168.1.3') + with pytest.raises(ValueError): + await cfg.option('c').value.set('192.168.2.1') + assert not await list_sessions() + + @pytest.mark.asyncio async def test_validator_ip_in_network_cidr(config_type): a = NetworkOption('a', '', cidr=True) @@ -621,6 +645,25 @@ async def test_validator_ip_in_network_cidr(config_type): assert not await list_sessions() +@pytest.mark.asyncio +async def test_validator_ip_in_network_cidr_incomplete(config_type): + a = NetworkOption('a', '', cidr=True) + c = IPOption('c', '', validators=[Calculation(valid_in_network, Params((ParamSelfOption(), ParamOption(a, todict=True))))]) + d = IPOption('d', '', validators=[Calculation(valid_in_network, Params((ParamSelfOption(), ParamOption(a, todict=True))), warnings_only=True)]) + od = OptionDescription('od', '', [a, c, d]) + warnings.simplefilter("always", ValueWarning) + async with await Config(od) as cfg: + cfg = await get_config(cfg, config_type) + # + await cfg.option('c').value.set('192.168.1.1') + # + await cfg.option('a').value.set('192.168.1.0/24') + await cfg.option('c').value.set('192.168.1.2') + with pytest.raises(ValueError): + await cfg.option('c').value.set('192.168.2.1') + assert not await list_sessions() + + @pytest.mark.asyncio async def test_validator_ip_netmask_multi(config_type): a = IPOption('a', '', multi=True) diff --git a/tiramisu/function.py b/tiramisu/function.py index 23e0da1..f96ea5d 100644 --- a/tiramisu/function.py +++ b/tiramisu/function.py @@ -88,17 +88,21 @@ def valid_in_network(ip, else: network_value = network network_display_name = '' - if netmask is None: + if isinstance(netmask, dict): + netmask_value = netmask['value'] + netmask_display_name = ' ({})'.format(netmask['name']) + else: + netmask_value = netmask + netmask_display_name = '' + if network_value is None: + return + if '/' in network_value: network_obj = ip_network('{0}'.format(network_value)) else: - if isinstance(netmask, dict): - netmask_value = netmask['value'] - netmask_display_name = ' ({})'.format(netmask['name']) - else: - netmask_value = netmask - netmask_display_name = '' + if netmask_value is None: + return network_obj = ip_network('{0}/{1}'.format(network_value, - netmask_value)) + netmask_value)) if ip_interface(ip) not in network_obj: if netmask is None: msg = _('this IP is not in network {0}{1}').format(network_value,