tom | 0eb04ca | 2014-08-25 14:34:51 -0700 | [diff] [blame^] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import static org.junit.Assert.assertEquals; |
| 4 | import static org.junit.Assert.assertFalse; |
| 5 | import static org.junit.Assert.assertNotNull; |
| 6 | import static org.junit.Assert.assertTrue; |
| 7 | import static org.junit.Assert.fail; |
| 8 | |
| 9 | import java.net.InetAddress; |
| 10 | import java.net.UnknownHostException; |
| 11 | |
| 12 | import org.junit.Test; |
| 13 | |
| 14 | /** |
| 15 | * Most tests are in IPv4AddressTest and IPv6AddressTest |
| 16 | * Just exception testing here |
| 17 | * @author gregor |
| 18 | * |
| 19 | */ |
| 20 | public class IPAddressTest { |
| 21 | @Test |
| 22 | public void testOfException() { |
| 23 | try { |
| 24 | IPAddress.of("Foobar"); |
| 25 | fail("Should have thrown IllegalArgumentException"); |
| 26 | } catch (IllegalArgumentException e) { |
| 27 | // expected |
| 28 | } |
| 29 | try { |
| 30 | IPAddressWithMask.of("Foobar"); |
| 31 | fail("Should have thrown IllegalArgumentException"); |
| 32 | } catch (IllegalArgumentException e) { |
| 33 | // expected |
| 34 | } |
| 35 | try { |
| 36 | IPAddress.of((String) null); |
| 37 | fail("Should have thrown NullPointerException"); |
| 38 | } catch (NullPointerException e) { |
| 39 | assertNotNull(e.getMessage()); |
| 40 | } |
| 41 | try { |
| 42 | IPAddressWithMask.of(null); |
| 43 | fail("Should have thrown NullPointerException"); |
| 44 | } catch (NullPointerException e) { |
| 45 | assertNotNull(e.getMessage()); |
| 46 | } |
| 47 | try { |
| 48 | IPAddress.of((String) null); |
| 49 | fail("Should have thrown NullPointerException"); |
| 50 | } catch (NullPointerException e) { |
| 51 | assertNotNull(e.getMessage()); |
| 52 | } |
| 53 | try { |
| 54 | IPAddressWithMask.of(null); |
| 55 | fail("Should have thrown NullPointerException"); |
| 56 | } catch (NullPointerException e) { |
| 57 | assertNotNull(e.getMessage()); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | @Test |
| 62 | public void testOfString() { |
| 63 | IPAddress<?> ip0 = IPAddress.of("1.2.3.4"); |
| 64 | IPAddress<?> ip1 = IPAddress.of("abcd::1234"); |
| 65 | assertTrue(ip0 instanceof IPv4Address); |
| 66 | assertTrue(ip1 instanceof IPv6Address); |
| 67 | assertEquals(ip0, IPv4Address.of("1.2.3.4")); |
| 68 | assertEquals(ip1, IPv6Address.of("abcd::1234")); |
| 69 | } |
| 70 | |
| 71 | @Test |
| 72 | public void testOfInetAddress() throws Exception { |
| 73 | InetAddress ia0 = InetAddress.getByName("192.168.1.123"); |
| 74 | InetAddress ia1 = InetAddress.getByName("fd00::4321"); |
| 75 | IPAddress<?> ip0 = IPAddress.of(ia0); |
| 76 | IPAddress<?> ip1 = IPAddress.of(ia1); |
| 77 | assertTrue(ip0 instanceof IPv4Address); |
| 78 | assertTrue(ip1 instanceof IPv6Address); |
| 79 | assertEquals(ip0, IPv4Address.of(ia0)); |
| 80 | assertEquals(ip1, IPv6Address.of(ia1)); |
| 81 | } |
| 82 | |
| 83 | @SuppressWarnings("deprecation") |
| 84 | @Test |
| 85 | public void testFromInetAddressException() throws UnknownHostException { |
| 86 | try { |
| 87 | IPAddress.fromInetAddress(null); |
| 88 | fail("Should have thrown NullPointerException"); |
| 89 | } catch (NullPointerException e) { |
| 90 | assertNotNull(e.getMessage()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testContains() { |
| 96 | |
| 97 | // Test IPv4 Mask |
| 98 | IPAddressWithMask<?> mask = IPAddressWithMask.of("1.2.3.4/24"); |
| 99 | |
| 100 | IPAddress<?> validIp = IPAddress.of("1.2.3.5"); |
| 101 | assertTrue(mask.contains(validIp)); |
| 102 | |
| 103 | IPAddress<?> invalidIp = IPAddress.of("1.2.5.5"); |
| 104 | assertFalse(mask.contains(invalidIp)); |
| 105 | |
| 106 | IPAddress<?> invalidIpv6 = IPAddress.of("10:10::ffff"); |
| 107 | assertFalse(mask.contains(invalidIpv6)); |
| 108 | |
| 109 | // Test IPv6 Mask |
| 110 | mask = IPAddressWithMask.of("10:10::1/112"); |
| 111 | |
| 112 | validIp = IPAddress.of("10:10::f"); |
| 113 | assertTrue(mask.contains(validIp)); |
| 114 | |
| 115 | invalidIp = IPAddress.of("11:10::f"); |
| 116 | assertFalse(mask.contains(invalidIp)); |
| 117 | |
| 118 | IPAddress<?> invalidIpv4 = IPAddress.of("10.0.0.1"); |
| 119 | assertFalse(mask.contains(invalidIpv4)); |
| 120 | } |
| 121 | |
| 122 | @Test |
| 123 | public void testContainsException() { |
| 124 | try { |
| 125 | IPAddressWithMask<?> mask = IPAddressWithMask.of("1.2.3.4/24"); |
| 126 | mask.contains(null); |
| 127 | fail("Should have thrown NullPointerException"); |
| 128 | } catch (NullPointerException e) { |
| 129 | assertNotNull(e.getMessage()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | } |