blob: 9f3025c4cc76f1adecabff742021318bf454af5a [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
Andreas Wundsam12cea882013-12-15 15:05:50 -08003import static org.hamcrest.CoreMatchers.equalTo;
Yotam Harcholf3f11152013-09-05 16:47:16 -07004import static org.junit.Assert.assertArrayEquals;
5import static org.junit.Assert.assertEquals;
Andreas Wundsamacaf6502013-12-15 15:08:21 -08006import static org.junit.Assert.assertFalse;
7import static org.junit.Assert.assertNotNull;
Andreas Wundsame04c86f2013-11-05 17:13:18 -08008import static org.junit.Assert.assertThat;
Andreas Wundsamacaf6502013-12-15 15:08:21 -08009import static org.junit.Assert.assertTrue;
Yotam Harcholf3f11152013-09-05 16:47:16 -070010import static org.junit.Assert.fail;
11
Andreas Wundsame04c86f2013-11-05 17:13:18 -080012import org.hamcrest.CoreMatchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -070013import org.jboss.netty.buffer.ChannelBuffers;
14import org.junit.Test;
15import org.projectfloodlight.openflow.exceptions.OFParseError;
16
Yotam Harchola289d552013-09-16 10:10:40 -070017public class IPv4AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070018 byte[][] testAddresses = new byte[][] {
19 {0x01, 0x02, 0x03, 0x04 },
20 {127, 0, 0, 1},
21 {(byte) 192, (byte) 168, 0, 100 },
22 {(byte) 255, (byte) 255, (byte) 255, (byte) 255 }
23 };
24
25 String[] testStrings = {
26 "1.2.3.4",
27 "127.0.0.1",
28 "192.168.0.100",
29 "255.255.255.255"
30 };
31
32 int[] testInts = {
33 0x01020304,
34 0x7f000001,
35 (192 << 24) | (168 << 16) | 100,
36 0xffffffff
37 };
38
39 String[] invalidIPs = {
40 "",
41 ".",
42 "1.2..3.4",
43 "1.2.3.4.",
44 "257.11.225.1",
Gregor Maier7f987e62013-12-10 19:34:18 -080045 "256.11.225.1",
Yotam Harcholf3f11152013-09-05 16:47:16 -070046 "-1.2.3.4",
47 "1.2.3.4.5",
48 "1.x.3.4",
49 "1.2x.3.4"
50 };
51
52 String[] ipsWithMask = {
53 "1.2.3.4/24",
54 "192.168.130.140/255.255.192.0",
55 "127.0.0.1/8",
56 "8.8.8.8",
Gregor Maier7f987e62013-12-10 19:34:18 -080057 "8.8.8.8/32",
58 "0.0.0.0/0",
59 "192.168.130.140/255.0.255.0",
60 "1.2.3.4/0.127.0.255"
Yotam Harcholf3f11152013-09-05 16:47:16 -070061 };
62
63 boolean[] hasMask = {
64 true,
65 true,
66 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080067 false,
Gregor Maier7f987e62013-12-10 19:34:18 -080068 false,
69 true,
70 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080071 true
Yotam Harcholf3f11152013-09-05 16:47:16 -070072 };
73
74 byte[][][] ipsWithMaskValues = {
75 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } },
76 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } },
77 new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } },
Gregor Maier7f987e62013-12-10 19:34:18 -080078 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
79 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
80 new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } },
81 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00 } },
82 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0xFF } }
83 };
84
85 int[] ipsWithMaskLengths = {
86 24,
87 18,
88 8,
89 32,
90 32,
91 0,
92 -1,
93 -1
94 };
95
96 String[] invalidIpsWithMask = {
97 "asdf",
98 "1.2.3.4/33",
99 "1.2.3.4/34",
100 "1.2.3.4/-1",
101 "1.2.3.4/256.0.0.0",
102 "1.256.3.4/255.255.0.0",
103 "1.2.3.4/255.255.0.0.0",
Yotam Harcholf3f11152013-09-05 16:47:16 -0700104 };
105
Andreas Wundsam12cea882013-12-15 15:05:50 -0800106 @Test
Aditya Vaja56b8b182014-03-11 13:13:58 -0700107 public void testLogicalOperatorsBroadcast() {
108 assertTrue(IPv4Address.NO_MASK.not().equals(IPv4Address.FULL_MASK));
109 assertTrue(IPv4Address.NO_MASK.or(IPv4Address.FULL_MASK).
110 equals(IPv4Address.NO_MASK));
111 assertTrue(IPv4Address.NO_MASK.and(IPv4Address.FULL_MASK).
112 equals(IPv4Address.FULL_MASK));
113
114 assertTrue(IPv4Address.NO_MASK.isBroadcast());
115 assertTrue(!IPv4Address.FULL_MASK.isBroadcast());
116 }
117
118 @Test
119 public void testMaskedSubnetBroadcast() {
120 assertTrue(IPv4AddressWithMask.of("10.10.10.1/24")
121 .getSubnetBroadcastAddress()
122 .equals(IPv4Address.of("10.10.10.255")));
123 assertTrue(IPv4AddressWithMask.of("10.10.10.1/24")
124 .isSubnetBroadcastAddress(IPv4Address.of("10.10.10.255")));
125 assertTrue(!IPv4AddressWithMask.of("10.10.10.1/24")
126 .isSubnetBroadcastAddress(IPv4Address.of("10.10.10.254")));
127 }
128
129 @Test
Andreas Wundsam12cea882013-12-15 15:05:50 -0800130 public void testMaskedMatchesCidr() {
131 IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("10.0.42.16/28");
132
133 String[] notContained = {"0.0.0.0", "11.0.42.16", "10.0.41.1", "10.0.42.0", "10.0.42.15",
134 "10.0.42.32", "255.255.255.255" };
135
136 for(String n: notContained) {
137 assertThat(String.format("slash 28 %s should not contain address %s",
138 slash28, n),
139 slash28.matches(IPv4Address.of(n)), equalTo(false));
140 }
141 for(int i=16; i < 32; i++) {
142 IPv4Address c = IPv4Address.of(String.format("10.0.42.%d", i));
143 assertThat(String.format("slash 28 %s should contain address %s",
144 slash28, c),
145 slash28.matches(c), equalTo(true));
146 }
147 }
148
149 @Test
150 public void testMaskedMatchesArbitrary() {
151 // irregular octect on the 3rd bitmask requires '1'bit to be set
152 // 4 bit unset, all others arbitrary
153 IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("1.2.1.4/255.255.5.255");
154
155 String[] notContained = {"0.0.0.0", "1.2.3.5", "1.2.3.3",
156 "1.2.0.4", "1.2.2.4", "1.2.4.4", "1.2.5.4", "1.2.6.4", "1.2.7.4",
157 "1.2.8.4", "1.2.12.4", "1.2.13.4"
158 };
159 String[] contained = {"1.2.1.4", "1.2.3.4", "1.2.9.4", "1.2.11.4", "1.2.251.4",
160 };
161
162 for(String n: notContained) {
163 assertThat(String.format("slash 28 %s should not contain address %s",
164 slash28, n),
165 slash28.matches(IPv4Address.of(n)), equalTo(false));
166 }
167 for(String c: contained) {
168 IPv4Address addr = IPv4Address.of(c);
169 assertThat(String.format("slash 28 %s should contain address %s",
170 slash28, addr),
171 slash28.matches(addr), equalTo(true));
172 }
173
174 }
175
Yotam Harcholf3f11152013-09-05 16:47:16 -0700176
177 @Test
Gregor Maier5615b6c2013-12-11 22:29:07 -0800178 public void testConstants() {
179 byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
180 byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
181 // Make sure class initializtation and static assignment don't get
182 // messed up. Test everything twice for cached values
183 assertTrue(IPv4Address.NONE.isCidrMask());
184 assertEquals(0, IPv4Address.NONE.asCidrMaskLength());
185 assertArrayEquals(zeros, IPv4Address.NONE.getBytes());
186 assertTrue(IPv4Address.NONE.isCidrMask());
187 assertEquals(0, IPv4Address.NONE.asCidrMaskLength());
188 assertArrayEquals(zeros, IPv4Address.NONE.getBytes());
189
190 assertTrue(IPv4Address.NO_MASK.isCidrMask());
191 assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength());
192 assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes());
193 assertTrue(IPv4Address.NO_MASK.isCidrMask());
194 assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength());
195 assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes());
196
197 assertTrue(IPv4Address.FULL_MASK.isCidrMask());
198 assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength());
199 assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes());
200 assertTrue(IPv4Address.FULL_MASK.isCidrMask());
201 assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength());
202 assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes());
203 }
204
205
206 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700207 public void testOfString() {
208 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700209 IPv4Address ip = IPv4Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700210 assertEquals(testInts[i], ip.getInt());
211 assertArrayEquals(testAddresses[i], ip.getBytes());
212 assertEquals(testStrings[i], ip.toString());
213 }
214 }
215
216 @Test
217 public void testOfByteArray() {
218 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700219 IPv4Address ip = IPv4Address.of(testAddresses[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700220 assertEquals(testInts[i], ip.getInt());
221 assertArrayEquals(testAddresses[i], ip.getBytes());
222 assertEquals(testStrings[i], ip.toString());
223 }
224 }
225
226 @Test
Ronald Lia7484222014-07-03 17:14:12 -0700227 public void testOfCidrMaskLength() {
228 for (int i = 0; i <= 32; i++) {
229 assertEquals(IPv4Address.ofCidrMaskLength(i).asCidrMaskLength(), i);
230 }
231
232 assertEquals(IPv4Address.ofCidrMaskLength(0).getInt(), 0x0000_0000);
233
234 assertEquals(IPv4Address.ofCidrMaskLength(1).getInt(), 0x8000_0000);
235 assertEquals(IPv4Address.ofCidrMaskLength(2).getInt(), 0xC000_0000);
236 assertEquals(IPv4Address.ofCidrMaskLength(3).getInt(), 0xE000_0000);
237 assertEquals(IPv4Address.ofCidrMaskLength(4).getInt(), 0xF000_0000);
238 assertEquals(IPv4Address.ofCidrMaskLength(5).getInt(), 0xF800_0000);
239 assertEquals(IPv4Address.ofCidrMaskLength(6).getInt(), 0xFC00_0000);
240 assertEquals(IPv4Address.ofCidrMaskLength(7).getInt(), 0xFE00_0000);
241 assertEquals(IPv4Address.ofCidrMaskLength(8).getInt(), 0xFF00_0000);
242
243 assertEquals(IPv4Address.ofCidrMaskLength(9).getInt(), 0xFF80_0000);
244 assertEquals(IPv4Address.ofCidrMaskLength(10).getInt(), 0xFFC0_0000);
245 assertEquals(IPv4Address.ofCidrMaskLength(11).getInt(), 0xFFE0_0000);
246 assertEquals(IPv4Address.ofCidrMaskLength(12).getInt(), 0xFFF0_0000);
247 assertEquals(IPv4Address.ofCidrMaskLength(13).getInt(), 0xFFF8_0000);
248 assertEquals(IPv4Address.ofCidrMaskLength(14).getInt(), 0xFFFC_0000);
249 assertEquals(IPv4Address.ofCidrMaskLength(15).getInt(), 0xFFFE_0000);
250 assertEquals(IPv4Address.ofCidrMaskLength(16).getInt(), 0xFFFF_0000);
251
252 assertEquals(IPv4Address.ofCidrMaskLength(17).getInt(), 0xFFFF_8000);
253 assertEquals(IPv4Address.ofCidrMaskLength(18).getInt(), 0xFFFF_C000);
254 assertEquals(IPv4Address.ofCidrMaskLength(19).getInt(), 0xFFFF_E000);
255 assertEquals(IPv4Address.ofCidrMaskLength(20).getInt(), 0xFFFF_F000);
256 assertEquals(IPv4Address.ofCidrMaskLength(21).getInt(), 0xFFFF_F800);
257 assertEquals(IPv4Address.ofCidrMaskLength(22).getInt(), 0xFFFF_FC00);
258 assertEquals(IPv4Address.ofCidrMaskLength(23).getInt(), 0xFFFF_FE00);
259 assertEquals(IPv4Address.ofCidrMaskLength(24).getInt(), 0xFFFF_FF00);
260
261 assertEquals(IPv4Address.ofCidrMaskLength(25).getInt(), 0xFFFF_FF80);
262 assertEquals(IPv4Address.ofCidrMaskLength(26).getInt(), 0xFFFF_FFC0);
263 assertEquals(IPv4Address.ofCidrMaskLength(27).getInt(), 0xFFFF_FFE0);
264 assertEquals(IPv4Address.ofCidrMaskLength(28).getInt(), 0xFFFF_FFF0);
265 assertEquals(IPv4Address.ofCidrMaskLength(29).getInt(), 0xFFFF_FFF8);
266 assertEquals(IPv4Address.ofCidrMaskLength(30).getInt(), 0xFFFF_FFFC);
267 assertEquals(IPv4Address.ofCidrMaskLength(31).getInt(), 0xFFFF_FFFE);
268 assertEquals(IPv4Address.ofCidrMaskLength(32).getInt(), 0xFFFF_FFFF);
269 }
270
271 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700272 public void testReadFrom() throws OFParseError {
273 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700274 IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700275 assertEquals(testInts[i], ip.getInt());
276 assertArrayEquals(testAddresses[i], ip.getBytes());
277 assertEquals(testStrings[i], ip.toString());
278 }
279 }
280
281
282 @Test
283 public void testInvalidIPs() throws OFParseError {
284 for(String invalid : invalidIPs) {
285 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700286 IPv4Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700287 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
288 } catch(IllegalArgumentException e) {
289 // ok
290 }
291 }
292 }
293
294 @Test
295 public void testOfMasked() throws OFParseError {
296 for (int i = 0; i < ipsWithMask.length; i++) {
Yotam Harchol9a617aa2013-09-16 14:10:17 -0700297 IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700298 if (!hasMask[i]) {
Yotam Harchola289d552013-09-16 10:10:40 -0700299 IPv4Address ip = value.getValue();
Yotam Harcholf3f11152013-09-05 16:47:16 -0700300 assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700301 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800302 IPv4Address mask = value.getMask();
Gregor Maier5615b6c2013-12-11 22:29:07 -0800303 if (ipsWithMaskLengths[i] == -1) {
304 assertFalse(mask.isCidrMask());
305 try {
306 mask.asCidrMaskLength();
307 fail("Expected IllegalStateException not thrown");
308 } catch(IllegalStateException e) {
309 //expected
310 }
311 } else {
312 assertTrue(mask.isCidrMask());
313 assertEquals(ipsWithMaskLengths[i], mask.asCidrMaskLength());
314 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800315 assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes());
316 byte[] ipBytes = new byte[4];
317 System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
318 assertEquals(ipBytes.length, value.getValue().getBytes().length);
319 for (int j = 0; j < ipBytes.length; j++) {
320 ipBytes[j] &= ipsWithMaskValues[i][1][j];
321 }
322
323 assertArrayEquals(ipBytes, value.getValue().getBytes());
324 assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
325 value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
326 }
327 }
328
329 @Test
330 public void testOfMaskedInvalid() throws Exception {
331 for(String invalid : invalidIpsWithMask) {
332 try {
333 IPv4Address.of(invalid);
334 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
335 } catch(IllegalArgumentException e) {
336 // ok
337 }
338 }
339 }
340
341 @Test
342 public void testSuperclass() throws Exception {
343 for(String ipString: testStrings) {
344 IPAddress<?> superIp = IPAddress.of(ipString);
345 assertEquals(IPVersion.IPv4, superIp.getIpVersion());
346 assertEquals(IPv4Address.of(ipString), superIp);
347 }
348
349 for(String ipMaskedString: ipsWithMask) {
350 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
351 assertEquals(IPVersion.IPv4, superIp.getIpVersion());
352 assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700353 }
354 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800355
356 @Test
357 public void testOfExceptions() {
358 // We check if the message of a caught NPE is set to a useful message
359 // as a hacky way of verifying that we got an NPE thrown by use rather
360 // than one the JVM created for a null access.
361 try {
362 String s = null;
363 IPv4Address.of(s);
364 fail("Should have thrown NullPointerException");
365 } catch (NullPointerException e) {
366 assertNotNull(e.getMessage());
367 }
368 try {
369 byte[] b = null;
370 IPv4Address.of(b);
371 fail("Should have thrown NullPointerException");
372 } catch (NullPointerException e) {
373 assertNotNull(e.getMessage());
374 }
375 try {
376 byte[] b = new byte[3];
377 IPv4Address.of(b);
378 fail("Should have thrown IllegalArgumentException");
379 } catch (IllegalArgumentException e) {
380 // expected
381 }
382 try {
383 byte[] b = new byte[5];
384 IPv4Address.of(b);
385 fail("Should have thrown IllegalArgumentException");
386 } catch (IllegalArgumentException e) {
387 // expected
388 }
389 try {
390 IPv4AddressWithMask.of(null);
391 fail("Should have thrown NullPointerException");
392 } catch (NullPointerException e) {
393 assertNotNull(e.getMessage());
394 }
395 try {
396 IPv4AddressWithMask.of(IPv4Address.of("1.2.3.4"), null);
397 fail("Should have thrown NullPointerException");
398 } catch (NullPointerException e) {
399 assertNotNull(e.getMessage());
400 }
401 try {
402 IPv4AddressWithMask.of(null, IPv4Address.of("255.0.0.0"));
403 fail("Should have thrown NullPointerException");
404 } catch (NullPointerException e) {
405 assertNotNull(e.getMessage());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700406 }
Aditya Vajab21043d2014-03-11 13:34:06 -0700407 try {
408 IPv4AddressWithMask.of(IPv4Address.of("10.10.10.0"),
409 IPv4Address.of("255.0.255.0"))
410 .getSubnetBroadcastAddress();
411 fail("Should have thrown IllegalArgumentException");
412 } catch (IllegalArgumentException e) {
413 assertNotNull(e.getMessage());
414 }
Ronald Lia7484222014-07-03 17:14:12 -0700415 try {
416 IPv4Address.ofCidrMaskLength(-1);
417 fail("Should have thrown IllegalArgumentException");
418 } catch (IllegalArgumentException e) {
419 assertNotNull(e.getMessage());
420 }
421 try {
422 IPv4Address.ofCidrMaskLength(33);
423 fail("Should have thrown IllegalArgumentException");
424 } catch (IllegalArgumentException e) {
425 assertNotNull(e.getMessage());
426 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700427 }
428}