From 859f22c1026deca7f663379356a1221a281c4208 Mon Sep 17 00:00:00 2001
From: Can Zhang <can@canx.me>
Date: Tue, 16 Apr 2013 09:27:19 +0800
Subject: [PATCH 4/4] add test, method name change

Signed-off-by: Can Zhang <can@canx.me>
---
 ryu/lib/ip.py                 |  8 +-----
 ryu/tests/unit/lib/test_ip.py | 60 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 7 deletions(-)
 create mode 100644 ryu/tests/unit/lib/test_ip.py

diff --git a/ryu/lib/ip.py b/ryu/lib/ip.py
index 3d2e822..b302d8d 100644
--- a/ryu/lib/ip.py
+++ b/ryu/lib/ip.py
@@ -56,15 +56,9 @@ def ipv6_to_bin(ipv6):
     return struct.pack(IPV6_PACK_STR, *args)
 
 
-def bin_to_ipv6(bin_addr):
+def ipv6_to_str(bin_addr):
     '''
         convert binary representation to human readable string
     '''
     args = struct.unpack_from(IPV6_PACK_STR, bin_addr)
     return ':'.join('%x' % x for x in args)
-
-if __name__ == '__main__':
-    a = ipv4_to_bin('10.28.197.1')
-    print ipv4_to_str(a)
-    print bin_to_ipv6(ipv6_to_bin('3f:10::1:2'))
-    print bin_to_ipv6(ipv6_to_bin('2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'))
diff --git a/ryu/tests/unit/lib/test_ip.py b/ryu/tests/unit/lib/test_ip.py
new file mode 100644
index 0000000..7663745
--- /dev/null
+++ b/ryu/tests/unit/lib/test_ip.py
@@ -0,0 +1,60 @@
+import unittest
+import logging
+import struct
+import netaddr
+from struct import *
+from nose.tools import *
+from nose.plugins.skip import Skip, SkipTest
+
+from ryu.lib import ip
+
+LOG = logging.getLogger('test_ip')
+
+
+class Test_ip(unittest.TestCase):
+    '''
+        test case for ip address module
+    '''
+
+    def setUp(self):
+        pass
+
+    def tearDown(self):
+        pass
+
+    def test_ipv4_to_bin(self):
+        ipv4_str = '10.28.197.1'
+        val = 0x0a1cc501
+
+        res = ip.ipv4_to_bin(ipv4_str)
+        eq_(val, res)
+
+    def test_ipv4_to_str(self):
+        ipv4_bin = 0x0a1cc501
+        val = '10.28.197.1'
+
+        res = ip.ipv4_to_str(ipv4_bin)
+        eq_(val, res)
+
+    def test_ipv6_to_bin(self):
+        ipv6_str = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
+        val = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20, 0x66ff,
+                          0xfe4c, 0x9c3c)
+        res = ip.ipv6_to_bin(ipv6_str)
+        eq_(val, res)
+
+    def test_ipv6_to_bin_with_shortcut(self):
+        ipv6_str = '3f:10::1:2'
+        val = struct.pack('!8H', 0x3f, 0x10, 0, 0, 0, 0, 0x1, 0x2)
+
+        res = ip.ipv6_to_bin(ipv6_str)
+        eq_(val, res)
+
+    def test_ipv6_to_str(self):
+        ipv6_bin = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20,
+                               0x66ff, 0xfe4c, 0x9c3c)
+        val = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
+
+        res = ip.ipv6_to_str(ipv6_bin)
+        print val, res
+        eq_(val, res)
-- 
1.7.12.4 (Apple Git-37)

