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