Skip to content

Commit

Permalink
tests: Add test cases for device names/password verification
Browse files Browse the repository at this point in the history
  • Loading branch information
dskorykh authored and egorf committed Dec 8, 2017
1 parent b24571d commit b35436d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ dist/
/htmlcov/
/.cache/
/.coverage
.idea/
.idea/
build/
11 changes: 1 addition & 10 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import platform
import sys
import pytest
import mock

not_edison = "edison" not in platform.platform()
edison = pytest.mark.skipif(not_edison,
reason="Not supported in this platform")

if not_edison:
sys.modules['dbus'] = mock.MagicMock()
sys.modules['dbus.service'] = mock.MagicMock()
sys.modules['dbus.mainloop'] = mock.MagicMock()
sys.modules['dbus.mainloop.glib'] = mock.MagicMock()
sys.modules['gobject'] = mock.MagicMock()
reason="Not supported in this platform")
6 changes: 5 additions & 1 deletion tests/test_edison_wificontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def test_wifi_turn_on(self):

def test_network_add(self, valid_network):
self.manager.add_network(valid_network)
self.manager.start_host_mode()

added_networks = self.manager.get_added_networks()

assert valid_network['ssid'] in [network['ssid'] for network in added_networks]
Expand All @@ -137,12 +139,14 @@ def test_network_add_with_different_security(self, valid_network):

def test_network_remove(self, valid_network):
self.test_network_add(valid_network)
self.manager.remove_network(valid_network)

self.manager.remove_network(valid_network)
added_networks = self.manager.get_added_networks()
assert valid_network['ssid'] not in [network['ssid'] for network in added_networks]

def test_hotspot_after_connect_failure(self, invalid_network):

self.manager.start_client_mode()
self.manager.add_network(invalid_network)
self.manager.start_connecting(invalid_network, timeout=1, callback=self.hostapd_callback)

Expand Down
27 changes: 21 additions & 6 deletions tests/test_hostapd.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,43 @@


import os
from random import randint
from wificontrol.hostapd import HostAP
from tests import edison
import pytest
import netifaces


@edison
@pytest.fixture
def get_interface():
interface = None
for iface in netifaces.interfaces():
if 'wl' in iface:
interface = iface
return interface
return interface


@pytest.mark.skipif(not get_interface(), reason="Hostapd is not installed")
class TestHostAP:
@classmethod
def setup_class(cls):
cur_path = os.getcwd()
hostapd_path = cur_path + "/tests/test_files/hostapd.conf"
hostname_path = cur_path + "/tests/test_files/hostname"
cls.hotspot = HostAP('wlan0', hostapd_path, hostname_path)

cls.hotspot = HostAP(get_interface(), hostapd_path, hostname_path)

@classmethod
def teardown_class(cls):
pass

def test_set_hostap_name(self):
new_name = "testname_{}".format(randint(0, 1000))
new_name = "testname"
self.hotspot.set_hostap_name(new_name)
mac_end = self.hotspot.get_device_mac()[-6:]
assert self.hotspot.get_hostap_name() == "{}{}".format(new_name, mac_end)

def test_set_host_name(self):
new_name = "testname_{}".format(randint(0, 1000))
new_name = "testname"
self.hotspot.set_host_name(new_name)
assert self.hotspot.get_host_name() == new_name

Expand All @@ -70,3 +81,7 @@ def test_start_hotspot(self):
def test_stop_hotspot(self):
self.hotspot.stop()
assert self.hotspot.started() is False

def test_verify_hostap_password(self):
wpa_pass = 'somepassword'
assert self.hotspot.verify_hostap_password(wpa_pass)
14 changes: 14 additions & 0 deletions tests/test_wificontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,17 @@ def test_supplicant_functions(self):

self.manager.set_hostap_password(name)
assert self.manager.hotspot.set_hostap_password.is_called_once_with(name)

def test_verify_names(self):
name = 'test'
mac_addr = '11:22:33:44:55:66'

self.manager.hotspot.get_host_name.return_value = name
self.manager.wpasupplicant.get_p2p_name.return_value = name
self.manager.hotspot.get_hostap_name.return_value = "{}{}".format(name, mac_addr[-6:])
self.manager.hotspot.get_device_mac.return_value = mac_addr[-6:]

assert self.manager.verify_hostap_name(name)
assert self.manager.verify_device_names(name)
assert self.manager.hotspot.get_host_name.call_count == 1
assert self.manager.wpasupplicant.get_p2p_name.call_count == 1
2 changes: 1 addition & 1 deletion tests/test_wifireconnect.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_worker_restart(self, valid_network):
self.reconnect_worker.start_reconnection(valid_network['ssid'])
self.reconnect_worker.interrupt.wait(1)
self.reconnect_worker.stop_reconnection()

self.reconnect_worker.start_reconnection(valid_network['ssid'])
self.reconnect_worker.interrupt.wait(1)
self.reconnect_worker.stop_reconnection()
Expand Down
6 changes: 5 additions & 1 deletion wificontrol/wificontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from hostapd import HostAP
from wificommon import WiFi
from wpasupplicant import WpaSupplicant
from utils import PropertyError


class WiFiControl(object):
Expand Down Expand Up @@ -111,7 +112,10 @@ def get_status(self):
wpa_status = None

if state == self.WPA_STATE:
wpa_status = self.wpasupplicant.get_status()
try:
wpa_status = self.wpasupplicant.get_status()
except PropertyError:
return state, wpa_status

return state, wpa_status

Expand Down

0 comments on commit b35436d

Please sign in to comment.