Merge "Statistics Northbound Test"
[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 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 };
257
258         IPv4 ip = new IPv4();
259
260         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header,
261                 0)) == 0xB1E6);
262         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header2,
263                 0)) == 0xb861);
264         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header3,
265                 0)) == 0xa2c4);
266         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header4,
267                 0)) == 0xf08e);
268         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header5,
269                 0)) == 0xef8d);
270         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header6,
271                 0)) == 0x0b92);
272         Assert.assertTrue(NetUtils.getUnsignedShort(ip.computeChecksum(header7,
273                 0)) == 0x0a91);
274     }
275
276     @Test
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);
299
300         IPv4 ip = new IPv4();
301         ip.setVersion((byte) 4);
302         ip.setIdentification((short) 5);
303         ip.setDiffServ((byte) 0);
304         ip.setECN((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"));
312         ip.setPayload(icmp);
313
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());
320         eth.setPayload(ip);
321
322         byte[] stream = eth.serialize();
323
324         Ethernet decEth = new Ethernet();
325         decEth.deserialize(stream, 0, stream.length * NetUtils.NumBitsInAByte);
326
327         IPv4 decIp = (IPv4) decEth.getPayload();
328         Assert.assertFalse(decIp.isCorrupted());
329         Assert.assertTrue(ip.equals(decIp));
330
331         ICMP decIcmp = (ICMP) decIp.getPayload();
332         Assert.assertFalse(decIcmp.isCorrupted());
333         Assert.assertTrue(Arrays.equals(icmpRawPayload, decIcmp.getRawPayload()));
334     }
335 }