File: test_connection.py

package info (click to toggle)
python-proton-vpn-api-core 0.39.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 892 kB
  • sloc: python: 6,582; makefile: 8
file content (252 lines) | stat: -rw-r--r-- 8,826 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
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
"""
Copyright (c) 2023 Proton AG

This file is part of Proton VPN.

Proton VPN is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Proton VPN is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY 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 ProtonVPN.  If not, see <https://www.gnu.org/licenses/>.
"""
from proton.vpn.core.refresher import VPNDataRefresher
from proton.vpn.session.servers import LogicalServer
from proton.vpn.session.client_config import ClientConfig
from proton.vpn.core.connection import VPNConnector
from proton.vpn.connection import events, exceptions, states
from unittest.mock import Mock, AsyncMock
import pytest


LOGICAL_SERVER_DATA = {
  "Name": "IS#1",
  "ID": "OYB-3pMQQA2Z2Qnp5s5nIvTVO2alU6h82EGLXYHn1mpbsRvE7UfyAHbt0_EilRjxhx9DCAUM9uXfM2ZUFjzPXw==",
  "Status": 1,
  "Servers": [
    {
      "EntryIP": "185.159.158.1",
      "Domain": "node-is-01.protonvpn.net",
      "X25519PublicKey": "yKbYe2XwbeNN9CuPZcwMF/lJp6a62NEGiHCCfpfxrnE=",
      "Status": 1,
    }
  ],
  "Label": "3",
}


def test_get_vpn_server_returns_vpn_server_built_from_logical_server_and_client_config():
    vpn_connector_wrapper = VPNConnector(
      session_holder=None,
      settings_persistence=None,
      usage_reporting=None
    )

    logical_server = LogicalServer(data=LOGICAL_SERVER_DATA)
    client_config = ClientConfig.default()

    vpn_server = vpn_connector_wrapper.get_vpn_server(logical_server, client_config)

    physical_server = logical_server.physical_servers[0]
    assert vpn_server.server_ip == physical_server.entry_ip
    assert vpn_server.domain == physical_server.domain
    assert vpn_server.x25519pk == physical_server.x25519_pk
    assert vpn_server.openvpn_ports.udp == client_config.openvpn_ports.udp
    assert vpn_server.openvpn_ports.tcp == client_config.openvpn_ports.tcp
    assert vpn_server.wireguard_ports.udp == client_config.wireguard_ports.udp
    assert vpn_server.wireguard_ports.tcp == client_config.wireguard_ports.tcp
    assert vpn_server.server_id == logical_server.id
    assert vpn_server.server_name == logical_server.name
    assert vpn_server.label == physical_server.label


@pytest.mark.asyncio
async def test__on_connection_event_swallows_and_does_not_report_policy_errors():
    vpn_connector_wrapper = VPNConnector(
      session_holder=None,
      settings_persistence=None,
      usage_reporting=Mock(),
      state=states.Connected(),
    )

    event = events.Disconnected()
    event.context.error = exceptions.FeaturePolicyError("Policy error")

    await vpn_connector_wrapper._on_connection_event(event)

    vpn_connector_wrapper._usage_reporting.report_error.assert_not_called()


@pytest.mark.asyncio
@pytest.mark.parametrize("error", [
    exceptions.FeatureError("generic feature error"),
    exceptions.FeatureSyntaxError("Feature syntax error")
])
async def test__on_connection_event_reports_feature_syntax_errors_but_no_other_feature_error(error):
    vpn_connector_wrapper = VPNConnector(
        session_holder=None,
        settings_persistence=None,
        usage_reporting=Mock(),
        state=states.Connected(),
    )

    event = events.Disconnected()
    event.context.error = error

    await vpn_connector_wrapper._on_connection_event(event)
    if isinstance(error, exceptions.FeatureSyntaxError):
        vpn_connector_wrapper._usage_reporting.report_error.assert_called_once_with(event.context.error)
    elif isinstance(error, exceptions.FeatureError):
        vpn_connector_wrapper._usage_reporting.report_error.assert_not_called()
    else:
        raise ValueError(f"Unexpected test parameter: {error}")

@pytest.mark.asyncio
async def test__on_connection_event_reports_unexpected_exceptions_and_bubbles_them_up():
    vpn_connector_wrapper = VPNConnector(
        session_holder=None,
        settings_persistence=None,
        usage_reporting=Mock(),
        state=states.Connected(),
    )

    event = events.Disconnected()
    event.context.error = Exception("Unexpected error")

    with pytest.raises(Exception):
        await vpn_connector_wrapper._on_connection_event(event)

    vpn_connector_wrapper._usage_reporting.report_error.assert_called_once_with(event.context.error)


def test_on_state_change_stores_new_device_ip_when_successfully_connected_to_vpn_and_connection_details_and_device_ip_are_set():
    publisher_mock = Mock()
    session_holder_mock = Mock()
    new_connection_details = events.ConnectionDetails(
        device_ip="192.168.0.1",
        device_country="PT",
        server_ipv4="0.0.0.0",
        server_ipv6=None,
    )
    _ = VPNConnector(
        session_holder=session_holder_mock,
        settings_persistence=None,
        usage_reporting=None,
        connection_persistence=Mock(),
        publisher=publisher_mock
    )

    on_state_change_callback = publisher_mock.register.call_args[0][0]

    connected_event = events.Connected(
        context=events.EventContext(
            connection=Mock(),
            connection_details=new_connection_details
        )
    )
    connected_state = states.Connected(context=states.StateContext(connected_event))

    on_state_change_callback(connected_state)

    vpn_location = session_holder_mock.session.set_location.call_args[0][0]

    session_holder_mock.session.set_location.assert_called_once()
    assert vpn_location.IP == new_connection_details.device_ip


def test_on_state_change_skip_store_new_device_ip_when_successfully_connected_to_vpn_and_connection_details_is_none():
    publisher_mock = Mock()
    session_holder_mock = Mock()
    _ = VPNConnector(
        session_holder=session_holder_mock,
        settings_persistence=None,
        usage_reporting=None,
        connection_persistence=Mock(),
        publisher=publisher_mock
    )

    on_state_change_callback = publisher_mock.register.call_args[0][0]

    connected_event = events.Connected(
        context=events.EventContext(
            connection=Mock(),
            connection_details=None
        )
    )
    connected_state = states.Connected(context=states.StateContext(connected_event))

    on_state_change_callback(connected_state)

    session_holder_mock.session.set_location.assert_not_called()


def test_on_state_change_skip_store_new_device_ip_when_successfully_connected_to_vpn_and_device_ip_is_none():
    publisher_mock = Mock()
    session_holder_mock = Mock()
    new_connection_details = events.ConnectionDetails(
        device_ip=None,
        device_country="PT",
        server_ipv4="0.0.0.0",
        server_ipv6=None,
    )
    _ = VPNConnector(
        session_holder=session_holder_mock,
        settings_persistence=None,
        usage_reporting=None,
        connection_persistence=Mock(),
        publisher=publisher_mock
    )

    on_state_change_callback = publisher_mock.register.call_args[0][0]

    connected_event = events.Connected(
        context=events.EventContext(
            connection=Mock(),
            connection_details=new_connection_details
        )
    )
    connected_state = states.Connected(context=states.StateContext(connected_event))

    on_state_change_callback(connected_state)

    session_holder_mock.session.set_location.assert_not_called()


@pytest.mark.asyncio
@pytest.mark.parametrize("state_class, update_credentials_expected", [
    (states.Connected, True),
    (states.Error, True),
    (states.Disconnected, False),
    (states.Connecting, False),
    (states.Disconnecting, False),
])
async def test_connector_updates_connection_credentials_when_certificate_is_refreshed_and_current_state_is_connected_or_error(
        state_class, update_credentials_expected
):
    session_holder = Mock()
    current_state = state_class(states.StateContext(connection=AsyncMock()))

    connector = VPNConnector(
        session_holder=session_holder,
        settings_persistence=Mock(),
        usage_reporting=Mock(),
        connection_persistence=Mock(),
        state=current_state
    )

    refresher = VPNDataRefresher(session_holder=session_holder, scheduler=Mock())
    connector.subscribe_to_certificate_updates(refresher)

    # Trigger certificated updated callback
    await refresher._certificate_refresher.certificate_updated_callback()

    assert current_state.context.connection.update_credentials.called is update_credentials_expected

    if update_credentials_expected:
        current_state.context.connection.update_credentials.assert_called_once_with(session_holder.vpn_credentials)