2016-05-13 23:18:20 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
#built-in python libraries
|
|
|
|
import unittest
|
|
|
|
from gi.repository import GLib
|
|
|
|
import dbus
|
|
|
|
import time
|
|
|
|
import logging
|
|
|
|
import os
|
2016-07-15 01:43:47 +02:00
|
|
|
from subprocess import Popen, PIPE, STDOUT
|
2016-05-13 23:18:20 +02:00
|
|
|
import sys
|
2016-05-17 02:02:29 +02:00
|
|
|
sys.path.append('../utility') #needed to import all the utility modules
|
2016-05-13 23:18:20 +02:00
|
|
|
import utility
|
2016-07-15 01:43:47 +02:00
|
|
|
import pty
|
2016-05-13 23:18:20 +02:00
|
|
|
|
|
|
|
class TestConnectDisconnect(unittest.TestCase):
|
|
|
|
def doConnectDisconnect(self):
|
2016-06-01 20:23:45 +02:00
|
|
|
objectList = utility.getObjectList(bus)
|
2016-05-13 23:18:20 +02:00
|
|
|
|
2016-07-15 01:43:47 +02:00
|
|
|
# start simpleAgent
|
|
|
|
master, slave = pty.openpty()
|
|
|
|
proc = Popen([sys.executable, '../utility/simpleAgent.py'],
|
|
|
|
stdin=PIPE, stdout=slave, close_fds=True)
|
|
|
|
stdout_handle = os.fdopen(master)
|
|
|
|
if stdout_handle.readline().rstrip() == "AGENT_REGISTERED":
|
|
|
|
logger.debug("Agent Registered")
|
|
|
|
else:
|
|
|
|
logger.debug("Agent failed to register")
|
|
|
|
|
|
|
|
# close the handles
|
|
|
|
stdout_handle.close()
|
|
|
|
os.close(slave)
|
|
|
|
|
2016-07-26 00:13:38 +02:00
|
|
|
networkToConnect = utility.getNetworkToConnectTo(objectList,
|
|
|
|
"IntelWIFI")
|
2016-05-13 23:18:20 +02:00
|
|
|
# check if networkToConnect is not null. If yes, restart program
|
|
|
|
# so that the network list is updated. Alternatively, we can scan
|
|
|
|
# for networks.
|
|
|
|
if (networkToConnect == ""):
|
2016-05-17 02:02:29 +02:00
|
|
|
time.sleep(2)
|
2016-05-13 23:18:20 +02:00
|
|
|
logger.debug("RESTART PROGRAM")
|
|
|
|
os.execl(sys.executable, sys.executable, * sys.argv)
|
|
|
|
|
|
|
|
self.assertNotEqual(networkToConnect, "")
|
2016-06-04 01:13:45 +02:00
|
|
|
network = dbus.Interface(bus.get_object(utility.IWD_SERVICE,
|
|
|
|
networkToConnect),
|
|
|
|
utility.IWD_NETWORK_INTERFACE)
|
|
|
|
|
2016-05-17 02:02:29 +02:00
|
|
|
status = utility.connect(networkToConnect, self, mainloop, bus)
|
2016-06-01 20:23:45 +02:00
|
|
|
|
2016-05-17 02:02:29 +02:00
|
|
|
if status == False:
|
|
|
|
#terminate proc
|
|
|
|
proc.terminate()
|
|
|
|
return
|
2016-05-13 23:18:20 +02:00
|
|
|
logger.info("Currently connected to: %s",
|
2016-06-01 20:23:45 +02:00
|
|
|
utility.getCurrentlyConnectedNetworkName())
|
|
|
|
self.assertEqual(utility.getCurrentlyConnectedNetworkName(),
|
|
|
|
"IntelWIFI")
|
2016-05-13 23:18:20 +02:00
|
|
|
|
|
|
|
# retrieve the deviceId form networkToConnect. This will be used
|
|
|
|
# for checking if we are disconnecting from the right device later.
|
|
|
|
deviceIdIndex = networkToConnect.rfind("/", 0,
|
|
|
|
len(networkToConnect))
|
|
|
|
deviceIdOfConnectedNetwork = networkToConnect[0:deviceIdIndex]
|
|
|
|
logger.debug("device id of connected network %s",
|
|
|
|
deviceIdOfConnectedNetwork)
|
|
|
|
|
|
|
|
# wait 2 seconds before initiating disconnect
|
|
|
|
time.sleep(2)
|
|
|
|
deviceIdToDisconnect = utility.getCurrentlyConnectedDevice()
|
|
|
|
logger.info("Disconnecting from: %s", deviceIdToDisconnect)
|
|
|
|
self.assertEqual(deviceIdToDisconnect, deviceIdOfConnectedNetwork)
|
|
|
|
utility.disconnect(deviceIdToDisconnect, mainloop, bus)
|
|
|
|
#terminate proc
|
|
|
|
proc.terminate()
|
|
|
|
|
|
|
|
# connect to network A. Wait for 2 seconds. Disconnect.
|
|
|
|
def test_connectDisconnect(self):
|
|
|
|
logger.info(sys._getframe().f_code.co_name)
|
|
|
|
while (True):
|
2016-06-04 01:13:45 +02:00
|
|
|
if bus.name_has_owner(utility.IWD_SERVICE) == True:
|
2016-05-13 23:18:20 +02:00
|
|
|
break
|
|
|
|
self.doConnectDisconnect()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
global logger, bus, mainloop
|
|
|
|
utility.initLogger()
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
bus = dbus.SystemBus()
|
|
|
|
mainloop = GLib.MainLoop()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
mainloop.quit()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(exit=True)
|