aioupnp/tests/test_interfaces.py

90 lines
2.7 KiB
Python
Raw Normal View History

2019-05-22 00:17:17 +02:00
from unittest import mock
2020-11-02 16:52:36 +01:00
from collections import OrderedDict
from aioupnp import interfaces
2019-05-22 08:56:59 +02:00
from aioupnp.fault import UPnPError
2019-05-22 08:14:09 +02:00
from aioupnp.upnp import UPnP
2019-05-22 08:56:59 +02:00
from tests import AsyncioTestCase
2019-05-22 00:17:17 +02:00
class mock_netifaces:
@staticmethod
def gateways():
return {
"default": {
2: [
"192.168.1.1",
"test0"
]
},
2: [
[
"192.168.1.1",
"test0",
True
]
]
}
@staticmethod
def interfaces():
return ['test0']
@staticmethod
def ifaddresses(interface):
return {
2020-11-02 16:52:36 +01:00
17: [
{
"addr": "01:02:03:04:05:06",
"broadcast": "ff:ff:ff:ff:ff:ff"
}
],
2: [
{
"addr": "192.168.1.2",
"netmask": "255.255.255.0",
"broadcast": "192.168.1.255"
}
],
}
class mock_netifaces_extra_interface(mock_netifaces):
@staticmethod
def interfaces():
return ['lo', 'test0', 'test1']
2019-05-22 00:17:17 +02:00
2019-05-22 08:56:59 +02:00
class TestParseInterfaces(AsyncioTestCase):
2019-05-22 00:17:17 +02:00
def test_parse_interfaces(self):
with mock.patch('aioupnp.interfaces.get_netifaces') as patch:
patch.return_value = mock_netifaces
2019-05-22 08:14:09 +02:00
lan, gateway = UPnP.get_lan_and_gateway(interface_name='test0')
2019-05-22 00:17:17 +02:00
self.assertEqual(gateway, '192.168.1.1')
self.assertEqual(lan, '192.168.1.2')
2019-05-22 08:56:59 +02:00
async def test_netifaces_fail(self):
checked = []
with mock.patch('aioupnp.interfaces.get_netifaces') as patch:
patch.return_value = mock_netifaces
try:
await UPnP.discover(interface_name='test1')
except UPnPError as err:
self.assertEqual(str(err), 'failed to get lan and gateway addresses for test1')
checked.append(True)
else:
self.assertTrue(False)
2019-10-08 23:59:35 +02:00
self.assertEqual(len(checked), 1)
2020-11-02 16:52:36 +01:00
def test_guess_gateway(self):
# handle edge case where netifaces gives more interfaces than it does gateways
with mock.patch('aioupnp.interfaces.get_netifaces') as patch:
patch.return_value = mock_netifaces_extra_interface
self.assertDictEqual(
OrderedDict(
[
('test0', ('192.168.1.1', '192.168.1.2')),
('test1', ('192.168.1.1', '192.168.1.2')),
('default', ('192.168.1.1', '192.168.1.2'))
]), interfaces.get_interfaces()
)