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 | }; |
| 45 | |
| 46 | |
| 47 | @Test |
| 48 | public void testOfString() { |
| 49 | for(int i=0; i < testAddresses.length; i++ ) { |
| 50 | IPv4 ip = IPv4.of(testStrings[i]); |
| 51 | assertEquals(testInts[i], ip.getInt()); |
| 52 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 53 | assertEquals(testStrings[i], ip.toString()); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testOfByteArray() { |
| 59 | for(int i=0; i < testAddresses.length; i++ ) { |
| 60 | IPv4 ip = IPv4.of(testAddresses[i]); |
| 61 | assertEquals(testInts[i], ip.getInt()); |
| 62 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 63 | assertEquals(testStrings[i], ip.toString()); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Test |
| 68 | public void testReadFrom() throws OFParseError, OFShortRead { |
| 69 | for(int i=0; i < testAddresses.length; i++ ) { |
| 70 | IPv4 ip = IPv4.readFrom(ChannelBuffers.copiedBuffer(testAddresses[i])); |
| 71 | assertEquals(testInts[i], ip.getInt()); |
| 72 | assertArrayEquals(testAddresses[i], ip.getBytes()); |
| 73 | assertEquals(testStrings[i], ip.toString()); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | @Test |
| 79 | public void testInvalidIPs() throws OFParseError, OFShortRead { |
| 80 | for(String invalid : invalidIPs) { |
| 81 | try { |
| 82 | IPv4.of(invalid); |
| 83 | fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException"); |
| 84 | } catch(IllegalArgumentException e) { |
| 85 | // ok |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | } |