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