Initial opendaylight infrastructure commit!!
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / packet / IPv4Test.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.sal.packet;
11
12 import junit.framework.Assert;
13
14 import org.junit.Test;
15 import org.opendaylight.controller.sal.packet.ICMP;
16 import org.opendaylight.controller.sal.packet.IPv4;
17 import org.opendaylight.controller.sal.packet.Packet;
18
19 public class IPv4Test {
20
21     @Test
22     public void testGetVersion() {
23         IPv4 ip = new IPv4();
24         byte[] ipVersion = { (byte) 4 };
25         ip.hdrFieldsMap.put("Version", ipVersion);
26         byte version = ip.getVersion();
27         Assert.assertTrue(version == (byte) 4);
28     }
29
30     @Test
31     public void testGetHeaderLength() {
32         IPv4 ip = new IPv4();
33         byte[] ipHeaderLength = { 5 };
34         ip.hdrFieldsMap.put("HeaderLength", ipHeaderLength);
35         byte headerLength = (byte) ip.getHeaderLen();
36         Assert.assertTrue(headerLength == 20);
37     }
38
39     @Test
40     public void testGetDiffServ() {
41         IPv4 ip = new IPv4();
42         byte[] ipDiffServ = { 20 };
43         ip.hdrFieldsMap.put("DiffServ", ipDiffServ);
44         byte diffServ = ip.getDiffServ();
45         Assert.assertTrue(diffServ == 20);
46     }
47
48     @Test
49     public void testGetTotalLength() {
50         IPv4 ip = new IPv4();
51         byte[] iptotLength = { 3, -24 };
52         ip.hdrFieldsMap.put("TotalLength", iptotLength);
53         short totalLength = ip.getTotalLength();
54         //System.out.println(totalLength);
55         Assert.assertTrue(totalLength == 1000);
56     }
57
58     @Test
59     public void testGetIdentification() {
60         IPv4 ip = new IPv4();
61         byte[] ipIdentification = { 7, -48 };
62         ip.hdrFieldsMap.put("Identification", ipIdentification);
63         short identification = ip.getIdentification();
64         Assert.assertTrue(identification == 2000);
65     }
66
67     @Test
68     public void testGetFlags() {
69         IPv4 ip = new IPv4();
70         byte[] ipFlags = { 7 };
71         ip.hdrFieldsMap.put("Flags", ipFlags);
72         byte flags = ip.getFlags();
73         Assert.assertTrue(flags == 7);
74     }
75
76     @Test
77     public void testGetTtl() {
78         IPv4 ip = new IPv4();
79         byte[] ipTtl = { 100 };
80         ip.hdrFieldsMap.put("TTL", ipTtl);
81         byte ttl = ip.getTtl();
82         Assert.assertTrue(ttl == 100);
83     }
84
85     @Test
86     public void testGetProtocol() {
87         IPv4 ip = new IPv4();
88         byte[] ipProtocol = { 1 };
89         ip.hdrFieldsMap.put("Protocol", ipProtocol);
90         byte protocol = ip.getProtocol();
91         Assert.assertTrue(protocol == 1);
92
93         Class<? extends Packet> clazz = ip.protocolClassMap.get(protocol);
94         System.out.printf("clazz = %s\n", clazz.getName());
95         Assert.assertTrue(clazz == ICMP.class);
96     }
97
98     @Test
99     public void testGetFragmentOffset() {
100         IPv4 ip = new IPv4();
101         byte[] ipFragmentOffset = { 6, -35 };
102         ip.hdrFieldsMap.put("FragmentOffset", ipFragmentOffset);
103         short fragmentOffset = ip.getFragmentOffset();
104         Assert.assertTrue(fragmentOffset == 1757);
105     }
106
107     @Test
108     public void testGetSourceAddress() {
109         IPv4 ip = new IPv4();
110         byte[] ipSourceAddress = { 10, 110, 31, 55 };
111         ip.hdrFieldsMap.put("SourceIPAddress", ipSourceAddress);
112         int sourceAddress = ip.getSourceAddress();
113         Assert.assertTrue(sourceAddress == 174989111);
114     }
115
116     @Test
117     public void testGetDestinationAddress() {
118         IPv4 ip = new IPv4();
119         byte[] ipDestinationAddress = { 20, 55, 62, 110 };
120         ip.hdrFieldsMap.put("DestinationIPAddress", ipDestinationAddress);
121         int destinationAddress = ip.getDestinationAddress();
122         Assert.assertTrue(destinationAddress == 339164782);
123     }
124
125     @Test
126     public void testSetVersion() {
127         IPv4 ip = new IPv4();
128         byte ipVersion = (byte) 4;
129         ip.setVersion(ipVersion);
130         byte[] version = ip.hdrFieldsMap.get("Version");
131         Assert.assertTrue(version[0] == (byte) 4);
132     }
133
134     @Test
135     public void testSetHeaderLength() {
136         IPv4 ip = new IPv4();
137         byte ipHeaderLength = 5;
138         ip.setHeaderLength(ipHeaderLength);
139         byte[] headerLength = ip.hdrFieldsMap.get("HeaderLength");
140         Assert.assertTrue(headerLength[0] == 5);
141     }
142
143     @Test
144     public void testSetDiffServ() {
145         IPv4 ip = new IPv4();
146         byte ipDiffServ = 20;
147         ip.setDiffServ(ipDiffServ);
148         byte[] diffServ = ip.hdrFieldsMap.get("DiffServ");
149         Assert.assertTrue(diffServ[0] == 20);
150     }
151
152     @Test
153     public void testSetTotalLength() {
154         IPv4 ip = new IPv4();
155         short iptotLength = 1000;
156         ip.setTotalLength(iptotLength);
157         byte[] totalLength = ip.hdrFieldsMap.get("TotalLength");
158         Assert.assertTrue(totalLength[0] == 3);
159         Assert.assertTrue(totalLength[1] == -24);
160     }
161
162     @Test
163     public void testSetIdentification() {
164         IPv4 ip = new IPv4();
165         short ipIdentification = 2000;
166         ip.setIdentification(ipIdentification);
167         byte[] identification = ip.hdrFieldsMap.get("Identification");
168         Assert.assertTrue(identification[0] == 7);
169         Assert.assertTrue(identification[1] == -48);
170     }
171
172     @Test
173     public void testSetFlags() {
174         IPv4 ip = new IPv4();
175         byte ipFlags = 7;
176         ip.setFlags(ipFlags);
177         byte[] flags = ip.hdrFieldsMap.get("Flags");
178         Assert.assertTrue(flags[0] == 7);
179     }
180
181     @Test
182     public void testSetTtl() {
183         IPv4 ip = new IPv4();
184         byte ipTtl = 100;
185         ip.setTtl(ipTtl);
186         byte[] ttl = ip.hdrFieldsMap.get("TTL");
187         Assert.assertTrue(ttl[0] == 100);
188     }
189
190     @Test
191     public void testSetProtocol() {
192         IPv4 ip = new IPv4();
193         byte ipProtocol = 11;
194         ip.setProtocol(ipProtocol);
195         byte[] protocol = ip.hdrFieldsMap.get("Protocol");
196         Assert.assertTrue(protocol[0] == 11);
197     }
198
199     @Test
200     public void testSetFragmentOffset() {
201         IPv4 ip = new IPv4();
202         short ipFragmentOffset = 1757;
203         ip.setFragmentOffset(ipFragmentOffset);
204         byte[] fragmentOffset = ip.hdrFieldsMap.get("FragmentOffset");
205         Assert.assertTrue(fragmentOffset[0] == 6);
206         Assert.assertTrue(fragmentOffset[1] == -35);
207     }
208
209
210     @Test
211     public void testSetDestinationAddress() {
212         IPv4 ip = new IPv4();
213         int ipDestinationAddress = 339164782;
214         ip.setDestinationAddress(ipDestinationAddress);
215         byte[] destinationAddress = ip.hdrFieldsMap.get("DestinationIPAddress");
216         Assert.assertTrue(destinationAddress[0] == 20);
217         Assert.assertTrue(destinationAddress[1] == 55);
218         Assert.assertTrue(destinationAddress[2] == 62);
219         Assert.assertTrue(destinationAddress[3] == 110);
220     }
221
222 }