Bug 1245: Dropped Binding prefix from Binding Data APIs.
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / packet / IPv4Test.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.packet;
10
11 import java.net.InetAddress;
12 import java.net.UnknownHostException;
13 import java.util.Arrays;
14
15 import junit.framework.Assert;
16
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;
21
22 public class IPv4Test {
23
24     @Test
25     public void testGetVersion() {
26         IPv4 ip = new IPv4();
27         byte[] ipVersion = { (byte) 4 };
28         ip.hdrFieldsMap.put("Version", ipVersion);
29         byte version = ip.getVersion();
30         Assert.assertTrue(version == (byte) 4);
31     }
32
33     @Test
34     public void testGetHeaderLength() {
35         IPv4 ip = new IPv4();
36         byte[] ipHeaderLength = { 5 };
37         ip.hdrFieldsMap.put("HeaderLength", ipHeaderLength);
38         byte headerLength = (byte) ip.getHeaderLen();
39         Assert.assertTrue(headerLength == 20);
40     }
41
42     @Test
43     public void testGetDiffServ() {
44         IPv4 ip = new IPv4();
45         byte[] ipDiffServ = { 20 };
46         ip.hdrFieldsMap.put("DiffServ", ipDiffServ);
47         byte diffServ = ip.getDiffServ();
48         Assert.assertTrue(diffServ == 20);
49     }
50
51     @Test
52     public void testGetTotalLength() {
53         IPv4 ip = new IPv4();
54         byte[] iptotLength = { 3, -24 };
55         ip.hdrFieldsMap.put("TotalLength", iptotLength);
56         short totalLength = ip.getTotalLength();
57         Assert.assertTrue(totalLength == 1000);
58     }
59
60     @Test
61     public void testGetIdentification() {
62         IPv4 ip = new IPv4();
63         byte[] ipIdentification = { 7, -48 };
64         ip.hdrFieldsMap.put("Identification", ipIdentification);
65         short identification = ip.getIdentification();
66         Assert.assertTrue(identification == 2000);
67     }
68
69     @Test
70     public void testGetFlags() {
71         IPv4 ip = new IPv4();
72         byte[] ipFlags = { 7 };
73         ip.hdrFieldsMap.put("Flags", ipFlags);
74         byte flags = ip.getFlags();
75         Assert.assertTrue(flags == 7);
76     }
77
78     @Test
79     public void testGetTtl() {
80         IPv4 ip = new IPv4();
81         byte[] ipTtl = { 100 };
82         ip.hdrFieldsMap.put("TTL", ipTtl);
83         byte ttl = ip.getTtl();
84         Assert.assertTrue(ttl == 100);
85     }
86
87     @Test
88     public void testGetProtocol() {
89         IPv4 ip = new IPv4();
90         byte[] ipProtocol = { 1 };
91         ip.hdrFieldsMap.put("Protocol", ipProtocol);
92         byte protocol = ip.getProtocol();
93         Assert.assertTrue(protocol == 1);
94
95         Class<? extends Packet> clazz = IPv4.protocolClassMap.get(protocol);
96         Assert.assertTrue(clazz == ICMP.class);
97     }
98
99     @Test
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);
106     }
107
108     @Test
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);
115     }
116
117     @Test
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);
124     }
125
126     @Test
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);
133     }
134
135     @Test
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);
142     }
143
144     @Test
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);
151     }
152
153     @Test
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);
161
162         ip.setTotalLength((short)84);
163         totalLength = ip.hdrFieldsMap.get("TotalLength");
164         Assert.assertTrue(totalLength[0] == 0);
165         Assert.assertTrue(totalLength[1] == 84);
166     }
167
168     @Test
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);
176     }
177
178     @Test
179     public void testSetFlags() {
180         IPv4 ip = new IPv4();
181         byte ipFlags = 7;
182         ip.setFlags(ipFlags);
183         byte[] flags = ip.hdrFieldsMap.get("Flags");
184         Assert.assertTrue(flags[0] == 7);
185     }
186
187     @Test
188     public void testSetTtl() {
189         IPv4 ip = new IPv4();
190         byte ipTtl = 100;
191         ip.setTtl(ipTtl);
192         byte[] ttl = ip.hdrFieldsMap.get("TTL");
193         Assert.assertTrue(ttl[0] == 100);
194     }
195
196     @Test
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);
203     }
204
205     @Test
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);
213     }
214
215     @Test
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);
225     }
226
227     @Test
228     public void testOptions() throws Exception {
229         IPv4 ip = new IPv4();
230         Assert.assertEquals(20, ip.getHeaderLen());
231         Assert.assertEquals(160, ip.getHeaderSize());
232         Assert.assertEquals(0, ip.getfieldnumBits("Options"));
233
234         byte[][] options = {
235             new byte[] {
236                 (byte)0x01,
237             },
238             new byte[] {
239                 (byte)0x01, (byte)0x02,
240             },
241             new byte[] {
242                 (byte)0x01, (byte)0x02, (byte)0x03,
243             },
244             new byte[] {
245                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
246             },
247             null,
248             new byte[] {
249                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
250                 (byte)0x05,
251             },
252             new byte[] {
253                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
254                 (byte)0x05, (byte)0x06,
255             },
256             new byte[] {
257                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
258                 (byte)0x05, (byte)0x06, (byte)0x07,
259             },
260             new byte[] {
261                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
262                 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08,
263             },
264             new byte[0],
265         };
266
267         byte[][] expected = {
268             new byte[] {
269                 (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00,
270             },
271             new byte[] {
272                 (byte)0x01, (byte)0x02, (byte)0x00, (byte)0x00,
273             },
274             new byte[] {
275                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x00,
276             },
277             new byte[] {
278                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
279             },
280             null,
281             new byte[] {
282                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
283                 (byte)0x05, (byte)0x00, (byte)0x00, (byte)0x00,
284             },
285             new byte[] {
286                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
287                 (byte)0x05, (byte)0x06, (byte)0x00, (byte)0x00,
288             },
289             new byte[] {
290                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
291                 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x00,
292             },
293             new byte[] {
294                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
295                 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08,
296             },
297             null,
298         };
299
300         byte[] echo = {
301             (byte)0x11, (byte)0x22, (byte)0x33, (byte)0x44,
302             (byte)0x55, (byte)0x66, (byte)0x77, (byte)0x88,
303             (byte)0x99, (byte)0xaa,
304         };
305         ICMP icmp = new ICMP();
306         icmp.setType((byte)8);
307         icmp.setCode((byte)0);
308         icmp.setIdentifier((short)0xabcd);
309         icmp.setSequenceNumber((short)7777);
310         icmp.setRawPayload(echo);
311
312         ip.setSourceAddress(InetAddress.getByName("192.168.10.20"));
313         ip.setDestinationAddress(InetAddress.getByName("192.168.30.40"));
314         ip.setProtocol(IPProtocols.ICMP.byteValue());
315
316         for (int i = 0; i < options.length; i++) {
317             byte[] opts = options[i];
318             byte[] exp = expected[i];
319
320             // Set IPv4 options.
321             int hlen = 20;
322             int optlen;
323             if (exp != null) {
324                 optlen = exp.length;
325                 hlen += optlen;
326             } else {
327                 optlen = 0;
328             }
329             ip.setOptions(opts);
330             Assert.assertTrue(Arrays.equals(exp, ip.getOptions()));
331             Assert.assertEquals(hlen, ip.getHeaderLen());
332             Assert.assertEquals(hlen * 8, ip.getHeaderSize());
333             Assert.assertEquals(optlen * 8, ip.getfieldnumBits("Options"));
334
335             // Serialize/Deserialize test.
336             ip.setPayload(icmp);
337
338             byte[] raw = ip.serialize();
339             IPv4 newip = new IPv4();
340             newip.deserialize(raw, 0, raw.length * 8);
341             Assert.assertEquals(ip, newip);
342             Assert.assertEquals(icmp, newip.getPayload());
343             Assert.assertTrue(Arrays.equals(exp, newip.getOptions()));
344         }
345     }
346
347     @Test
348     public void testChecksum() {
349         byte header[] = { (byte) 0x45, 00, 00, (byte) 0x3c, (byte) 0x1c,
350                 (byte) 0x46, (byte) 0x40, 00, (byte) 0x40, 06, (byte) 0xb1,
351                 (byte) 0xe6, (byte) 0xac, (byte) 0x10, (byte) 0x0a,
352                 (byte) 0x63, (byte) 0xac, (byte) 0x10, (byte) 0x0a, (byte) 0x0c };
353         byte header2[] = { (byte) 0x45, 00, 00, (byte) 0x73, 00, 00,
354                 (byte) 0x40, 00, (byte) 0x40, (byte) 0x11, (byte) 0xb8,
355                 (byte) 0x61, (byte) 0xc0, (byte) 0xa8, 00, 01, (byte) 0xc0,
356                 (byte) 0xa8, 00, (byte) 0xc7 };
357         byte header3[] = { (byte) 0x45, 00, 00, (byte) 0x47, (byte) 0x73,
358                 (byte) 0x88, (byte) 0x40, 00, (byte) 0x40, 06, (byte) 0xA2,
359                 (byte) 0xC4, (byte) 0x83, (byte) 0x9F, (byte) 0x0E,
360                 (byte) 0x85, (byte) 0x83, (byte) 0x9F, (byte) 0x0E, (byte) 0xA1 };
361         byte header4[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
362                 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0xf0, (byte) 0x8e,
363                 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65,
364                 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x64 };
365         byte header5[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
366                 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0xef, (byte) 0x8d,
367                 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65,
368                 (byte) 0xc0, (byte) 0xa8, (byte) 0x65, (byte) 0x65 };
369         byte header6[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
370                 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0x0b, (byte) 0x92,
371                 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65, (byte) 0x9,
372                 (byte) 0x9, (byte) 0x1, (byte) 0x1 };
373         byte header7[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
374                 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0, (byte) 0,
375                 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65, (byte) 0x9,
376                 (byte) 0x9, (byte) 0x2, (byte) 0x2 };
377
378         IPv4 ip = new IPv4();
379
380         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header,
381                 0)) == 0xB1E6);
382         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header2,
383                 0)) == 0xb861);
384         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header3,
385                 0)) == 0xa2c4);
386         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header4,
387                 0)) == 0xf08e);
388         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header5,
389                 0)) == 0xef8d);
390         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header6,
391                 0)) == 0x0b92);
392         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header7,
393                 0)) == 0x0a91);
394     }
395
396     @Test
397     public void testFullIP() throws UnknownHostException, PacketException {
398         byte[] icmpRawPayload = new byte[] { (byte) 0x38, (byte) 0x26,
399                 (byte) 0x9e, (byte) 0x51, (byte) 0x00, (byte) 0x00,
400                 (byte) 0x00, (byte) 0x00, (byte) 0x2e, (byte) 0x6a,
401                 (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00,
402                 (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0x11,
403                 (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15,
404                 (byte) 0x16, (byte) 0x17, (byte) 0x18, (byte) 0x19,
405                 (byte) 0x1a, (byte) 0x1b, (byte) 0x1c, (byte) 0x1d,
406                 (byte) 0x1e, (byte) 0x1f, (byte) 0x20, (byte) 0x21,
407                 (byte) 0x22, (byte) 0x23, (byte) 0x24, (byte) 0x25,
408                 (byte) 0x26, (byte) 0x27, (byte) 0x28, (byte) 0x29,
409                 (byte) 0x2a, (byte) 0x2b, (byte) 0x2c, (byte) 0x2d,
410                 (byte) 0x2e, (byte) 0x2f, (byte) 0x30, (byte) 0x31,
411                 (byte) 0x32, (byte) 0x33, (byte) 0x34, (byte) 0x35,
412                 (byte) 0x36, (byte) 0x37 };
413         ICMP icmp = new ICMP();
414         icmp.setType((byte) 8);
415         icmp.setCode((byte) 0);
416         icmp.setIdentifier((short) 0x46f5);
417         icmp.setSequenceNumber((short) 2);
418         icmp.setRawPayload(icmpRawPayload);
419
420         IPv4 ip = new IPv4();
421         ip.setVersion((byte) 4);
422         ip.setIdentification((short) 5);
423         ip.setDiffServ((byte) 0);
424         ip.setECN((byte) 0);
425         ip.setTotalLength((short) 84);
426         ip.setFlags((byte) 2);
427         ip.setFragmentOffset((short) 0);
428         ip.setTtl((byte) 64);
429         ip.setProtocol(IPProtocols.ICMP.byteValue());
430         ip.setDestinationAddress(InetAddress.getByName("192.168.100.100"));
431         ip.setSourceAddress(InetAddress.getByName("192.168.100.101"));
432         ip.setPayload(icmp);
433
434         Ethernet eth = new Ethernet();
435         eth.setDestinationMACAddress(new byte[] { (byte) 0x98, (byte) 0xfc,
436                 (byte) 0x11, (byte) 0x93, (byte) 0x5c, (byte) 0xb8 });
437         eth.setSourceMACAddress(new byte[] { (byte) 0x00, (byte) 0x24,
438                 (byte) 0xd7, (byte) 0xa9, (byte) 0xa3, (byte) 0x50 });
439         eth.setEtherType(EtherTypes.IPv4.shortValue());
440         eth.setPayload(ip);
441
442         byte[] stream = eth.serialize();
443
444         Ethernet decEth = new Ethernet();
445         decEth.deserialize(stream, 0, stream.length * NetUtils.NumBitsInAByte);
446
447         IPv4 decIp = (IPv4) decEth.getPayload();
448         Assert.assertFalse(decIp.isCorrupted());
449         Assert.assertTrue(ip.equals(decIp));
450
451         ICMP decIcmp = (ICMP) decIp.getPayload();
452         Assert.assertFalse(decIcmp.isCorrupted());
453         Assert.assertTrue(Arrays.equals(icmpRawPayload, decIcmp.getRawPayload()));
454     }
455 }