blob: d9c6681ff171de636978f7063d40eabb0987db81 [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",
Gregor Maier7f987e62013-12-10 19:34:18 -080041 "256.11.225.1",
Yotam Harcholf3f11152013-09-05 16:47:16 -070042 "-1.2.3.4",
43 "1.2.3.4.5",
44 "1.x.3.4",
45 "1.2x.3.4"
46 };
47
48 String[] ipsWithMask = {
49 "1.2.3.4/24",
50 "192.168.130.140/255.255.192.0",
51 "127.0.0.1/8",
52 "8.8.8.8",
Gregor Maier7f987e62013-12-10 19:34:18 -080053 "8.8.8.8/32",
54 "0.0.0.0/0",
55 "192.168.130.140/255.0.255.0",
56 "1.2.3.4/0.127.0.255"
Yotam Harcholf3f11152013-09-05 16:47:16 -070057 };
58
59 boolean[] hasMask = {
60 true,
61 true,
62 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080063 false,
Gregor Maier7f987e62013-12-10 19:34:18 -080064 false,
65 true,
66 true,
Andreas Wundsame04c86f2013-11-05 17:13:18 -080067 true
Yotam Harcholf3f11152013-09-05 16:47:16 -070068 };
69
70 byte[][][] ipsWithMaskValues = {
71 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x00 } },
72 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xC0, (byte)0x00 } },
73 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 -080074 new byte[][] { new byte[] { (byte)0x08, (byte)0x08, (byte)0x08, (byte)0x08 }, new byte[] { (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF } },
75 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)0x00, (byte)0x00, (byte)0x00, (byte)0x00 }, new byte[] { (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00 } },
77 new byte[][] { new byte[] { (byte)0xC0, (byte)0xA8, (byte)0x82, (byte)0x8C }, new byte[] { (byte)0xFF, (byte)0x00, (byte)0xFF, (byte)0x00 } },
78 new byte[][] { new byte[] { (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04 }, new byte[] { (byte)0x00, (byte)0x7F, (byte)0x00, (byte)0xFF } }
79 };
80
81 int[] ipsWithMaskLengths = {
82 24,
83 18,
84 8,
85 32,
86 32,
87 0,
88 -1,
89 -1
90 };
91
92 String[] invalidIpsWithMask = {
93 "asdf",
94 "1.2.3.4/33",
95 "1.2.3.4/34",
96 "1.2.3.4/-1",
97 "1.2.3.4/256.0.0.0",
98 "1.256.3.4/255.255.0.0",
99 "1.2.3.4/255.255.0.0.0",
Yotam Harcholf3f11152013-09-05 16:47:16 -0700100 };
101
102
103 @Test
104 public void testOfString() {
105 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700106 IPv4Address ip = IPv4Address.of(testStrings[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700107 assertEquals(testInts[i], ip.getInt());
108 assertArrayEquals(testAddresses[i], ip.getBytes());
109 assertEquals(testStrings[i], ip.toString());
110 }
111 }
112
113 @Test
114 public void testOfByteArray() {
115 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700116 IPv4Address ip = IPv4Address.of(testAddresses[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700117 assertEquals(testInts[i], ip.getInt());
118 assertArrayEquals(testAddresses[i], ip.getBytes());
119 assertEquals(testStrings[i], ip.toString());
120 }
121 }
122
123 @Test
124 public void testReadFrom() throws OFParseError {
125 for(int i=0; i < testAddresses.length; i++ ) {
Yotam Harchola289d552013-09-16 10:10:40 -0700126 IPv4Address ip = IPv4Address.read4Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
Yotam Harcholf3f11152013-09-05 16:47:16 -0700127 assertEquals(testInts[i], ip.getInt());
128 assertArrayEquals(testAddresses[i], ip.getBytes());
129 assertEquals(testStrings[i], ip.toString());
130 }
131 }
132
133
134 @Test
135 public void testInvalidIPs() throws OFParseError {
136 for(String invalid : invalidIPs) {
137 try {
Yotam Harchola289d552013-09-16 10:10:40 -0700138 IPv4Address.of(invalid);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700139 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
140 } catch(IllegalArgumentException e) {
141 // ok
142 }
143 }
144 }
145
146 @Test
147 public void testOfMasked() throws OFParseError {
148 for (int i = 0; i < ipsWithMask.length; i++) {
Yotam Harchol9a617aa2013-09-16 14:10:17 -0700149 IPv4AddressWithMask value = IPv4AddressWithMask.of(ipsWithMask[i]);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700150 if (!hasMask[i]) {
Yotam Harchola289d552013-09-16 10:10:40 -0700151 IPv4Address ip = value.getValue();
Yotam Harcholf3f11152013-09-05 16:47:16 -0700152 assertArrayEquals(ipsWithMaskValues[i][0], ip.getBytes());
Yotam Harcholf3f11152013-09-05 16:47:16 -0700153 }
Gregor Maier7f987e62013-12-10 19:34:18 -0800154 IPv4Address mask = value.getMask();
155 assertEquals(ipsWithMaskLengths[i], value.getMask().asCidrMaskLength());
156 assertArrayEquals(ipsWithMaskValues[i][1], mask.getBytes());
157 byte[] ipBytes = new byte[4];
158 System.arraycopy(ipsWithMaskValues[i][0], 0, ipBytes, 0, 4);
159 assertEquals(ipBytes.length, value.getValue().getBytes().length);
160 for (int j = 0; j < ipBytes.length; j++) {
161 ipBytes[j] &= ipsWithMaskValues[i][1][j];
162 }
163
164 assertArrayEquals(ipBytes, value.getValue().getBytes());
165 assertThat(String.format("Byte comparison for mask of %s (%s)", ipsWithMask[i], value),
166 value.getMask().getBytes(), CoreMatchers.equalTo(ipsWithMaskValues[i][1]));
167 }
168 }
169
170 @Test
171 public void testOfMaskedInvalid() throws Exception {
172 for(String invalid : invalidIpsWithMask) {
173 try {
174 IPv4Address.of(invalid);
175 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
176 } catch(IllegalArgumentException e) {
177 // ok
178 }
179 }
180 }
181
182 @Test
183 public void testSuperclass() throws Exception {
184 for(String ipString: testStrings) {
185 IPAddress<?> superIp = IPAddress.of(ipString);
186 assertEquals(IPVersion.IPv4, superIp.getIpVersion());
187 assertEquals(IPv4Address.of(ipString), superIp);
188 }
189
190 for(String ipMaskedString: ipsWithMask) {
191 IPAddressWithMask<?> superIp = IPAddressWithMask.of(ipMaskedString);
192 assertEquals(IPVersion.IPv4, superIp.getIpVersion());
193 assertEquals(IPv4AddressWithMask.of(ipMaskedString), superIp);
Yotam Harcholf3f11152013-09-05 16:47:16 -0700194 }
195 }
196}