Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Sovietaced | 88e74ec | 2014-06-16 23:02:45 -0700 | [diff] [blame] | 3 | import static org.junit.Assert.assertFalse; |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 4 | import static org.junit.Assert.assertNotNull; |
Sovietaced | 88e74ec | 2014-06-16 23:02:45 -0700 | [diff] [blame] | 5 | import static org.junit.Assert.assertTrue; |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 6 | import static org.junit.Assert.fail; |
| 7 | |
| 8 | import java.net.UnknownHostException; |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 9 | |
| 10 | import org.junit.Test; |
| 11 | |
| 12 | /** |
| 13 | * Most tests are in IPv4AddressTest and IPv6AddressTest |
| 14 | * Just exception testing here |
| 15 | * @author gregor |
| 16 | * |
| 17 | */ |
| 18 | public 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 Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 34 | IPAddress.of((String) null); |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 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 { |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 46 | IPAddress.of((String) null); |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 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 | |
Ronald Li | ffa8079 | 2014-07-04 17:50:49 -0700 | [diff] [blame] | 59 | @SuppressWarnings("deprecation") |
Sovietaced | c6f9121 | 2014-06-13 18:36:28 -0700 | [diff] [blame] | 60 | @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 | |
Sovietaced | 88e74ec | 2014-06-16 23:02:45 -0700 | [diff] [blame] | 70 | @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 | |
Sovietaced | 07cc8eb | 2014-06-24 11:55:26 -0700 | [diff] [blame] | 98 | @Test |
| 99 | public void testContainsException() { |
Sovietaced | 30a1cee | 2014-06-16 23:04:40 -0700 | [diff] [blame] | 100 | 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 Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame] | 109 | } |