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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
|
# Copyright (C) 2013 Nippon Telegraph and Telephone Corporation.
# Copyright (C) 2013 Isaku Yamahata <yamahata at private email ne jp>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# vim: tabstop=4 shiftwidth=4 softtabstop=4
from __future__ import print_function
import unittest
import logging
import six
import struct
import inspect
from nose.tools import eq_, ok_
from nose.tools import raises
from ryu.ofproto import inet
from ryu.lib.packet import ipv4
from ryu.lib.packet import ipv6
from ryu.lib.packet import packet
from ryu.lib.packet import packet_utils
from ryu.lib.packet import vrrp
from ryu.lib import addrconv
LOG = logging.getLogger(__name__)
class Test_vrrpv2(unittest.TestCase):
""" Test case for vrrp v2
"""
version = vrrp.VRRP_VERSION_V2
type_ = vrrp.VRRP_TYPE_ADVERTISEMENT
vrid = 128
priority = 100
count_ip = 1
auth_type = vrrp.VRRP_AUTH_NO_AUTH
max_adver_int = 100
checksum = 0
ip_address = '192.168.0.1'
auth_data = (0, 0)
vrrpv2 = vrrp.vrrpv2.create(type_, vrid, priority, max_adver_int,
[ip_address])
buf = struct.pack(vrrp.vrrpv2._PACK_STR + '4sII',
vrrp.vrrp_to_version_type(vrrp.VRRP_VERSION_V2, type_),
vrid, priority, count_ip,
auth_type, max_adver_int, checksum,
addrconv.ipv4.text_to_bin(ip_address),
auth_data[0], auth_data[1])
def setUp(self):
pass
def tearDown(self):
pass
def test_init(self):
eq_(self.type_, self.vrrpv2.type)
eq_(self.vrid, self.vrrpv2.vrid)
eq_(self.priority, self.vrrpv2.priority)
eq_(self.count_ip, self.vrrpv2.count_ip)
eq_(self.auth_type, self.vrrpv2.auth_type)
eq_(1, len(self.vrrpv2.ip_addresses))
eq_(self.ip_address, self.vrrpv2.ip_addresses[0])
eq_(self.auth_data, self.vrrpv2.auth_data)
def test_parser(self):
vrrpv2, _cls, _ = self.vrrpv2.parser(self.buf)
eq_(self.version, vrrpv2.version)
eq_(self.type_, vrrpv2.type)
eq_(self.vrid, vrrpv2.vrid)
eq_(self.priority, vrrpv2.priority)
eq_(self.count_ip, vrrpv2.count_ip)
eq_(self.auth_type, vrrpv2.auth_type)
eq_(self.max_adver_int, vrrpv2.max_adver_int)
eq_(self.checksum, vrrpv2.checksum)
eq_(1, len(vrrpv2.ip_addresses))
eq_(str, type(vrrpv2.ip_addresses[0]))
eq_(self.ip_address, vrrpv2.ip_addresses[0])
eq_(self.auth_data, vrrpv2.auth_data)
def test_serialize(self):
src_ip = '192.168.0.1'
dst_ip = vrrp.VRRP_IPV4_DST_ADDRESS
prev = ipv4.ipv4(4, 5, 0, 0, 0, 0, 0, vrrp.VRRP_IPV4_TTL,
inet.IPPROTO_VRRP, 0, src_ip, dst_ip)
type_ = vrrp.VRRP_TYPE_ADVERTISEMENT
vrid = 5
priority = 10
max_adver_int = 30
ip_address = '192.168.0.2'
ip_addresses = [ip_address]
vrrp_ = vrrp.vrrpv2.create(
type_, vrid, priority, max_adver_int, ip_addresses)
buf = vrrp_.serialize(bytearray(), prev)
pack_str = vrrp.vrrpv2._PACK_STR + '4sII'
pack_len = struct.calcsize(pack_str)
res = struct.unpack(pack_str, six.binary_type(buf))
eq_(res[0], vrrp.vrrp_to_version_type(vrrp.VRRP_VERSION_V2, type_))
eq_(res[1], vrid)
eq_(res[2], priority)
eq_(res[3], len(ip_addresses))
eq_(res[4], vrrp.VRRP_AUTH_NO_AUTH)
eq_(res[5], max_adver_int)
# res[6] is checksum
eq_(res[7], addrconv.ipv4.text_to_bin(ip_address))
eq_(res[8], 0)
eq_(res[9], 0)
eq_(len(buf), pack_len)
# checksum
s = packet_utils.checksum(buf)
eq_(0, s)
@raises(Exception)
def test_malformed_vrrpv2(self):
m_short_buf = self.buf[1:vrrp.vrrpv2._MIN_LEN]
vrrp.vrrp.parser(m_short_buf)
def test_create_packet(self):
primary_ip = '192.168.0.2'
p0 = self.vrrpv2.create_packet(primary_ip)
p0.serialize()
p1 = packet.Packet(six.binary_type(p0.data))
p1.serialize()
eq_(p0.data, p1.data)
def _test_is_valid(self, type_=None, vrid=None, priority=None,
max_adver_int=None):
if type_ is None:
type_ = self.type_
if vrid is None:
vrid = self.vrid
if priority is None:
priority = self.priority
if max_adver_int is None:
max_adver_int = self.max_adver_int
vrrp_ = vrrp.vrrpv2.create(type_, vrid, priority, max_adver_int,
[self.ip_address])
return vrrp_.is_valid()
def test_is_valid_ok(self):
ok_(self._test_is_valid())
def test_is_valid_ng_type(self):
ok_(not self._test_is_valid(type_=15))
def test_is_valid_ng_vrid_min(self):
vrid = vrrp.VRRP_VRID_MIN - 1
ok_(not self._test_is_valid(vrid=vrid))
def test_is_valid_ng_vrid_max(self):
vrid = vrrp.VRRP_VRID_MAX + 1
ok_(not self._test_is_valid(vrid=vrid))
def test_is_valid_ng_priority_min(self):
priority = vrrp.VRRP_PRIORITY_MIN - 1
ok_(not self._test_is_valid(priority=priority))
def test_is_valid_ng_priority_max(self):
priority = vrrp.VRRP_PRIORITY_MAX + 1
ok_(not self._test_is_valid(priority=priority))
def test_is_valid_ng_adver_min(self):
max_adver_int = vrrp.VRRP_V2_MAX_ADVER_INT_MIN - 1
ok_(not self._test_is_valid(max_adver_int=max_adver_int))
def test_is_valid_ng_adver_max(self):
max_adver_int = vrrp.VRRP_V2_MAX_ADVER_INT_MAX + 1
ok_(not self._test_is_valid(max_adver_int=max_adver_int))
def test_to_string(self):
vrrpv2_values = {'version': self.version,
'type': self.type_,
'vrid': self.vrid,
'priority': self.priority,
'count_ip': self.count_ip,
'max_adver_int': self.max_adver_int,
'checksum': self.vrrpv2.checksum,
'ip_addresses': [self.ip_address],
'auth_type': self.auth_type,
'auth_data': self.auth_data,
'identification': self.vrrpv2.identification}
_vrrpv2_str = ','.join(['%s=%s' % (k, repr(vrrpv2_values[k]))
for k, v in inspect.getmembers(self.vrrpv2)
if k in vrrpv2_values])
vrrpv2_str = '%s(%s)' % (vrrp.vrrpv2.__name__, _vrrpv2_str)
eq_(str(self.vrrpv2), vrrpv2_str)
eq_(repr(self.vrrpv2), vrrpv2_str)
class Test_vrrpv3_ipv4(unittest.TestCase):
""" Test case for vrrp v3 IPv4
"""
version = vrrp.VRRP_VERSION_V3
type_ = vrrp.VRRP_TYPE_ADVERTISEMENT
vrid = 128
priority = 99
count_ip = 1
max_adver_int = 111
checksum = 0
ip_address = '192.168.0.1'
vrrpv3 = vrrp.vrrpv3.create(type_, vrid, priority, max_adver_int,
[ip_address])
buf = struct.pack(vrrp.vrrpv3._PACK_STR + '4s',
vrrp.vrrp_to_version_type(vrrp.VRRP_VERSION_V3, type_),
vrid, priority, count_ip,
max_adver_int, checksum,
addrconv.ipv4.text_to_bin(ip_address))
def setUp(self):
pass
def tearDown(self):
pass
def test_init(self):
eq_(self.type_, self.vrrpv3.type)
eq_(self.vrid, self.vrrpv3.vrid)
eq_(self.priority, self.vrrpv3.priority)
eq_(self.count_ip, self.vrrpv3.count_ip)
eq_(1, len(self.vrrpv3.ip_addresses))
eq_(self.ip_address, self.vrrpv3.ip_addresses[0])
def test_parser(self):
vrrpv3, _cls, _ = self.vrrpv3.parser(self.buf)
eq_(self.version, vrrpv3.version)
eq_(self.type_, vrrpv3.type)
eq_(self.vrid, vrrpv3.vrid)
eq_(self.priority, vrrpv3.priority)
eq_(self.count_ip, vrrpv3.count_ip)
eq_(self.max_adver_int, vrrpv3.max_adver_int)
eq_(self.checksum, vrrpv3.checksum)
eq_(1, len(vrrpv3.ip_addresses))
eq_(str, type(vrrpv3.ip_addresses[0]))
eq_(self.ip_address, vrrpv3.ip_addresses[0])
def test_serialize(self):
src_ip = '192.168.0.1'
dst_ip = vrrp.VRRP_IPV4_DST_ADDRESS
prev = ipv4.ipv4(4, 5, 0, 0, 0, 0, 0, vrrp.VRRP_IPV4_TTL,
inet.IPPROTO_VRRP, 0, src_ip, dst_ip)
type_ = vrrp.VRRP_TYPE_ADVERTISEMENT
vrid = 5
priority = 10
max_adver_int = 30
ip_address = '192.168.0.2'
ip_addresses = [ip_address]
vrrp_ = vrrp.vrrpv3.create(
type_, vrid, priority, max_adver_int, ip_addresses)
buf = vrrp_.serialize(bytearray(), prev)
print(len(buf), type(buf), buf)
pack_str = vrrp.vrrpv3._PACK_STR + '4s'
pack_len = struct.calcsize(pack_str)
res = struct.unpack(pack_str, six.binary_type(buf))
eq_(res[0], vrrp.vrrp_to_version_type(vrrp.VRRP_VERSION_V3, type_))
eq_(res[1], vrid)
eq_(res[2], priority)
eq_(res[3], len(ip_addresses))
eq_(res[4], max_adver_int)
# res[5] is checksum
eq_(res[6], addrconv.ipv4.text_to_bin(ip_address))
eq_(len(buf), pack_len)
print(res)
# checksum
ph = struct.pack('!4s4sxBH',
addrconv.ipv4.text_to_bin(src_ip),
addrconv.ipv4.text_to_bin(dst_ip),
inet.IPPROTO_VRRP, pack_len)
s = packet_utils.checksum(ph + buf)
eq_(0, s)
@raises(Exception)
def test_malformed_vrrpv3(self):
m_short_buf = self.buf[1:vrrp.vrrpv3._MIN_LEN]
vrrp.vrrp.parser(m_short_buf)
def test_create_packet(self):
primary_ip = '192.168.0.2'
p0 = self.vrrpv3.create_packet(primary_ip)
p0.serialize()
p1 = packet.Packet(six.binary_type(p0.data))
p1.serialize()
eq_(p0.data, p1.data)
def _test_is_valid(self, type_=None, vrid=None, priority=None,
max_adver_int=None):
if type_ is None:
type_ = self.type_
if vrid is None:
vrid = self.vrid
if priority is None:
priority = self.priority
if max_adver_int is None:
max_adver_int = self.max_adver_int
vrrp_ = vrrp.vrrpv3.create(type_, vrid, priority, max_adver_int,
[self.ip_address])
return vrrp_.is_valid()
def test_is_valid_ok(self):
ok_(self._test_is_valid())
def test_is_valid_ng_type(self):
ok_(not self._test_is_valid(type_=15))
def test_is_valid_ng_vrid_min(self):
vrid = vrrp.VRRP_VRID_MIN - 1
ok_(not self._test_is_valid(vrid=vrid))
def test_is_valid_ng_vrid_max(self):
vrid = vrrp.VRRP_VRID_MAX + 1
ok_(not self._test_is_valid(vrid=vrid))
def test_is_valid_ng_priority_min(self):
priority = vrrp.VRRP_PRIORITY_MIN - 1
ok_(not self._test_is_valid(priority=priority))
def test_is_valid_ng_priority_max(self):
priority = vrrp.VRRP_PRIORITY_MAX + 1
ok_(not self._test_is_valid(priority=priority))
def test_is_valid_ng_adver_min(self):
max_adver_int = vrrp.VRRP_V3_MAX_ADVER_INT_MIN - 1
ok_(not self._test_is_valid(max_adver_int=max_adver_int))
def test_is_valid_ng_adver_max(self):
max_adver_int = vrrp.VRRP_V3_MAX_ADVER_INT_MAX + 1
ok_(not self._test_is_valid(max_adver_int=max_adver_int))
def test_to_string(self):
vrrpv3_values = {'version': self.version,
'type': self.type_,
'vrid': self.vrid,
'priority': self.priority,
'count_ip': self.count_ip,
'max_adver_int': self.max_adver_int,
'checksum': self.vrrpv3.checksum,
'ip_addresses': [self.ip_address],
'auth_type': None,
'auth_data': None,
'identification': self.vrrpv3.identification}
_vrrpv3_str = ','.join(['%s=%s' % (k, repr(vrrpv3_values[k]))
for k, v in inspect.getmembers(self.vrrpv3)
if k in vrrpv3_values])
vrrpv3_str = '%s(%s)' % (vrrp.vrrpv3.__name__, _vrrpv3_str)
eq_(str(self.vrrpv3), vrrpv3_str)
eq_(repr(self.vrrpv3), vrrpv3_str)
class Test_vrrpv3_ipv6(unittest.TestCase):
""" Test case for vrrp v3 IPv6
"""
version = vrrp.VRRP_VERSION_V3
type_ = vrrp.VRRP_TYPE_ADVERTISEMENT
vrid = 128
priority = 99
count_ip = 1
max_adver_int = 111
checksum = 0
ip_address = '2001:db8:2000::1'
vrrpv3 = vrrp.vrrpv3.create(type_, vrid, priority, max_adver_int,
[ip_address])
buf = struct.pack(vrrp.vrrpv3._PACK_STR + '16s',
vrrp.vrrp_to_version_type(vrrp.VRRP_VERSION_V3, type_),
vrid, priority, count_ip,
max_adver_int, checksum,
addrconv.ipv6.text_to_bin(ip_address))
def setUp(self):
pass
def tearDown(self):
pass
def test_init(self):
eq_(self.type_, self.vrrpv3.type)
eq_(self.vrid, self.vrrpv3.vrid)
eq_(self.priority, self.vrrpv3.priority)
eq_(self.count_ip, self.vrrpv3.count_ip)
eq_(1, len(self.vrrpv3.ip_addresses))
eq_(self.ip_address, self.vrrpv3.ip_addresses[0])
def test_parser(self):
vrrpv3, _cls, _ = self.vrrpv3.parser(self.buf)
eq_(self.version, vrrpv3.version)
eq_(self.type_, vrrpv3.type)
eq_(self.vrid, vrrpv3.vrid)
eq_(self.priority, vrrpv3.priority)
eq_(self.count_ip, vrrpv3.count_ip)
eq_(self.max_adver_int, vrrpv3.max_adver_int)
eq_(self.checksum, vrrpv3.checksum)
eq_(1, len(vrrpv3.ip_addresses))
eq_(str, type(vrrpv3.ip_addresses[0]))
eq_(self.ip_address, vrrpv3.ip_addresses[0])
def test_serialize(self):
src_ip = '2001:db8:2000::1'
dst_ip = vrrp.VRRP_IPV6_DST_ADDRESS
prev = ipv6.ipv6(6, 0, 0, 0, inet.IPPROTO_VRRP,
vrrp.VRRP_IPV6_HOP_LIMIT, src_ip, dst_ip)
type_ = vrrp.VRRP_TYPE_ADVERTISEMENT
vrid = 5
priority = 10
max_adver_int = 30
ip_address = '2001:db8:2000::2'
ip_addresses = [ip_address]
vrrp_ = vrrp.vrrpv3.create(
type_, vrid, priority, max_adver_int, ip_addresses)
buf = vrrp_.serialize(bytearray(), prev)
print(len(buf), type(buf), buf)
pack_str = vrrp.vrrpv3._PACK_STR + '16s'
pack_len = struct.calcsize(pack_str)
res = struct.unpack(pack_str, six.binary_type(buf))
eq_(res[0], vrrp.vrrp_to_version_type(vrrp.VRRP_VERSION_V3, type_))
eq_(res[1], vrid)
eq_(res[2], priority)
eq_(res[3], len(ip_addresses))
eq_(res[4], max_adver_int)
# res[5] is checksum
eq_(res[6], addrconv.ipv6.text_to_bin(ip_address))
eq_(len(buf), pack_len)
print(res)
# checksum
ph = struct.pack('!16s16sI3xB',
addrconv.ipv6.text_to_bin(src_ip),
addrconv.ipv6.text_to_bin(dst_ip),
pack_len, inet.IPPROTO_VRRP)
s = packet_utils.checksum(ph + buf)
eq_(0, s)
@raises(Exception)
def test_malformed_vrrpv3(self):
m_short_buf = self.buf[1:vrrp.vrrpv3._MIN_LEN]
vrrp.vrrp.parser(m_short_buf)
def test_create_packet(self):
primary_ip = '2001:db8:2000::3'
p0 = self.vrrpv3.create_packet(primary_ip)
p0.serialize()
print(len(p0.data), p0.data)
p1 = packet.Packet(six.binary_type(p0.data))
p1.serialize()
print(len(p0.data), p0.data)
print(len(p1.data), p1.data)
eq_(p0.data, p1.data)
def test_to_string(self):
vrrpv3_values = {'version': self.version,
'type': self.type_,
'vrid': self.vrid,
'priority': self.priority,
'count_ip': self.count_ip,
'max_adver_int': self.max_adver_int,
'checksum': self.vrrpv3.checksum,
'ip_addresses': [self.ip_address],
'auth_type': None,
'auth_data': None,
'identification': self.vrrpv3.identification}
_vrrpv3_str = ','.join(['%s=%s' % (k, repr(vrrpv3_values[k]))
for k, v in inspect.getmembers(self.vrrpv3)
if k in vrrpv3_values])
vrrpv3_str = '%s(%s)' % (vrrp.vrrpv3.__name__, _vrrpv3_str)
eq_(str(self.vrrpv3), vrrpv3_str)
eq_(repr(self.vrrpv3), vrrpv3_str)
|