blob: 975c7d343fbc429a4012c71874e4270522532309 [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;
Gregor Maier1ff55972013-12-11 02:22:56 -08005import static org.junit.Assert.assertNotNull;
Andreas Wundsame04c86f2013-11-05 17:13:18 -08006import static org.junit.Assert.assertThat;
Yotam Harcholf3f11152013-09-05 16:47:16 -07007import static org.junit.Assert.fail;
8
Andreas Wundsame04c86f2013-11-05 17:13:18 -08009import org.hamcrest.CoreMatchers;
Yotam Harcholf3f11152013-09-05 16:47:16 -070010import org.jboss.netty.buffer.ChannelBuffers;
11import org.junit.Test;
12import org.projectfloodlight.openflow.exceptions.OFParseError;
13
Yotam Harchola289d552013-09-16 10:10:40 -070014public class IPv4AddressTest {
Yotam Harcholf3f11152013-09-05 16:47:16 -070015 byte[][] testAddresses = new byte[][] {
16 {0x01, 0x02, 0x03, 0x04 },
17 {127, 0, 0, 1},
18 {(byte) 192, (byte) 168, 0, 100 },
19 {(byte) 255, (byte) 255, (byte) 255, (byte) 255 }
20 };
21
22 String[] testStrings = {
23 "1.2.3.4",
24 "127.0.0.1",
25 "192.168.0.100",
26 "255.255.255.255"
27 };
28
29 int[] testInts = {
30 0x01020304,
31 0x7f000001,
32 (192 << 24) | (168 << 16) | 100,
33 0xffffffff
34 };
35
36 String[] invalidIPs = {
37 "",
38 ".",
39 "1.2..3.4",
40 "1.2.3.4.",
41 "257.11.225.1",
Gregor Maier7f987e62013-12-10 19:34:18 -080042 "256.11.225.1",
Yotam Harcholf3f11152013-09-05 16:47:16 -070043 "-1.2.3.4",
44 "1.2.3.4.5",
45 "1.x.3.4",
46 "1.2x.3.4"
47 };
48
49 String[] ipsWithMask = {
50 "1.2.3.4/24",
51 "192.168.130.140/255.255.192.0",
52 "127.0.0.1/8",
53 "8.8.8.8",
Gregor Maier7f987e62013-12-10 19:34:18 -080054 "8.8.8.8/32",
55 "0.0.0.0/0",
56 "192.168.130.140/255.0.255.0",
57 "1.2.3.4/0.127.0.255"
Yotam Harcholf3f11152013-09-05 16:47:16 -070058 };
59
60 boolean[] hasMask = {
61 true,
62 true,
63 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080064 false,
Gregor Maier7f987e62013-12-10 19:34:18 -080065 false,
66 true,
67 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080068 true
Yotam Harcholf3f11152013-09-05 16:47:16 -070069 };
70
71 byte[][][] ipsWithMaskValues = {
72 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } },
73 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } },
74 new byte[][] { new byte[] { (byte)0x7F, (byte)0x00, (byte)0x00, (byte)0x01 }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0x00, (byte)0x00 } },
Gregor Maier7f987e62013-12-10 19:34:18 -080075 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
76 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
77 new byte[][] { new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } },
78 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00 } },
79 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0xFF } }
80 };
81
82 int[] ipsWithMaskLengths = {
83 24,
84 18,
85 8,
86 32,
87 32,
88 0,
89 -1,
90 -1
91 };
92
93 String[] invalidIpsWithMask = {
94 "asdf",
95 "1.2.3.4/33",
96 "1.2.3.4/34",
97 "1.2.3.4/-1",
98 "1.2.3.4/256.0.0.0",
99 "1.256.3.4/255.255.0.0",
100 "1.2.3.4/255.255.0.0.0",
Yotam Harcholf3f11152013-09-05 16:47:16 -0700101 };
102
103
104 @Test
105 public void testOfString() {
106 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700107 IPv4Address ip = IPv4Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700108 assertEquals(testInts[i], ip.getInt());
109 assertArrayEquals(testAddresses[i], ip.getBytes());
110 assertEquals(testStrings[i], ip.toString());
111 }
112 }
113
114 @Test
115 public void testOfByteArray() {
116 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700117 IPv4Address ip = IPv4Address.of(testAddresses[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700118 assertEquals(testInts[i], ip.getInt());
119 assertArrayEquals(testAddresses[i], ip.getBytes());
120 assertEquals(testStrings[i], ip.toString());
121 }
122 }
123
124 @Test
125 public void testReadFrom() throws OFParseError {
126 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700127 IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700128 assertEquals(testInts[i], ip.getInt());
129 assertArrayEquals(testAddresses[i], ip.getBytes());
130 assertEquals(testStrings[i], ip.toString());
131 }
132 }
133
134
135 @Test
136 public void testInvalidIPs() throws OFParseError {
137 for(String invalid : invalidIPs) {
138 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700139 IPv4Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700140 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
141 } catch(IllegalArgumentException e) {
142 // ok
143 }
144 }
145 }
146
147 @Test
148 public void testOfMasked() throws OFParseError {
149 for (int i = 0; i < ipsWithMask.length; i++) {
Yotam Harchol9a617aa2013-09-16 14:10:17 -0700150 IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700151 if (!hasMask[i]) {
Yotam Harchola289d552013-09-16 10:10:40 -0700152 IPv4Address ip = value.getValue();
Yotam Harcholf3f11152013-09-05 16:47:16 -0700153 assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700154 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800155 IPv4Address mask = value.getMask();
156 assertEquals(ipsWithMaskLengths[i], value.getMask().asCidrMaskLength());
157 assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes());
158 byte[] ipBytes = new byte[4];
159 System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
160 assertEquals(ipBytes.length, value.getValue().getBytes().length);
161 for (int j = 0; j < ipBytes.length; j++) {
162 ipBytes[j] &= ipsWithMaskValues[i][1][j];
163 }
164
165 assertArrayEquals(ipBytes, value.getValue().getBytes());
166 assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
167 value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
168 }
169 }
170
171 @Test
172 public void testOfMaskedInvalid() throws Exception {
173 for(String invalid : invalidIpsWithMask) {
174 try {
175 IPv4Address.of(invalid);
176 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
177 } catch(IllegalArgumentException e) {
178 // ok
179 }
180 }
181 }
182
183 @Test
184 public void testSuperclass() throws Exception {
185 for(String ipString: testStrings) {
186 IPAddress<?> superIp = IPAddress.of(ipString);
187 assertEquals(IPVersion.IPv4, superIp.getIpVersion());
188 assertEquals(IPv4Address.of(ipString), superIp);
189 }
190
191 for(String ipMaskedString: ipsWithMask) {
192 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
193 assertEquals(IPVersion.IPv4, superIp.getIpVersion());
194 assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700195 }
196 }
Gregor Maier1ff55972013-12-11 02:22:56 -0800197
198 @Test
199 public void testOfExceptions() {
200 // We check if the message of a caught NPE is set to a useful message
201 // as a hacky way of verifying that we got an NPE thrown by use rather
202 // than one the JVM created for a null access.
203 try {
204 String s = null;
205 IPv4Address.of(s);
206 fail("Should have thrown NullPointerException");
207 } catch (NullPointerException e) {
208 assertNotNull(e.getMessage());
209 }
210 try {
211 byte[] b = null;
212 IPv4Address.of(b);
213 fail("Should have thrown NullPointerException");
214 } catch (NullPointerException e) {
215 assertNotNull(e.getMessage());
216 }
217 try {
218 byte[] b = new byte[3];
219 IPv4Address.of(b);
220 fail("Should have thrown IllegalArgumentException");
221 } catch (IllegalArgumentException e) {
222 // expected
223 }
224 try {
225 byte[] b = new byte[5];
226 IPv4Address.of(b);
227 fail("Should have thrown IllegalArgumentException");
228 } catch (IllegalArgumentException e) {
229 // expected
230 }
231 try {
232 IPv4AddressWithMask.of(null);
233 fail("Should have thrown NullPointerException");
234 } catch (NullPointerException e) {
235 assertNotNull(e.getMessage());
236 }
237 try {
238 IPv4AddressWithMask.of(IPv4Address.of("1.2.3.4"), null);
239 fail("Should have thrown NullPointerException");
240 } catch (NullPointerException e) {
241 assertNotNull(e.getMessage());
242 }
243 try {
244 IPv4AddressWithMask.of(null, IPv4Address.of("255.0.0.0"));
245 fail("Should have thrown NullPointerException");
246 } catch (NullPointerException e) {
247 assertNotNull(e.getMessage());
248 }
249 }
Yotam Harcholf3f11152013-09-05 16:47:16 -0700250}