-
Notifications
You must be signed in to change notification settings - Fork 71
/
test_network.py
387 lines (340 loc) · 10.4 KB
/
test_network.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import unittest
from netjsonconfig import OpenWrt
from netjsonconfig.exceptions import ValidationError
from netjsonconfig.utils import _TabsMixin
class TestNetwork(unittest.TestCase, _TabsMixin):
maxDiff = None
_ula_netjson = {"general": {"ula_prefix": "fd8e:f40a:6701::/48"}}
_ula_uci = """package network
config globals 'globals'
option ula_prefix 'fd8e:f40a:6701::/48'
"""
_ula_netjson_id = {
"general": {"ula_prefix": "fd8e:f40a:6701::/48", "globals_id": "arbitrary_id"}
}
_ula_uci_id = """package network
config globals 'arbitrary_id'
option ula_prefix 'fd8e:f40a:6701::/48'
"""
def test_render_ula_prefix(self):
o = OpenWrt(self._ula_netjson)
expected = self._tabs(self._ula_uci)
self.assertEqual(o.render(), expected)
def test_parse_ula_prefix(self):
o = OpenWrt(native=self._ula_uci)
self.assertEqual(o.config, self._ula_netjson)
def test_render_ula_prefix_id(self):
o = OpenWrt(self._ula_netjson_id)
expected = self._tabs(self._ula_uci_id)
self.assertEqual(o.render(), expected)
def test_parse_ula_prefix_id(self):
o = OpenWrt(native=self._ula_uci_id)
self.assertEqual(o.config, self._ula_netjson_id)
_routes_netjson = {
"routes": [
{
"device": "eth1",
"destination": "192.168.3.1/24",
"next": "192.168.2.1",
"cost": 0,
},
{
"device": "eth1",
"destination": "192.168.4.1/24",
"next": "192.168.2.2",
"cost": 2,
"source": "192.168.1.10",
"table": "2",
"onlink": True,
"mtu": "1450",
"type": "unicast",
},
{
"name": "arbitrary_name",
"device": "eth1",
"destination": "fd89::1/128",
"next": "fd88::1",
"cost": 0,
},
{
"device": "eth1",
"destination": "fd90::1/128",
"next": "fd88::2",
"cost": 3,
"source": "fd87::10",
},
]
}
_routes_uci = """package network
config route 'route1'
option gateway '192.168.2.1'
option interface 'eth1'
option metric '0'
option netmask '255.255.255.0'
option target '192.168.3.1'
config route 'route2'
option gateway '192.168.2.2'
option interface 'eth1'
option metric '2'
option mtu '1450'
option netmask '255.255.255.0'
option onlink '1'
option source '192.168.1.10'
option table '2'
option target '192.168.4.1'
option type 'unicast'
config route6 'arbitrary_name'
option gateway 'fd88::1'
option interface 'eth1'
option metric '0'
option target 'fd89::1/128'
config route6 'route4'
option gateway 'fd88::2'
option interface 'eth1'
option metric '3'
option source 'fd87::10'
option target 'fd90::1/128'
"""
def test_render_routes(self):
o = OpenWrt(self._routes_netjson)
expected = self._tabs(self._routes_uci)
self.assertEqual(o.render(), expected)
def test_parse_routes(self):
o = OpenWrt(native=self._routes_uci)
self.assertEqual(o.config, self._routes_netjson)
_routes_nogw_netjson = {
"routes": [
{"device": "wan", "destination": "192.168.100.1/32", "next": "", "cost": 0}
]
}
_routes_nogw_uci = """package network
config route 'route1'
option interface 'wan'
option metric '0'
option netmask '255.255.255.255'
option target '192.168.100.1'
"""
def test_render_route_nogw(self):
o = OpenWrt(self._routes_nogw_netjson)
expected = self._tabs(self._routes_nogw_uci)
self.assertEqual(o.render(), expected)
def test_parse_routes_nogw(self):
o = OpenWrt(native=self._routes_nogw_uci)
self.assertEqual(o.config, self._routes_nogw_netjson)
with self.subTest('minimalistic route'):
minimal_uci = """package network
config route 'route1'
option interface 'wan'
option target '192.168.100.1'
option netmask '255.255.255.255'
"""
o = OpenWrt(native=minimal_uci)
self.assertEqual(o.config, self._routes_nogw_netjson)
_rules_netjson = {
"ip_rules": [
{
"in": "eth0",
"out": "eth1",
"src": "192.168.1.0/24",
"dest": "192.168.2.0/24",
"tos": 2,
"mark": "0x0/0x1",
"invert": True,
"lookup": "0",
"action": "blackhole",
},
{"src": "192.168.1.0/24", "dest": "192.168.3.0/24", "goto": 0},
{
"name": "arbitrary_name",
"in": "vpn",
"dest": "fdca:1234::/64",
"action": "prohibit",
},
{"in": "vpn", "src": "fdca:1235::/64", "action": "prohibit"},
]
}
_rules_uci = """package network
config rule 'rule1'
option action 'blackhole'
option dest '192.168.2.0/24'
option in 'eth0'
option invert '1'
option lookup '0'
option mark '0x0/0x1'
option out 'eth1'
option src '192.168.1.0/24'
option tos '2'
config rule 'rule2'
option dest '192.168.3.0/24'
option goto '0'
option src '192.168.1.0/24'
config rule6 'arbitrary_name'
option action 'prohibit'
option dest 'fdca:1234::/64'
option in 'vpn'
config rule6 'rule4'
option action 'prohibit'
option in 'vpn'
option src 'fdca:1235::/64'
"""
def test_render_rules(self):
o = OpenWrt(self._rules_netjson)
expected = self._tabs(self._rules_uci)
self.assertEqual(o.render(), expected)
def test_parse_rules(self):
o = OpenWrt(native=self._rules_uci)
self.assertEqual(o.config, self._rules_netjson)
def test_rules_no_src_dest(self):
o = OpenWrt(
{
"ip_rules": [
{
"in": "eth0",
"out": "eth1",
"tos": 2,
"mark": "0x0/0x1",
"invert": True,
"lookup": "0",
"action": "blackhole",
}
]
}
)
expected = self._tabs(
"""package network
config rule 'rule1'
option action 'blackhole'
option in 'eth0'
option invert '1'
option lookup '0'
option mark '0x0/0x1'
option out 'eth1'
option tos '2'
"""
)
self.assertEqual(o.render(), expected)
def test_render_rule_wrong(self):
rule = {
"ip_rules": [
{
"in": "eth0",
"out": "eth1",
"src": "wrong",
"dest": "wrong1",
"tos": 2,
"action": "blackhole",
}
]
}
o = OpenWrt(rule)
with self.assertRaisesRegex(ValidationError, "'wrong' is not a 'cidr'"):
o.validate()
rule['ip_rules'][0]['src'] = '192.168.1.0/24'
o = OpenWrt(rule)
with self.assertRaisesRegexp(ValidationError, "'wrong1' is not a 'cidr'"):
o.validate()
# fix 'dest' and expect no ValidationError raised
rule['ip_rules'][0]['dest'] = '192.168.1.0/24'
o = OpenWrt(rule)
o.validate()
def test_parse_rules_zone(self):
with self.assertRaisesRegexp(ValidationError, "'wrong' is not a 'cidr'"):
OpenWrt(
native="""package network
config rule 'rule1'
option action 'blackhole'
option dest 'wrong'
option in 'eth0'
option out 'eth1'
option src 'wrong'
option tos '2'
"""
)
_switch_netjson = {
"switch": [
{
"name": "switch0",
"reset": True,
"enable_vlan": True,
"vlan": [
{"device": "switch0", "vlan": 1, "ports": "0t 2 3 4 5"},
{
"device": "switch0",
"vlan": 2,
"vid": None, # ``None`` or empty string disable ``vid``
"ports": "0t 1",
},
],
},
{
"id": "s1",
"name": "switch1",
"reset": True,
"enable_vlan": True,
"vlan": [
{
"id": "v1",
"device": "switch1",
"vlan": 3,
"vid": 130,
"ports": "0t 6 7",
}
],
},
]
}
_switch_uci = """package network
config switch 'switch0'
option enable_vlan '1'
option name 'switch0'
option reset '1'
config switch_vlan 'switch0_vlan1'
option device 'switch0'
option ports '0t 2 3 4 5'
option vid '1'
option vlan '1'
config switch_vlan 'switch0_vlan2'
option device 'switch0'
option ports '0t 1'
option vlan '2'
config switch 's1'
option enable_vlan '1'
option name 'switch1'
option reset '1'
config switch_vlan 'v1'
option device 'switch1'
option ports '0t 6 7'
option vid '130'
option vlan '3'
"""
_switch_uci_reorder = """package network
config switch_vlan 'switch0_vlan1'
option device 'switch0'
option ports '0t 2 3 4 5'
option vid '1'
option vlan '1'
config switch 'switch0'
option enable_vlan '1'
option name 'switch0'
option reset '1'
"""
_switch_netjson_reorder = {
"switch": [
{
"name": "switch0",
"reset": True,
"enable_vlan": True,
"vlan": [{"device": "switch0", "vlan": 1, "ports": "0t 2 3 4 5"}],
}
]
}
def test_render_switch(self):
o = OpenWrt(self._switch_netjson)
expected = self._tabs(self._switch_uci)
self.assertEqual(o.render(), expected)
def test_parse_switch(self):
o = OpenWrt(native=self._switch_uci)
self.assertEqual(o.config, self._switch_netjson)
def test_parse_switch_reorder(self):
o = OpenWrt(native=self._switch_uci_reorder)
self.assertEqual(o.config, self._switch_netjson_reorder)