Skip to content

Commit

Permalink
test: add wifi test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
posterzh committed Apr 27, 2022
1 parent 36a5475 commit e3cf0d6
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
32 changes: 32 additions & 0 deletions tests/gatewayconfig/bluetooth/test_wifi_connect_characteristic.py
Original file line number Diff line number Diff line change
@@ -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))
28 changes: 28 additions & 0 deletions tests/gatewayconfig/bluetooth/test_wifi_remove_characteristic.py
Original file line number Diff line number Diff line change
@@ -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))
30 changes: 30 additions & 0 deletions tests/gatewayconfig/bluetooth/test_wifi_services_characteristic.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions tests/gatewayconfig/bluetooth/test_wifi_ssid_characteristic.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e3cf0d6

Please sign in to comment.