77d1fe216413d38477eaffabdc5f8f1dea9bfea4
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / utils / NetUtilsTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.utils;
11
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.Arrays;
15
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.utils.NetUtils;
19
20 public class NetUtilsTest {
21
22     @Test
23     public void testByteArrayMethods() {
24         int ip = 8888;
25         Assert.assertTrue(NetUtils
26                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
27
28         ip = 0xffffffff;
29         Assert.assertTrue(NetUtils
30                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
31
32         ip = 0;
33         Assert.assertTrue(NetUtils
34                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
35
36         ip = 0x1fffffff;
37         Assert.assertTrue(NetUtils
38                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
39
40         ip = 0xfffffff;
41         Assert.assertTrue(NetUtils
42                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
43
44         ip = 0xf000ffff;
45         Assert.assertTrue(NetUtils
46                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
47
48         byte ba[] = { (byte) 0xf, (byte) 0xf, (byte) 0xf, (byte) 0xff };
49         Assert.assertTrue(Arrays.equals(ba, NetUtils.intToByteArray4(NetUtils
50                 .byteArray4ToInt(ba))));
51
52         byte ba1[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255 };
53         Assert.assertTrue(Arrays.equals(ba1, NetUtils.intToByteArray4(NetUtils
54                 .byteArray4ToInt(ba1))));
55
56         byte ba2[] = { (byte) 255, (byte) 0, (byte) 0, (byte) 0 };
57         Assert.assertTrue(Arrays.equals(ba2, NetUtils.intToByteArray4(NetUtils
58                 .byteArray4ToInt(ba2))));
59
60         byte ba3[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
61         Assert.assertTrue(Arrays.equals(ba3, NetUtils.intToByteArray4(NetUtils
62                 .byteArray4ToInt(ba3))));
63
64         byte ba4[] = { (byte) 255, (byte) 128, (byte) 0, (byte) 0 };
65         Assert.assertTrue(Arrays.equals(ba4, NetUtils.intToByteArray4(NetUtils
66                 .byteArray4ToInt(ba4))));
67     }
68
69     @Test
70     public void testInetMethods() throws UnknownHostException {
71         int ip = 0xfffffff0;
72         InetAddress inet = InetAddress.getByName("255.255.255.240");
73         Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
74
75         ip = 0;
76         inet = InetAddress.getByName("0.0.0.0");
77         Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
78
79         ip = 0x9ffff09;
80         inet = InetAddress.getByName("9.255.255.9");
81         Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
82     }
83
84     @Test
85     public void testMasksV4() throws UnknownHostException {
86
87         InetAddress mask = InetAddress.getByName("128.0.0.0");
88         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, false)));
89
90         mask = InetAddress.getByName("192.0.0.0");
91         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(2, false)));
92
93         mask = InetAddress.getByName("224.0.0.0");
94         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(3, false)));
95
96         mask = InetAddress.getByName("240.0.0.0");
97         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(4, false)));
98
99         mask = InetAddress.getByName("248.0.0.0");
100         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, false)));
101
102         mask = InetAddress.getByName("252.0.0.0");
103         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(6, false)));
104
105         mask = InetAddress.getByName("254.0.0.0");
106         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(7, false)));
107
108         mask = InetAddress.getByName("255.0.0.0");
109         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, false)));
110
111         mask = InetAddress.getByName("255.128.0.0");
112         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(9, false)));
113
114         mask = InetAddress.getByName("255.192.0.0");
115         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(10, false)));
116
117         mask = InetAddress.getByName("255.224.0.0");
118         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(11, false)));
119
120         mask = InetAddress.getByName("255.240.0.0");
121         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(12, false)));
122
123         mask = InetAddress.getByName("255.248.0.0");
124         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(13, false)));
125
126         mask = InetAddress.getByName("255.252.0.0");
127         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(14, false)));
128
129         mask = InetAddress.getByName("255.254.0.0");
130         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(15, false)));
131
132         mask = InetAddress.getByName("255.255.0.0");
133         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(16, false)));
134
135         mask = InetAddress.getByName("255.255.128.0");
136         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(17, false)));
137
138         mask = InetAddress.getByName("255.255.192.0");
139         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(18, false)));
140
141         mask = InetAddress.getByName("255.255.224.0");
142         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(19, false)));
143
144         mask = InetAddress.getByName("255.255.240.0");
145         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(20, false)));
146
147         mask = InetAddress.getByName("255.255.248.0");
148         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(21, false)));
149
150         mask = InetAddress.getByName("255.255.252.0");
151         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(22, false)));
152
153         mask = InetAddress.getByName("255.255.254.0");
154         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(23, false)));
155
156         mask = InetAddress.getByName("255.255.255.0");
157         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(24, false)));
158
159         mask = InetAddress.getByName("255.255.255.128");
160         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(25, false)));
161
162         mask = InetAddress.getByName("255.255.255.192");
163         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(26, false)));
164
165         mask = InetAddress.getByName("255.255.255.224");
166         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(27, false)));
167
168         mask = InetAddress.getByName("255.255.255.240");
169         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(28, false)));
170
171         mask = InetAddress.getByName("255.255.255.248");
172         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(29, false)));
173
174         mask = InetAddress.getByName("255.255.255.252");
175         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(30, false)));
176
177         mask = InetAddress.getByName("255.255.255.254");
178         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(31, false)));
179
180         mask = InetAddress.getByName("255.255.255.255");
181         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(32, false)));
182     }
183
184     @Test
185     public void testMasksV6() throws UnknownHostException {
186
187         InetAddress mask = InetAddress.getByName("ff00::0");
188         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, true)));
189
190         mask = InetAddress.getByName("8000::0");
191         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, true)));
192
193         mask = InetAddress.getByName("f800::0");
194         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, true)));
195
196         mask = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
197         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(127, true)));
198     }
199
200     @Test
201     public void testGetSubnetLen() {
202
203         byte address[] = { (byte) 128, (byte) 0, (byte) 0, 0 };
204         Assert.assertTrue(NetUtils.getSubnetMaskLength(address) == 31);
205
206         byte address1[] = { (byte) 255, 0, 0, 0 };
207         Assert.assertTrue(NetUtils.getSubnetMaskLength(address1) == 24);
208
209         byte address2[] = { (byte) 255, (byte) 255, (byte) 248, 0 };
210         Assert.assertTrue(NetUtils.getSubnetMaskLength(address2) == 11);
211
212         byte address4[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 254 };
213         Assert.assertTrue(NetUtils.getSubnetMaskLength(address4) == 1);
214     }
215
216     @Test
217     public void testGetSubnetPrefix() throws UnknownHostException {
218         InetAddress ip = InetAddress.getByName("192.128.64.252");
219         int maskLen = 25;
220         Assert.assertTrue(NetUtils.getSubnetPrefix(ip, maskLen).equals(
221                 InetAddress.getByName("192.128.64.128")));
222     }
223
224     @Test
225     public void testIsIPv6Valid() throws UnknownHostException {
226         Assert.assertTrue(NetUtils
227                 .isIPv6AddressValid("fe80:0000:0000:0000:0204:61ff:fe9d:f156")); //normal ipv6
228         Assert.assertTrue(NetUtils
229                 .isIPv6AddressValid("fe80:0:0:0:204:61ff:fe9d:f156")); //no leading zeroes
230         Assert.assertTrue(NetUtils
231                 .isIPv6AddressValid("fe80::204:61ff:fe9d:f156")); //zeroes to ::
232         Assert
233                 .assertTrue(NetUtils
234                         .isIPv6AddressValid("fe80:0000:0000:0000:0204:61ff:254.157.241.86")); // ipv4 ending
235         Assert.assertTrue(NetUtils
236                 .isIPv6AddressValid("fe80:0:0:0:0204:61ff:254.157.241.86")); // no leading zeroes, ipv4 end
237         Assert.assertTrue(NetUtils
238                 .isIPv6AddressValid("fe80::204:61ff:254.157.241.86")); // zeroes ::, no leading zeroes
239
240         Assert.assertTrue(NetUtils.isIPv6AddressValid("2001::")); //link-local prefix
241         Assert.assertTrue(NetUtils.isIPv6AddressValid("::1")); //localhost
242         Assert.assertTrue(NetUtils.isIPv6AddressValid("fe80::")); //global-unicast
243         Assert.assertFalse(NetUtils.isIPv6AddressValid("abcd")); //not valid
244         Assert.assertFalse(NetUtils.isIPv6AddressValid("1")); //not valid
245         Assert.assertFalse(NetUtils
246                 .isIPv6AddressValid("fe80:0:0:0:204:61ff:fe9d")); //not valid, too short
247         Assert.assertFalse(NetUtils
248                 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d")); //not valid
249         Assert.assertFalse(NetUtils.isIPv6AddressValid("192.168.1.1")); //not valid,ipv4
250         Assert
251                 .assertFalse(NetUtils
252                         .isIPv6AddressValid("2001:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
253         Assert
254                 .assertFalse(NetUtils
255                         .isIPv6AddressValid("20010:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
256
257         Assert
258                 .assertTrue(NetUtils
259                         .isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/60")); //full with mask
260         Assert.assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/64")); //shortened with mask
261         Assert.assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/0")); //0 subnet with mask
262         Assert.assertTrue(NetUtils.isIPv6AddressValid("::1/128")); //localhost 128 mask
263
264         Assert.assertFalse(NetUtils.isIPv6AddressValid("124.15.6.89/60")); //invalid, ip with mask
265         Assert
266                 .assertFalse(NetUtils
267                         .isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/130")); //invalid, mask >128
268         Assert
269                 .assertFalse(NetUtils
270                         .isIPv6AddressValid("2001:0DB8:0:CD30::/-5")); //invalid, mask < 0
271         Assert.assertFalse(NetUtils
272                 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/64")); //not valid ip, valid netmask
273         Assert.assertFalse(NetUtils
274                 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/-1")); //not valid both
275
276     }
277 }