File: test_darwin.py

package info (click to toggle)
vorta 0.11.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,532 kB
  • sloc: python: 12,262; makefile: 89; xml: 65; sh: 51
file content (206 lines) | stat: -rw-r--r-- 5,349 bytes parent folder | download
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
import sys
from unittest.mock import MagicMock

import pytest

pytestmark = pytest.mark.skipif(sys.platform != 'darwin', reason="macOS only")

from vorta.network_status import darwin  # noqa: E402


def test_get_current_wifi_when_wifi_is_on(mocker):
    mock_interface = MagicMock()
    mock_network = MagicMock()
    mock_interface.lastNetworkJoined.return_value = mock_network
    mock_network.ssid.return_value = "Coffee Shop Wifi"

    instance = darwin.DarwinNetworkStatus()
    mocker.patch.object(instance, "_get_wifi_interface", return_value=mock_interface)

    result = instance.get_current_wifi()

    assert result == "Coffee Shop Wifi"


def test_get_current_wifi_when_wifi_is_off(mocker):
    mock_interface = MagicMock()
    mock_interface.lastNetworkJoined.return_value = None

    instance = darwin.DarwinNetworkStatus()
    mocker.patch.object(instance, "_get_wifi_interface", return_value=mock_interface)

    result = instance.get_current_wifi()

    assert result is None


def test_get_current_wifi_when_no_wifi_interface(mocker):
    instance = darwin.DarwinNetworkStatus()
    mocker.patch.object(instance, "_get_wifi_interface", return_value=None)

    result = instance.get_current_wifi()

    assert result is None


@pytest.mark.parametrize("is_hotspot_enabled", [True, False])
def test_network_is_metered_with_ios(mocker, is_hotspot_enabled):
    mock_interface = MagicMock()
    mock_network = MagicMock()
    mock_interface.lastNetworkJoined.return_value = mock_network
    mock_network.isPersonalHotspot.return_value = is_hotspot_enabled

    instance = darwin.DarwinNetworkStatus()
    mocker.patch.object(instance, "_get_wifi_interface", return_value=mock_interface)

    result = instance.is_network_metered()

    assert result == is_hotspot_enabled


def test_network_is_metered_when_wifi_is_off(mocker):
    mock_interface = MagicMock()
    mock_interface.lastNetworkJoined.return_value = None

    instance = darwin.DarwinNetworkStatus()
    mocker.patch.object(instance, "_get_wifi_interface", return_value=mock_interface)

    result = instance.is_network_metered()

    assert result is False


@pytest.mark.parametrize(
    'getpacket_output_name, expected',
    [
        ('normal_router', False),
        ('android_phone', True),
    ],
)
def test_is_network_metered_with_android(getpacket_output_name, expected, monkeypatch):
    def mock_getpacket(device):
        assert device == 'en0'
        return GETPACKET_OUTPUTS[getpacket_output_name]

    monkeypatch.setattr(darwin, 'call_ipconfig_getpacket', mock_getpacket)

    result = darwin.is_network_metered_with_android('en0')
    assert result == expected


def test_get_known_wifi_networks_when_wifi_interface_exists(monkeypatch):
    networksetup_output = """
Preferred networks on en0:
    Home Network
    Coffee Shop Wifi
    iPhone

    Office Wifi
    """
    monkeypatch.setattr(
        darwin, "call_networksetup_listpreferredwirelessnetworks", lambda interface_name: networksetup_output
    )

    network_status = darwin.DarwinNetworkStatus()
    result = network_status.get_known_wifis()

    assert len(result) == 4
    assert result[0].ssid == "Home Network"


def test_get_known_wifi_networks_when_no_wifi_interface(mocker):
    instance = darwin.DarwinNetworkStatus()
    mocker.patch.object(instance, "_get_wifi_interface", return_value=None)

    results = instance.get_known_wifis()

    assert results == []


def test_get_network_devices(monkeypatch):
    monkeypatch.setattr(darwin, 'call_networksetup_listallhardwareports', lambda: NETWORKSETUP_OUTPUT)

    result = list(darwin.get_network_devices())
    assert result == ['Bluetooth-Modem', 'en0', 'en1', 'en2', 'bridge0']


GETPACKET_OUTPUTS = {
    'normal_router': b"""\
op = BOOTREPLY
htype = 1
flags = 0
hlen = 6
hops = 0
xid = 0x8dc8db4d
secs = 0
ciaddr = 0.0.0.0
yiaddr = 172.16.13.237
siaddr = 0.0.0.0
giaddr = 0.0.0.0
chaddr = 8c:85:90:ad:ee:a3
sname =
file =
options:
Options count is 9
dhcp_message_type (uint8): ACK 0x5
subnet_mask (ip): 255.255.252.0
router (ip_mult): {172.16.12.1}
domain_name_server (ip_mult): {172.16.12.1, 8.8.8.8}
domain_name (string): .
lease_time (uint32): 0xe10
interface_mtu (uint16): 0x5dc
server_identifier (ip): 172.16.12.1
end (none):
""",
    'android_phone': b"""\
op = BOOTREPLY
htype = 1
flags = 0
hlen = 6
hops = 0
xid = 0x8dc8db4e
secs = 0
ciaddr = 0.0.0.0
yiaddr = 192.168.43.223
siaddr = 192.168.43.242
giaddr = 0.0.0.0
chaddr = 8c:85:90:ad:ee:a3
sname =
file =
options:
Options count is 11
dhcp_message_type (uint8): ACK 0x5
server_identifier (ip): 192.168.43.242
lease_time (uint32): 0xe0f
renewal_t1_time_value (uint32): 0x707
rebinding_t2_time_value (uint32): 0xc4d
subnet_mask (ip): 255.255.255.0
broadcast_address (ip): 192.168.43.255
router (ip_mult): {192.168.43.242}
domain_name_server (ip_mult): {192.168.43.242}
vendor_specific (opaque):
0000  41 4e 44 52 4f 49 44 5f  4d 45 54 45 52 45 44     ANDROID_METERED
""",
}

NETWORKSETUP_OUTPUT = b"""\
Hardware Port: Bluetooth DUN
Device: Bluetooth-Modem
Ethernet Address: N/A

Hardware Port: Wi-Fi
Device: en0
Ethernet Address: d7:02:65:7c:1e:14

Hardware Port: Bluetooth PAN
Device: en1
Ethernet Address: N/A

Hardware Port: Thunderbolt 1
Device: en2
Ethernet Address: bb:e8:c3:25:2b:12

Hardware Port: Thunderbolt Bridge
Device: bridge0
Ethernet Address: N/A
"""