blob: 38d60b322e0122f808fc6d444a1e184340ecf0b9 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import static org.junit.Assert.assertArrayEquals;
4import static org.junit.Assert.assertEquals;
Andreas Wundsame04c86f2013-11-05 17:13:18 -08005import static org.junit.Assert.assertThat;
Yotam Harcholf3f11152013-09-05 16:47:16 -07006import static org.junit.Assert.fail;
7
Andreas Wundsame04c86f2013-11-05 17:13:18 -08008import org.hamcrest.CoreMatchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -07009import org.jboss.netty.buffer.ChannelBuffers;
10import org.junit.Test;
11import org.projectfloodlight.openflow.exceptions.OFParseError;
12
Yotam Harchola289d552013-09-16 10:10:40 -070013public class IPv4AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070014 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 Wundsame04c86f2013-11-05 17:13:18 -080052 "0.0.0.0/0"
Yotam Harcholf3f11152013-09-05 16:47:16 -070053 };
54
55 boolean[] hasMask = {
56 true,
57 true,
58 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080059 false,
60 true
Yotam Harcholf3f11152013-09-05 16:47:16 -070061 };
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 Wundsame04c86f2013-11-05 17:13:18 -080067 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 Harcholf3f11152013-09-05 16:47:16 -070069 };
70
71
72 @Test
73 public void testOfString() {
74 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -070075 IPv4Address ip = IPv4Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -070076 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 Harchola289d552013-09-16 10:10:40 -070085 IPv4Address ip = IPv4Address.of(testAddresses[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -070086 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 Harchola289d552013-09-16 10:10:40 -070095 IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
Yotam Harcholf3f11152013-09-05 16:47:16 -070096 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 Harchola289d552013-09-16 10:10:40 -0700107 IPv4Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700108 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 Harchol9a617aa2013-09-16 14:10:17 -0700118 IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700119 if (!hasMask[i]) {
Yotam Harchola289d552013-09-16 10:10:40 -0700120 IPv4Address ip = value.getValue();
Yotam Harcholf3f11152013-09-05 16:47:16 -0700121 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 Wundsame04c86f2013-11-05 17:13:18 -0800131 assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
132 value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700133 }
134 }
135 }
136}