Merge "Fixed a bug and added a function for operating on MAC addresses."
[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
19 public class NetUtilsTest {
20
21     @Test
22     public void testByteArrayMethods() {
23         int ip = 8888;
24         Assert.assertTrue(NetUtils
25                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
26
27         ip = 0xffffffff;
28         Assert.assertTrue(NetUtils
29                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
30
31         ip = 0;
32         Assert.assertTrue(NetUtils
33                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
34
35         ip = 0x1fffffff;
36         Assert.assertTrue(NetUtils
37                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
38
39         ip = 0xfffffff;
40         Assert.assertTrue(NetUtils
41                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
42
43         ip = 0xf000ffff;
44         Assert.assertTrue(NetUtils
45                 .byteArray4ToInt(NetUtils.intToByteArray4(ip)) == ip);
46
47         byte ba[] = { (byte) 0xf, (byte) 0xf, (byte) 0xf, (byte) 0xff };
48         Assert.assertTrue(Arrays.equals(ba, NetUtils.intToByteArray4(NetUtils
49                 .byteArray4ToInt(ba))));
50
51         byte ba1[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 255 };
52         Assert.assertTrue(Arrays.equals(ba1, NetUtils.intToByteArray4(NetUtils
53                 .byteArray4ToInt(ba1))));
54
55         byte ba2[] = { (byte) 255, (byte) 0, (byte) 0, (byte) 0 };
56         Assert.assertTrue(Arrays.equals(ba2, NetUtils.intToByteArray4(NetUtils
57                 .byteArray4ToInt(ba2))));
58
59         byte ba3[] = { (byte) 0, (byte) 0, (byte) 0, (byte) 0 };
60         Assert.assertTrue(Arrays.equals(ba3, NetUtils.intToByteArray4(NetUtils
61                 .byteArray4ToInt(ba3))));
62
63         byte ba4[] = { (byte) 255, (byte) 128, (byte) 0, (byte) 0 };
64         Assert.assertTrue(Arrays.equals(ba4, NetUtils.intToByteArray4(NetUtils
65                 .byteArray4ToInt(ba4))));
66     }
67
68     @Test
69     public void testByteArrayMethodsForLong() {
70         // Test of longToByteArray6 method.
71         byte ba[] = {
72             (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
73             (byte) 0x55, (byte) 0x66
74         };
75         long mac = 0x112233445566L;
76         Assert.assertTrue(Arrays.equals(ba, NetUtils.longToByteArray6(mac)));
77
78         byte ba1[] = {
79             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
80             (byte) 0xff, (byte) 0xff
81         };
82         long mac1 = 0xffffffffffffL;
83         Assert.assertTrue(Arrays.equals(ba1, NetUtils.longToByteArray6(mac1)));
84
85         byte ba2[] = {
86             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
87             (byte) 0x00, (byte) 0x00
88         };
89         long mac2 = 0x000000000000L;
90         Assert.assertTrue(Arrays.equals(ba2, NetUtils.longToByteArray6(mac2)));
91
92         byte ba3[] = {
93             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x00,
94             (byte) 0x00, (byte) 0x00
95         };
96         long mac3 = 0xffffff000000L;
97         Assert.assertTrue(Arrays.equals(ba3, NetUtils.longToByteArray6(mac3)));
98
99         byte ba4[] = {
100             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0xff,
101             (byte) 0xff, (byte) 0xff
102         };
103         long mac4 = 0x000000ffffffL;
104         Assert.assertTrue(Arrays.equals(ba4, NetUtils.longToByteArray6(mac4)));
105
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);
110
111         Assert.assertTrue(NetUtils
112                 .byteArray6ToLong(NetUtils.longToByteArray6(mac1)) == mac1);
113
114         Assert.assertTrue(NetUtils
115                 .byteArray6ToLong(NetUtils.longToByteArray6(mac2)) == mac2);
116
117         Assert.assertTrue(NetUtils
118                 .byteArray6ToLong(NetUtils.longToByteArray6(mac3)) == mac3);
119
120         Assert.assertTrue(NetUtils
121                 .byteArray6ToLong(NetUtils.longToByteArray6(mac4)) == mac4);
122
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))));
127
128         Assert.assertTrue(Arrays.equals(ba1,
129                     NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba1))));
130
131         Assert.assertTrue(Arrays.equals(ba2,
132                     NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba2))));
133
134         Assert.assertTrue(Arrays.equals(ba3,
135                     NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba3))));
136
137         Assert.assertTrue(Arrays.equals(ba4,
138                     NetUtils.longToByteArray6(NetUtils.byteArray6ToLong(ba4))));
139
140         // Test of paramter validation of byteArray6ToLong method.
141         byte array5[] = {
142             (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44
143         };
144         Assert.assertEquals(0, NetUtils.byteArray6ToLong(array5));
145
146         byte array7[] = {
147             (byte) 0x11, (byte) 0x22, (byte) 0x33, (byte) 0x44,
148             (byte) 0x55, (byte) 0x66, (byte) 0x77
149         };
150         Assert.assertEquals(0, NetUtils.byteArray6ToLong(array7));
151
152         byte arrayNull[] = null;
153         Assert.assertEquals(0, NetUtils.byteArray6ToLong(arrayNull));
154     }
155
156     @Test
157     public void testInetMethods() throws UnknownHostException {
158         int ip = 0xfffffff0;
159         InetAddress inet = InetAddress.getByName("255.255.255.240");
160         Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
161
162         ip = 0;
163         inet = InetAddress.getByName("0.0.0.0");
164         Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
165
166         ip = 0x9ffff09;
167         inet = InetAddress.getByName("9.255.255.9");
168         Assert.assertTrue(inet.equals(NetUtils.getInetAddress(ip)));
169     }
170
171     @Test
172     public void testMasksV4() throws UnknownHostException {
173
174         InetAddress mask = InetAddress.getByName("128.0.0.0");
175         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, false)));
176
177         mask = InetAddress.getByName("192.0.0.0");
178         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(2, false)));
179
180         mask = InetAddress.getByName("224.0.0.0");
181         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(3, false)));
182
183         mask = InetAddress.getByName("240.0.0.0");
184         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(4, false)));
185
186         mask = InetAddress.getByName("248.0.0.0");
187         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, false)));
188
189         mask = InetAddress.getByName("252.0.0.0");
190         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(6, false)));
191
192         mask = InetAddress.getByName("254.0.0.0");
193         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(7, false)));
194
195         mask = InetAddress.getByName("255.0.0.0");
196         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, false)));
197
198         mask = InetAddress.getByName("255.128.0.0");
199         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(9, false)));
200
201         mask = InetAddress.getByName("255.192.0.0");
202         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(10, false)));
203
204         mask = InetAddress.getByName("255.224.0.0");
205         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(11, false)));
206
207         mask = InetAddress.getByName("255.240.0.0");
208         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(12, false)));
209
210         mask = InetAddress.getByName("255.248.0.0");
211         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(13, false)));
212
213         mask = InetAddress.getByName("255.252.0.0");
214         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(14, false)));
215
216         mask = InetAddress.getByName("255.254.0.0");
217         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(15, false)));
218
219         mask = InetAddress.getByName("255.255.0.0");
220         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(16, false)));
221
222         mask = InetAddress.getByName("255.255.128.0");
223         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(17, false)));
224
225         mask = InetAddress.getByName("255.255.192.0");
226         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(18, false)));
227
228         mask = InetAddress.getByName("255.255.224.0");
229         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(19, false)));
230
231         mask = InetAddress.getByName("255.255.240.0");
232         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(20, false)));
233
234         mask = InetAddress.getByName("255.255.248.0");
235         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(21, false)));
236
237         mask = InetAddress.getByName("255.255.252.0");
238         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(22, false)));
239
240         mask = InetAddress.getByName("255.255.254.0");
241         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(23, false)));
242
243         mask = InetAddress.getByName("255.255.255.0");
244         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(24, false)));
245
246         mask = InetAddress.getByName("255.255.255.128");
247         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(25, false)));
248
249         mask = InetAddress.getByName("255.255.255.192");
250         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(26, false)));
251
252         mask = InetAddress.getByName("255.255.255.224");
253         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(27, false)));
254
255         mask = InetAddress.getByName("255.255.255.240");
256         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(28, false)));
257
258         mask = InetAddress.getByName("255.255.255.248");
259         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(29, false)));
260
261         mask = InetAddress.getByName("255.255.255.252");
262         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(30, false)));
263
264         mask = InetAddress.getByName("255.255.255.254");
265         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(31, false)));
266
267         mask = InetAddress.getByName("255.255.255.255");
268         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(32, false)));
269     }
270
271     @Test
272     public void testMasksV6() throws UnknownHostException {
273
274         InetAddress mask = InetAddress.getByName("ff00::0");
275         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(8, true)));
276
277         mask = InetAddress.getByName("8000::0");
278         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(1, true)));
279
280         mask = InetAddress.getByName("f800::0");
281         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(5, true)));
282
283         mask = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
284         Assert.assertTrue(mask.equals(NetUtils.getInetNetworkMask(127, true)));
285     }
286
287     @Test
288     public void testGetSubnetLen() {
289
290         byte address[] = { (byte) 128, (byte) 0, (byte) 0, 0 };
291         Assert.assertTrue(NetUtils.getSubnetMaskLength(address) == 1);
292
293         byte address1[] = { (byte) 255, 0, 0, 0 };
294         Assert.assertTrue(NetUtils.getSubnetMaskLength(address1) == 8);
295
296         byte address2[] = { (byte) 255, (byte) 255, (byte) 248, 0 };
297         Assert.assertTrue(NetUtils.getSubnetMaskLength(address2) == 21);
298
299         byte address4[] = { (byte) 255, (byte) 255, (byte) 255, (byte) 254 };
300         Assert.assertTrue(NetUtils.getSubnetMaskLength(address4) == 31);
301
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,
304                 (byte) 255 };
305         Assert.assertTrue(NetUtils.getSubnetMaskLength(address5) == 128);
306
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,
309                 (byte) 254 };
310         Assert.assertTrue(NetUtils.getSubnetMaskLength(address6) == 127);
311
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);
315
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);
319
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);
323
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);
327
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);
331     }
332
333     @Test
334     public void testGetSubnetPrefix() throws UnknownHostException {
335         InetAddress ip = InetAddress.getByName("192.128.64.252");
336         int maskLen = 25;
337         Assert.assertTrue(NetUtils.getSubnetPrefix(ip, maskLen).equals(
338                 InetAddress.getByName("192.128.64.128")));
339     }
340
341     @Test
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 ::
349         Assert
350                 .assertTrue(NetUtils
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
356
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
367         Assert
368                 .assertFalse(NetUtils
369                         .isIPv6AddressValid("2001:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
370         Assert
371                 .assertFalse(NetUtils
372                         .isIPv6AddressValid("20010:0000:1234:0000:10001:C1C0:ABCD:0876")); //not valid, extra number
373
374         Assert
375                 .assertTrue(NetUtils
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
380
381         Assert.assertFalse(NetUtils.isIPv6AddressValid("124.15.6.89/60")); //invalid, ip with mask
382         Assert
383                 .assertFalse(NetUtils
384                         .isIPv6AddressValid("2001:0DB8:0000:CD30:0000:0000:0000:0000/130")); //invalid, mask >128
385         Assert
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
392
393     }
394
395     @Test
396     public void testInetAddressConflict() throws UnknownHostException {
397
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")));
404
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")));
411
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")));
419
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")));
428
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")));
437
438     }
439
440     @Test
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"));
446
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"));
451     }
452 }