backing up
[vsorcdistro/.git] / ryu / build / lib.linux-armv7l-2.7 / ryu / tests / unit / lib / test_ip.py
1 # Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
2 #
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
6 #
7 #    http://www.apache.org/licenses/LICENSE-2.0
8 #
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
12 # implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 from __future__ import print_function
17
18 import logging
19 import struct
20 import unittest
21
22 from nose.tools import eq_
23 from nose.tools import raises
24
25 from ryu.lib import ip
26
27 LOG = logging.getLogger('test_ip')
28
29
30 class Test_ip(unittest.TestCase):
31     """
32     test case for ip address module
33     """
34
35     def setUp(self):
36         pass
37
38     def tearDown(self):
39         pass
40
41     def test_ipv4_to_bin(self):
42         ipv4_str = '10.28.197.1'
43         val = 0x0a1cc501
44
45         (res,) = struct.unpack('!I', ip.ipv4_to_bin(ipv4_str))
46         eq_(val, res)
47
48     def test_ipv4_to_int(self):
49         ipv4_str = '10.28.197.1'
50         val = 169657601
51
52         res = ip.ipv4_to_int(ipv4_str)
53         eq_(val, res)
54
55     def test_ipv4_to_str_from_bin(self):
56         ipv4_bin = struct.pack('!I', 0x0a1cc501)
57         val = '10.28.197.1'
58
59         res = ip.ipv4_to_str(ipv4_bin)
60         eq_(val, res)
61
62     def test_ipv4_to_str_from_int(self):
63         ipv4_int = 169657601
64         val = '10.28.197.1'
65
66         res = ip.ipv4_to_str(ipv4_int)
67         eq_(val, res)
68
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,
72                           0xfe4c, 0x9c3c)
73         res = ip.ipv6_to_bin(ipv6_str)
74         eq_(val, res)
75
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)
79
80         res = ip.ipv6_to_bin(ipv6_str)
81         eq_(val, res)
82
83     def test_ipv6_to_int(self):
84         ipv6_str = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
85         val = 0x20130da8021508f2aa2066fffe4c9c3c
86
87         res = ip.ipv6_to_int(ipv6_str)
88         eq_(val, res)
89
90     def test_ipv6_to_int_with_shortcut(self):
91         ipv6_str = '3f:10::1:2'
92         val = 0x003f0010000000000000000000010002
93
94         res = ip.ipv6_to_int(ipv6_str)
95         eq_(val, res)
96
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'
101
102         res = ip.ipv6_to_str(ipv6_bin)
103         eq_(val, res)
104
105     def test_ipv6_to_str_from_int(self):
106         ipv6_int = 0x20130da8021508f2aa2066fffe4c9c3c
107         val = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
108
109         res = ip.ipv6_to_str(ipv6_int)
110         eq_(val, res)
111
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)
116         eq_(val, res)
117
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)
123         eq_(val, res)
124
125     def test_text_to_int_from_ipv4_text(self):
126         ipv4_str = '10.28.197.1'  # 0a.1c.c5.01
127         val = 0x0a1cc501
128
129         res = ip.text_to_int(ipv4_str)
130         eq_(val, res)
131
132     def test_text_to_int_from_ipv6_text(self):
133         ipv6_str = '2013:da8:215:8f2:aa20:66ff:fe4c:9c3c'
134         val = 0x20130da8021508f2aa2066fffe4c9c3c
135
136         res = ip.text_to_int(ipv6_str)
137         eq_(val, res)
138
139     def test_bin_to_text_from_ipv4_bin(self):
140         ipv4_bin = struct.pack('!4B', 10, 28, 197, 1)
141         val = '10.28.197.1'
142         res = ip.bin_to_text(ipv4_bin)
143         eq_(val, res)
144
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)
150         eq_(val, res)
151
152     @raises(struct.error)
153     def test_bin_to_text_with_invalid_bin(self):
154         invalid_bin = b'invalid'
155
156         ip.bin_to_text(invalid_bin)