/usr/lib/python2.7/dist-packages/ubuntu-sso-client/ubuntu_sso/networkstate/tests/test_linux.py is in python-ubuntu-sso-client.tests 13.10-0ubuntu11.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | # -*- coding: utf-8 -*-
#
# Copyright 2010-2012 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the
# OpenSSL library under certain conditions as described in each
# individual source file, and distribute linked combinations
# including the two.
# You must obey the GNU General Public License in all respects
# for all of the code used other than OpenSSL. If you modify
# file(s) with this exception, you may extend this exception to your
# version of the file(s), but you are not obligated to do so. If you
# do not wish to do so, delete this exception statement from your
# version. If you delete this exception statement from all source
# files in the program, then also delete it here.
"""Tests for the network state detection code."""
from collections import defaultdict
from twisted.internet.defer import inlineCallbacks
from mocker import ARGS, KWARGS, ANY, MockerTestCase
from ubuntu_sso.tests import TestCase
from ubuntu_sso.networkstate import (
linux,
NetworkFailException,
NetworkManagerState,
)
from ubuntu_sso.networkstate.networkstates import (
ONLINE, OFFLINE, UNKNOWN,
NM_STATE_ASLEEP,
NM_STATE_ASLEEP_OLD,
NM_STATE_CONNECTING,
NM_STATE_CONNECTING_OLD,
NM_STATE_CONNECTED_OLD,
NM_STATE_CONNECTED_LOCAL,
NM_STATE_CONNECTED_SITE,
NM_STATE_CONNECTED_GLOBAL,
NM_STATE_DISCONNECTED,
NM_STATE_DISCONNECTED_OLD,
NM_STATE_UNKNOWN,
)
from ubuntu_sso.networkstate.linux import (
is_machine_connected,
NM_DBUS_INTERFACE,
NM_DBUS_OBJECTPATH,
)
class TestException(Exception):
"""An exception to test error conditions."""
def get_dbus_name(self):
"""A fake dbus name for this exception."""
return "Test Exception Message"
class FakeNetworkManagerState(object):
"""Fake Network Manager State."""
connection_state = None
def __init__(self, function):
"""Initialize Fake class."""
self.call_function = function
def find_online_state(self):
"""Fake find_online_state for linux module."""
self.call_function(self.connection_state)
class FakeDBusMatch(object):
"""Fake a DBus match."""
def __init__(self, name, callback, interface):
self.name = name
self.callback = callback
self.interface = interface
self.removed = False
def remove(self):
"""Stop calling the handler function on remove."""
self.removed = True
class FakeDBusInterface(object):
"""Fake DBus Interface."""
def __init__(self):
self._signals = defaultdict(list)
# pylint: disable=C0103
def Get(self, *args, **kwargs):
"""Fake Get."""
# pylint: enable=C0103
def connect_to_signal(self, signal_name, handler_function, dbus_interface):
"""Fake connect_to_signal."""
match = FakeDBusMatch(signal_name, handler_function, dbus_interface)
self._signals[signal_name].append(match)
return match
def emit_signal(self, signal_name, state):
"""Emit signal for network state change."""
for match in self._signals[signal_name]:
match.callback(state)
class FakeSystemBus(object):
"""Fake SystemBus."""
objects = {(NM_DBUS_INTERFACE, NM_DBUS_OBJECTPATH, True): object()}
def get_object(self, interface, object_path, follow_name_owner_changes):
"""Fake get_object."""
key = (interface, object_path, follow_name_owner_changes)
return self.objects[key]
class TestConnection(TestCase):
"""Test the state of the connection.
This TestCase tests over all the connection states possible.
"""
@inlineCallbacks
def setUp(self):
"""Setup the mocker dbus object tree."""
yield super(TestConnection, self).setUp()
self.patch(linux, "NetworkManagerState", FakeNetworkManagerState)
self.patch(linux.dbus, 'SystemBus', FakeSystemBus)
self.nm_interface = FakeDBusInterface()
self.patch(linux.dbus, 'Interface', lambda *a: self.nm_interface)
self.network_changes = []
def _listen_network_changes(self, state):
"""Fake callback function."""
self.network_changes.append(state)
def test_network_state_change(self):
"""Test the changes in the network connection."""
nms = NetworkManagerState(self._listen_network_changes)
nms.find_online_state()
self.nm_interface.emit_signal('StateChanged',
NM_STATE_CONNECTED_GLOBAL)
self.nm_interface.emit_signal('StateChanged', NM_STATE_DISCONNECTED)
self.nm_interface.emit_signal('StateChanged',
NM_STATE_CONNECTED_GLOBAL)
self.assertEqual(nms.state_signal.name, "StateChanged")
self.assertEqual(nms.state_signal.callback, nms.state_changed)
self.assertEqual(nms.state_signal.interface,
"org.freedesktop.NetworkManager")
self.assertEqual(self.network_changes,
[ONLINE, ONLINE, OFFLINE, ONLINE])
self.assertFalse(nms.state_signal.removed)
@inlineCallbacks
def test_is_machine_connected_nm_state_online(self):
"""Callback given ONLINE should mean we are online"""
self.patch(FakeNetworkManagerState, "connection_state",
ONLINE)
d = yield is_machine_connected()
self.assertTrue(d)
@inlineCallbacks
def test_is_machine_connected_nm_state_offline(self):
"""Callback given OFFLINE should mean we are offline"""
self.patch(FakeNetworkManagerState, "connection_state",
OFFLINE)
d = yield is_machine_connected()
self.assertFalse(d)
@inlineCallbacks
def test_is_machine_connected_nm_state_unknown(self):
"""Callback given ONLINE should mean we are not online"""
self.patch(FakeNetworkManagerState, "connection_state",
UNKNOWN)
d = yield is_machine_connected()
self.assertFalse(d)
@inlineCallbacks
def test_is_machine_connected_callback_error(self):
"""Test bad argument to is_machine_connected's internal callback.
Passing anything other than ONLINE/OFFLINE/UNKNOWN should
cause an exception.
"""
self.patch(FakeNetworkManagerState, "connection_state",
NM_STATE_CONNECTED_GLOBAL)
yield self.assertFailure(is_machine_connected(), NetworkFailException)
class NetworkManagerBaseTestCase(MockerTestCase):
"""Base test case for NM state tests."""
def setUp(self):
"""Setup the dbus object tree."""
super(NetworkManagerBaseTestCase, self).setUp()
self.dbusmock = self.mocker.mock()
self.dbusmock.SystemBus()
sysbusmock = self.mocker.mock()
self.mocker.result(sysbusmock)
sysbusmock.get_object(ARGS, KWARGS)
def connect_proxy(self, exc=None):
"""Get a proxy mock object, and allow failing with specified exc."""
proxymock = self.mocker.mock()
self.mocker.result(proxymock)
self.dbusmock.Interface(proxymock, ANY)
ifmock = self.mocker.mock()
self.mocker.result(ifmock)
ifmock.connect_to_signal(ARGS, KWARGS)
signalmock = self.mocker.mock()
self.mocker.result(signalmock)
proxymock.Get(ARGS, KWARGS)
if exc is not None:
self.mocker.result(exc)
self.mocker.replay()
# Invalid name "assert[A-Z].*"
# pylint: disable=C0103
def assertOnline(self, state):
"""Check that the state given is ONLINE."""
self.assertEquals(state, ONLINE)
def assertOffline(self, state):
"""Check that the state given is OFFLINE."""
self.assertEquals(state, OFFLINE)
def assertUnknown(self, state):
"""Check that the state was UNKNOWN."""
self.assertEquals(state, UNKNOWN)
# pylint: enable=C0103
def get_nms(self, callback):
"""Get the NetworkManagerState object."""
nms = NetworkManagerState(callback, self.dbusmock)
nms.find_online_state()
return nms
def check_nm_error(self, callback, error):
"""Check that the error handling is correct."""
nms = self.get_nms(callback)
nms.got_error(error)
def check_nm_state(self, callback, state):
"""Check the state handling is correct."""
nms = self.get_nms(callback)
nms.got_state(state)
def check_nm_state_change(self, callback, fmstate, tostate):
"""Check the state change handling is correct."""
nms = self.get_nms(callback)
nms.got_state(fmstate)
nms.state_changed(tostate)
class NetworkManagerStateTestCase(NetworkManagerBaseTestCase):
"""Test NetworkManager state retrieval code."""
def setUp(self):
"""Setup the dbus object tree."""
super(NetworkManagerStateTestCase, self).setUp()
self.connect_proxy()
def test_nm_asleep(self):
"""Asleep status should mean offline."""
self.check_nm_state(self.assertOffline, NM_STATE_ASLEEP)
def test_nm_asleep_old(self):
"""Asleep, old status, should mean offline."""
self.check_nm_state(self.assertOffline, NM_STATE_ASLEEP_OLD)
def test_nm_unknown(self):
"""Unknown status should be treated the same as OFFLINE."""
self.check_nm_state(self.assertOffline, NM_STATE_UNKNOWN)
def test_nm_online_old(self):
"""Check the connected, old status, case."""
self.check_nm_state(self.assertOnline, NM_STATE_CONNECTED_OLD)
def test_nm_offline_local(self):
"""Check the connected, local status, case."""
self.check_nm_state(self.assertOffline, NM_STATE_CONNECTED_LOCAL)
def test_nm_offline_site(self):
"""Check the connected, site status, case."""
self.check_nm_state(self.assertOffline, NM_STATE_CONNECTED_SITE)
def test_nm_online_global(self):
"""Check the connected, global status, case."""
self.check_nm_state(self.assertOnline, NM_STATE_CONNECTED_GLOBAL)
def test_nm_offline_old(self):
"""Check the disconnected, old status, case."""
self.check_nm_state(self.assertOffline, NM_STATE_DISCONNECTED_OLD)
def test_nm_offline(self):
"""Check the disconnected case."""
self.check_nm_state(self.assertOffline, NM_STATE_DISCONNECTED)
def test_nm_connecting_then_online_old(self):
"""Check the waiting for connection, old status, case."""
self.check_nm_state_change(self.assertOnline,
NM_STATE_CONNECTING_OLD,
NM_STATE_CONNECTED_OLD)
def test_nm_connecting_then_online(self):
"""Check the waiting for connection case."""
self.check_nm_state_change(self.assertOnline,
NM_STATE_CONNECTING,
NM_STATE_CONNECTED_GLOBAL)
def test_nm_connecting_then_offline_old(self):
"""Check the waiting but fail, old status, case."""
self.check_nm_state_change(self.assertOffline,
NM_STATE_CONNECTING_OLD,
NM_STATE_DISCONNECTED_OLD)
def test_nm_connecting_then_offline(self):
"""Check the waiting but fail case."""
self.check_nm_state_change(self.assertOffline,
NM_STATE_CONNECTING, NM_STATE_DISCONNECTED)
class NetworkManagerStateErrorsTestCase(NetworkManagerBaseTestCase):
"""Test NetworkManager state retrieval code."""
def mock_except_while_getting_proxy(self, exc):
"""Simulate an exception while getting the DBus proxy object."""
self.mocker.throw(exc)
self.mocker.result(exc)
self.mocker.replay()
def test_nm_check_errors(self):
"""Trying to reach NM fails with some error."""
self.connect_proxy(Exception)
self.check_nm_error(self.assertOnline, Exception())
def test_dbus_problem(self):
"""Check the case when DBus throws some other exception."""
self.mock_except_while_getting_proxy(TestException)
self.get_nms(self.assertOnline)
|