3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
10 package org.opendaylight.controller.sal.packet;
12 import junit.framework.Assert;
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;
19 public class IPv4Test {
22 public void testGetVersion() {
24 byte[] ipVersion = { (byte) 4 };
25 ip.hdrFieldsMap.put("Version", ipVersion);
26 byte version = ip.getVersion();
27 Assert.assertTrue(version == (byte) 4);
31 public void testGetHeaderLength() {
33 byte[] ipHeaderLength = { 5 };
34 ip.hdrFieldsMap.put("HeaderLength", ipHeaderLength);
35 byte headerLength = (byte) ip.getHeaderLen();
36 Assert.assertTrue(headerLength == 20);
40 public void testGetDiffServ() {
42 byte[] ipDiffServ = { 20 };
43 ip.hdrFieldsMap.put("DiffServ", ipDiffServ);
44 byte diffServ = ip.getDiffServ();
45 Assert.assertTrue(diffServ == 20);
49 public void testGetTotalLength() {
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);
59 public void testGetIdentification() {
61 byte[] ipIdentification = { 7, -48 };
62 ip.hdrFieldsMap.put("Identification", ipIdentification);
63 short identification = ip.getIdentification();
64 Assert.assertTrue(identification == 2000);
68 public void testGetFlags() {
70 byte[] ipFlags = { 7 };
71 ip.hdrFieldsMap.put("Flags", ipFlags);
72 byte flags = ip.getFlags();
73 Assert.assertTrue(flags == 7);
77 public void testGetTtl() {
79 byte[] ipTtl = { 100 };
80 ip.hdrFieldsMap.put("TTL", ipTtl);
81 byte ttl = ip.getTtl();
82 Assert.assertTrue(ttl == 100);
86 public void testGetProtocol() {
88 byte[] ipProtocol = { 1 };
89 ip.hdrFieldsMap.put("Protocol", ipProtocol);
90 byte protocol = ip.getProtocol();
91 Assert.assertTrue(protocol == 1);
93 Class<? extends Packet> clazz = IPv4.protocolClassMap.get(protocol);
94 System.out.printf("clazz = %s\n", clazz.getName());
95 Assert.assertTrue(clazz == ICMP.class);
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);
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);
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);
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);
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);
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);
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);
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);
173 public void testSetFlags() {
174 IPv4 ip = new IPv4();
176 ip.setFlags(ipFlags);
177 byte[] flags = ip.hdrFieldsMap.get("Flags");
178 Assert.assertTrue(flags[0] == 7);
182 public void testSetTtl() {
183 IPv4 ip = new IPv4();
186 byte[] ttl = ip.hdrFieldsMap.get("TTL");
187 Assert.assertTrue(ttl[0] == 100);
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);
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);
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);