1
1
# -*- coding: utf-8 -*-
3
# Copyright 2011-2012 Canonical Ltd.
3
# Author: Manuel de la Pena<manuel@canonical.com>
5
# Copyright 2011 Canonical Ltd.
5
7
# This program is free software: you can redistribute it and/or modify it
6
8
# under the terms of the GNU General Public License version 3, as published
14
16
# You should have received a copy of the GNU General Public License along
15
17
# with this program. If not, see <http://www.gnu.org/licenses/>.
17
# In addition, as a special exception, the copyright holders give
18
# permission to link the code of portions of this program with the
19
# OpenSSL library under certain conditions as described in each
20
# individual source file, and distribute linked combinations
22
# You must obey the GNU General Public License in all respects
23
# for all of the code used other than OpenSSL. If you modify
24
# file(s) with this exception, you may extend this exception to your
25
# version of the file(s), but you are not obligated to do so. If you
26
# do not wish to do so, delete this exception statement from your
27
# version. If you delete this exception statement from all source
28
# files in the program, then also delete it here.
29
18
"""Tests for the network manager."""
31
from ctypes import windll
32
19
from mocker import MockerTestCase
33
from twisted.internet.defer import inlineCallbacks
35
from ubuntu_sso.tests import TestCase
36
from ubuntu_sso.networkstate import NetworkFailException
37
20
from ubuntu_sso.networkstate.windows import (
40
22
NetworkManagerState,
93
75
class TestNetworkManagerState(MockerTestCase):
94
"""Test the Network Manager State."""
76
"""Test he Network Manager State."""
97
79
super(TestNetworkManagerState, self).setUp()
120
102
self.mocker.result(False)
122
104
self.mocker.result(self.thread)
123
self.thread.daemon = True
124
105
self.thread.start()
125
106
self.mocker.replay()
126
107
self.state.find_online_state(listener=self.network_manager,
132
113
self.mocker.result(ONLINE)
134
115
self.mocker.result(self.thread)
135
self.thread.daemon = True
136
116
self.thread.start()
137
117
self.mocker.replay()
138
118
self.state.find_online_state(listener=self.network_manager,
139
119
listener_thread=self.thread)
142
class FakeWininet(object):
143
"""Fake wininet for windll."""
145
connection_state = -1
147
# pylint: disable=C0103
148
def InternetGetConnectedState(self, *args, **kwargs):
149
"""Fake InternetGetConnectedState function from wininet."""
150
return self.connection_state
151
# pylint: enable=C0103
154
class FakeWininetException(object):
155
"""Fake wininet for windll."""
157
connection_state = -1
159
# pylint: disable=C0103
160
def InternetGetConnectedState(self, *args, **kwargs):
161
"""Fake InternetGetConnectedState function from wininet."""
163
# pylint: enable=C0103
166
class TestConnection(TestCase):
167
"""Test the state of the connection."""
171
"""Setup the mocker dbus object tree."""
172
yield super(TestConnection, self).setUp()
173
self.patch(windll, "wininet", FakeWininet())
176
def test_is_machine_connected_connected(self):
177
"""Fake the NetworkManagerState."""
178
self.patch(FakeWininet, "connection_state", 1)
179
result = yield is_machine_connected()
180
self.assertTrue(result)
183
def test_is_machine_connected_disconnected(self):
184
"""Fake the NetworkManagerState."""
185
self.patch(FakeWininet, "connection_state", 0)
186
result = yield is_machine_connected()
187
self.assertFalse(result)
190
def test_is_machine_connected_error(self):
191
"""Fake the NetworkManagerState."""
192
self.patch(windll, "wininet", FakeWininetException())
193
yield self.assertFailure(is_machine_connected(), NetworkFailException)