-
Notifications
You must be signed in to change notification settings - Fork 45
/
test_allowlist_subnet.py
194 lines (132 loc) · 6.89 KB
/
test_allowlist_subnet.py
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
import socket
from urllib.parse import urlparse
import pytest
import sh
import lib
from lib import (
allowlist,
daemon,
firewall,
info,
logging,
login,
network,
)
CIDR_32 = "/32"
def setup_module(module): # noqa: ARG001
firewall.add_and_delete_random_route()
def setup_function(function): # noqa: ARG001
daemon.start()
login.login_as("default")
logging.log()
def teardown_function(function): # noqa: ARG001
logging.log(data=info.collect())
logging.log()
sh.nordvpn.logout("--persist-token")
sh.nordvpn.set.defaults()
daemon.stop()
@pytest.mark.parametrize("subnet", lib.SUBNETS)
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_allowlist_does_not_create_new_routes_when_adding_deleting_subnets_disconnected(tech, proto, obfuscated, subnet):
lib.set_technology_and_protocol(tech, proto, obfuscated)
output_before_add = sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
allowlist.add_subnet_to_allowlist([subnet])
assert not firewall.is_active(None, [subnet])
output_after_add = sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
allowlist.remove_subnet_from_allowlist([subnet])
assert not firewall.is_active(None, [subnet])
output_after_delete = sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
assert output_before_add == output_after_add
assert output_after_add == output_after_delete
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_connect_allowlist_subnet(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
my_ip = network.get_external_device_ip()
sh.nordvpn.connect()
assert network.is_connected()
assert my_ip != network.get_external_device_ip()
ip_provider_addresses = socket.gethostbyname_ex(urlparse(lib.API_EXTERNAL_IP).netloc)[2]
ip_addresses_with_subnet = [ip + CIDR_32 for ip in ip_provider_addresses]
allowlist.add_subnet_to_allowlist(ip_addresses_with_subnet)
assert firewall.is_active(None, ip_addresses_with_subnet)
assert my_ip == network.get_external_device_ip()
sh.nordvpn.disconnect()
assert not firewall.is_active(None, ip_addresses_with_subnet)
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_allowlist_subnet_connect(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
my_ip = network.get_external_device_ip()
ip_provider_addresses = socket.gethostbyname_ex(urlparse(lib.API_EXTERNAL_IP).netloc)[2]
ip_addresses_with_subnet = [ip + CIDR_32 for ip in ip_provider_addresses]
allowlist.add_subnet_to_allowlist(ip_addresses_with_subnet)
assert not firewall.is_active(None, ip_addresses_with_subnet)
sh.nordvpn.connect()
assert firewall.is_active(None, ip_addresses_with_subnet)
assert my_ip == network.get_external_device_ip()
sh.nordvpn.disconnect()
assert not firewall.is_active(None, ip_addresses_with_subnet)
@pytest.mark.parametrize("subnet", lib.SUBNETS)
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_allowlist_subnet_twice_disconnected(tech, proto, obfuscated, subnet):
lib.set_technology_and_protocol(tech, proto, obfuscated)
allowlist.add_subnet_to_allowlist([subnet])
with pytest.raises(sh.ErrorReturnCode_1) as ex:
sh.nordvpn(allowlist.get_alias(), "add", "subnet", subnet)
expected_message = allowlist.MSG_ALLOWLIST_SUBNET_ADD_ERROR % subnet
assert expected_message in str(ex)
assert str(sh.nordvpn.settings()).count(subnet) == 1
assert not firewall.is_active(None, [subnet])
@pytest.mark.parametrize("subnet", lib.SUBNETS)
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_allowlist_subnet_twice_connected(tech, proto, obfuscated, subnet):
lib.set_technology_and_protocol(tech, proto, obfuscated)
sh.nordvpn.connect()
allowlist.add_subnet_to_allowlist([subnet])
with pytest.raises(sh.ErrorReturnCode_1) as ex:
sh.nordvpn(allowlist.get_alias(), "add", "subnet", subnet)
expected_message = allowlist.MSG_ALLOWLIST_SUBNET_ADD_ERROR % subnet
assert expected_message in str(ex)
assert str(sh.nordvpn.settings()).count(subnet) == 1
assert firewall.is_active(None, [subnet])
sh.nordvpn.disconnect()
assert not firewall.is_active(None, [subnet])
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_allowlist_subnet_and_remove_disconnected(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
ip_provider_addresses = socket.gethostbyname_ex(urlparse(lib.API_EXTERNAL_IP).netloc)[2]
ip_addresses_with_subnet = [ip + CIDR_32 for ip in ip_provider_addresses]
allowlist.add_subnet_to_allowlist(ip_addresses_with_subnet)
assert not firewall.is_active(None, ip_addresses_with_subnet)
allowlist.remove_subnet_from_allowlist(ip_addresses_with_subnet)
assert not firewall.is_active(None, ip_addresses_with_subnet)
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_allowlist_subnet_and_remove_connected(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
my_ip = network.get_external_device_ip()
sh.nordvpn.connect()
assert my_ip != network.get_external_device_ip()
ip_provider_addresses = socket.gethostbyname_ex(urlparse(lib.API_EXTERNAL_IP).netloc)[2]
ip_addresses_with_subnet = [ip + CIDR_32 for ip in ip_provider_addresses]
allowlist.add_subnet_to_allowlist(ip_addresses_with_subnet)
assert firewall.is_active(None, ip_addresses_with_subnet)
assert my_ip == network.get_external_device_ip()
allowlist.remove_subnet_from_allowlist(ip_addresses_with_subnet)
assert firewall.is_active() and not firewall.is_active(None, ip_addresses_with_subnet)
assert my_ip != network.get_external_device_ip()
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
@pytest.mark.parametrize("subnet", lib.SUBNETS)
def test_allowlist_subnet_remove_nonexistent_disconnected(tech, proto, obfuscated, subnet):
lib.set_technology_and_protocol(tech, proto, obfuscated)
with pytest.raises(sh.ErrorReturnCode_1) as ex:
sh.nordvpn(allowlist.get_alias(), "remove", "subnet", subnet)
expected_message = allowlist.MSG_ALLOWLIST_SUBNET_REMOVE_ERROR % subnet
assert expected_message in str(ex)
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
@pytest.mark.parametrize("subnet", lib.SUBNETS)
def test_allowlist_subnet_remove_nonexistent_connected(tech, proto, obfuscated, subnet):
lib.set_technology_and_protocol(tech, proto, obfuscated)
sh.nordvpn.connect()
with pytest.raises(sh.ErrorReturnCode_1) as ex:
sh.nordvpn(allowlist.get_alias(), "remove", "subnet", subnet)
expected_message = allowlist.MSG_ALLOWLIST_SUBNET_REMOVE_ERROR % subnet
assert expected_message in str(ex)