This file is indexed.

/usr/lib/python2.7/dist-packages/ubuntu-kylin-sso-client/ubuntu_kylin_sso/networkstate/tests/test_darwin.py is in python-ubuntu-kylin-sso-client.tests 0.1.2.5.

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
# -*- 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 twisted.internet.defer import inlineCallbacks

from ubuntu_kylin_sso.tests import TestCase
from ubuntu_kylin_sso.networkstate import (
    darwin,
    NetworkFailException,
)

from ubuntu_kylin_sso.networkstate.networkstates import (
    ONLINE, OFFLINE, UNKNOWN,
)

from ubuntu_kylin_sso.networkstate.darwin import (
    is_machine_connected,
    NetworkManagerState
)

from ubuntu_kylin_sso.networkstate.darwin import flags_say_reachable


REACHABLE_FLAG = 1 << 1
CONNECTION_REQUIRED_FLAG = 1 << 2


class TestSCNRFailingInDirect(TestCase):

    """Test that we handle a problem getting status in a direct call
    to check_connected_state (used by is_machine_connected) by
    raising an exception.
    """

    def test_cant_create_target(self):
        """SCNRCreateWithName returning None should cause an exception."""
        self.patch(darwin, "SCNRCreateWithName",
                   lambda _1, _2: None)
        # pylint: disable=W0212
        self.assertRaises(NetworkFailException,
                          darwin.check_connected_state)

    def test_cant_get_flags(self):
        """SCNRGetFlags returning False should cause an exception."""
        self.patch(darwin, "SCNRGetFlags",
                   lambda _1, _2: False)
        # pylint: disable=W0212
        self.assertRaises(NetworkFailException,
                          darwin.check_connected_state)


class TestFailingSCNRInCallbacks(TestCase):

    """Test that we handle a problem getting status in the separate
    listening thread by updating the status to UNKNOWN.
    """

    def expect_unknown(self, state):
        """A convenience callback that fails unless it sees UNKNOWN."""
        self.assertEquals(state, UNKNOWN)

    def test_exc_in_find_online_state(self):
        """Expect UNKNOWN from find_online_state in case of exception."""
        def fake_check_connected_state():
            "fake a broken check_connected_state"
            raise NetworkFailException()

        self.patch(darwin, "check_connected_state",
                   fake_check_connected_state)
        NetworkManagerState(self.expect_unknown)

    def test_cant_create_target(self):
        """SCNRCreateWithName returning None -> callback gets UNKNOWN."""
        self.patch(darwin, "SCNRCreateWithName", lambda _1, _2: None)
        nms = NetworkManagerState(self.expect_unknown)
        # pylint: disable=W0212
        nms._listen_on_separate_thread()

    def test_cant_set_callback(self):
        """SCNRSetCallback returning false -> callback gets UNKNOWN."""
        self.patch(darwin, "SCNRSetCallback", lambda _1, _2, _3: False)
        nms = NetworkManagerState(self.expect_unknown)
        # pylint: disable=W0212
        nms._listen_on_separate_thread()

    def test_cant_schedule_with_runloop(self):
        """SCNRScheduleWithRunLoop returning false -> callback gets UNKNOWN."""
        self.patch(darwin, "SCNRScheduleWithRunLoop",
                   lambda _1, _2, _3: False)
        nms = NetworkManagerState(self.expect_unknown)
        # pylint: disable=W0212
        nms._listen_on_separate_thread()


class TestReadingFlags(TestCase):
    """Test interpretation of flags returned from SCNR API"""

    def test_flag_reachable(self):
        """Reachable by itself is OK."""
        flag = REACHABLE_FLAG
        self.assertTrue(flags_say_reachable(flag))

    def test_flag_reachable_and_flag_connection_required(self):
        """Reachable and connection-required is NOT OK"""
        flag = REACHABLE_FLAG | CONNECTION_REQUIRED_FLAG
        self.assertFalse(flags_say_reachable(flag))

    def test_other_flagvals(self):
        """All other flag configurations are false for our purposes.

        They either indicate an iOS device, which we won't run this
        code on, or that the server we're testing for is on this
        machine or wired directly to it. These cases won't happen.
        """
        for flag in range(0, 17) + [1 << 16, 1 << 17, 1 << 18]:
            # only test cases without the reachable bit set:
            flag = flag & ~ 2
            self.assertEqual(False, flags_say_reachable(flag))


class TestNMSListeningForNWStateChanges(TestCase):
    """
    Test that the NetworkManagerState class calls the callback with
    ONLINE/OFFLINE when the state changes appropriately
    """

    @inlineCallbacks
    def setUp(self):
        """Setup array to hold state changes."""
        yield super(TestNMSListeningForNWStateChanges, self).setUp()
        self.network_changes = []

    def _listen_network_changes(self, state):
        """Fake callback function, records state changes."""
        self.network_changes.append(state)

    def test_network_state_change(self):
        """Test the changes in the network connection."""
        nms = NetworkManagerState(self._listen_network_changes)
        # pylint: disable=W0212
        nms._state_changed(2)
        nms._state_changed(0)            # 0 or anything other than 2.
        nms._state_changed(2)

        self.assertEqual(self.network_changes,
            [ONLINE, OFFLINE, ONLINE])


class TestIsMachineConnectedFunc(TestCase):
    """Simple test of is_machine_connected."""

    @inlineCallbacks
    def test_not_connected_returns_false(self):
        """test that False comes back False"""
        self.patch(darwin, "check_connected_state",
                   lambda: False)
        con = yield is_machine_connected()
        self.assertEqual(con, False)

    @inlineCallbacks
    def test_connected_returns_true(self):
        """check that True comes back True"""
        self.patch(darwin, "check_connected_state",
                   lambda: True)
        con = yield is_machine_connected()
        self.assertEqual(con, True)