blob: cbab734d07e5021eaa7e22a9bf78f6a1a2c1542b [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 {
34 IPAddress.of(null);
35 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 {
46 IPAddress.of(null);
47 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
Sovietacedc6f91212014-06-13 18:36:28 -070059 @Test
60 public void testFromInetAddressException() throws UnknownHostException {
61 try {
62 IPAddress.fromInetAddress(null);
63 fail("Should have thrown NullPointerException");
64 } catch (NullPointerException e) {
65 assertNotNull(e.getMessage());
66 }
67 }
68
Sovietaced88e74ec2014-06-16 23:02:45 -070069 @Test
70 public void testContains() {
71
72 // Test IPv4 Mask
73 IPAddressWithMask<?> mask = IPAddressWithMask.of("1.2.3.4/24");
74
75 IPAddress<?> validIp = IPAddress.of("1.2.3.5");
76 assertTrue(mask.contains(validIp));
77
78 IPAddress<?> invalidIp = IPAddress.of("1.2.5.5");
79 assertFalse(mask.contains(invalidIp));
80
81 IPAddress<?> invalidIpv6 = IPAddress.of("10:10::ffff");
82 assertFalse(mask.contains(invalidIpv6));
83
84 // Test IPv6 Mask
85 mask = IPAddressWithMask.of("10:10::1/112");
86
87 validIp = IPAddress.of("10:10::f");
88 assertTrue(mask.contains(validIp));
89
90 invalidIp = IPAddress.of("11:10::f");
91 assertFalse(mask.contains(invalidIp));
92
93 IPAddress<?> invalidIpv4 = IPAddress.of("10.0.0.1");
94 assertFalse(mask.contains(invalidIpv4));
95 }
96
Sovietaced07cc8eb2014-06-24 11:55:26 -070097 @Test
98 public void testContainsException() {
Sovietaced30a1cee2014-06-16 23:04:40 -070099 try {
100 IPAddressWithMask<?> mask = IPAddressWithMask.of("1.2.3.4/24");
101 mask.contains(null);
102 fail("Should have thrown NullPointerException");
103 } catch (NullPointerException e) {
104 assertNotNull(e.getMessage());
105 }
106 }
107
Gregor Maier1ff55972013-12-11 02:22:56 -0800108}