1 # Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 from __future__ import print_function
22 from nose.tools import eq_
23 from nose.tools import raises
25 from ryu.lib import ip
27 LOG = logging.getLogger('test_ip')
30 class Test_ip(unittest.TestCase):
32 test case for ip address module
41 def test_ipv4_to_bin(self):
42 ipv4_str = '10.28.197.1'
45 (res,) = struct.unpack('!I', ip.ipv4_to_bin(ipv4_str))
48 def test_ipv4_to_int(self):
49 ipv4_str = '10.28.197.1'
52 res = ip.ipv4_to_int(ipv4_str)
55 def test_ipv4_to_str_from_bin(self):
56 ipv4_bin = struct.pack('!I', 0x0a1cc501)
59 res = ip.ipv4_to_str(ipv4_bin)
62 def test_ipv4_to_str_from_int(self):
66 res = ip.ipv4_to_str(ipv4_int)
69 def test_ipv6_to_bin(self):
70 ipv6_str = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
71 val = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20, 0x66ff,
73 res = ip.ipv6_to_bin(ipv6_str)
76 def test_ipv6_to_bin_with_shortcut(self):
77 ipv6_str = '3f:10::1:2'
78 val = struct.pack('!8H', 0x3f, 0x10, 0, 0, 0, 0, 0x1, 0x2)
80 res = ip.ipv6_to_bin(ipv6_str)
83 def test_ipv6_to_int(self):
84 ipv6_str = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
85 val = 0x20130da8021508f2aa2066fffe4c9c3c
87 res = ip.ipv6_to_int(ipv6_str)
90 def test_ipv6_to_int_with_shortcut(self):
91 ipv6_str = '3f:10::1:2'
92 val = 0x003f0010000000000000000000010002
94 res = ip.ipv6_to_int(ipv6_str)
97 def test_ipv6_to_str_from_bin(self):
98 ipv6_bin = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20,
99 0x66ff, 0xfe4c, 0x9c3c)
100 val = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
102 res = ip.ipv6_to_str(ipv6_bin)
105 def test_ipv6_to_str_from_int(self):
106 ipv6_int = 0x20130da8021508f2aa2066fffe4c9c3c
107 val = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
109 res = ip.ipv6_to_str(ipv6_int)
112 def test_text_to_bin_from_ipv4_text(self):
113 ipv4_str = '10.28.197.1'
114 val = struct.pack('!4B', 10, 28, 197, 1)
115 res = ip.text_to_bin(ipv4_str)
118 def test_text_to_bin_from_ipv6_text(self):
119 ipv6_str = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
120 val = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20,
121 0x66ff, 0xfe4c, 0x9c3c)
122 res = ip.text_to_bin(ipv6_str)
125 def test_text_to_int_from_ipv4_text(self):
126 ipv4_str = '10.28.197.1' # 0a.1c.c5.01
129 res = ip.text_to_int(ipv4_str)
132 def test_text_to_int_from_ipv6_text(self):
133 ipv6_str = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
134 val = 0x20130da8021508f2aa2066fffe4c9c3c
136 res = ip.text_to_int(ipv6_str)
139 def test_bin_to_text_from_ipv4_bin(self):
140 ipv4_bin = struct.pack('!4B', 10, 28, 197, 1)
142 res = ip.bin_to_text(ipv4_bin)
145 def test_bin_to_text_from_ipv6_bin(self):
146 ipv6_bin = struct.pack('!8H', 0x2013, 0xda8, 0x215, 0x8f2, 0xaa20,
147 0x66ff, 0xfe4c, 0x9c3c)
148 val = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
149 res = ip.bin_to_text(ipv6_bin)
152 @raises(struct.error)
153 def test_bin_to_text_with_invalid_bin(self):
154 invalid_bin = b'invalid'
156 ip.bin_to_text(invalid_bin)