On 32bit environment integer representation of ipv4 address can be
python int or long. Some unit tests failed.
On the other hand 64 bit environment it is int.
And some clean up.

FAIL: Failure: AssertionError ()

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/nose/loader.py", line 413, in
loadTestsFromName
    addr.filename, addr.module)
  File "/usr/local/lib/python2.7/dist-packages/nose/importer.py", line 47, in
importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/local/lib/python2.7/dist-packages/nose/importer.py", line 94, in
importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/oliver/tmp/ryu/ryu/tests/unit/packet/test_vrrp.py", line 38, in
<module>
    class Test_vrrpv2(unittest.TestCase):
  File "/home/oliver/tmp/ryu/ryu/tests/unit/packet/test_vrrp.py", line 52, in
Test_vrrpv2
    [ip_address])
  File "/home/oliver/tmp/ryu/ryu/lib/packet/vrrp.py", line 440, in create
    auth_data=VRRP_AUTH_DATA)
  File "/home/oliver/tmp/ryu/ryu/lib/packet/vrrp.py", line 308, in
create_version
    auth_type=auth_type, auth_data=auth_data)
  File "/home/oliver/tmp/ryu/ryu/lib/packet/vrrp.py", line 272, in __init__
    self._is_ipv6 = is_ipv6(self.ip_addresses[0])
  File "/home/oliver/tmp/ryu/ryu/lib/packet/vrrp.py", line 172, in is_ipv6
    assert type(ip_address) == str
AssertionError

Cc: Kei Ohmura <[email protected]>
Reported-by: Oren Spector <[email protected]>
Signed-off-by: Isaku Yamahata <[email protected]>
---
 ryu/lib/packet/vrrp.py             |   31 +++++++++++++++++++++++--------
 ryu/tests/unit/packet/test_vrrp.py |    6 +++---
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/ryu/lib/packet/vrrp.py b/ryu/lib/packet/vrrp.py
index 8b6140a..a197629 100644
--- a/ryu/lib/packet/vrrp.py
+++ b/ryu/lib/packet/vrrp.py
@@ -165,12 +165,29 @@ VRRP_V2_MAX_ADVER_INT_MIN = 1           # don't allow 0
 VRRP_V2_MAX_ADVER_INT_MAX = 0xff
 
 
+# On 32bit environment, C-uint32_t can be python-long
+_IPV4_VALUE_TYPES = list(set((int, type(0xffffffff))))
+_IPV6_PACKED_TYPE = type(netaddr.IPAddress('::1').packed)       # = str
+_IPV6_PACKED_LEN = len(netaddr.IPAddress('::1').packed)         # = 16
+
+
+def _assert_ipv4_address(ipv4_address):
+    assert type(ipv4_address) in _IPV4_VALUE_TYPES
+    assert ipv4_address >= 0
+    assert ipv4_address <= 0xffffffff
+
+
+def _assert_ipv6_address(ipv6_address):
+    assert type(ipv6_address) == _IPV6_PACKED_TYPE
+    assert len(ipv6_address) == _IPV6_PACKED_LEN
+
+
 def is_ipv6(ip_address):
-    if type(ip_address) == int:
+    if type(ip_address) in _IPV4_VALUE_TYPES:
+        _assert_ipv4_address(ip_address)
         return False
 
-    assert type(ip_address) == str
-    assert len(ip_address) == 16
+    _assert_ipv6_address(ip_address)
     return True
 
 
@@ -206,8 +223,7 @@ class vrrp(packet_base.PacketBase):
     _IPV4_ADDRESS_PACK_STR_RAW = 'I'
     _IPV4_ADDRESS_PACK_STR = '!' + _IPV4_ADDRESS_PACK_STR_RAW
     _IPV4_ADDRESS_LEN = struct.calcsize(_IPV4_ADDRESS_PACK_STR)
-    _IPV6_ADDRESS_LEN = 16
-    _IPV6_ADDRESS_PACK_STR_RAW = '%ds' % _IPV6_ADDRESS_LEN
+    _IPV6_ADDRESS_PACK_STR_RAW = '%ds' % _IPV6_PACKED_LEN
     _IPV6_ADDRESS_PACK_STR = '!' + _IPV6_ADDRESS_PACK_STR_RAW
     _IPV6_ADDRESS_LEN = struct.calcsize(_IPV6_ADDRESS_PACK_STR)
 
@@ -579,11 +595,10 @@ class vrrpv3(vrrp):
     @staticmethod
     def serialize_static(vrrp_, prev):
         if isinstance(prev, ipv4.ipv4):
-            assert type(vrrp_.ip_addresses[0]) == int
+            _assert_ipv4_address(vrrp_.ip_addresses[0])
             ip_address_pack_raw = vrrpv3._IPV4_ADDRESS_PACK_STR_RAW
         elif isinstance(prev, ipv6.ipv6):
-            assert type(vrrp_.ip_addresses[0]) == str
-            assert len(vrrp_.ip_addresses[0]) == vrrp._IPV6_ADDRESS_LEN
+            _assert_ipv6_address(vrrp_.ip_addresses[0])
             ip_address_pack_raw = vrrpv3._IPV6_ADDRESS_PACK_STR_RAW
         else:
             raise ValueError('Unkown network layer %s' % type(prev))
diff --git a/ryu/tests/unit/packet/test_vrrp.py 
b/ryu/tests/unit/packet/test_vrrp.py
index 282c9fa..56812b2 100644
--- a/ryu/tests/unit/packet/test_vrrp.py
+++ b/ryu/tests/unit/packet/test_vrrp.py
@@ -84,7 +84,7 @@ class Test_vrrpv2(unittest.TestCase):
         eq_(self.max_adver_int, vrrpv2.max_adver_int)
         eq_(self.checksum, vrrpv2.checksum)
         eq_(1, len(vrrpv2.ip_addresses))
-        eq_(int, type(vrrpv2.ip_addresses[0]))
+        vrrp._assert_ipv4_address(vrrpv2.ip_addresses[0])
         eq_(self.ip_address, vrrpv2.ip_addresses[0])
         eq_(self.auth_data, vrrpv2.auth_data)
 
@@ -226,7 +226,7 @@ class Test_vrrpv3_ipv4(unittest.TestCase):
         eq_(self.max_adver_int, vrrpv3.max_adver_int)
         eq_(self.checksum, vrrpv3.checksum)
         eq_(1, len(vrrpv3.ip_addresses))
-        eq_(int, type(vrrpv3.ip_addresses[0]))
+        vrrp._assert_ipv4_address(vrrpv3.ip_addresses[0])
         eq_(self.ip_address, vrrpv3.ip_addresses[0])
 
     def test_serialize(self):
@@ -367,7 +367,7 @@ class Test_vrrpv3_ipv6(unittest.TestCase):
         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]))
+        vrrp._assert_ipv6_address(vrrpv3.ip_addresses[0])
         eq_(self.ip_address, vrrpv3.ip_addresses[0])
 
     def test_serialize(self):
-- 
1.7.10.4


------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:

Build for Windows Store.

http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to