-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathtest_routing.py
231 lines (161 loc) · 7.55 KB
/
test_routing.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
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
import time
import pytest
import sh
import lib
from lib import allowlist, daemon, firewall, info, logging, login, network, settings
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()
SUBNET_1 = "2.2.2.2"
SUBNET_2 = "3.3.3.3"
SUBNET_3 = "4.4.4.4"
MSG_ROUTING_OFF = "Routing is set to 'disabled' successfully."
MSG_ROUTING_ON = "Routing is set to 'enabled' successfully."
MSG_ROUTING_OFF_ALREADY = "Routing is already set to 'disabled'."
MSG_ROUTING_ON_ALREADY = "Routing is already set to 'enabled'."
MSG_ROUTING_USED_BY_MESH = "Routing is currently used by Meshnet. Disable it first."
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_routing_enabled_connect(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
allowlist.add_subnet_to_allowlist([f"{SUBNET_1}/32", f"{SUBNET_2}/32", f"{SUBNET_3}/32"])
print(sh.nordvpn.connect())
assert network.is_available()
assert "fwmark" in sh.ip.rule.show.table(firewall.IP_ROUTE_TABLE)
policy_rules = sh.ip.rule.show()
assert SUBNET_1 in policy_rules
assert SUBNET_2 in policy_rules
assert SUBNET_3 in policy_rules
policy_routes = sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
network_interface = "nordtun" if tech == "openvpn" else "nordlynx"
assert network_interface in policy_routes
assert settings.is_routing_enabled()
@pytest.mark.skip("LVPN-3273; LVPN-1574")
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_routing_disabled_connect(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
allowlist.add_subnet_to_allowlist([f"{SUBNET_1}/32"])
assert MSG_ROUTING_OFF in sh.nordvpn.set.routing.off()
assert not settings.is_routing_enabled()
print(sh.nordvpn.connect())
assert network.is_not_available()
assert "fwmark" not in sh.ip.rule.show.table(firewall.IP_ROUTE_TABLE)
assert SUBNET_1 not in sh.ip.route()
network_interface = "nordtun" if tech == "openvpn" else "nordlynx"
assert network_interface not in sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
assert MSG_ROUTING_ON_ALREADY in sh.nordvpn.set.routing.on()
assert settings.is_routing_enabled()
@pytest.mark.skip("LVPN-3273")
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_connected_routing_disable_enable(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
network_interface = "nordtun" if tech == "openvpn" else "nordlynx"
print(sh.nordvpn.connect())
assert network.is_available()
assert MSG_ROUTING_OFF in sh.nordvpn.set.routing.off()
assert not settings.is_routing_enabled()
assert network_interface not in sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
assert "mark" not in sh.ip.rule()
assert network.is_not_available()
assert MSG_ROUTING_ON in sh.nordvpn.set.routing.on()
assert settings.is_routing_enabled()
assert network_interface in sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
assert "mark" in sh.ip.rule()
assert network.is_available()
@pytest.mark.skip("LVPN-3273; LVPN-1574")
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_connected_routing_enable_disable(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
network_interface = "nordtun" if tech == "openvpn" else "nordlynx"
assert MSG_ROUTING_OFF in sh.nordvpn.set.routing.off()
assert not settings.is_routing_enabled()
print(sh.nordvpn.connect())
assert network.is_not_available()
assert MSG_ROUTING_ON in sh.nordvpn.set.routing.on()
assert settings.is_routing_enabled()
assert network_interface in sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
assert "mark" in sh.ip.rule()
assert network.is_available()
assert MSG_ROUTING_OFF in sh.nordvpn.set.routing.off()
assert not settings.is_routing_enabled()
assert network_interface not in sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
assert "mark" not in sh.ip.rule()
assert network.is_not_available()
@pytest.mark.skip("LVPN-4360")
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES_BASIC1)
def test_meshnet_on_routing_disable(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
sh.nordvpn.set.mesh.on()
assert MSG_ROUTING_USED_BY_MESH in sh.nordvpn.set.routing.off()
assert settings.is_routing_enabled()
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_routing_already_enabled(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
lib.set_routing("on")
assert MSG_ROUTING_ON_ALREADY in sh.nordvpn.set.routing.on()
assert settings.is_routing_enabled()
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_routing_already_disabled(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
lib.set_routing("off")
assert MSG_ROUTING_OFF_ALREADY in sh.nordvpn.set.routing.off()
assert not settings.is_routing_enabled()
@pytest.mark.skip("LVPN-3273")
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_toggle_routing_in_the_middle_of_the_connection(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
network_interface = "nordtun" if tech == "openvpn" else "nordlynx"
print(sh.nordvpn.connect())
routes = sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
rules = sh.ip.rule()
assert network_interface in routes
assert "mark" in rules
assert network.is_available()
lib.set_routing("off")
routes = sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
rules = sh.ip.rule()
assert network_interface not in routes
assert "mark" not in rules
assert network.is_not_available()
lib.set_routing("on")
routes = sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
rules = sh.ip.rule()
assert network_interface in routes
assert "mark" in rules
assert network.is_available()
@pytest.mark.parametrize(("tech", "proto", "obfuscated"), lib.TECHNOLOGIES)
def test_routing_when_iprule_already_exists(tech, proto, obfuscated):
lib.set_technology_and_protocol(tech, proto, obfuscated)
network_interface = "nordtun" if tech == "openvpn" else "nordlynx"
print(sh.nordvpn.connect())
routes = sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
rules = sh.ip.rule()
assert f"default dev {network_interface}" in routes
assert "mark" in rules
assert network.is_available()
rule = []
for line in rules:
if "fwmark" in line:
rule = line.split()[1:]
print(sh.nordvpn.disconnect())
daemon.stop()
with lib.Defer(lambda: sh.sudo.ip.rule('del', *rule, _ok_code=(0, 2))):
sh.sudo.ip.rule.add(*rule)
daemon.start()
time.sleep(5)
print(sh.nordvpn.connect())
routes = sh.ip.route.show.table(firewall.IP_ROUTE_TABLE)
rules = sh.ip.rule()
assert f"default dev {network_interface}" in routes
assert "mark" in rules
assert network.is_available()
routes = sh.ip.route.show.table("main")
assert f"default dev {network_interface}" not in routes