From e3cf0d64035973c355ea3abaa06f1d8c89d3f6d4 Mon Sep 17 00:00:00 2001 From: Zhang Date: Wed, 27 Apr 2022 20:03:03 +0800 Subject: [PATCH] test: add wifi test cases --- .../wifi_connect_characteristic.py | 9 +++-- ...wifi_configured_services_characteristic.py | 34 +++++++++++++++++++ .../test_wifi_connect_characteristic.py | 32 +++++++++++++++++ .../test_wifi_remove_characteristic.py | 28 +++++++++++++++ .../test_wifi_services_characteristic.py | 30 ++++++++++++++++ .../test_wifi_ssid_characteristic.py | 29 ++++++++++++++++ 6 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 tests/gatewayconfig/bluetooth/test_wifi_configured_services_characteristic.py create mode 100644 tests/gatewayconfig/bluetooth/test_wifi_connect_characteristic.py create mode 100644 tests/gatewayconfig/bluetooth/test_wifi_remove_characteristic.py create mode 100644 tests/gatewayconfig/bluetooth/test_wifi_services_characteristic.py create mode 100644 tests/gatewayconfig/bluetooth/test_wifi_ssid_characteristic.py diff --git a/gatewayconfig/bluetooth/characteristics/wifi_connect_characteristic.py b/gatewayconfig/bluetooth/characteristics/wifi_connect_characteristic.py index 87c252c..b9c5e43 100644 --- a/gatewayconfig/bluetooth/characteristics/wifi_connect_characteristic.py +++ b/gatewayconfig/bluetooth/characteristics/wifi_connect_characteristic.py @@ -110,13 +110,11 @@ def StopNotify(self): self.notifying = False def WriteValue(self, value, options): - logger.debug("Write WiFi Connect %s" % value) + logger.debug("Write WiFi Connect") wifi_details = wifi_connect_pb2.wifi_connect_v1() wifi_details.ParseFromString(bytes(value)) - logger.debug(str(wifi_details.service)) - self.wifi_service = str(wifi_details.service) self.wifi_password = str(wifi_details.password) @@ -127,8 +125,9 @@ def WriteValue(self, value, options): try: cmd_thread = CommandThread(self) cmd_thread.start() - except Exception as ex: - print(str(ex)) + logger.debug("Connecting to %s is started" % str(wifi_details.service)) + except Exception as e: + logger.debug("Connecting to %s is failed" % str(e)) def ReadValue(self, options): logger.debug('Read WiFi Connect') diff --git a/tests/gatewayconfig/bluetooth/test_wifi_configured_services_characteristic.py b/tests/gatewayconfig/bluetooth/test_wifi_configured_services_characteristic.py new file mode 100644 index 0000000..b6c12f7 --- /dev/null +++ b/tests/gatewayconfig/bluetooth/test_wifi_configured_services_characteristic.py @@ -0,0 +1,34 @@ +from nmcli import Connection +from gatewayconfig.bluetooth.characteristics.wifi_configured_services_characteristic import \ + WifiConfiguredServicesCharacteristic +from gatewayconfig.gatewayconfig_shared_state import GatewayconfigSharedState +from unittest import TestCase +from unittest.mock import patch +from gatewayconfig.helpers import string_to_dbus_byte_array +from lib.cputemp.service import Service + + +class TestWifiConfiguredServicesCharacteristic(TestCase): + + @classmethod + def setUpClass(cls): + cls.service = Service(201, '1111', True) + + @patch('lib.nmcli_custom.connection', + return_value=[ + Connection(name='supervisor0', uuid='00000005-0006-0007-0008-000000000009', + conn_type='bridge', device='supervisor0'), + Connection(name='TP-LINK', uuid='00000001-0002-0003-0004-000000000005', + conn_type='wifi', device='wlan0'), + Connection(name='Wired connection 1', uuid='0000000a-000b-000c-000d-00000000000e', + conn_type='ethernet', device='eth0') + + ]) + def test_ReadValue(self, mock1): + characteristic = WifiConfiguredServicesCharacteristic( + self.service, + GatewayconfigSharedState()) + wifi_configured_services = characteristic.ReadValue({}) + + expected = string_to_dbus_byte_array(b'\n\x07TP-LINK') + self.assertEqual(wifi_configured_services, expected) diff --git a/tests/gatewayconfig/bluetooth/test_wifi_connect_characteristic.py b/tests/gatewayconfig/bluetooth/test_wifi_connect_characteristic.py new file mode 100644 index 0000000..d866b50 --- /dev/null +++ b/tests/gatewayconfig/bluetooth/test_wifi_connect_characteristic.py @@ -0,0 +1,32 @@ +import dbus + +from gatewayconfig.bluetooth.characteristics.wifi_connect_characteristic import \ + WifiConnectCharacteristic +from unittest import TestCase +from unittest.mock import patch +from gatewayconfig.helpers import string_to_dbus_byte_array +from lib.cputemp.service import Service + + +class TestWifiConnectCharacteristic(TestCase): + + @classmethod + def setUpClass(cls): + cls.service = Service(202, '1111', True) + + @patch('gatewayconfig.bluetooth.characteristics.wifi_connect_characteristic.CommandThread') + def test_WriteValue(self, mock1): + try: + with self.assertLogs( + 'gatewayconfig.bluetooth.characteristics', + level='DEBUG') as cm: + characteristic = WifiConnectCharacteristic(self.service) + characteristic.WriteValue( + dbus.Array(string_to_dbus_byte_array(b'\n\fTP-LINK_4672\x12\t123456789'), + signature='y'), {}) + + self.assertGreaterEqual(len(cm.output), 2) + self.assertIn('Write WiFi Connect', cm.output[0]) + self.assertIn('Connecting to TP-LINK_4672 is started', cm.output[1]) + except Exception as e: + self.fail("Unexpected exception while testing WifiRemoveCharacteristic: %s" % str(e)) diff --git a/tests/gatewayconfig/bluetooth/test_wifi_remove_characteristic.py b/tests/gatewayconfig/bluetooth/test_wifi_remove_characteristic.py new file mode 100644 index 0000000..00d578c --- /dev/null +++ b/tests/gatewayconfig/bluetooth/test_wifi_remove_characteristic.py @@ -0,0 +1,28 @@ +from gatewayconfig.bluetooth.characteristics.wifi_remove_characteristic import \ + WifiRemoveCharacteristic +from unittest import TestCase +from unittest.mock import patch +from gatewayconfig.helpers import string_to_dbus_byte_array +from lib.cputemp.service import Service + + +class TestWifiRemoveCharacteristic(TestCase): + + @classmethod + def setUpClass(cls): + cls.service = Service(203, '1111', True) + + @patch('lib.nmcli_custom.connection.delete') + def test_WriteValue(self, mock1): + try: + with self.assertLogs( + 'gatewayconfig.bluetooth.characteristics.wifi_remove_characteristic', + level='DEBUG') as cm: + characteristic = WifiRemoveCharacteristic(self.service) + characteristic.WriteValue(string_to_dbus_byte_array(b'\n\x07TP-LINK'), {}) + + self.assertEqual(len(cm.output), 2) + self.assertIn('Write WiFi Remove', cm.output[0]) + self.assertIn('Connection TP-LINK should be deleted', cm.output[1]) + except Exception as e: + self.fail("Unexpected exception while testing WifiRemoveCharacteristic: %s" % str(e)) diff --git a/tests/gatewayconfig/bluetooth/test_wifi_services_characteristic.py b/tests/gatewayconfig/bluetooth/test_wifi_services_characteristic.py new file mode 100644 index 0000000..0320f1c --- /dev/null +++ b/tests/gatewayconfig/bluetooth/test_wifi_services_characteristic.py @@ -0,0 +1,30 @@ +from nmcli import DeviceWifi +from gatewayconfig.bluetooth.characteristics.wifi_services_characteristic import \ + WifiServicesCharacteristic +from gatewayconfig.gatewayconfig_shared_state import GatewayconfigSharedState +from unittest import TestCase +from unittest.mock import patch +from gatewayconfig.helpers import string_to_dbus_byte_array +from lib.cputemp.service import Service + + +class TestWifiServicesCharacteristic(TestCase): + + @classmethod + def setUpClass(cls): + cls.service = Service(204, '1111', True) + + @patch('lib.nmcli_custom.device.wifi_rescan') + @patch('lib.nmcli_custom.device.wifi', + return_value=[ + DeviceWifi(in_use=False, ssid='TP-LINK', mode='Infra', chan=5, rate=54, signal=100, + security='WPA1'), + DeviceWifi(in_use=True, ssid='PHICOMM', mode='Infra', chan=1, rate=44, signal=96, + security='WPA2') + ]) + def test_ReadValue(self, mock1, mock2): + characteristic = WifiServicesCharacteristic(self.service, GatewayconfigSharedState()) + wifi_services = characteristic.ReadValue({}) + + expected = string_to_dbus_byte_array(b'\n\x07TP-LINK\n\x07PHICOMM') + self.assertEqual(wifi_services, expected) diff --git a/tests/gatewayconfig/bluetooth/test_wifi_ssid_characteristic.py b/tests/gatewayconfig/bluetooth/test_wifi_ssid_characteristic.py new file mode 100644 index 0000000..3de8df8 --- /dev/null +++ b/tests/gatewayconfig/bluetooth/test_wifi_ssid_characteristic.py @@ -0,0 +1,29 @@ +from nmcli import DeviceWifi +from gatewayconfig.bluetooth.characteristics.wifi_ssid_characteristic import WifiSSIDCharacteristic +from gatewayconfig.gatewayconfig_shared_state import GatewayconfigSharedState +from unittest import TestCase +from unittest.mock import patch +from gatewayconfig.helpers import string_to_dbus_byte_array +from lib.cputemp.service import Service + + +class TestWifiSSIDCharacteristic(TestCase): + + @classmethod + def setUpClass(cls): + cls.service = Service(205, '1111', True) + + @patch('lib.nmcli_custom.device.wifi_rescan') + @patch('lib.nmcli_custom.device.wifi', + return_value=[ + DeviceWifi(in_use=False, ssid='TP-LINK', mode='Infra', chan=5, rate=54, signal=100, + security='WPA1'), + DeviceWifi(in_use=True, ssid='PHICOMM', mode='Infra', chan=1, rate=44, signal=96, + security='WPA2') + ]) + def test_ReadValue(self, mock1, mock2): + characteristic = WifiSSIDCharacteristic(self.service, GatewayconfigSharedState()) + wifi_ssid = characteristic.ReadValue({}) + + expected = string_to_dbus_byte_array(b'PHICOMM') + self.assertEqual(wifi_ssid, expected)