Merge "Bug 1227: Removed #close() from Write Transactions."
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / packet / IPv4Test.java
1 /*
2  * Copyright (c) 2013-2014 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.match.Match;
19 import org.opendaylight.controller.sal.match.MatchType;
20 import org.opendaylight.controller.sal.utils.EtherTypes;
21 import org.opendaylight.controller.sal.utils.IPProtocols;
22 import org.opendaylight.controller.sal.utils.NetUtils;
23
24 public class IPv4Test {
25
26     @Test
27     public void testGetVersion() {
28         IPv4 ip = new IPv4();
29         byte[] ipVersion = { (byte) 4 };
30         ip.hdrFieldsMap.put("Version", ipVersion);
31         byte version = ip.getVersion();
32         Assert.assertTrue(version == (byte) 4);
33     }
34
35     @Test
36     public void testGetHeaderLength() {
37         IPv4 ip = new IPv4();
38         byte[] ipHeaderLength = { 5 };
39         ip.hdrFieldsMap.put("HeaderLength", ipHeaderLength);
40         byte headerLength = (byte) ip.getHeaderLen();
41         Assert.assertTrue(headerLength == 20);
42     }
43
44     @Test
45     public void testGetDiffServ() {
46         IPv4 ip = new IPv4();
47         byte[] ipDiffServ = { 20 };
48         ip.hdrFieldsMap.put("DiffServ", ipDiffServ);
49         byte diffServ = ip.getDiffServ();
50         Assert.assertTrue(diffServ == 20);
51     }
52
53     @Test
54     public void testGetTotalLength() {
55         IPv4 ip = new IPv4();
56         byte[] iptotLength = { 3, -24 };
57         ip.hdrFieldsMap.put("TotalLength", iptotLength);
58         short totalLength = ip.getTotalLength();
59         Assert.assertTrue(totalLength == 1000);
60     }
61
62     @Test
63     public void testGetIdentification() {
64         IPv4 ip = new IPv4();
65         byte[] ipIdentification = { 7, -48 };
66         ip.hdrFieldsMap.put("Identification", ipIdentification);
67         short identification = ip.getIdentification();
68         Assert.assertTrue(identification == 2000);
69     }
70
71     @Test
72     public void testGetFlags() {
73         IPv4 ip = new IPv4();
74         byte[] ipFlags = { 7 };
75         ip.hdrFieldsMap.put("Flags", ipFlags);
76         byte flags = ip.getFlags();
77         Assert.assertTrue(flags == 7);
78     }
79
80     @Test
81     public void testGetTtl() {
82         IPv4 ip = new IPv4();
83         byte[] ipTtl = { 100 };
84         ip.hdrFieldsMap.put("TTL", ipTtl);
85         byte ttl = ip.getTtl();
86         Assert.assertTrue(ttl == 100);
87     }
88
89     @Test
90     public void testGetProtocol() {
91         IPv4 ip = new IPv4();
92         byte[] ipProtocol = { 1 };
93         ip.hdrFieldsMap.put("Protocol", ipProtocol);
94         byte protocol = ip.getProtocol();
95         Assert.assertTrue(protocol == 1);
96
97         Class<? extends Packet> clazz = IPv4.protocolClassMap.get(protocol);
98         Assert.assertTrue(clazz == ICMP.class);
99     }
100
101     @Test
102     public void testGetFragmentOffset() {
103         IPv4 ip = new IPv4();
104         byte[] ipFragmentOffset = { 6, -35 };
105         ip.hdrFieldsMap.put("FragmentOffset", ipFragmentOffset);
106         short fragmentOffset = ip.getFragmentOffset();
107         Assert.assertTrue(fragmentOffset == 1757);
108     }
109
110     @Test
111     public void testGetSourceAddress() {
112         IPv4 ip = new IPv4();
113         byte[] ipSourceAddress = { 10, 110, 31, 55 };
114         ip.hdrFieldsMap.put("SourceIPAddress", ipSourceAddress);
115         int sourceAddress = ip.getSourceAddress();
116         Assert.assertTrue(sourceAddress == 174989111);
117     }
118
119     @Test
120     public void testGetDestinationAddress() {
121         IPv4 ip = new IPv4();
122         byte[] ipDestinationAddress = { 20, 55, 62, 110 };
123         ip.hdrFieldsMap.put("DestinationIPAddress", ipDestinationAddress);
124         int destinationAddress = ip.getDestinationAddress();
125         Assert.assertTrue(destinationAddress == 339164782);
126     }
127
128     @Test
129     public void testSetVersion() {
130         IPv4 ip = new IPv4();
131         byte ipVersion = (byte) 4;
132         ip.setVersion(ipVersion);
133         byte[] version = ip.hdrFieldsMap.get("Version");
134         Assert.assertTrue(version[0] == (byte) 4);
135     }
136
137     @Test
138     public void testSetHeaderLength() {
139         IPv4 ip = new IPv4();
140         byte ipHeaderLength = 5;
141         ip.setHeaderLength(ipHeaderLength);
142         byte[] headerLength = ip.hdrFieldsMap.get("HeaderLength");
143         Assert.assertTrue(headerLength[0] == 5);
144     }
145
146     @Test
147     public void testSetDiffServ() {
148         IPv4 ip = new IPv4();
149         byte ipDiffServ = 20;
150         ip.setDiffServ(ipDiffServ);
151         byte[] diffServ = ip.hdrFieldsMap.get("DiffServ");
152         Assert.assertTrue(diffServ[0] == 20);
153     }
154
155     @Test
156     public void testSetTotalLength() {
157         IPv4 ip = new IPv4();
158         short iptotLength = 1000;
159         ip.setTotalLength(iptotLength);
160         byte[] totalLength = ip.hdrFieldsMap.get("TotalLength");
161         Assert.assertTrue(totalLength[0] == 3);
162         Assert.assertTrue(totalLength[1] == -24);
163
164         ip.setTotalLength((short)84);
165         totalLength = ip.hdrFieldsMap.get("TotalLength");
166         Assert.assertTrue(totalLength[0] == 0);
167         Assert.assertTrue(totalLength[1] == 84);
168     }
169
170     @Test
171     public void testSetIdentification() {
172         IPv4 ip = new IPv4();
173         short ipIdentification = 2000;
174         ip.setIdentification(ipIdentification);
175         byte[] identification = ip.hdrFieldsMap.get("Identification");
176         Assert.assertTrue(identification[0] == 7);
177         Assert.assertTrue(identification[1] == -48);
178     }
179
180     @Test
181     public void testSetFlags() {
182         IPv4 ip = new IPv4();
183         byte ipFlags = 7;
184         ip.setFlags(ipFlags);
185         byte[] flags = ip.hdrFieldsMap.get("Flags");
186         Assert.assertTrue(flags[0] == 7);
187     }
188
189     @Test
190     public void testSetTtl() {
191         IPv4 ip = new IPv4();
192         byte ipTtl = 100;
193         ip.setTtl(ipTtl);
194         byte[] ttl = ip.hdrFieldsMap.get("TTL");
195         Assert.assertTrue(ttl[0] == 100);
196     }
197
198     @Test
199     public void testSetProtocol() {
200         IPv4 ip = new IPv4();
201         byte ipProtocol = 11;
202         ip.setProtocol(ipProtocol);
203         byte[] protocol = ip.hdrFieldsMap.get("Protocol");
204         Assert.assertTrue(protocol[0] == 11);
205     }
206
207     @Test
208     public void testSetFragmentOffset() {
209         IPv4 ip = new IPv4();
210         short ipFragmentOffset = 1757;
211         ip.setFragmentOffset(ipFragmentOffset);
212         byte[] fragmentOffset = ip.hdrFieldsMap.get("FragmentOffset");
213         Assert.assertTrue(fragmentOffset[0] == 6);
214         Assert.assertTrue(fragmentOffset[1] == -35);
215     }
216
217     @Test
218     public void testSetDestinationAddress() {
219         IPv4 ip = new IPv4();
220         int ipDestinationAddress = 339164782;
221         ip.setDestinationAddress(ipDestinationAddress);
222         byte[] destinationAddress = ip.hdrFieldsMap.get("DestinationIPAddress");
223         Assert.assertTrue(destinationAddress[0] == 20);
224         Assert.assertTrue(destinationAddress[1] == 55);
225         Assert.assertTrue(destinationAddress[2] == 62);
226         Assert.assertTrue(destinationAddress[3] == 110);
227     }
228
229     @Test
230     public void testOptions() throws Exception {
231         IPv4 ip = new IPv4();
232         Assert.assertEquals(20, ip.getHeaderLen());
233         Assert.assertEquals(160, ip.getHeaderSize());
234         Assert.assertEquals(0, ip.getfieldnumBits("Options"));
235
236         byte[][] options = {
237             new byte[] {
238                 (byte)0x01,
239             },
240             new byte[] {
241                 (byte)0x01, (byte)0x02,
242             },
243             new byte[] {
244                 (byte)0x01, (byte)0x02, (byte)0x03,
245             },
246             new byte[] {
247                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
248             },
249             null,
250             new byte[] {
251                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
252                 (byte)0x05,
253             },
254             new byte[] {
255                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
256                 (byte)0x05, (byte)0x06,
257             },
258             new byte[] {
259                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
260                 (byte)0x05, (byte)0x06, (byte)0x07,
261             },
262             new byte[] {
263                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
264                 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08,
265             },
266             new byte[0],
267         };
268
269         byte[][] expected = {
270             new byte[] {
271                 (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00,
272             },
273             new byte[] {
274                 (byte)0x01, (byte)0x02, (byte)0x00, (byte)0x00,
275             },
276             new byte[] {
277                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x00,
278             },
279             new byte[] {
280                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
281             },
282             null,
283             new byte[] {
284                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
285                 (byte)0x05, (byte)0x00, (byte)0x00, (byte)0x00,
286             },
287             new byte[] {
288                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
289                 (byte)0x05, (byte)0x06, (byte)0x00, (byte)0x00,
290             },
291             new byte[] {
292                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
293                 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x00,
294             },
295             new byte[] {
296                 (byte)0x01, (byte)0x02, (byte)0x03, (byte)0x04,
297                 (byte)0x05, (byte)0x06, (byte)0x07, (byte)0x08,
298             },
299             null,
300         };
301
302         byte[] echo = {
303             (byte)0x11, (byte)0x22, (byte)0x33, (byte)0x44,
304             (byte)0x55, (byte)0x66, (byte)0x77, (byte)0x88,
305             (byte)0x99, (byte)0xaa,
306         };
307         ICMP icmp = new ICMP();
308         icmp.setType((byte)8);
309         icmp.setCode((byte)0);
310         icmp.setIdentifier((short)0xabcd);
311         icmp.setSequenceNumber((short)7777);
312         icmp.setRawPayload(echo);
313
314         ip.setSourceAddress(InetAddress.getByName("192.168.10.20"));
315         ip.setDestinationAddress(InetAddress.getByName("192.168.30.40"));
316         ip.setProtocol(IPProtocols.ICMP.byteValue());
317
318         for (int i = 0; i < options.length; i++) {
319             byte[] opts = options[i];
320             byte[] exp = expected[i];
321
322             // Set IPv4 options.
323             int hlen = 20;
324             int optlen;
325             if (exp != null) {
326                 optlen = exp.length;
327                 hlen += optlen;
328             } else {
329                 optlen = 0;
330             }
331             ip.setOptions(opts);
332             Assert.assertTrue(Arrays.equals(exp, ip.getOptions()));
333             Assert.assertEquals(hlen, ip.getHeaderLen());
334             Assert.assertEquals(hlen * 8, ip.getHeaderSize());
335             Assert.assertEquals(optlen * 8, ip.getfieldnumBits("Options"));
336
337             // Serialize/Deserialize test.
338             ip.setPayload(icmp);
339
340             byte[] raw = ip.serialize();
341             IPv4 newip = new IPv4();
342             newip.deserialize(raw, 0, raw.length * 8);
343             Assert.assertEquals(ip, newip);
344             Assert.assertEquals(icmp, newip.getPayload());
345             Assert.assertTrue(Arrays.equals(exp, newip.getOptions()));
346         }
347     }
348
349     @Test
350     public void testChecksum() {
351         byte header[] = { (byte) 0x45, 00, 00, (byte) 0x3c, (byte) 0x1c,
352                 (byte) 0x46, (byte) 0x40, 00, (byte) 0x40, 06, (byte) 0xb1,
353                 (byte) 0xe6, (byte) 0xac, (byte) 0x10, (byte) 0x0a,
354                 (byte) 0x63, (byte) 0xac, (byte) 0x10, (byte) 0x0a, (byte) 0x0c };
355         byte header2[] = { (byte) 0x45, 00, 00, (byte) 0x73, 00, 00,
356                 (byte) 0x40, 00, (byte) 0x40, (byte) 0x11, (byte) 0xb8,
357                 (byte) 0x61, (byte) 0xc0, (byte) 0xa8, 00, 01, (byte) 0xc0,
358                 (byte) 0xa8, 00, (byte) 0xc7 };
359         byte header3[] = { (byte) 0x45, 00, 00, (byte) 0x47, (byte) 0x73,
360                 (byte) 0x88, (byte) 0x40, 00, (byte) 0x40, 06, (byte) 0xA2,
361                 (byte) 0xC4, (byte) 0x83, (byte) 0x9F, (byte) 0x0E,
362                 (byte) 0x85, (byte) 0x83, (byte) 0x9F, (byte) 0x0E, (byte) 0xA1 };
363         byte header4[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
364                 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0xf0, (byte) 0x8e,
365                 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65,
366                 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x64 };
367         byte header5[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
368                 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0xef, (byte) 0x8d,
369                 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65,
370                 (byte) 0xc0, (byte) 0xa8, (byte) 0x65, (byte) 0x65 };
371         byte header6[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
372                 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0x0b, (byte) 0x92,
373                 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65, (byte) 0x9,
374                 (byte) 0x9, (byte) 0x1, (byte) 0x1 };
375         byte header7[] = { (byte) 0x45, 00, 00, (byte) 0x54, 00, 00,
376                 (byte) 0x40, 00, (byte) 0x40, 01, (byte) 0, (byte) 0,
377                 (byte) 0xc0, (byte) 0xa8, (byte) 0x64, (byte) 0x65, (byte) 0x9,
378                 (byte) 0x9, (byte) 0x2, (byte) 0x2 };
379
380         IPv4 ip = new IPv4();
381
382         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header,
383                 0)) == 0xB1E6);
384         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header2,
385                 0)) == 0xb861);
386         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header3,
387                 0)) == 0xa2c4);
388         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header4,
389                 0)) == 0xf08e);
390         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header5,
391                 0)) == 0xef8d);
392         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header6,
393                 0)) == 0x0b92);
394         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header7,
395                 0)) == 0x0a91);
396     }
397
398     @Test
399     public void testFullIP() throws UnknownHostException, PacketException {
400         byte[] icmpRawPayload = new byte[] { (byte) 0x38, (byte) 0x26,
401                 (byte) 0x9e, (byte) 0x51, (byte) 0x00, (byte) 0x00,
402                 (byte) 0x00, (byte) 0x00, (byte) 0x2e, (byte) 0x6a,
403                 (byte) 0x08, (byte) 0x00, (byte) 0x00, (byte) 0x00,
404                 (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0x11,
405                 (byte) 0x12, (byte) 0x13, (byte) 0x14, (byte) 0x15,
406                 (byte) 0x16, (byte) 0x17, (byte) 0x18, (byte) 0x19,
407                 (byte) 0x1a, (byte) 0x1b, (byte) 0x1c, (byte) 0x1d,
408                 (byte) 0x1e, (byte) 0x1f, (byte) 0x20, (byte) 0x21,
409                 (byte) 0x22, (byte) 0x23, (byte) 0x24, (byte) 0x25,
410                 (byte) 0x26, (byte) 0x27, (byte) 0x28, (byte) 0x29,
411                 (byte) 0x2a, (byte) 0x2b, (byte) 0x2c, (byte) 0x2d,
412                 (byte) 0x2e, (byte) 0x2f, (byte) 0x30, (byte) 0x31,
413                 (byte) 0x32, (byte) 0x33, (byte) 0x34, (byte) 0x35,
414                 (byte) 0x36, (byte) 0x37 };
415         ICMP icmp = new ICMP();
416         icmp.setType((byte) 8);
417         icmp.setCode((byte) 0);
418         icmp.setIdentifier((short) 0x46f5);
419         icmp.setSequenceNumber((short) 2);
420         icmp.setRawPayload(icmpRawPayload);
421
422         IPv4 ip = new IPv4();
423         ip.setVersion((byte) 4);
424         ip.setIdentification((short) 5);
425         ip.setDiffServ((byte) 0);
426         ip.setECN((byte) 0);
427         ip.setTotalLength((short) 84);
428         ip.setFlags((byte) 2);
429         ip.setFragmentOffset((short) 0);
430         ip.setTtl((byte) 64);
431         ip.setProtocol(IPProtocols.ICMP.byteValue());
432         ip.setDestinationAddress(InetAddress.getByName("192.168.100.100"));
433         ip.setSourceAddress(InetAddress.getByName("192.168.100.101"));
434         ip.setPayload(icmp);
435
436         Ethernet eth = new Ethernet();
437         eth.setDestinationMACAddress(new byte[] { (byte) 0x98, (byte) 0xfc,
438                 (byte) 0x11, (byte) 0x93, (byte) 0x5c, (byte) 0xb8 });
439         eth.setSourceMACAddress(new byte[] { (byte) 0x00, (byte) 0x24,
440                 (byte) 0xd7, (byte) 0xa9, (byte) 0xa3, (byte) 0x50 });
441         eth.setEtherType(EtherTypes.IPv4.shortValue());
442         eth.setPayload(ip);
443
444         byte[] stream = eth.serialize();
445
446         Ethernet decEth = new Ethernet();
447         decEth.deserialize(stream, 0, stream.length * NetUtils.NumBitsInAByte);
448
449         IPv4 decIp = (IPv4) decEth.getPayload();
450         Assert.assertFalse(decIp.isCorrupted());
451         Assert.assertTrue(ip.equals(decIp));
452
453         ICMP decIcmp = (ICMP) decIp.getPayload();
454         Assert.assertFalse(decIcmp.isCorrupted());
455         Assert.assertTrue(Arrays.equals(icmpRawPayload, decIcmp.getRawPayload()));
456     }
457
458     @Test
459     public void testGetMatch() throws Exception {
460         IPv4 ip = new IPv4();
461         InetAddress sourceAddress = InetAddress.getByName("172.168.190.15");
462         InetAddress destintationAddress = InetAddress.getByName("23.128.0.11");
463         byte protocol = IPProtocols.TCP.byteValue();
464         byte tos = 7;
465         ip.setVersion((byte) 4);
466         ip.setIdentification((short) 5);
467         ip.setDiffServ(tos);
468         ip.setECN((byte) 0);
469         ip.setTotalLength((short) 84);
470         ip.setFlags((byte) 2);
471         ip.setFragmentOffset((short) 0);
472         ip.setTtl((byte) 64);
473         ip.setProtocol(protocol);
474         ip.setDestinationAddress(destintationAddress);
475         ip.setSourceAddress(sourceAddress);
476
477         Match match = ip.getMatch();
478
479         Assert.assertEquals(sourceAddress, match.getField(MatchType.NW_SRC).getValue());
480         Assert.assertEquals(destintationAddress, match.getField(MatchType.NW_DST).getValue());
481         Assert.assertEquals(protocol, (byte) match.getField(MatchType.NW_PROTO).getValue());
482         Assert.assertEquals(tos, (byte) match.getField(MatchType.NW_TOS).getValue());
483     }
484 }