2016-05-19 01:28:10 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
#built-in python libraries
|
|
|
|
import unittest
|
|
|
|
from gi.repository import GLib
|
|
|
|
import dbus
|
|
|
|
import dbus.service
|
|
|
|
import dbus.mainloop.glib
|
|
|
|
import time
|
|
|
|
import threading
|
|
|
|
import logging
|
|
|
|
from subprocess import Popen
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
sys.path.append('../utility') #needed to import all the utlilty modules
|
|
|
|
import utility
|
|
|
|
|
2016-06-01 20:23:47 +02:00
|
|
|
def getSecondNetworkToConnect(objectList, firstNetworkName):
|
2016-05-19 01:28:10 +02:00
|
|
|
logger.debug(sys._getframe().f_code.co_name)
|
2016-06-01 20:23:47 +02:00
|
|
|
for path in objectList:
|
2016-06-04 01:13:46 +02:00
|
|
|
if utility.IWD_DEVICE_INTERFACE not in objectList[path]:
|
2016-06-01 20:23:47 +02:00
|
|
|
continue
|
|
|
|
for path2 in objectList:
|
|
|
|
if not path2.startswith(path) or \
|
2016-06-04 01:13:46 +02:00
|
|
|
utility.IWD_NETWORK_INTERFACE not in objectList[path2]:
|
2016-05-19 01:28:10 +02:00
|
|
|
continue
|
2016-06-04 01:13:46 +02:00
|
|
|
network = objectList[path2][utility.IWD_NETWORK_INTERFACE]
|
2016-06-01 20:23:47 +02:00
|
|
|
for key in network.keys():
|
|
|
|
if key in ["Name"]:
|
|
|
|
# skip the first connected network
|
|
|
|
if (network[key] == firstNetworkName):
|
|
|
|
continue
|
|
|
|
return path2
|
2016-05-19 01:28:10 +02:00
|
|
|
return ""
|
|
|
|
|
|
|
|
class TestTwoNetworks(unittest.TestCase):
|
|
|
|
# connect to network A. disconnect from network A.
|
|
|
|
# connect to network B. disconnect from network B.
|
|
|
|
def connectToNetwork(self, networkToConnect):
|
|
|
|
# start simpleAgent
|
2016-06-01 20:23:47 +02:00
|
|
|
proc = Popen([sys.executable, '../utility/simpleAgent.py'])
|
2016-05-19 01:28:10 +02:00
|
|
|
time.sleep(2)
|
2016-06-04 01:13:46 +02:00
|
|
|
network = dbus.Interface(bus.get_object(utility.IWD_SERVICE,
|
|
|
|
networkToConnect),
|
|
|
|
utility.IWD_NETWORK_INTERFACE)
|
2016-05-19 01:28:10 +02:00
|
|
|
status = utility.connect(networkToConnect, self, mainloop, bus)
|
|
|
|
if status == False:
|
|
|
|
#terminate proc
|
|
|
|
proc.terminate()
|
|
|
|
return
|
|
|
|
|
|
|
|
connectedNetworkName = utility.getCurrentlyConnectedNetworkName()
|
|
|
|
logger.info("Currently connected to: %s", connectedNetworkName)
|
|
|
|
self.assertIn(connectedNetworkName, networkListToMatch)
|
|
|
|
# remove the network we just matched from the list
|
|
|
|
networkListToMatch.remove(connectedNetworkName)
|
|
|
|
|
|
|
|
# wait 2 seconds before initiating disconnect
|
|
|
|
time.sleep(2)
|
|
|
|
# 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]
|
|
|
|
|
|
|
|
deviceIdToDisconnect = utility.getCurrentlyConnectedDevice()
|
|
|
|
logger.info("Disconnecting from: %s", deviceIdToDisconnect)
|
|
|
|
self.assertEqual(deviceIdToDisconnect, deviceIdOfConnectedNetwork)
|
|
|
|
utility.disconnect(deviceIdToDisconnect, mainloop, bus)
|
|
|
|
#terminate proc
|
|
|
|
proc.terminate()
|
|
|
|
return connectedNetworkName
|
|
|
|
|
|
|
|
def doConnectDisconnectTwoNetworks(self):
|
2016-06-01 20:23:47 +02:00
|
|
|
objectList = utility.getObjectList(bus)
|
|
|
|
networkToConnect = utility.getNetworkToConnectTo(objectList)
|
2016-05-19 01:28:10 +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 == ""):
|
|
|
|
time.sleep(2)
|
|
|
|
logger.debug("RESTART PROGRAM")
|
|
|
|
os.execl(sys.executable, sys.executable, * sys.argv)
|
|
|
|
|
|
|
|
self.assertNotEqual(networkToConnect, "")
|
|
|
|
# connect to 1st network
|
|
|
|
connectedNetworkName = self.connectToNetwork(networkToConnect)
|
|
|
|
|
|
|
|
# connect to the 2nd network
|
2016-06-01 20:23:47 +02:00
|
|
|
secondNetworkToConnect = getSecondNetworkToConnect(objectList,
|
|
|
|
connectedNetworkName)
|
|
|
|
connectedNetwork = self.connectToNetwork(secondNetworkToConnect)
|
2016-05-19 01:28:10 +02:00
|
|
|
|
|
|
|
def test_twoNetworks(self):
|
|
|
|
logger.info(sys._getframe().f_code.co_name)
|
|
|
|
while (True):
|
2016-06-04 01:13:46 +02:00
|
|
|
if bus.name_has_owner(utility.IWD_SERVICE) == True:
|
2016-05-19 01:28:10 +02:00
|
|
|
break
|
|
|
|
self.doConnectDisconnectTwoNetworks()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
|
|
|
global logger, bus, mainloop, networkListToMatch
|
|
|
|
utility.initLogger()
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
mainloop = GLib.MainLoop()
|
|
|
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
|
|
|
bus = dbus.SystemBus()
|
|
|
|
# we should connect to these networks - in any order
|
|
|
|
networkListToMatch = ["IntelWIFI", "PersonalWIFI"]
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDown(self):
|
|
|
|
mainloop.quit()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main(exit=True)
|