diff options
author | Will Thompson <will.thompson@collabora.co.uk> | 2011-09-16 11:33:28 +0100 |
---|---|---|
committer | Will Thompson <will.thompson@collabora.co.uk> | 2011-09-28 11:32:19 +0100 |
commit | 8af66a228e79470cde692d3aeaf43e70168f1b86 (patch) | |
tree | b89c122446c2bdd9fd149ed6c3e869bc9bac28a1 /tests | |
parent | dc460ae72ec2c29f42cf375e7e3366105998d132 (diff) |
tests: add fake implementations of MC and NM
Diffstat (limited to 'tests')
-rw-r--r-- | tests/twisted/Makefile.am | 2 | ||||
-rw-r--r-- | tests/twisted/fakeconnectivity.py | 103 | ||||
-rw-r--r-- | tests/twisted/mctest.py | 16 | ||||
-rw-r--r-- | tests/twisted/tools/exec-with-log.sh.in | 3 |
4 files changed, 119 insertions, 5 deletions
diff --git a/tests/twisted/Makefile.am b/tests/twisted/Makefile.am index 392dbe05..bc3dc09d 100644 --- a/tests/twisted/Makefile.am +++ b/tests/twisted/Makefile.am @@ -184,6 +184,7 @@ endif WITH_SESSION_BUS = \ sh $(srcdir)/tools/with-session-bus.sh \ + --also-for-system \ --config-file=tools/tmp-session-bus.conf -- COMBINED_TESTS_ENVIRONMENT = \ @@ -283,6 +284,7 @@ EXTRA_DIST = \ constants.py \ fakeclient.py \ fakecm.py \ + fakeconnectivity.py \ mctest.py \ servicetest.py diff --git a/tests/twisted/fakeconnectivity.py b/tests/twisted/fakeconnectivity.py new file mode 100644 index 00000000..5228a3aa --- /dev/null +++ b/tests/twisted/fakeconnectivity.py @@ -0,0 +1,103 @@ +import dbus +from dbus.service import Object, method, signal + +import sys + +class FakeConnectivity(object): + NM_BUS_NAME = 'org.freedesktop.NetworkManager' + NM_PATH = '/org/freedesktop/NetworkManager' + NM_INTERFACE = NM_BUS_NAME + + NM_STATE_UNKNOWN = 0 + NM_STATE_ASLEEP = 10 + NM_STATE_DISCONNECTED = 20 + NM_STATE_DISCONNECTING = 30 + NM_STATE_CONNECTING = 40 + NM_STATE_CONNECTED_LOCAL = 50 + NM_STATE_CONNECTED_SITE = 60 + NM_STATE_CONNECTED_GLOBAL = 70 + + CONNMAN_BUS_NAME = 'net.connman' + CONNMAN_PATH = '/' + CONNMAN_INTERFACE = 'net.connman.Manager' + + CONNMAN_OFFLINE = "offline" + CONNMAN_ONLINE = "online" + + def __init__(self, q, bus, initially_online): + self.q = q + self.bus = bus + + self.nm_name_ref = dbus.service.BusName(self.NM_BUS_NAME, bus) + self.connman_name_ref = dbus.service.BusName(self.CONNMAN_BUS_NAME, bus) + + q.add_dbus_method_impl(self.NM_GetPermissions, + path=self.NM_PATH, interface=self.NM_INTERFACE, + method='GetPermissions') + q.add_dbus_method_impl(self.NM_Get, + path=self.NM_PATH, interface=dbus.PROPERTIES_IFACE, method='Get', + predicate=lambda e: e.args[0] == "org.freedesktop.NetworkManager") + + q.add_dbus_method_impl(self.ConnMan_GetState, + path=self.CONNMAN_PATH, interface=self.CONNMAN_INTERFACE, + method='GetState') + + self.change_state(initially_online) + + def NM_GetPermissions(self, e): + permissions = { + self.NM_INTERFACE + '.network-control': 'yes', + self.NM_INTERFACE + '.enable-disable-wwan': 'yes', + self.NM_INTERFACE + '.settings.modify.own': 'yes', + self.NM_INTERFACE + '.wifi.share.protected': 'yes', + self.NM_INTERFACE + '.wifi.share.open': 'yes', + self.NM_INTERFACE + '.enable-disable-network': 'yes', + self.NM_INTERFACE + '.enable-disable-wimax': 'yes', + self.NM_INTERFACE + '.sleep-wake': 'no', + self.NM_INTERFACE + '.enable-disable-wifi': 'yes', + self.NM_INTERFACE + '.settings.modify.system': 'auth', + self.NM_INTERFACE + '.settings.modify.hostname': 'auth', + } + self.q.dbus_return(e.message, permissions, signature='a{ss}') + + def NM_Get(self, e): + props = { + 'NetworkingEnabled': True, + 'WirelessEnabled': True, + 'WirelessHardwareEnabled': True, + 'WwanEnabled': False, + 'WwanHardwareEnabled': True, + 'WimaxEnabled': True, + 'WimaxHardwareEnabled': True, + 'ActiveConnections': dbus.Array([], signature='o'), + 'Version': '0.9.0', + 'State': dbus.UInt32(self.nm_state), + } + + self.q.dbus_return(e.message, props[e.args[1]], signature='v') + + def ConnMan_GetState(self, e): + self.q.dbus_return(e.message, self.connman_state, signature='s') + + def change_state(self, online): + if online: + self.nm_state = self.NM_STATE_CONNECTED_GLOBAL + self.connman_state = self.CONNMAN_ONLINE + else: + self.nm_state = self.NM_STATE_DISCONNECTED + self.connman_state = self.CONNMAN_OFFLINE + + self.q.dbus_emit(self.NM_PATH, self.NM_INTERFACE, + 'PropertiesChanged', { "State": dbus.UInt32(self.nm_state) }, + signature='a{sv}') + self.q.dbus_emit(self.NM_PATH, self.NM_INTERFACE, + 'StateChanged', self.nm_state, + signature='u') + self.q.dbus_emit(self.CONNMAN_PATH, self.CONNMAN_INTERFACE, + 'StateChanged', self.connman_state, signature='s') + + def go_online(self): + self.change_state(True) + + def go_offline(self): + self.change_state(False) diff --git a/tests/twisted/mctest.py b/tests/twisted/mctest.py index e2a5b54e..72d3db66 100644 --- a/tests/twisted/mctest.py +++ b/tests/twisted/mctest.py @@ -33,6 +33,8 @@ from twisted.internet import reactor import dbus import dbus.service +from fakeconnectivity import FakeConnectivity + def install_colourer(): def red(s): return '\x1b[31m%s\x1b[0m' % s @@ -58,7 +60,7 @@ def install_colourer(): return sys.stdout class MC(dbus.proxies.ProxyObject): - def __init__(self, queue, bus, wait_for_names=True): + def __init__(self, queue, bus, wait_for_names=True, initially_online=True): """ Arguments: @@ -67,6 +69,8 @@ class MC(dbus.proxies.ProxyObject): wait_for_names: if True, the constructor will wait for MC to have been service-activated before returning. if False, the caller may later call wait_for_names(). + initially_online: whether the fake implementations of Network Manager + and ConnMan should claim to be online or offline. """ dbus.proxies.ProxyObject.__init__(self, conn=bus, @@ -74,6 +78,7 @@ class MC(dbus.proxies.ProxyObject): object_path=cs.MC_PATH, follow_name_owner_changes=True) + self.connectivity = FakeConnectivity(queue, bus, initially_online) self.q = queue self.bus = bus @@ -99,7 +104,7 @@ class MC(dbus.proxies.ProxyObject): return events[3:] def exec_test_deferred (fun, params, protocol=None, timeout=None, - preload_mc=True): + preload_mc=True, initially_online=True): colourer = None if sys.stdout.isatty(): @@ -116,7 +121,7 @@ def exec_test_deferred (fun, params, protocol=None, timeout=None, if preload_mc: try: - mc = MC(queue, bus) + mc = MC(queue, bus, initially_online=initially_online) except Exception, e: import traceback traceback.print_exc() @@ -179,9 +184,10 @@ def exec_test_deferred (fun, params, protocol=None, timeout=None, if colourer: sys.stdout = colourer.fh -def exec_test(fun, params=None, protocol=None, timeout=None, preload_mc=True): +def exec_test(fun, params=None, protocol=None, timeout=None, + preload_mc=True, initially_online=True): reactor.callWhenRunning (exec_test_deferred, fun, params, protocol, timeout, - preload_mc) + preload_mc, initially_online) reactor.run() class SimulatedConnection(object): diff --git a/tests/twisted/tools/exec-with-log.sh.in b/tests/twisted/tools/exec-with-log.sh.in index 0b40a24b..84b13ea2 100644 --- a/tests/twisted/tools/exec-with-log.sh.in +++ b/tests/twisted/tools/exec-with-log.sh.in @@ -50,6 +50,9 @@ elif test -n "$MISSIONCONTROL_TEST_REFDBG"; then fi fi +# The bus-daemon that is activating us doesn't know it's also the system bus +export DBUS_SYSTEM_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" + if test "z$MC_EXECUTABLE" = z; then MC_EXECUTABLE=@abs_top_builddir@/tests/twisted/mc-debug-server fi |