blob: 5d95a5a3e0d8710ece2a662ad312a10d385d8eff [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
Ronald Li5d0ca652014-07-05 02:14:11 -070012import java.net.Inet4Address;
13import java.net.InetAddress;
14
Andreas Wundsame04c86f2013-11-05 17:13:18 -080015import org.hamcrest.CoreMatchers;
Ronald Li9d777a52015-02-07 06:19:29 -080016import org.hamcrest.Matchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -070017import org.jboss.netty.buffer.ChannelBuffers;
18import org.junit.Test;
19import org.projectfloodlight.openflow.exceptions.OFParseError;
20
Yotam Harchola289d552013-09-16 10:10:40 -070021public class IPv4AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070022 byte[][] testAddresses = new byte[][] {
23 {0x01, 0x02, 0x03, 0x04 },
24 {127, 0, 0, 1},
25 {(byte) 192, (byte) 168, 0, 100 },
26 {(byte) 255, (byte) 255, (byte) 255, (byte) 255 }
27 };
28
29 String[] testStrings = {
30 "1.2.3.4",
31 "127.0.0.1",
32 "192.168.0.100",
33 "255.255.255.255"
34 };
35
36 int[] testInts = {
37 0x01020304,
38 0x7f000001,
39 (192 << 24) | (168 << 16) | 100,
40 0xffffffff
41 };
42
43 String[] invalidIPs = {
44 "",
45 ".",
46 "1.2..3.4",
47 "1.2.3.4.",
48 "257.11.225.1",
Gregor Maier7f987e62013-12-10 19:34:18 -080049 "256.11.225.1",
Yotam Harcholf3f11152013-09-05 16:47:16 -070050 "-1.2.3.4",
51 "1.2.3.4.5",
52 "1.x.3.4",
53 "1.2x.3.4"
54 };
55
56 String[] ipsWithMask = {
57 "1.2.3.4/24",
58 "192.168.130.140/255.255.192.0",
59 "127.0.0.1/8",
60 "8.8.8.8",
Gregor Maier7f987e62013-12-10 19:34:18 -080061 "8.8.8.8/32",
62 "0.0.0.0/0",
63 "192.168.130.140/255.0.255.0",
64 "1.2.3.4/0.127.0.255"
Yotam Harcholf3f11152013-09-05 16:47:16 -070065 };
66
67 boolean[] hasMask = {
68 true,
69 true,
70 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080071 false,
Gregor Maier7f987e62013-12-10 19:34:18 -080072 false,
73 true,
74 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080075 true
Yotam Harcholf3f11152013-09-05 16:47:16 -070076 };
77
78 byte[][][] ipsWithMaskValues = {
79 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } },
80 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } },
81 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 -080082 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
83 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
84 new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } },
85 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00 } },
86 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0xFF } }
87 };
88
89 int[] ipsWithMaskLengths = {
90 24,
91 18,
92 8,
93 32,
94 32,
95 0,
96 -1,
97 -1
98 };
99
100 String[] invalidIpsWithMask = {
101 "asdf",
102 "1.2.3.4/33",
103 "1.2.3.4/34",
104 "1.2.3.4/-1",
105 "1.2.3.4/256.0.0.0",
106 "1.256.3.4/255.255.0.0",
107 "1.2.3.4/255.255.0.0.0",
Yotam Harcholf3f11152013-09-05 16:47:16 -0700108 };
109
Andreas Wundsam12cea882013-12-15 15:05:50 -0800110 @Test
Aditya Vaja56b8b182014-03-11 13:13:58 -0700111 public void testLogicalOperatorsBroadcast() {
112 assertTrue(IPv4Address.NO_MASK.not().equals(IPv4Address.FULL_MASK));
113 assertTrue(IPv4Address.NO_MASK.or(IPv4Address.FULL_MASK).
114 equals(IPv4Address.NO_MASK));
115 assertTrue(IPv4Address.NO_MASK.and(IPv4Address.FULL_MASK).
116 equals(IPv4Address.FULL_MASK));
117
118 assertTrue(IPv4Address.NO_MASK.isBroadcast());
119 assertTrue(!IPv4Address.FULL_MASK.isBroadcast());
120 }
121
122 @Test
123 public void testMaskedSubnetBroadcast() {
124 assertTrue(IPv4AddressWithMask.of("10.10.10.1/24")
125 .getSubnetBroadcastAddress()
126 .equals(IPv4Address.of("10.10.10.255")));
127 assertTrue(IPv4AddressWithMask.of("10.10.10.1/24")
128 .isSubnetBroadcastAddress(IPv4Address.of("10.10.10.255")));
129 assertTrue(!IPv4AddressWithMask.of("10.10.10.1/24")
130 .isSubnetBroadcastAddress(IPv4Address.of("10.10.10.254")));
131 }
132
133 @Test
Andreas Wundsam12cea882013-12-15 15:05:50 -0800134 public void testMaskedMatchesCidr() {
135 IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("10.0.42.16/28");
136
137 String[] notContained = {"0.0.0.0", "11.0.42.16", "10.0.41.1", "10.0.42.0", "10.0.42.15",
138 "10.0.42.32", "255.255.255.255" };
139
140 for(String n: notContained) {
141 assertThat(String.format("slash 28 %s should not contain address %s",
142 slash28, n),
143 slash28.matches(IPv4Address.of(n)), equalTo(false));
144 }
145 for(int i=16; i < 32; i++) {
146 IPv4Address c = IPv4Address.of(String.format("10.0.42.%d", i));
147 assertThat(String.format("slash 28 %s should contain address %s",
148 slash28, c),
149 slash28.matches(c), equalTo(true));
150 }
151 }
152
153 @Test
154 public void testMaskedMatchesArbitrary() {
155 // irregular octect on the 3rd bitmask requires '1'bit to be set
156 // 4 bit unset, all others arbitrary
157 IPv4AddressWithMask slash28 = IPv4AddressWithMask.of("1.2.1.4/255.255.5.255");
158
159 String[] notContained = {"0.0.0.0", "1.2.3.5", "1.2.3.3",
160 "1.2.0.4", "1.2.2.4", "1.2.4.4", "1.2.5.4", "1.2.6.4", "1.2.7.4",
161 "1.2.8.4", "1.2.12.4", "1.2.13.4"
162 };
163 String[] contained = {"1.2.1.4", "1.2.3.4", "1.2.9.4", "1.2.11.4", "1.2.251.4",
164 };
165
166 for(String n: notContained) {
167 assertThat(String.format("slash 28 %s should not contain address %s",
168 slash28, n),
169 slash28.matches(IPv4Address.of(n)), equalTo(false));
170 }
171 for(String c: contained) {
172 IPv4Address addr = IPv4Address.of(c);
173 assertThat(String.format("slash 28 %s should contain address %s",
174 slash28, addr),
175 slash28.matches(addr), equalTo(true));
176 }
177
178 }
179
Yotam Harcholf3f11152013-09-05 16:47:16 -0700180
181 @Test
Gregor Maier5615b6c2013-12-11 22:29:07 -0800182 public void testConstants() {
183 byte[] zeros = { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 };
184 byte[] ones = { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF };
185 // Make sure class initializtation and static assignment don't get
186 // messed up. Test everything twice for cached values
187 assertTrue(IPv4Address.NONE.isCidrMask());
188 assertEquals(0, IPv4Address.NONE.asCidrMaskLength());
189 assertArrayEquals(zeros, IPv4Address.NONE.getBytes());
190 assertTrue(IPv4Address.NONE.isCidrMask());
191 assertEquals(0, IPv4Address.NONE.asCidrMaskLength());
192 assertArrayEquals(zeros, IPv4Address.NONE.getBytes());
193
194 assertTrue(IPv4Address.NO_MASK.isCidrMask());
195 assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength());
196 assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes());
197 assertTrue(IPv4Address.NO_MASK.isCidrMask());
198 assertEquals(32, IPv4Address.NO_MASK.asCidrMaskLength());
199 assertArrayEquals(ones, IPv4Address.NO_MASK.getBytes());
200
201 assertTrue(IPv4Address.FULL_MASK.isCidrMask());
202 assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength());
203 assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes());
204 assertTrue(IPv4Address.FULL_MASK.isCidrMask());
205 assertEquals(0, IPv4Address.FULL_MASK.asCidrMaskLength());
206 assertArrayEquals(zeros, IPv4Address.FULL_MASK.getBytes());
207 }
208
209
210 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700211 public void testOfString() {
212 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700213 IPv4Address ip = IPv4Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700214 assertEquals(testInts[i], ip.getInt());
215 assertArrayEquals(testAddresses[i], ip.getBytes());
216 assertEquals(testStrings[i], ip.toString());
217 }
218 }
219
220 @Test
221 public void testOfByteArray() {
222 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700223 IPv4Address ip = IPv4Address.of(testAddresses[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700224 assertEquals(testInts[i], ip.getInt());
225 assertArrayEquals(testAddresses[i], ip.getBytes());
226 assertEquals(testStrings[i], ip.toString());
227 }
228 }
229
230 @Test
Ronald Lia7484222014-07-03 17:14:12 -0700231 public void testOfCidrMaskLength() {
232 for (int i = 0; i <= 32; i++) {
233 assertEquals(IPv4Address.ofCidrMaskLength(i).asCidrMaskLength(), i);
234 }
235
236 assertEquals(IPv4Address.ofCidrMaskLength(0).getInt(), 0x0000_0000);
237
238 assertEquals(IPv4Address.ofCidrMaskLength(1).getInt(), 0x8000_0000);
239 assertEquals(IPv4Address.ofCidrMaskLength(2).getInt(), 0xC000_0000);
240 assertEquals(IPv4Address.ofCidrMaskLength(3).getInt(), 0xE000_0000);
241 assertEquals(IPv4Address.ofCidrMaskLength(4).getInt(), 0xF000_0000);
242 assertEquals(IPv4Address.ofCidrMaskLength(5).getInt(), 0xF800_0000);
243 assertEquals(IPv4Address.ofCidrMaskLength(6).getInt(), 0xFC00_0000);
244 assertEquals(IPv4Address.ofCidrMaskLength(7).getInt(), 0xFE00_0000);
245 assertEquals(IPv4Address.ofCidrMaskLength(8).getInt(), 0xFF00_0000);
246
247 assertEquals(IPv4Address.ofCidrMaskLength(9).getInt(), 0xFF80_0000);
248 assertEquals(IPv4Address.ofCidrMaskLength(10).getInt(), 0xFFC0_0000);
249 assertEquals(IPv4Address.ofCidrMaskLength(11).getInt(), 0xFFE0_0000);
250 assertEquals(IPv4Address.ofCidrMaskLength(12).getInt(), 0xFFF0_0000);
251 assertEquals(IPv4Address.ofCidrMaskLength(13).getInt(), 0xFFF8_0000);
252 assertEquals(IPv4Address.ofCidrMaskLength(14).getInt(), 0xFFFC_0000);
253 assertEquals(IPv4Address.ofCidrMaskLength(15).getInt(), 0xFFFE_0000);
254 assertEquals(IPv4Address.ofCidrMaskLength(16).getInt(), 0xFFFF_0000);
255
256 assertEquals(IPv4Address.ofCidrMaskLength(17).getInt(), 0xFFFF_8000);
257 assertEquals(IPv4Address.ofCidrMaskLength(18).getInt(), 0xFFFF_C000);
258 assertEquals(IPv4Address.ofCidrMaskLength(19).getInt(), 0xFFFF_E000);
259 assertEquals(IPv4Address.ofCidrMaskLength(20).getInt(), 0xFFFF_F000);
260 assertEquals(IPv4Address.ofCidrMaskLength(21).getInt(), 0xFFFF_F800);
261 assertEquals(IPv4Address.ofCidrMaskLength(22).getInt(), 0xFFFF_FC00);
262 assertEquals(IPv4Address.ofCidrMaskLength(23).getInt(), 0xFFFF_FE00);
263 assertEquals(IPv4Address.ofCidrMaskLength(24).getInt(), 0xFFFF_FF00);
264
265 assertEquals(IPv4Address.ofCidrMaskLength(25).getInt(), 0xFFFF_FF80);
266 assertEquals(IPv4Address.ofCidrMaskLength(26).getInt(), 0xFFFF_FFC0);
267 assertEquals(IPv4Address.ofCidrMaskLength(27).getInt(), 0xFFFF_FFE0);
268 assertEquals(IPv4Address.ofCidrMaskLength(28).getInt(), 0xFFFF_FFF0);
269 assertEquals(IPv4Address.ofCidrMaskLength(29).getInt(), 0xFFFF_FFF8);
270 assertEquals(IPv4Address.ofCidrMaskLength(30).getInt(), 0xFFFF_FFFC);
271 assertEquals(IPv4Address.ofCidrMaskLength(31).getInt(), 0xFFFF_FFFE);
272 assertEquals(IPv4Address.ofCidrMaskLength(32).getInt(), 0xFFFF_FFFF);
273 }
274
275 @Test
Ronald Li5d0ca652014-07-05 02:14:11 -0700276 public void testWithMask() throws Exception {
277 // Sanity tests for the withMask*() syntactic sugars
278
279 IPv4Address original = IPv4Address.of("192.168.1.101");
280 IPv4Address expectedValue = IPv4Address.of("192.168.1.0");
281 IPv4Address expectedMask = IPv4Address.of("255.255.255.0");
282
283 IPv4AddressWithMask v;
284
Ronald Libbf01942014-07-07 17:00:13 -0700285 v = original.withMask(IPv4Address.of(new byte[] {-1, -1, -1, 0}));
Ronald Lica08d5f2014-07-05 02:43:18 -0700286 assertEquals(v.getValue(), expectedValue);
287 assertEquals(v.getMask(), expectedMask);
288
Ronald Libbf01942014-07-07 17:00:13 -0700289 v = original.withMask(IPv4Address.of(0xFFFF_FF00));
Ronald Li5d0ca652014-07-05 02:14:11 -0700290 assertEquals(v.getValue(), expectedValue);
291 assertEquals(v.getMask(), expectedMask);
292
Ronald Libbf01942014-07-07 17:00:13 -0700293 v = original.withMask(IPv4Address.of("255.255.255.0"));
Ronald Li5d0ca652014-07-05 02:14:11 -0700294 assertEquals(v.getValue(), expectedValue);
295 assertEquals(v.getMask(), expectedMask);
296
297 Inet4Address i4a = (Inet4Address) InetAddress.getByName("255.255.255.0");
Ronald Libbf01942014-07-07 17:00:13 -0700298 v = original.withMask(IPv4Address.of(i4a));
Ronald Li5d0ca652014-07-05 02:14:11 -0700299 assertEquals(v.getValue(), expectedValue);
300 assertEquals(v.getMask(), expectedMask);
301
302 v = original.withMaskOfLength(24);
303 assertEquals(v.getValue(), expectedValue);
304 assertEquals(v.getMask(), expectedMask);
305 }
306
307 @Test
Yotam Harcholf3f11152013-09-05 16:47:16 -0700308 public void testReadFrom() throws OFParseError {
309 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700310 IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700311 assertEquals(testInts[i], ip.getInt());
312 assertArrayEquals(testAddresses[i], ip.getBytes());
313 assertEquals(testStrings[i], ip.toString());
314 }
315 }
316
317
318 @Test
319 public void testInvalidIPs() throws OFParseError {
320 for(String invalid : invalidIPs) {
321 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700322 IPv4Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700323 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
324 } catch(IllegalArgumentException e) {
325 // ok
326 }
327 }
328 }
329
330 @Test
331 public void testOfMasked() throws OFParseError {
332 for (int i = 0; i < ipsWithMask.length; i++) {
Yotam Harchol9a617aa2013-09-16 14:10:17 -0700333 IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700334 if (!hasMask[i]) {
Yotam Harchola289d552013-09-16 10:10:40 -0700335 IPv4Address ip = value.getValue();
Yotam Harcholf3f11152013-09-05 16:47:16 -0700336 assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700337 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800338 IPv4Address mask = value.getMask();
Gregor Maier5615b6c2013-12-11 22:29:07 -0800339 if (ipsWithMaskLengths[i] == -1) {
340 assertFalse(mask.isCidrMask());
341 try {
342 mask.asCidrMaskLength();
343 fail("Expected IllegalStateException not thrown");
344 } catch(IllegalStateException e) {
345 //expected
346 }
347 } else {
348 assertTrue(mask.isCidrMask());
349 assertEquals(ipsWithMaskLengths[i], mask.asCidrMaskLength());
350 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800351 assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes());
352 byte[] ipBytes = new byte[4];
353 System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
354 assertEquals(ipBytes.length, value.getValue().getBytes().length);
355 for (int j = 0; j < ipBytes.length; j++) {
356 ipBytes[j] &= ipsWithMaskValues[i][1][j];
357 }
358
359 assertArrayEquals(ipBytes, value.getValue().getBytes());
360 assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
361 value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
362 }
363 }
364
365 @Test
366 public void testOfMaskedInvalid() throws Exception {
367 for(String invalid : invalidIpsWithMask) {
368 try {
369 IPv4Address.of(invalid);
370 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
371 } catch(IllegalArgumentException e) {
372 // ok
373 }
374 }
375 }
376
377 @Test
378 public void testSuperclass() throws Exception {
379 for(String ipString: testStrings) {
380 IPAddress<?> superIp = IPAddress.of(ipString);
381 assertEquals(IPVersion.IPv4, superIp.getIpVersion());
382 assertEquals(IPv4Address.of(ipString), superIp);
383 }
384
385 for(String ipMaskedString: ipsWithMask) {
386 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
387 assertEquals(IPVersion.IPv4, superIp.getIpVersion());
388 assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700389 }
390 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800391
392 @Test
Ronald Li9d777a52015-02-07 06:19:29 -0800393 public void testCompareTo() {
394 assertThat(
395 IPv4Address.of("1.0.0.1").compareTo(IPv4Address.of("1.0.0.2")),
396 Matchers.lessThan(0));
397 assertThat(
398 IPv4Address.of("1.0.0.3").compareTo(IPv4Address.of("3.0.0.1")),
399 Matchers.lessThan(0));
400
401 // Make sure that unsigned comparison is used
402 assertThat(
403 IPv4Address.of("201.0.0.1").compareTo(IPv4Address.NONE),
404 Matchers.greaterThan(0));
405 }
406
407 @Test
Gregor Maier1ff55972013-12-11 02:22:56 -0800408 public void testOfExceptions() {
409 // We check if the message of a caught NPE is set to a useful message
410 // as a hacky way of verifying that we got an NPE thrown by use rather
411 // than one the JVM created for a null access.
412 try {
413 String s = null;
414 IPv4Address.of(s);
415 fail("Should have thrown NullPointerException");
416 } catch (NullPointerException e) {
417 assertNotNull(e.getMessage());
418 }
419 try {
420 byte[] b = null;
421 IPv4Address.of(b);
422 fail("Should have thrown NullPointerException");
423 } catch (NullPointerException e) {
424 assertNotNull(e.getMessage());
425 }
426 try {
427 byte[] b = new byte[3];
428 IPv4Address.of(b);
429 fail("Should have thrown IllegalArgumentException");
430 } catch (IllegalArgumentException e) {
431 // expected
432 }
433 try {
434 byte[] b = new byte[5];
435 IPv4Address.of(b);
436 fail("Should have thrown IllegalArgumentException");
437 } catch (IllegalArgumentException e) {
438 // expected
439 }
440 try {
441 IPv4AddressWithMask.of(null);
442 fail("Should have thrown NullPointerException");
443 } catch (NullPointerException e) {
444 assertNotNull(e.getMessage());
445 }
446 try {
447 IPv4AddressWithMask.of(IPv4Address.of("1.2.3.4"), null);
448 fail("Should have thrown NullPointerException");
449 } catch (NullPointerException e) {
450 assertNotNull(e.getMessage());
451 }
452 try {
453 IPv4AddressWithMask.of(null, IPv4Address.of("255.0.0.0"));
454 fail("Should have thrown NullPointerException");
455 } catch (NullPointerException e) {
456 assertNotNull(e.getMessage());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700457 }
Aditya Vajab21043d2014-03-11 13:34:06 -0700458 try {
459 IPv4AddressWithMask.of(IPv4Address.of("10.10.10.0"),
460 IPv4Address.of("255.0.255.0"))
461 .getSubnetBroadcastAddress();
462 fail("Should have thrown IllegalArgumentException");
463 } catch (IllegalArgumentException e) {
464 assertNotNull(e.getMessage());
465 }
Ronald Lia7484222014-07-03 17:14:12 -0700466 try {
467 IPv4Address.ofCidrMaskLength(-1);
468 fail("Should have thrown IllegalArgumentException");
469 } catch (IllegalArgumentException e) {
470 assertNotNull(e.getMessage());
471 }
472 try {
473 IPv4Address.ofCidrMaskLength(33);
474 fail("Should have thrown IllegalArgumentException");
475 } catch (IllegalArgumentException e) {
476 assertNotNull(e.getMessage());
477 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700478 }
479}