3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.sal.utils;
12 import java.net.InetAddress;
13 import java.net.UnknownHostException;
14 import java.util.Arrays;
16 import org.junit.Assert;
17 import org.junit.Test;
19 public class NetUtilsTest {
22 public void testByteArrayMethods() {
24 Assert.assertTrue(NetUtils
25 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
28 Assert.assertTrue(NetUtils
29 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
32 Assert.assertTrue(NetUtils
33 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
36 Assert.assertTrue(NetUtils
37 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
40 Assert.assertTrue(NetUtils
41 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
44 Assert.assertTrue(NetUtils
45 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
47 byte ba[] = { (byte) 0xf, (byte) 0xf, (byte) 0xf, (byte) 0xff };
48 Assert.assertTrue(Arrays.equals(ba, NetUtils.intToByteArray4(NetUtils
49 .byteArray4ToInt(ba))));
51 byte ba1[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255 };
52 Assert.assertTrue(Arrays.equals(ba1, NetUtils.intToByteArray4(NetUtils
53 .byteArray4ToInt(ba1))));
55 byte ba2[] = { (byte) 255, (byte) 0, (byte) 0, (byte) 0 };
56 Assert.assertTrue(Arrays.equals(ba2, NetUtils.intToByteArray4(NetUtils
57 .byteArray4ToInt(ba2))));
59 byte ba3[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
60 Assert.assertTrue(Arrays.equals(ba3, NetUtils.intToByteArray4(NetUtils
61 .byteArray4ToInt(ba3))));
63 byte ba4[] = { (byte) 255, (byte) 128, (byte) 0, (byte) 0 };
64 Assert.assertTrue(Arrays.equals(ba4, NetUtils.intToByteArray4(NetUtils
65 .byteArray4ToInt(ba4))));
69 public void testByteArrayMethodsForLong() {
70 // Test of longToByteArray6 method.
72 (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
73 (byte) 0x55, (byte) 0x66
75 long mac = 0x112233445566L;
76 Assert.assertTrue(Arrays.equals(ba, NetUtils.longToByteArray6(mac)));
79 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
80 (byte) 0xff, (byte) 0xff
82 long mac1 = 0xffffffffffffL;
83 Assert.assertTrue(Arrays.equals(ba1, NetUtils.longToByteArray6(mac1)));
86 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
87 (byte) 0x00, (byte) 0x00
89 long mac2 = 0x000000000000L;
90 Assert.assertTrue(Arrays.equals(ba2, NetUtils.longToByteArray6(mac2)));
93 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00,
94 (byte) 0x00, (byte) 0x00
96 long mac3 = 0xffffff000000L;
97 Assert.assertTrue(Arrays.equals(ba3, NetUtils.longToByteArray6(mac3)));
100 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff,
101 (byte) 0xff, (byte) 0xff
103 long mac4 = 0x000000ffffffL;
104 Assert.assertTrue(Arrays.equals(ba4, NetUtils.longToByteArray6(mac4)));
106 // Convert a long number to a byte array,
107 // and revert it to the long number again.
108 Assert.assertTrue(NetUtils
109 .byteArray6ToLong(NetUtils.longToByteArray6(mac)) == mac);
111 Assert.assertTrue(NetUtils
112 .byteArray6ToLong(NetUtils.longToByteArray6(mac1)) == mac1);
114 Assert.assertTrue(NetUtils
115 .byteArray6ToLong(NetUtils.longToByteArray6(mac2)) == mac2);
117 Assert.assertTrue(NetUtils
118 .byteArray6ToLong(NetUtils.longToByteArray6(mac3)) == mac3);
120 Assert.assertTrue(NetUtils
121 .byteArray6ToLong(NetUtils.longToByteArray6(mac4)) == mac4);
123 // Convert a byte array to a long nubmer,
124 // and revert it to the byte array again.
125 Assert.assertTrue(Arrays.equals(ba,
126 NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba))));
128 Assert.assertTrue(Arrays.equals(ba1,
129 NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba1))));
131 Assert.assertTrue(Arrays.equals(ba2,
132 NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba2))));
134 Assert.assertTrue(Arrays.equals(ba3,
135 NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba3))));
137 Assert.assertTrue(Arrays.equals(ba4,
138 NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba4))));
140 // Test of paramter validation of byteArray6ToLong method.
142 (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44
144 Assert.assertEquals(0, NetUtils.byteArray6ToLong(array5));
147 (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
148 (byte) 0x55, (byte) 0x66, (byte) 0x77
150 Assert.assertEquals(0, NetUtils.byteArray6ToLong(array7));
152 byte arrayNull[] = null;
153 Assert.assertEquals(0, NetUtils.byteArray6ToLong(arrayNull));
157 public void testInetMethods() throws UnknownHostException {
159 InetAddress inet = InetAddress.getByName("255.255.255.240");
160 Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
163 inet = InetAddress.getByName("0.0.0.0");
164 Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
167 inet = InetAddress.getByName("9.255.255.9");
168 Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
172 public void testMasksV4() throws UnknownHostException {
174 InetAddress mask = InetAddress.getByName("128.0.0.0");
175 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, false)));
177 mask = InetAddress.getByName("192.0.0.0");
178 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(2, false)));
180 mask = InetAddress.getByName("224.0.0.0");
181 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(3, false)));
183 mask = InetAddress.getByName("240.0.0.0");
184 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(4, false)));
186 mask = InetAddress.getByName("248.0.0.0");
187 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, false)));
189 mask = InetAddress.getByName("252.0.0.0");
190 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(6, false)));
192 mask = InetAddress.getByName("254.0.0.0");
193 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(7, false)));
195 mask = InetAddress.getByName("255.0.0.0");
196 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, false)));
198 mask = InetAddress.getByName("255.128.0.0");
199 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(9, false)));
201 mask = InetAddress.getByName("255.192.0.0");
202 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(10, false)));
204 mask = InetAddress.getByName("255.224.0.0");
205 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(11, false)));
207 mask = InetAddress.getByName("255.240.0.0");
208 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(12, false)));
210 mask = InetAddress.getByName("255.248.0.0");
211 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(13, false)));
213 mask = InetAddress.getByName("255.252.0.0");
214 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(14, false)));
216 mask = InetAddress.getByName("255.254.0.0");
217 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(15, false)));
219 mask = InetAddress.getByName("255.255.0.0");
220 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(16, false)));
222 mask = InetAddress.getByName("255.255.128.0");
223 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(17, false)));
225 mask = InetAddress.getByName("255.255.192.0");
226 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(18, false)));
228 mask = InetAddress.getByName("255.255.224.0");
229 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(19, false)));
231 mask = InetAddress.getByName("255.255.240.0");
232 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(20, false)));
234 mask = InetAddress.getByName("255.255.248.0");
235 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(21, false)));
237 mask = InetAddress.getByName("255.255.252.0");
238 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(22, false)));
240 mask = InetAddress.getByName("255.255.254.0");
241 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(23, false)));
243 mask = InetAddress.getByName("255.255.255.0");
244 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(24, false)));
246 mask = InetAddress.getByName("255.255.255.128");
247 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(25, false)));
249 mask = InetAddress.getByName("255.255.255.192");
250 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(26, false)));
252 mask = InetAddress.getByName("255.255.255.224");
253 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(27, false)));
255 mask = InetAddress.getByName("255.255.255.240");
256 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(28, false)));
258 mask = InetAddress.getByName("255.255.255.248");
259 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(29, false)));
261 mask = InetAddress.getByName("255.255.255.252");
262 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(30, false)));
264 mask = InetAddress.getByName("255.255.255.254");
265 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(31, false)));
267 mask = InetAddress.getByName("255.255.255.255");
268 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(32, false)));
272 public void testMasksV6() throws UnknownHostException {
274 InetAddress mask = InetAddress.getByName("ff00::0");
275 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, true)));
277 mask = InetAddress.getByName("8000::0");
278 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, true)));
280 mask = InetAddress.getByName("f800::0");
281 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, true)));
283 mask = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
284 Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(127, true)));
288 public void testGetSubnetLen() {
290 byte address[] = { (byte) 128, (byte) 0, (byte) 0, 0 };
291 Assert.assertTrue(NetUtils.getSubnetMaskLength(address) == 1);
293 byte address1[] = { (byte) 255, 0, 0, 0 };
294 Assert.assertTrue(NetUtils.getSubnetMaskLength(address1) == 8);
296 byte address2[] = { (byte) 255, (byte) 255, (byte) 248, 0 };
297 Assert.assertTrue(NetUtils.getSubnetMaskLength(address2) == 21);
299 byte address4[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 254 };
300 Assert.assertTrue(NetUtils.getSubnetMaskLength(address4) == 31);
302 byte address5[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
303 (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
305 Assert.assertTrue(NetUtils.getSubnetMaskLength(address5) == 128);
307 byte address6[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
308 (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
310 Assert.assertTrue(NetUtils.getSubnetMaskLength(address6) == 127);
312 byte address7[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
313 (byte) 255, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
314 Assert.assertTrue(NetUtils.getSubnetMaskLength(address7) == 64);
316 byte address8[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255,
317 (byte) 254, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
318 Assert.assertTrue(NetUtils.getSubnetMaskLength(address8) == 63);
320 byte address9[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 128,
321 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
322 Assert.assertTrue(NetUtils.getSubnetMaskLength(address9) == 49);
324 byte address10[] = { (byte) 128, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
325 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
326 Assert.assertTrue(NetUtils.getSubnetMaskLength(address10) == 1);
328 byte address11[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
329 (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
330 Assert.assertTrue(NetUtils.getSubnetMaskLength(address11) == 0);
334 public void testGetSubnetPrefix() throws UnknownHostException {
335 InetAddress ip = InetAddress.getByName("192.128.64.252");
337 Assert.assertTrue(NetUtils.getSubnetPrefix(ip, maskLen).equals(
338 InetAddress.getByName("192.128.64.128")));
342 public void testIsIPv6Valid() throws UnknownHostException {
343 Assert.assertTrue(NetUtils
344 .isIPv6AddressValid("fe80:0000:0000:0000:0204:61ff:fe9d:f156")); //normal ipv6
345 Assert.assertTrue(NetUtils
346 .isIPv6AddressValid("fe80:0:0:0:204:61ff:fe9d:f156")); //no leading zeroes
347 Assert.assertTrue(NetUtils
348 .isIPv6AddressValid("fe80::204:61ff:fe9d:f156")); //zeroes to ::
351 .isIPv6AddressValid("fe80:0000:0000:0000:0204:61ff:254.157.241.86")); // ipv4 ending
352 Assert.assertTrue(NetUtils
353 .isIPv6AddressValid("fe80:0:0:0:0204:61ff:254.157.241.86")); // no leading zeroes, ipv4 end
354 Assert.assertTrue(NetUtils
355 .isIPv6AddressValid("fe80::204:61ff:254.157.241.86")); // zeroes ::, no leading zeroes
357 Assert.assertTrue(NetUtils.isIPv6AddressValid("2001::")); //link-local prefix
358 Assert.assertTrue(NetUtils.isIPv6AddressValid("::1")); //localhost
359 Assert.assertTrue(NetUtils.isIPv6AddressValid("fe80::")); //global-unicast
360 Assert.assertFalse(NetUtils.isIPv6AddressValid("abcd")); //not valid
361 Assert.assertFalse(NetUtils.isIPv6AddressValid("1")); //not valid
362 Assert.assertFalse(NetUtils
363 .isIPv6AddressValid("fe80:0:0:0:204:61ff:fe9d")); //not valid, too short
364 Assert.assertFalse(NetUtils
365 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d")); //not valid
366 Assert.assertFalse(NetUtils.isIPv6AddressValid("192.168.1.1")); //not valid,ipv4
368 .assertFalse(NetUtils
369 .isIPv6AddressValid("2001:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
371 .assertFalse(NetUtils
372 .isIPv6AddressValid("20010:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
376 .isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/60")); //full with mask
377 Assert.assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/64")); //shortened with mask
378 Assert.assertTrue(NetUtils.isIPv6AddressValid("2001:0DB8:0:CD30::/0")); //0 subnet with mask
379 Assert.assertTrue(NetUtils.isIPv6AddressValid("::1/128")); //localhost 128 mask
381 Assert.assertFalse(NetUtils.isIPv6AddressValid("124.15.6.89/60")); //invalid, ip with mask
383 .assertFalse(NetUtils
384 .isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/130")); //invalid, mask >128
386 .assertFalse(NetUtils
387 .isIPv6AddressValid("2001:0DB8:0:CD30::/-5")); //invalid, mask < 0
388 Assert.assertFalse(NetUtils
389 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/64")); //not valid ip, valid netmask
390 Assert.assertFalse(NetUtils
391 .isIPv6AddressValid("fe80:::0:0:0:204:61ff:fe9d/-1")); //not valid both
396 public void testInetAddressConflict() throws UnknownHostException {
398 // test a ipv4 testAddress in the same subnet as the filter
399 // the method should return false as there is no conflict
400 Assert.assertFalse(NetUtils.inetAddressConflict(
401 InetAddress.getByName("9.9.1.1"),
402 InetAddress.getByName("9.9.1.0"), null,
403 InetAddress.getByName("255.255.255.0")));
405 // test a ipv4 testAddress not in the same subnet as the filter
406 // the method should return true as there is a conflict
407 Assert.assertTrue(NetUtils.inetAddressConflict(
408 InetAddress.getByName("9.9.2.1"),
409 InetAddress.getByName("9.9.1.0"), null,
410 InetAddress.getByName("255.255.255.0")));
412 // test a ipv4 testAddress more generic than the filter
413 // the method should return true as there is a conflict
414 Assert.assertTrue(NetUtils.inetAddressConflict(
415 InetAddress.getByName("9.9.1.1"),
416 InetAddress.getByName("9.9.1.0"),
417 InetAddress.getByName("255.255.0.0"),
418 InetAddress.getByName("255.255.255.0")));
420 // test a ipv4 testAddress less generic than the filter and in the same
421 // subnet as the filter
422 // the method should return false as there is no conflict
423 Assert.assertFalse(NetUtils.inetAddressConflict(
424 InetAddress.getByName("9.9.1.0"),
425 InetAddress.getByName("9.9.0.0"),
426 InetAddress.getByName("255.255.255.0"),
427 InetAddress.getByName("255.255.0.0")));
429 // test a ipv4 testAddress less generic than the filter and not in the
430 // same subnet as the filter
431 // the method should return true as there is a conflict
432 Assert.assertTrue(NetUtils.inetAddressConflict(
433 InetAddress.getByName("9.8.1.0"),
434 InetAddress.getByName("9.9.0.0"),
435 InetAddress.getByName("255.255.255.0"),
436 InetAddress.getByName("255.255.0.0")));
441 public void testIPAddressValidity() {
442 Assert.assertFalse(NetUtils.isIPAddressValid(null));
443 Assert.assertFalse(NetUtils.isIPAddressValid("abc"));
444 Assert.assertFalse(NetUtils.isIPAddressValid("1.1.1"));
445 Assert.assertFalse(NetUtils.isIPAddressValid("1.1.1.1/49"));
447 Assert.assertTrue(NetUtils.isIPAddressValid("1.1.1.1"));
448 Assert.assertTrue(NetUtils.isIPAddressValid("1.1.1.1/32"));
449 Assert.assertTrue(NetUtils
450 .isIPAddressValid("2001:420:281:1004:407a:57f4:4d15:c355"));