tiramisu/tests/test_config_ip.py

401 lines
15 KiB
Python
Raw Normal View History

2017-07-09 09:49:03 +02:00
from .autopath import do_autopath
2015-07-24 17:54:10 +02:00
do_autopath()
2019-06-21 23:04:04 +02:00
from .config import config_type, get_config, value_list, global_owner
2015-07-24 17:54:10 +02:00
import warnings
2019-12-24 15:24:20 +01:00
import pytest
2019-02-24 15:11:08 +01:00
from tiramisu import Config, IPOption, NetworkOption, NetmaskOption, \
PortOption, BroadcastOption, OptionDescription
from tiramisu.error import ValueWarning
2018-10-31 08:00:19 +01:00
from tiramisu.storage import list_sessions
def teardown_function(function):
assert list_sessions() == [], 'session list is not empty when leaving "{}"'.format(function.__name__)
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_ip(config_type):
a = IPOption('a', '')
2013-09-27 11:28:23 +02:00
b = IPOption('b', '', private_only=True)
d = IPOption('d', '', warnings_only=True, private_only=True)
warnings.simplefilter("always", ValueWarning)
od = OptionDescription('od', '', [a, b, d])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
cfg = await get_config(cfg, config_type)
await cfg.option('a').value.set('192.168.1.1')
await cfg.option('a').value.set('192.168.1.0')
await cfg.option('a').value.set('88.88.88.88')
await cfg.option('a').value.set('0.0.0.0')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('a').value.set('255.255.255.0')
await cfg.option('b').value.set('192.168.1.1')
await cfg.option('b').value.set('192.168.1.0')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('b').value.set('88.88.88.88')
await cfg.option('b').value.set('0.0.0.0')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('b').value.set('255.255.255.0')
with pytest.raises(ValueError):
await cfg.option('a').value.set('333.0.1.20')
2013-07-11 23:05:33 +02:00
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
IPOption('a', 'ip', default='192.000.023.01')
2019-06-21 23:04:04 +02:00
with warnings.catch_warnings(record=True) as w:
2019-12-24 15:24:20 +01:00
await cfg.option('d').value.set('88.88.88.88')
2019-06-21 23:04:04 +02:00
assert len(w) == 1
2013-07-11 23:05:33 +02:00
2015-07-24 17:54:10 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_ip_cidr():
2019-02-24 15:11:08 +01:00
b = IPOption('b', '', private_only=True, cidr=True)
c = IPOption('c', '', private_only=True)
warnings.simplefilter("always", ValueWarning)
od = OptionDescription('od', '', [b, c])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
with pytest.raises(ValueError):
await cfg.option('b').value.set('192.168.1.1')
await cfg.option('b').value.set('192.168.1.1/24')
with pytest.raises(ValueError):
await cfg.option('b').value.set('192.168.1.1/32')
2019-02-24 15:11:08 +01:00
#
2019-12-24 15:24:20 +01:00
await cfg.option('c').value.set('192.168.1.1')
with pytest.raises(ValueError):
await cfg.option('c').value.set('192.168.1.1/24')
with pytest.raises(ValueError):
await cfg.option('c').value.set('192.168.1.1/32')
2019-02-24 15:11:08 +01:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_ip_default():
2013-07-11 23:05:33 +02:00
a = IPOption('a', '', '88.88.88.88')
od = OptionDescription('od', '', [a])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
await cfg.option('a').value.get() == '88.88.88.88'
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_ip_reserved(config_type):
2013-09-19 21:51:55 +02:00
a = IPOption('a', '')
b = IPOption('b', '', allow_reserved=True)
c = IPOption('c', '', warnings_only=True)
od = OptionDescription('od', '', [a, b, c])
warnings.simplefilter("always", ValueWarning)
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
cfg = await get_config(cfg, config_type)
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('a').value.set('240.94.1.1')
await cfg.option('b').value.set('240.94.1.1')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
with warnings.catch_warnings(record=True) as w:
2019-12-24 15:24:20 +01:00
await cfg.option('c').value.set('240.94.1.1')
2019-06-21 23:04:04 +02:00
assert len(w) == 1
2013-09-19 21:51:55 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_network(config_type):
a = NetworkOption('a', '')
b = NetworkOption('b', '', warnings_only=True)
od = OptionDescription('od', '', [a, b])
warnings.simplefilter("always", ValueWarning)
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
cfg = await get_config(cfg, config_type)
await cfg.option('a').value.set('192.168.1.1')
await cfg.option('a').value.set('192.168.1.0')
await cfg.option('a').value.set('88.88.88.88')
await cfg.option('a').value.set('0.0.0.0')
with pytest.raises(ValueError):
await cfg.option('a').value.set(1)
with pytest.raises(ValueError):
await cfg.option('a').value.set('1.1.1.1.1')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('a').value.set('255.255.255.0')
with pytest.raises(ValueError):
await cfg.option('a').value.set('192.168.001.0')
with pytest.raises(ValueError):
await cfg.option('a').value.set('333.168.1.1')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
with warnings.catch_warnings(record=True) as w:
2019-12-24 15:24:20 +01:00
await cfg.option('b').value.set('255.255.255.0')
2019-06-21 23:04:04 +02:00
assert len(w) == 1
2013-07-11 23:05:33 +02:00
2019-12-24 15:24:20 +01:00
@pytest.mark.asyncio
async def test_network_cidr(config_type):
2019-02-24 15:11:08 +01:00
a = NetworkOption('a', '', cidr=True)
od = OptionDescription('od', '', [a])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
# FIXME cfg = await get_config(cfg, config_type)
await cfg.option('a').value.set('192.168.1.1/32')
await cfg.option('a').value.set('192.168.1.0/24')
await cfg.option('a').value.set('88.88.88.88/32')
await cfg.option('a').value.set('0.0.0.0/0')
with pytest.raises(ValueError):
await cfg.option('a').value.set('192.168.1.1')
with pytest.raises(ValueError):
await cfg.option('a').value.set('192.168.1.1/24')
with pytest.raises(ValueError):
await cfg.option('a').value.set('2001:db00::0/24')
@pytest.mark.asyncio
async def test_network_invalid():
with pytest.raises(ValueError):
NetworkOption('a', '', default='toto')
@pytest.mark.asyncio
async def test_netmask(config_type):
a = NetmaskOption('a', '')
od = OptionDescription('od', '', [a])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
cfg = await get_config(cfg, config_type)
with pytest.raises(ValueError):
await cfg.option('a').value.set('192.168.1.1.1')
2019-06-21 23:04:04 +02:00
if config_type != 'tiramisu-api':
# FIXME
2019-12-24 15:24:20 +01:00
with pytest.raises(ValueError):
await cfg.option('a').value.set('192.168.1.1')
with pytest.raises(ValueError):
await cfg.option('a').value.set('192.168.1.0')
with pytest.raises(ValueError):
await cfg.option('a').value.set('88.88.88.88')
with pytest.raises(ValueError):
await cfg.option('a').value.set('255.255.255.000')
with pytest.raises(ValueError):
await cfg.option('a').value.set(2)
await cfg.option('a').value.set('0.0.0.0')
await cfg.option('a').value.set('255.255.255.0')
@pytest.mark.asyncio
async def test_broadcast(config_type):
2017-02-04 10:21:44 +01:00
a = BroadcastOption('a', '')
od = OptionDescription('od', '', [a])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
# FIXME cfg = await get_config(cfg, config_type)
with pytest.raises(ValueError):
await cfg.option('a').value.set('192.168.1.255.1')
with pytest.raises(ValueError):
await cfg.option('a').value.set('192.168.001.255')
with pytest.raises(ValueError):
await cfg.option('a').value.set('192.168.0.300')
with pytest.raises(ValueError):
await cfg.option('a').value.set(1)
with pytest.raises(ValueError):
await cfg.option('a').value.set(2)
with pytest.raises(ValueError):
await cfg.option('a').value.set('2001:db8::1')
await cfg.option('a').value.set('0.0.0.0')
await cfg.option('a').value.set('255.255.255.0')
@pytest.mark.asyncio
async def test_port(config_type):
2013-07-11 23:05:33 +02:00
a = PortOption('a', '')
b = PortOption('b', '', allow_zero=True)
c = PortOption('c', '', allow_zero=True, allow_registred=False)
d = PortOption('d', '', allow_zero=True, allow_wellknown=False, allow_registred=False)
e = PortOption('e', '', allow_zero=True, allow_private=True)
f = PortOption('f', '', allow_private=True)
od = OptionDescription('od', '', [a, b, c, d, e, f])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
# FIXME cfg = await get_config(cfg, config_type)
with pytest.raises(ValueError):
await cfg.option('a').value.set('0')
await cfg.option('a').value.set('1')
await cfg.option('a').value.set('1023')
await cfg.option('a').value.set('1024')
await cfg.option('a').value.set('49151')
with pytest.raises(ValueError):
await cfg.option('a').value.set('49152')
with pytest.raises(ValueError):
await cfg.option('a').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('a').value.set('65536')
await cfg.option('b').value.set('0')
await cfg.option('b').value.set('1')
await cfg.option('b').value.set('1023')
await cfg.option('b').value.set('1024')
await cfg.option('b').value.set('49151')
with pytest.raises(ValueError):
await cfg.option('b').value.set('49152')
with pytest.raises(ValueError):
await cfg.option('b').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('b').value.set('65536')
await cfg.option('c').value.set('0')
await cfg.option('c').value.set('1')
await cfg.option('c').value.set('1023')
with pytest.raises(ValueError):
await cfg.option('c').value.set('1024')
with pytest.raises(ValueError):
await cfg.option('c').value.set('49151')
with pytest.raises(ValueError):
await cfg.option('c').value.set('49152')
with pytest.raises(ValueError):
await cfg.option('c').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('c').value.set('65536')
await cfg.option('d').value.set('0')
with pytest.raises(ValueError):
await cfg.option('d').value.set('1')
with pytest.raises(ValueError):
await cfg.option('d').value.set('1023')
with pytest.raises(ValueError):
await cfg.option('d').value.set('1024')
with pytest.raises(ValueError):
await cfg.option('d').value.set('49151')
with pytest.raises(ValueError):
await cfg.option('d').value.set('49152')
with pytest.raises(ValueError):
await cfg.option('d').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('d').value.set('65536')
await cfg.option('e').value.set('0')
await cfg.option('e').value.set('1')
await cfg.option('e').value.set('1023')
await cfg.option('e').value.set('1024')
await cfg.option('e').value.set('49151')
await cfg.option('e').value.set('49152')
await cfg.option('e').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('f').value.set('0')
await cfg.option('f').value.set('1')
await cfg.option('f').value.set('1023')
await cfg.option('f').value.set('1024')
await cfg.option('f').value.set('49151')
await cfg.option('f').value.set('49152')
await cfg.option('f').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('f').value.set('65536')
@pytest.mark.asyncio
async def test_port_range(config_type):
2013-07-11 23:05:33 +02:00
a = PortOption('a', '', allow_range=True)
b = PortOption('b', '', allow_range=True, allow_zero=True)
c = PortOption('c', '', allow_range=True, allow_zero=True, allow_registred=False)
d = PortOption('d', '', allow_range=True, allow_zero=True, allow_wellknown=False, allow_registred=False)
e = PortOption('e', '', allow_range=True, allow_zero=True, allow_private=True)
f = PortOption('f', '', allow_range=True, allow_private=True)
od = OptionDescription('od', '', [a, b, c, d, e, f])
2019-12-24 15:24:20 +01:00
cfg = await Config(od)
# FIXME cfg = await get_config(cfg, config_type)
with pytest.raises(ValueError):
await cfg.option('a').value.set('0')
await cfg.option('a').value.set('1')
await cfg.option('a').value.set('1023')
await cfg.option('a').value.set('1024')
await cfg.option('a').value.set('49151')
with pytest.raises(ValueError):
await cfg.option('a').value.set('49152')
with pytest.raises(ValueError):
await cfg.option('a').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('a').value.set('65536')
await cfg.option('a').value.set('1:49151')
with pytest.raises(ValueError):
await cfg.option('a').value.set('0:49151')
with pytest.raises(ValueError):
await cfg.option('a').value.set('1:49152')
await cfg.option('b').value.set('0')
await cfg.option('b').value.set('1')
await cfg.option('b').value.set('1023')
await cfg.option('b').value.set('1024')
await cfg.option('b').value.set('49151')
with pytest.raises(ValueError):
await cfg.option('b').value.set('49152')
with pytest.raises(ValueError):
await cfg.option('b').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('b').value.set('65536')
await cfg.option('b').value.set('0:49151')
with pytest.raises(ValueError):
await cfg.option('b').value.set('0:49152')
await cfg.option('c').value.set('0')
await cfg.option('c').value.set('1')
await cfg.option('c').value.set('1023')
with pytest.raises(ValueError):
await cfg.option('c').value.set('1024')
with pytest.raises(ValueError):
await cfg.option('c').value.set('49151')
with pytest.raises(ValueError):
await cfg.option('c').value.set('49152')
with pytest.raises(ValueError):
await cfg.option('c').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('c').value.set('65536')
await cfg.option('c').value.set('0:1023')
with pytest.raises(ValueError):
await cfg.option('c').value.set('0:1024')
await cfg.option('d').value.set('0')
with pytest.raises(ValueError):
await cfg.option('d').value.set('1')
with pytest.raises(ValueError):
await cfg.option('d').value.set('1023')
with pytest.raises(ValueError):
await cfg.option('d').value.set('1024')
with pytest.raises(ValueError):
await cfg.option('d').value.set('49151')
with pytest.raises(ValueError):
await cfg.option('d').value.set('49152')
with pytest.raises(ValueError):
await cfg.option('d').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('d').value.set('65536')
with pytest.raises(ValueError):
await cfg.option('d').value.set('0:0')
with pytest.raises(ValueError):
await cfg.option('d').value.set('0:1')
await cfg.option('e').value.set('0')
await cfg.option('e').value.set('1')
await cfg.option('e').value.set('1023')
await cfg.option('e').value.set('1024')
await cfg.option('e').value.set('49151')
await cfg.option('e').value.set('49152')
await cfg.option('e').value.set('65535')
await cfg.option('e').value.set('0:65535')
with pytest.raises(ValueError):
await cfg.option('e').value.set('0:65536')
with pytest.raises(ValueError):
await cfg.option('f').value.set('0')
await cfg.option('f').value.set('1')
await cfg.option('f').value.set('1023')
await cfg.option('f').value.set('1024')
await cfg.option('f').value.set('49151')
await cfg.option('f').value.set('49152')
await cfg.option('f').value.set('65535')
with pytest.raises(ValueError):
await cfg.option('f').value.set('65536')
await cfg.option('f').value.set('1:65535')
await cfg.option('f').value.set('3:4')
with pytest.raises(ValueError):
await cfg.option('f').value.set('0:65535')
with pytest.raises(ValueError):
await cfg.option('f').value.set('4:3')