Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
| 3 | import static org.junit.Assert.assertArrayEquals; |
| 4 | import static org.junit.Assert.assertEquals; |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 5 | import static org.junit.Assert.assertThat; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 6 | import static org.junit.Assert.fail; |
| 7 | |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 8 | import org.hamcrest.CoreMatchers; |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 9 | import org.jboss.netty.buffer.ChannelBuffers; |
| 10 | import org.junit.Test; |
| 11 | import org.projectfloodlight.openflow.exceptions.OFParseError; |
| 12 | |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 13 | public class IPv4AddressTest { |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 14 | byte[][] testAddresses = new byte[][] { |
| 15 | {0x01, 0x02, 0x03, 0x04 }, |
| 16 | {127, 0, 0, 1}, |
| 17 | {(byte) 192, (byte) 168, 0, 100 }, |
| 18 | {(byte) 255, (byte) 255, (byte) 255, (byte) 255 } |
| 19 | }; |
| 20 | |
| 21 | String[] testStrings = { |
| 22 | "1.2.3.4", |
| 23 | "127.0.0.1", |
| 24 | "192.168.0.100", |
| 25 | "255.255.255.255" |
| 26 | }; |
| 27 | |
| 28 | int[] testInts = { |
| 29 | 0x01020304, |
| 30 | 0x7f000001, |
| 31 | (192 << 24) | (168 << 16) | 100, |
| 32 | 0xffffffff |
| 33 | }; |
| 34 | |
| 35 | String[] invalidIPs = { |
| 36 | "", |
| 37 | ".", |
| 38 | "1.2..3.4", |
| 39 | "1.2.3.4.", |
| 40 | "257.11.225.1", |
| 41 | "-1.2.3.4", |
| 42 | "1.2.3.4.5", |
| 43 | "1.x.3.4", |
| 44 | "1.2x.3.4" |
| 45 | }; |
| 46 | |
| 47 | String[] ipsWithMask = { |
| 48 | "1.2.3.4/24", |
| 49 | "192.168.130.140/255.255.192.0", |
| 50 | "127.0.0.1/8", |
| 51 | "8.8.8.8", |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 52 | "0.0.0.0/0" |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | boolean[] hasMask = { |
| 56 | true, |
| 57 | true, |
| 58 | true, |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 59 | false, |
| 60 | true |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | byte[][][] ipsWithMaskValues = { |
| 64 | new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } }, |
| 65 | new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } }, |
| 66 | new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } }, |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 67 | new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, null }, |
| 68 | new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } } |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | |
| 72 | @Test |
| 73 | public void testOfString() { |
| 74 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 75 | IPv4Address ip = IPv4Address.of(testStrings[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 76 | assertEquals(testInts[i], ip.getInt()); |
| 77 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 78 | assertEquals(testStrings[i], ip.toString()); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testOfByteArray() { |
| 84 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 85 | IPv4Address ip = IPv4Address.of(testAddresses[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 86 | assertEquals(testInts[i], ip.getInt()); |
| 87 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 88 | assertEquals(testStrings[i], ip.toString()); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @Test |
| 93 | public void testReadFrom() throws OFParseError { |
| 94 | for(int i=0; i < testAddresses.length; i++ ) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 95 | IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i])); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 96 | assertEquals(testInts[i], ip.getInt()); |
| 97 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 98 | assertEquals(testStrings[i], ip.toString()); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | |
| 103 | @Test |
| 104 | public void testInvalidIPs() throws OFParseError { |
| 105 | for(String invalid : invalidIPs) { |
| 106 | try { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 107 | IPv4Address.of(invalid); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 108 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 109 | } catch(IllegalArgumentException e) { |
| 110 | // ok |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testOfMasked() throws OFParseError { |
| 117 | for (int i = 0; i < ipsWithMask.length; i++) { |
Yotam Harchol | 9a617aa | 2013-09-16 14:10:17 -0700 | [diff] [blame] | 118 | IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 119 | if (!hasMask[i]) { |
Yotam Harchol | a289d55 | 2013-09-16 10:10:40 -0700 | [diff] [blame] | 120 | IPv4Address ip = value.getValue(); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 121 | assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes()); |
| 122 | } else if (hasMask[i]) { |
| 123 | byte[] ipBytes = new byte[4]; |
| 124 | System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4); |
| 125 | assertEquals(ipBytes.length, value.getValue().getBytes().length); |
| 126 | for (int j = 0; j < ipBytes.length; j++) { |
| 127 | ipBytes[j] &= ipsWithMaskValues[i][1][j]; |
| 128 | } |
| 129 | |
| 130 | assertArrayEquals(ipBytes, value.getValue().getBytes()); |
Andreas Wundsam | e04c86f | 2013-11-05 17:13:18 -0800 | [diff] [blame] | 131 | assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value), |
| 132 | value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1])); |
Yotam Harchol | f3f1115 | 2013-09-05 16:47:16 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |