ip_in_network without all information

This commit is contained in:
Emmanuel Garette 2020-02-18 22:08:52 +01:00
parent 3bef45c9db
commit 91c44b38bc
2 changed files with 55 additions and 8 deletions

View File

@ -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)

View File

@ -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,