8f3b283b2ea020a175b53e952d08f41cd7ca1c69
[controller.git] / opendaylight / sal / api / src / test / java / org / opendaylight / controller / sal / packet / PacketTest.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 java.util.Map;
13
14 import junit.framework.Assert;
15
16 import org.junit.Test;
17
18 public class PacketTest {
19
20     @Test
21     public void testDeserialize() throws NoSuchFieldException, Exception {
22         short startOffset, numBits;
23
24         Ethernet eth = new Ethernet();
25         byte[] data = { 10, 12, 14, 20, 55, 69, //DMAC
26                 -90, -20, -100, -82, -78, -97, //SMAC
27                 8, 6, //ethype
28                 0, 1, // hw type
29                 8, 0, // proto (ip)
30                 6, // hw addr len
31                 4, // proto addr len
32                 0, 1, // op codes
33                 -90, -20, -100, -82, -78, -97, //src hw addr
34                 9, 9, 9, 1, // src proto
35                 0, 0, 0, 0, 0, 0, // target hw addr
36                 9, 9, 9, -2 }; // target proto
37
38         startOffset = 0;
39         numBits = 42 * 8;
40         eth.deserialize(data, startOffset, numBits);
41
42         byte[] dMAC = eth.getDestinationMACAddress();
43         byte[] sMAC = eth.getSourceMACAddress();
44         short etherType = eth.getEtherType();
45
46         Assert.assertTrue(dMAC[0] == 10);
47         Assert.assertTrue(dMAC[1] == 12);
48         Assert.assertTrue(dMAC[2] == 14);
49         Assert.assertTrue(dMAC[3] == 20);
50         Assert.assertTrue(dMAC[4] == 55);
51         Assert.assertTrue(dMAC[5] == 69);
52
53         Assert.assertTrue(sMAC[0] == -90);
54         Assert.assertTrue(sMAC[1] == -20);
55         Assert.assertTrue(sMAC[2] == -100);
56         Assert.assertTrue(sMAC[3] == -82);
57         Assert.assertTrue(sMAC[4] == -78);
58         Assert.assertTrue(sMAC[5] == -97);
59
60         Assert.assertTrue(etherType == 0x806);
61
62         ARP arpPacket = (ARP) eth.getPayload();
63
64         Assert.assertTrue(arpPacket.getHardwareType() == (byte)0x1);
65         Assert.assertTrue(arpPacket.getProtocolType() == 2048);
66         Assert.assertTrue(arpPacket.getHardwareAddressLength() == (byte)0x6);
67         Assert.assertTrue(arpPacket.getProtocolAddressLength() == (byte)0x4);
68         Assert.assertTrue(arpPacket.getOpCode() == 1);
69
70         byte[] senderHwAddress = arpPacket.getSenderHardwareAddress();
71         byte[] senderProtocolAddress = arpPacket.getSenderProtocolAddress();
72
73         byte[] targetHwAddress = arpPacket.getTargetHardwareAddress();
74         byte[] targetProtocolAddress = arpPacket.getTargetProtocolAddress();
75
76
77         Assert.assertTrue(senderHwAddress[0] == (byte)0xA6);
78         Assert.assertTrue(senderHwAddress[1] == (byte)0xEC);
79         Assert.assertTrue(senderHwAddress[2] == (byte)0x9C);
80         Assert.assertTrue(senderHwAddress[3] == (byte)0xAE);
81         Assert.assertTrue(senderHwAddress[4] == (byte)0xB2);
82         Assert.assertTrue(senderHwAddress[5] == (byte)0x9F);
83
84         Assert.assertTrue(senderProtocolAddress[0] == (byte)0x9);
85         Assert.assertTrue(senderProtocolAddress[1] == (byte)0x9);
86         Assert.assertTrue(senderProtocolAddress[2] == (byte)0x9);
87         Assert.assertTrue(senderProtocolAddress[3] == (byte)0x1);
88
89         Assert.assertTrue(targetHwAddress[0] == (byte)0x0);
90         Assert.assertTrue(targetHwAddress[1] == (byte)0x0);
91         Assert.assertTrue(targetHwAddress[2] == (byte)0x0);
92         Assert.assertTrue(targetHwAddress[3] == (byte)0x0);
93         Assert.assertTrue(targetHwAddress[4] == (byte)0x0);
94         Assert.assertTrue(targetHwAddress[5] == (byte)0x0);
95
96         Assert.assertTrue(senderProtocolAddress[0] == (byte)0x9);
97         Assert.assertTrue(senderProtocolAddress[1] == (byte)0x9);
98         Assert.assertTrue(senderProtocolAddress[2] == (byte)0x9);
99         Assert.assertTrue(senderProtocolAddress[3] == (byte)0x1);
100
101         Assert.assertTrue(targetProtocolAddress[0] == (byte)0x9);
102         Assert.assertTrue(targetProtocolAddress[1] == (byte)0x9);
103         Assert.assertTrue(targetProtocolAddress[2] == (byte)0x9);
104         Assert.assertTrue(targetProtocolAddress[3] == (byte)0xFE);
105     }
106
107     @Test
108     public void testSerialize() throws NoSuchFieldException, Exception {
109         Ethernet eth = new Ethernet();
110         Map<String, byte[]> fCValues = eth.hdrFieldsMap;
111
112         byte[] dMAC = { 10, 12, 14, 20, 55, 69 };
113         byte[] sMAC = { 82, 97, 109, 117, 127, -50 };
114         short etherType = 2054;
115
116         byte[] dMACdata, sMACdata, etherTypedata;
117         byte[] data = new byte[20];
118
119         eth.setDestinationMACAddress(dMAC);
120         eth.setSourceMACAddress(sMAC);
121         eth.setEtherType(etherType);
122
123         dMACdata = (byte[]) fCValues.get("DestinationMACAddress");
124         sMACdata = (byte[]) fCValues.get("SourceMACAddress");
125         etherTypedata = (byte[]) fCValues.get("EtherType");
126
127         Assert.assertTrue(dMACdata[0] == 10);
128         Assert.assertTrue(dMACdata[1] == 12);
129         Assert.assertTrue(dMACdata[2] == 14);
130         Assert.assertTrue(dMACdata[3] == 20);
131         Assert.assertTrue(dMACdata[4] == 55);
132         Assert.assertTrue(dMACdata[5] == 69);
133
134         Assert.assertTrue(sMACdata[0] == 82);
135         Assert.assertTrue(sMACdata[1] == 97);
136         Assert.assertTrue(sMACdata[2] == 109);
137         Assert.assertTrue(sMACdata[3] == 117);
138         Assert.assertTrue(sMACdata[4] == 127);
139         Assert.assertTrue(sMACdata[5] == -50);
140
141         Assert.assertTrue(etherTypedata[0] == 8);
142         Assert.assertTrue(etherTypedata[1] == 6);
143         data = eth.serialize();
144
145         Assert.assertTrue(data[0] == 10);
146         Assert.assertTrue(data[1] == 12);
147         Assert.assertTrue(data[2] == 14);
148         Assert.assertTrue(data[3] == 20);
149         Assert.assertTrue(data[4] == 55);
150         Assert.assertTrue(data[5] == 69);
151
152         Assert.assertTrue(data[6] == 82);
153         Assert.assertTrue(data[7] == 97);
154         Assert.assertTrue(data[8] == 109);
155         Assert.assertTrue(data[9] == 117);
156         Assert.assertTrue(data[10] == 127);
157         Assert.assertTrue(data[11] == -50);
158
159         Assert.assertTrue(data[12] == 8);
160         Assert.assertTrue(data[13] == 6);
161
162     }
163 }