2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.sal.packet;
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.Arrays;
15 import junit.framework.Assert;
17 import org.junit.Test;
18 import org.opendaylight.controller.sal.utils.EtherTypes;
19 import org.opendaylight.controller.sal.utils.IPProtocols;
20 import org.opendaylight.controller.sal.utils.NetUtils;
22 public class IPv4Test {
25 public void testGetVersion() {
27 byte[] ipVersion = { (byte) 4 };
28 ip.hdrFieldsMap.put("Version", ipVersion);
29 byte version = ip.getVersion();
30 Assert.assertTrue(version == (byte) 4);
34 public void testGetHeaderLength() {
36 byte[] ipHeaderLength = { 5 };
37 ip.hdrFieldsMap.put("HeaderLength", ipHeaderLength);
38 byte headerLength = (byte) ip.getHeaderLen();
39 Assert.assertTrue(headerLength == 20);
43 public void testGetDiffServ() {
45 byte[] ipDiffServ = { 20 };
46 ip.hdrFieldsMap.put("DiffServ", ipDiffServ);
47 byte diffServ = ip.getDiffServ();
48 Assert.assertTrue(diffServ == 20);
52 public void testGetTotalLength() {
54 byte[] iptotLength = { 3, -24 };
55 ip.hdrFieldsMap.put("TotalLength", iptotLength);
56 short totalLength = ip.getTotalLength();
57 Assert.assertTrue(totalLength == 1000);
61 public void testGetIdentification() {
63 byte[] ipIdentification = { 7, -48 };
64 ip.hdrFieldsMap.put("Identification", ipIdentification);
65 short identification = ip.getIdentification();
66 Assert.assertTrue(identification == 2000);
70 public void testGetFlags() {
72 byte[] ipFlags = { 7 };
73 ip.hdrFieldsMap.put("Flags", ipFlags);
74 byte flags = ip.getFlags();
75 Assert.assertTrue(flags == 7);
79 public void testGetTtl() {
81 byte[] ipTtl = { 100 };
82 ip.hdrFieldsMap.put("TTL", ipTtl);
83 byte ttl = ip.getTtl();
84 Assert.assertTrue(ttl == 100);
88 public void testGetProtocol() {
90 byte[] ipProtocol = { 1 };
91 ip.hdrFieldsMap.put("Protocol", ipProtocol);
92 byte protocol = ip.getProtocol();
93 Assert.assertTrue(protocol == 1);
95 Class<? extends Packet> clazz = IPv4.protocolClassMap.get(protocol);
96 Assert.assertTrue(clazz == ICMP.class);
100 public void testGetFragmentOffset() {
101 IPv4 ip = new IPv4();
102 byte[] ipFragmentOffset = { 6, -35 };
103 ip.hdrFieldsMap.put("FragmentOffset", ipFragmentOffset);
104 short fragmentOffset = ip.getFragmentOffset();
105 Assert.assertTrue(fragmentOffset == 1757);
109 public void testGetSourceAddress() {
110 IPv4 ip = new IPv4();
111 byte[] ipSourceAddress = { 10, 110, 31, 55 };
112 ip.hdrFieldsMap.put("SourceIPAddress", ipSourceAddress);
113 int sourceAddress = ip.getSourceAddress();
114 Assert.assertTrue(sourceAddress == 174989111);
118 public void testGetDestinationAddress() {
119 IPv4 ip = new IPv4();
120 byte[] ipDestinationAddress = { 20, 55, 62, 110 };
121 ip.hdrFieldsMap.put("DestinationIPAddress", ipDestinationAddress);
122 int destinationAddress = ip.getDestinationAddress();
123 Assert.assertTrue(destinationAddress == 339164782);
127 public void testSetVersion() {
128 IPv4 ip = new IPv4();
129 byte ipVersion = (byte) 4;
130 ip.setVersion(ipVersion);
131 byte[] version = ip.hdrFieldsMap.get("Version");
132 Assert.assertTrue(version[0] == (byte) 4);
136 public void testSetHeaderLength() {
137 IPv4 ip = new IPv4();
138 byte ipHeaderLength = 5;
139 ip.setHeaderLength(ipHeaderLength);
140 byte[] headerLength = ip.hdrFieldsMap.get("HeaderLength");
141 Assert.assertTrue(headerLength[0] == 5);
145 public void testSetDiffServ() {
146 IPv4 ip = new IPv4();
147 byte ipDiffServ = 20;
148 ip.setDiffServ(ipDiffServ);
149 byte[] diffServ = ip.hdrFieldsMap.get("DiffServ");
150 Assert.assertTrue(diffServ[0] == 20);
154 public void testSetTotalLength() {
155 IPv4 ip = new IPv4();
156 short iptotLength = 1000;
157 ip.setTotalLength(iptotLength);
158 byte[] totalLength = ip.hdrFieldsMap.get("TotalLength");
159 Assert.assertTrue(totalLength[0] == 3);
160 Assert.assertTrue(totalLength[1] == -24);
162 ip.setTotalLength((short)84);
163 totalLength = ip.hdrFieldsMap.get("TotalLength");
164 Assert.assertTrue(totalLength[0] == 0);
165 Assert.assertTrue(totalLength[1] == 84);
169 public void testSetIdentification() {
170 IPv4 ip = new IPv4();
171 short ipIdentification = 2000;
172 ip.setIdentification(ipIdentification);
173 byte[] identification = ip.hdrFieldsMap.get("Identification");
174 Assert.assertTrue(identification[0] == 7);
175 Assert.assertTrue(identification[1] == -48);
179 public void testSetFlags() {
180 IPv4 ip = new IPv4();
182 ip.setFlags(ipFlags);
183 byte[] flags = ip.hdrFieldsMap.get("Flags");
184 Assert.assertTrue(flags[0] == 7);
188 public void testSetTtl() {
189 IPv4 ip = new IPv4();
192 byte[] ttl = ip.hdrFieldsMap.get("TTL");
193 Assert.assertTrue(ttl[0] == 100);
197 public void testSetProtocol() {
198 IPv4 ip = new IPv4();
199 byte ipProtocol = 11;
200 ip.setProtocol(ipProtocol);
201 byte[] protocol = ip.hdrFieldsMap.get("Protocol");
202 Assert.assertTrue(protocol[0] == 11);
206 public void testSetFragmentOffset() {
207 IPv4 ip = new IPv4();
208 short ipFragmentOffset = 1757;
209 ip.setFragmentOffset(ipFragmentOffset);
210 byte[] fragmentOffset = ip.hdrFieldsMap.get("FragmentOffset");
211 Assert.assertTrue(fragmentOffset[0] == 6);
212 Assert.assertTrue(fragmentOffset[1] == -35);
216 public void testSetDestinationAddress() {
217 IPv4 ip = new IPv4();
218 int ipDestinationAddress = 339164782;
219 ip.setDestinationAddress(ipDestinationAddress);
220 byte[] destinationAddress = ip.hdrFieldsMap.get("DestinationIPAddress");
221 Assert.assertTrue(destinationAddress[0] == 20);
222 Assert.assertTrue(destinationAddress[1] == 55);
223 Assert.assertTrue(destinationAddress[2] == 62);
224 Assert.assertTrue(destinationAddress[3] == 110);
228 public void testChecksum() {
229 byte header[] = { (byte) 0x45, 00, 00, (byte) 0x3c, (byte) 0x1c,
230 (byte) 0x46, (byte) 0x40, 00, (byte) 0x40, 06, (byte) 0xb1,
231 (byte) 0xe6, (byte) 0xac, (byte) 0x10, (byte) 0x0a,
232 (byte) 0x63, (byte) 0xac, (byte) 0x10, (byte) 0x0a, (byte) 0x0c };
233 byte header2[] = { (byte) 0x45, 00, 00, (byte) 0x73, 00, 00,
234 (byte) 0x40, 00, (byte) 0x40, (byte) 0x11, (byte) 0xb8,
235 (byte) 0x61, (byte) 0xc0, (byte) 0xa8, 00, 01, (byte) 0xc0,
236 (byte) 0xa8, 00, (byte) 0xc7 };
237 byte header3[] = { (byte) 0x45, 00, 00, (byte) 0x47, (byte) 0x73,
238 (byte) 0x88, (byte) 0x40, 00, (byte) 0x40, 06, (byte) 0xA2,
239 (byte) 0xC4, (byte) 0x83, (byte) 0x9F, (byte) 0x0E,
240 (byte) 0x85, (byte) 0x83, (byte) 0x9F, (byte) 0x0E, (byte) 0xA1 };
241 byte header4[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
242 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0xf0, (byte) 0x8e,
243 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65,
244 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x64 };
245 byte header5[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
246 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0xef, (byte) 0x8d,
247 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65,
248 (byte) 0xc0, (byte) 0xa8, (byte) 0x65, (byte) 0x65 };
249 byte header6[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
250 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0x0b, (byte) 0x92,
251 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65, (byte) 0x9,
252 (byte) 0x9, (byte) 0x1, (byte) 0x1 };
253 byte header7[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
254 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0, (byte) 0,
255 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65, (byte) 0x9,
256 (byte) 0x9, (byte) 0x2, (byte) 0x2 };
258 IPv4 ip = new IPv4();
260 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header,
262 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header2,
264 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header3,
266 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header4,
268 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header5,
270 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header6,
272 Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header7,
277 public void testFullIP() throws UnknownHostException, PacketException {
278 byte[] icmpRawPayload = new byte[] { (byte) 0x38, (byte) 0x26,
279 (byte) 0x9e, (byte) 0x51, (byte) 0x00, (byte) 0x00,
280 (byte) 0x00, (byte) 0x00, (byte) 0x2e, (byte) 0x6a,
281 (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00,
282 (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0x11,
283 (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15,
284 (byte) 0x16, (byte) 0x17, (byte) 0x18, (byte) 0x19,
285 (byte) 0x1a, (byte) 0x1b, (byte) 0x1c, (byte) 0x1d,
286 (byte) 0x1e, (byte) 0x1f, (byte) 0x20, (byte) 0x21,
287 (byte) 0x22, (byte) 0x23, (byte) 0x24, (byte) 0x25,
288 (byte) 0x26, (byte) 0x27, (byte) 0x28, (byte) 0x29,
289 (byte) 0x2a, (byte) 0x2b, (byte) 0x2c, (byte) 0x2d,
290 (byte) 0x2e, (byte) 0x2f, (byte) 0x30, (byte) 0x31,
291 (byte) 0x32, (byte) 0x33, (byte) 0x34, (byte) 0x35,
292 (byte) 0x36, (byte) 0x37 };
293 ICMP icmp = new ICMP();
294 icmp.setType((byte) 8);
295 icmp.setCode((byte) 0);
296 icmp.setIdentifier((short) 0x46f5);
297 icmp.setSequenceNumber((short) 2);
298 icmp.setRawPayload(icmpRawPayload);
300 IPv4 ip = new IPv4();
301 ip.setVersion((byte) 4);
302 ip.setIdentification((short) 5);
303 ip.setDiffServ((byte) 0);
305 ip.setTotalLength((short) 84);
306 ip.setFlags((byte) 2);
307 ip.setFragmentOffset((short) 0);
308 ip.setTtl((byte) 64);
309 ip.setProtocol(IPProtocols.ICMP.byteValue());
310 ip.setDestinationAddress(InetAddress.getByName("192.168.100.100"));
311 ip.setSourceAddress(InetAddress.getByName("192.168.100.101"));
314 Ethernet eth = new Ethernet();
315 eth.setDestinationMACAddress(new byte[] { (byte) 0x98, (byte) 0xfc,
316 (byte) 0x11, (byte) 0x93, (byte) 0x5c, (byte) 0xb8 });
317 eth.setSourceMACAddress(new byte[] { (byte) 0x00, (byte) 0x24,
318 (byte) 0xd7, (byte) 0xa9, (byte) 0xa3, (byte) 0x50 });
319 eth.setEtherType(EtherTypes.IPv4.shortValue());
322 byte[] stream = eth.serialize();
324 Ethernet decEth = new Ethernet();
325 decEth.deserialize(stream, 0, stream.length * NetUtils.NumBitsInAByte);
327 IPv4 decIp = (IPv4) decEth.getPayload();
328 Assert.assertFalse(decIp.isCorrupted());
329 Assert.assertTrue(ip.equals(decIp));
331 ICMP decIcmp = (ICMP) decIp.getPayload();
332 Assert.assertFalse(decIcmp.isCorrupted());
333 Assert.assertTrue(Arrays.equals(icmpRawPayload, decIcmp.getRawPayload()));