tiramisu/test/test_config_ip.py
Emmanuel Garette d5d826f967 adapt tests
2018-03-19 08:33:53 +01:00

254 lines
10 KiB
Python

from .autopath import do_autopath
do_autopath()
import warnings
from py.test import raises
from tiramisu import Config ,IPOption, NetworkOption, NetmaskOption, \
PortOption, BroadcastOption, OptionDescription, getapi
from tiramisu.error import ValueWarning
def test_ip():
a = IPOption('a', '')
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])
c = Config(od)
api = getapi(c)
api.option('a').value.set('192.168.1.1')
api.option('a').value.set('192.168.1.0')
api.option('a').value.set('88.88.88.88')
api.option('a').value.set('0.0.0.0')
raises(ValueError, "api.option('a').value.set('255.255.255.0')")
api.option('b').value.set('192.168.1.1')
api.option('b').value.set('192.168.1.0')
raises(ValueError, "api.option('b').value.set('88.88.88.88')")
api.option('b').value.set('0.0.0.0')
raises(ValueError, "api.option('b').value.set('255.255.255.0')")
raises(ValueError, "api.option('a').value.set('333.0.1.20')")
raises(ValueError, "IPOption('a', 'ip', default='192.000.023.01')")
with warnings.catch_warnings(record=True) as w:
api.option('d').value.set('88.88.88.88')
assert len(w) == 1
def test_ip_default():
a = IPOption('a', '', '88.88.88.88')
od = OptionDescription('od', '', [a])
c = Config(od)
api = getapi(c)
api.option('a').value.get() == '88.88.88.88'
def test_ip_reserved():
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)
cfg = Config(od)
api = getapi(cfg)
raises(ValueError, "api.option('a').value.set('226.94.1.1')")
api.option('b').value.set('226.94.1.1')
with warnings.catch_warnings(record=True) as w:
api.option('c').value.set('226.94.1.1')
assert len(w) == 1
def test_network():
a = NetworkOption('a', '')
b = NetworkOption('b', '', warnings_only=True)
od = OptionDescription('od', '', [a, b])
warnings.simplefilter("always", ValueWarning)
c = Config(od)
api = getapi(c)
api.option('a').value.set('192.168.1.1')
api.option('a').value.set('192.168.1.0')
api.option('a').value.set('88.88.88.88')
api.option('a').value.set('0.0.0.0')
raises(ValueError, "api.option('a').value.set(1)")
raises(ValueError, "api.option('a').value.set('1.1.1.1.1')")
raises(ValueError, "api.option('a').value.set('255.255.255.0')")
raises(ValueError, "api.option('a').value.set('192.168.001.0')")
raises(ValueError, "api.option('a').value.set('333.168.1.1')")
with warnings.catch_warnings(record=True) as w:
api.option('b').value.set('255.255.255.0')
assert len(w) == 1
def test_network_invalid():
raises(ValueError, "NetworkOption('a', '', default='toto')")
def test_netmask():
a = NetmaskOption('a', '')
od = OptionDescription('od', '', [a])
c = Config(od)
api = getapi(c)
raises(ValueError, "api.option('a').value.set('192.168.1.1.1')")
raises(ValueError, "api.option('a').value.set('192.168.1.1')")
raises(ValueError, "api.option('a').value.set('192.168.1.0')")
raises(ValueError, "api.option('a').value.set('88.88.88.88')")
raises(ValueError, "api.option('a').value.set('255.255.255.000')")
raises(ValueError, "api.option('a').value.set(2)")
api.option('a').value.set('0.0.0.0')
api.option('a').value.set('255.255.255.0')
def test_broadcast():
a = BroadcastOption('a', '')
od = OptionDescription('od', '', [a])
c = Config(od)
api = getapi(c)
raises(ValueError, "api.option('a').value.set('192.168.1.255.1')")
raises(ValueError, "api.option('a').value.set('192.168.001.255')")
raises(ValueError, "api.option('a').value.set('192.168.0.300')")
raises(ValueError, "api.option('a').value.set(1)")
raises(ValueError, "api.option('a').value.set(2)")
api.option('a').value.set('0.0.0.0')
api.option('a').value.set('255.255.255.0')
def test_port():
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])
c = Config(od)
api = getapi(c)
raises(ValueError, "api.option('a').value.set(0)")
api.option('a').value.set(1)
api.option('a').value.set(1023)
api.option('a').value.set(1024)
api.option('a').value.set(49151)
raises(ValueError, "api.option('a').value.set(49152)")
raises(ValueError, "api.option('a').value.set(65535)")
raises(ValueError, "api.option('a').value.set(65536)")
api.option('b').value.set(0)
api.option('b').value.set(1)
api.option('b').value.set(1023)
api.option('b').value.set(1024)
api.option('b').value.set(49151)
raises(ValueError, "api.option('b').value.set(49152)")
raises(ValueError, "api.option('b').value.set(65535)")
raises(ValueError, "api.option('b').value.set(65536)")
api.option('c').value.set(0)
api.option('c').value.set(1)
api.option('c').value.set(1023)
raises(ValueError, "api.option('c').value.set(1024)")
raises(ValueError, "api.option('c').value.set(49151)")
raises(ValueError, "api.option('c').value.set(49152)")
raises(ValueError, "api.option('c').value.set(65535)")
raises(ValueError, "api.option('c').value.set(65536)")
api.option('d').value.set(0)
raises(ValueError, "api.option('d').value.set(1)")
raises(ValueError, "api.option('d').value.set(1023)")
raises(ValueError, "api.option('d').value.set(1024)")
raises(ValueError, "api.option('d').value.set(49151)")
raises(ValueError, "api.option('d').value.set(49152)")
raises(ValueError, "api.option('d').value.set(65535)")
raises(ValueError, "api.option('d').value.set(65536)")
api.option('e').value.set(0)
api.option('e').value.set(1)
api.option('e').value.set(1023)
api.option('e').value.set(1024)
api.option('e').value.set(49151)
api.option('e').value.set(49152)
api.option('e').value.set(65535)
raises(ValueError, "api.option('f').value.set(0)")
api.option('f').value.set(1)
api.option('f').value.set(1023)
api.option('f').value.set(1024)
api.option('f').value.set(49151)
api.option('f').value.set(49152)
api.option('f').value.set(65535)
raises(ValueError, "api.option('f').value.set(65536)")
def test_port_range():
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])
c = Config(od)
api = getapi(c)
raises(ValueError, "api.option('a').value.set(0)")
api.option('a').value.set(1)
api.option('a').value.set(1023)
api.option('a').value.set(1024)
api.option('a').value.set(49151)
raises(ValueError, "api.option('a').value.set(49152)")
raises(ValueError, "api.option('a').value.set(65535)")
raises(ValueError, "api.option('a').value.set(65536)")
api.option('a').value.set('1:49151')
raises(ValueError, "api.option('a').value.set('0:49151')")
raises(ValueError, "api.option('a').value.set('1:49152')")
api.option('b').value.set(0)
api.option('b').value.set(1)
api.option('b').value.set(1023)
api.option('b').value.set(1024)
api.option('b').value.set(49151)
raises(ValueError, "api.option('b').value.set(49152)")
raises(ValueError, "api.option('b').value.set(65535)")
raises(ValueError, "api.option('b').value.set(65536)")
api.option('b').value.set('0:49151')
raises(ValueError, "api.option('b').value.set('0:49152')")
api.option('c').value.set(0)
api.option('c').value.set(1)
api.option('c').value.set(1023)
raises(ValueError, "api.option('c').value.set(1024)")
raises(ValueError, "api.option('c').value.set(49151)")
raises(ValueError, "api.option('c').value.set(49152)")
raises(ValueError, "api.option('c').value.set(65535)")
raises(ValueError, "api.option('c').value.set(65536)")
api.option('c').value.set('0:1023')
raises(ValueError, "api.option('c').value.set('0:1024')")
api.option('d').value.set(0)
raises(ValueError, "api.option('d').value.set(1)")
raises(ValueError, "api.option('d').value.set(1023)")
raises(ValueError, "api.option('d').value.set(1024)")
raises(ValueError, "api.option('d').value.set(49151)")
raises(ValueError, "api.option('d').value.set(49152)")
raises(ValueError, "api.option('d').value.set(65535)")
raises(ValueError, "api.option('d').value.set(65536)")
raises(ValueError, "api.option('d').value.set('0:0')")
raises(ValueError, "api.option('d').value.set('0:1')")
api.option('e').value.set(0)
api.option('e').value.set(1)
api.option('e').value.set(1023)
api.option('e').value.set(1024)
api.option('e').value.set(49151)
api.option('e').value.set(49152)
api.option('e').value.set(65535)
api.option('e').value.set('0:65535')
raises(ValueError, "api.option('e').value.set('0:65536')")
raises(ValueError, "api.option('f').value.set(0)")
api.option('f').value.set(1)
api.option('f').value.set(1023)
api.option('f').value.set(1024)
api.option('f').value.set(49151)
api.option('f').value.set(49152)
api.option('f').value.set(65535)
raises(ValueError, "api.option('f').value.set(65536)")
api.option('f').value.set('1:65535')
api.option('f').value.set('3:4')
raises(ValueError, "api.option('f').value.set('0:65535')")
raises(ValueError, "api.option('f').value.set('4:3')")