From edada54d7e7d6c885a7a61a60e8f489eb84ca45c Mon Sep 17 00:00:00 2001 From: Ken Roberts Date: Fri, 3 Aug 2018 15:32:32 -0700 Subject: [PATCH] Fix windows not using lo as network interface --- openlp/core/common/__init__.py | 15 +++- .../common/test_network_interfaces.py | 78 +++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 tests/functional/openlp_core/common/test_network_interfaces.py diff --git a/openlp/core/common/__init__.py b/openlp/core/common/__init__.py index a6e7620b5..0148bce99 100644 --- a/openlp/core/common/__init__.py +++ b/openlp/core/common/__init__.py @@ -64,13 +64,17 @@ def get_local_ip4(): log.debug('Getting local IPv4 interface(es) information') my_ip4 = {} for iface in QNetworkInterface.allInterfaces(): + log.debug('Checking for isValid and flags == IsUP | IsRunning') if not iface.isValid() or not (iface.flags() & (QNetworkInterface.IsUp | QNetworkInterface.IsRunning)): continue + log.debug('Checking address(es) protocol') for address in iface.addressEntries(): ip = address.ip() # NOTE: Next line will skip if interface is localhost - keep for now until we decide about it later # if (ip.protocol() == QAbstractSocket.IPv4Protocol) and (ip != QHostAddress.LocalHost): + log.debug('Checking for protocol == IPv4Protocol') if ip.protocol() == QAbstractSocket.IPv4Protocol: + log.debug('Getting interface information') my_ip4[iface.name()] = {'ip': ip.toString(), 'broadcast': address.broadcast().toString(), 'netmask': address.netmask().toString(), @@ -79,14 +83,21 @@ def get_local_ip4(): ip.toIPv4Address()).toString() } log.debug('Adding {iface} to active list'.format(iface=iface.name())) + if 'localhost' in my_ip4: + log.debug('Renaming windows localhost to lo') + my_ip4['lo'] = my_ip4['localhost'] + my_ip4.pop('localhost') + if len(my_ip4) == 0: + log.warning('No active IPv4 network interfaces detected') if len(my_ip4) == 1: if 'lo' in my_ip4: # No active interfaces - so leave localhost in there log.warning('No active IPv4 interfaces found except localhost') else: # Since we have a valid IP4 interface, remove localhost - log.debug('Found at least one IPv4 interface, removing localhost') - my_ip4.pop('lo') + if 'lo' in my_ip4: + log.debug('Found at least one IPv4 interface, removing localhost') + my_ip4.pop('lo') return my_ip4 diff --git a/tests/functional/openlp_core/common/test_network_interfaces.py b/tests/functional/openlp_core/common/test_network_interfaces.py new file mode 100644 index 000000000..d1547bd0a --- /dev/null +++ b/tests/functional/openlp_core/common/test_network_interfaces.py @@ -0,0 +1,78 @@ +# -*- coding: utf-8 -*- +# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4 + +############################################################################### +# OpenLP - Open Source Lyrics Projection # +# --------------------------------------------------------------------------- # +# Copyright (c) 2008-2017 OpenLP Developers # +# --------------------------------------------------------------------------- # +# This program is free software; you can redistribute it and/or modify it # +# under the terms of the GNU General Public License as published by the Free # +# Software Foundation; version 2 of the License. # +# # +# This program is distributed in the hope that it will be useful, but WITHOUT # +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # +# more details. # +# # +# You should have received a copy of the GNU General Public License along # +# with this program; if not, write to the Free Software Foundation, Inc., 59 # +# Temple Place, Suite 330, Boston, MA 02111-1307 USA # +############################################################################### +""" +Functional tests to test calls for network interfaces. +""" +from unittest import TestCase +from unittest.mock import MagicMock, call, patch + +import openlp.core.common +from openlp.core.common import get_local_ip4 + +from tests.helpers.testmixin import TestMixin + +lo_address_attrs = {'isValid.return_value': True, + 'flags.return_value': True, + 'InterfaceFlags.return_value': True, + 'name.return_value': 'lo', + 'broadcast.toString.return_value': '127.0.0.255', + 'netmask.toString.return_value': '255.0.0.0', + 'prefixLength.return_value': 8, + 'ip.protocol.return_value': True} + + +class TestInterfaces(TestCase, TestMixin): + """ + A test suite to test out functions/methods that use network interface(s). + """ + def setUp(self): + """ + Create an instance and a few example actions. + """ + self.build_settings() + + self.ip4_lo_address = MagicMock() + self.ip4_lo_address.configure_mock(**lo_address_attrs) + + def tearDown(self): + """ + Clean up + """ + self.destroy_settings() + + @patch.object(openlp.core.common, 'log') + def test_ip4_no_interfaces(self, mock_log): + """ + Test no interfaces available + """ + # GIVEN: Test environment + call_warning = [call('No active IPv4 network interfaces detected')] + + with patch('openlp.core.common.QNetworkInterface') as mock_newtork_interface: + mock_newtork_interface.allInterfaces.return_value = [] + + # WHEN: get_local_ip4 is called + ifaces = get_local_ip4() + + # THEN: There should not be any interfaces detected + assert not ifaces, 'There should have been no active interfaces' + mock_log.warning.assert_has_calls(call_warning)