2017-11-22 00:25:45 +00:00
|
|
|
import pytest
|
2018-01-22 14:30:50 +00:00
|
|
|
from pydu.network import (dotted_netmask, is_ipv4, is_ipv6, get_free_port,
|
|
|
|
ip2int, int2ip)
|
2017-11-22 00:25:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
'mask, expected', (
|
|
|
|
(8, '255.0.0.0'),
|
|
|
|
(24, '255.255.255.0'),
|
|
|
|
(25, '255.255.255.128'),
|
|
|
|
))
|
|
|
|
def test_dotted_netmask(mask, expected):
|
|
|
|
assert dotted_netmask(mask) == expected
|
2017-11-22 00:29:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestIsIPv4Address:
|
|
|
|
|
|
|
|
def test_valid(self):
|
2017-12-08 14:13:22 +00:00
|
|
|
assert is_ipv4('8.8.8.8')
|
2017-11-22 00:29:06 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize('value', ('8.8.8.8.8', 'localhost.localdomain'))
|
|
|
|
def test_invalid(self, value):
|
2017-12-08 14:13:22 +00:00
|
|
|
assert not is_ipv4(value)
|
2017-12-08 14:18:06 +00:00
|
|
|
|
|
|
|
|
2017-12-08 14:54:47 +00:00
|
|
|
class TestIsIPv6Address:
|
|
|
|
|
|
|
|
def test_valid(self):
|
|
|
|
assert is_ipv6('fe80::9e5b:b149:e187:1a18')
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('value', ('fe80::9e5b:b149:e187::', 'localhost.localdomain'))
|
|
|
|
def test_invalid(self, value):
|
|
|
|
assert not is_ipv6(value)
|
|
|
|
|
|
|
|
|
2017-12-08 14:18:06 +00:00
|
|
|
def test_get_free_port():
|
|
|
|
port = get_free_port()
|
|
|
|
assert isinstance(port, int)
|
2017-12-16 17:07:29 +00:00
|
|
|
assert 65536 > port > 0
|
2018-01-22 14:30:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_ip2int():
|
|
|
|
assert ip2int('10.1.1.1') == 167837953
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ip2int('255.255.255.256')
|
|
|
|
|
|
|
|
assert ip2int('fe80::9e5b:b149:e187:1a18') == 338288524927261089665429805853095434776
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ip2int('fe80::9e5b:b149:e187::')
|
|
|
|
|
|
|
|
|
|
|
|
def test_int2ip():
|
|
|
|
assert int2ip(167837953) == '10.1.1.1'
|
|
|
|
assert int2ip(338288524927261089665429805853095434776) == 'fe80::9e5b:b149:e187:1a18'
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
int2ip(10**50)
|