File: test_vpnconfiguration.py

package info (click to toggle)
python-proton-vpn-api-core 4.16.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,312 kB
  • sloc: python: 11,057; makefile: 9
file content (138 lines) | stat: -rw-r--r-- 4,551 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
"""
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/>.
"""
import os

import pytest

import tempfile

from proton.vpn.connection import VPNServer, ProtocolPorts
from proton.vpn.connection.vpnconfiguration import VPNConfiguration

from .common import (CWD, MockSettings, MockVpnCredentials)
import shutil

VPNCONFIG_DIR = os.path.join(CWD, "vpnconfig")


def setup_module(module):
    if not os.path.isdir(VPNCONFIG_DIR):
        os.makedirs(VPNCONFIG_DIR)


def teardown_module(module):
    if os.path.isdir(VPNCONFIG_DIR):
        shutil.rmtree(VPNCONFIG_DIR)


@pytest.fixture
def modified_exec_env(monkeypatch):
    from proton.utils.environment import ExecutionEnvironment
    with tempfile.TemporaryDirectory() as temp_dir:
        monkeypatch.setattr(ExecutionEnvironment, "path_runtime", temp_dir)
        yield


@pytest.fixture
def vpn_server():
    return VPNServer(
        server_ip="10.10.1.1",
        domain="com.test-domain.www",
        x25519pk="wg_public_key",
        openvpn_ports=ProtocolPorts(tcp=[80, 1194], udp=[445, 5995]),
        wireguard_ports=ProtocolPorts(tcp=[443, 88], udp=[445]),
        server_name="TestServer#10",
        server_id="OYB-3pMQQA2Z2Qnp5s5nIvTVO2...lRjxhx9DCAUM9uXfM2ZUFjzPXw==",
        has_ipv6_support=False,
        label="0"

    )


class MockVpnConfiguration(VPNConfiguration):
    extension = ".test-extension"

    def generate(self):
        return "test-content"


def test_not_implemented_generate(vpn_server):
    cfg = VPNConfiguration(vpn_server, MockVpnCredentials(), MockSettings())
    with pytest.raises(NotImplementedError):
        cfg.generate()


def test_ensure_configuration_file_is_created(modified_exec_env, vpn_server):
    cfg = MockVpnConfiguration(vpn_server, MockVpnCredentials(), MockSettings())
    with cfg as f:
        assert os.path.isfile(f)


def test_ensure_configuration_file_is_deleted(monkeypatch, vpn_server):
    monkeypatch.setattr(
        "proton.utils.environment.ExecutionEnvironment.path_runtime",
        tempfile.mkdtemp()
    )
    cfg = MockVpnConfiguration(vpn_server, MockVpnCredentials(), MockSettings())
    with cfg as f:
        assert os.path.isfile(f)

    assert not os.path.isfile(f)

def test_ensure_generate_is_returning_expected_content(monkeypatch, vpn_server):
    temp_dir = tempfile.mkdtemp()
    monkeypatch.setattr("proton.utils.environment.ExecutionEnvironment.path_runtime", temp_dir)
    cfg = MockVpnConfiguration(vpn_server, MockVpnCredentials(), MockSettings())
    with cfg as f, open(f) as _f:
        assert _f.readline() == MockVpnConfiguration(vpn_server, MockVpnCredentials(), MockSettings()).generate()


def test_ensure_same_configuration_file_in_case_of_duplicate(monkeypatch, vpn_server):
    monkeypatch.setattr(
        "proton.utils.environment.ExecutionEnvironment.path_runtime",
        tempfile.mkdtemp()
    )
    cfg = MockVpnConfiguration(vpn_server, MockVpnCredentials(), MockSettings())
    with cfg as f, cfg as _f:
        assert os.path.isfile(f) and os.path.isfile(_f) and f == _f

@pytest.mark.parametrize(
    "expected_mask, cidr", [
        ("0.0.0.0", "0"),
        ("255.0.0.0", "8"),
        ("255.255.0.0", "16"),
        ("255.255.255.0", "24"),
        ("255.255.255.255", "32")
    ]
)
def test_cidr_to_netmask(cidr, expected_mask, vpn_server):
    cfg = MockVpnConfiguration(vpn_server, MockVpnCredentials(), MockSettings())
    assert cfg.cidr_to_netmask(cidr) == expected_mask


@pytest.mark.parametrize("ipv4", ["192.168.1.1", "109.162.10.9", "1.1.1.1", "10.10.10.10"])
def test_valid_ips(ipv4, vpn_server):
    cfg = MockVpnConfiguration(vpn_server, MockVpnCredentials(), MockSettings())
    cfg.is_valid_ipv4(ipv4)


@pytest.mark.parametrize("ipv4", ["192.168.1.90451", "109.", "1.-.1.1", "1111.10.10.10"])
def test_not_valid_ips(ipv4, vpn_server):
    cfg = MockVpnConfiguration(vpn_server, MockVpnCredentials(), MockSettings())
    cfg.is_valid_ipv4(ipv4)