blob: 63c9c8f321e3983cbaffdbe7809557f4d866b043 [file] [log] [blame]
Gregor Maier1ff55972013-12-11 02:22:56 -08001package org.projectfloodlight.openflow.types;
2
Sovietaced88e74ec2014-06-16 23:02:45 -07003import static org.junit.Assert.assertFalse;
Sovietacedc6f91212014-06-13 18:36:28 -07004import static org.junit.Assert.assertNotNull;
Sovietaced88e74ec2014-06-16 23:02:45 -07005import static org.junit.Assert.assertTrue;
Sovietacedc6f91212014-06-13 18:36:28 -07006import static org.junit.Assert.fail;
7
8import java.net.UnknownHostException;
Gregor Maier1ff55972013-12-11 02:22:56 -08009
10import org.junit.Test;
11
12/**
13 * Most tests are in IPv4AddressTest and IPv6AddressTest
14 * Just exception testing here
15 * @author gregor
16 *
17 */
18public class IPAddressTest {
19 @Test
20 public void testOfException() {
21 try {
22 IPAddress.of("Foobar");
23 fail("Should have thrown IllegalArgumentException");
24 } catch (IllegalArgumentException e) {
25 // expected
26 }
27 try {
28 IPAddressWithMask.of("Foobar");
29 fail("Should have thrown IllegalArgumentException");
30 } catch (IllegalArgumentException e) {
31 // expected
32 }
33 try {
Ronald Liffa80792014-07-04 17:50:49 -070034 IPAddress.of((String) null);
Gregor Maier1ff55972013-12-11 02:22:56 -080035 fail("Should have thrown NullPointerException");
36 } catch (NullPointerException e) {
37 assertNotNull(e.getMessage());
38 }
39 try {
40 IPAddressWithMask.of(null);
41 fail("Should have thrown NullPointerException");
42 } catch (NullPointerException e) {
43 assertNotNull(e.getMessage());
44 }
45 try {
Ronald Liffa80792014-07-04 17:50:49 -070046 IPAddress.of((String) null);
Gregor Maier1ff55972013-12-11 02:22:56 -080047 fail("Should have thrown NullPointerException");
48 } catch (NullPointerException e) {
49 assertNotNull(e.getMessage());
50 }
51 try {
52 IPAddressWithMask.of(null);
53 fail("Should have thrown NullPointerException");
54 } catch (NullPointerException e) {
55 assertNotNull(e.getMessage());
56 }
57 }
58
Ronald Liffa80792014-07-04 17:50:49 -070059 @SuppressWarnings("deprecation")
Sovietacedc6f91212014-06-13 18:36:28 -070060 @Test
61 public void testFromInetAddressException() throws UnknownHostException {
62 try {
63 IPAddress.fromInetAddress(null);
64 fail("Should have thrown NullPointerException");
65 } catch (NullPointerException e) {
66 assertNotNull(e.getMessage());
67 }
68 }
69
Sovietaced88e74ec2014-06-16 23:02:45 -070070 @Test
71 public void testContains() {
72
73 // Test IPv4 Mask
74 IPAddressWithMask<?> mask = IPAddressWithMask.of("1.2.3.4/24");
75
76 IPAddress<?> validIp = IPAddress.of("1.2.3.5");
77 assertTrue(mask.contains(validIp));
78
79 IPAddress<?> invalidIp = IPAddress.of("1.2.5.5");
80 assertFalse(mask.contains(invalidIp));
81
82 IPAddress<?> invalidIpv6 = IPAddress.of("10:10::ffff");
83 assertFalse(mask.contains(invalidIpv6));
84
85 // Test IPv6 Mask
86 mask = IPAddressWithMask.of("10:10::1/112");
87
88 validIp = IPAddress.of("10:10::f");
89 assertTrue(mask.contains(validIp));
90
91 invalidIp = IPAddress.of("11:10::f");
92 assertFalse(mask.contains(invalidIp));
93
94 IPAddress<?> invalidIpv4 = IPAddress.of("10.0.0.1");
95 assertFalse(mask.contains(invalidIpv4));
96 }
97
Sovietaced07cc8eb2014-06-24 11:55:26 -070098 @Test
99 public void testContainsException() {
Sovietaced30a1cee2014-06-16 23:04:40 -0700100 try {
101 IPAddressWithMask<?> mask = IPAddressWithMask.of("1.2.3.4/24");
102 mask.contains(null);
103 fail("Should have thrown NullPointerException");
104 } catch (NullPointerException e) {
105 assertNotNull(e.getMessage());
106 }
107 }
108
Gregor Maier1ff55972013-12-11 02:22:56 -0800109}