3 * Copyright (c) 2013-2014 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 java.util.Arrays;
14 import junit.framework.Assert;
16 import org.junit.Test;
17 import org.opendaylight.controller.sal.match.Match;
18 import org.opendaylight.controller.sal.match.MatchType;
19 import org.opendaylight.controller.sal.utils.EtherTypes;
21 public class EthernetTest {
24 public void testGetDestinationMACAddress() {
25 Ethernet eth = new Ethernet();
26 byte mac[] = { 10, 12, 14, 20, 55, 69 };
27 eth.hdrFieldsMap.put("DestinationMACAddress", mac);
28 byte[] dMAC = eth.getDestinationMACAddress();
29 Assert.assertTrue(dMAC[0] == 10);
30 Assert.assertTrue(dMAC[1] == 12);
31 Assert.assertTrue(dMAC[2] == 14);
32 Assert.assertTrue(dMAC[3] == 20);
33 Assert.assertTrue(dMAC[4] == 55);
34 Assert.assertTrue(dMAC[5] == 69);
39 public void testSourceMACAddress() {
40 Ethernet eth = new Ethernet();
41 byte mac[] = { 120, 30, 25, 80, 66, 99 };
42 eth.hdrFieldsMap.put("SourceMACAddress", mac);
43 byte[] sMAC = eth.getSourceMACAddress();
44 Assert.assertTrue(sMAC[0] == 120);
45 Assert.assertTrue(sMAC[1] == 30);
46 Assert.assertTrue(sMAC[2] == 25);
47 Assert.assertTrue(sMAC[3] == 80);
48 Assert.assertTrue(sMAC[4] == 66);
49 Assert.assertTrue(sMAC[5] == 99);
54 public void testGetEthertype() throws Exception {
55 Ethernet eth = new Ethernet();
56 byte ethType[] = { 8, 6 };
57 eth.hdrFieldsMap.put("EtherType", ethType);
58 short etherType = eth.getEtherType();
59 Assert.assertTrue(etherType == 2054);
63 public void testSetDestinationMACAddress() {
64 Ethernet eth = new Ethernet();
65 byte mac[] = { 10, 12, 14, 20, 55, 69 };
66 eth.setDestinationMACAddress(mac);
67 byte[] dMAC = eth.hdrFieldsMap.get("DestinationMACAddress");
68 Assert.assertTrue(dMAC[0] == 10);
69 Assert.assertTrue(dMAC[1] == 12);
70 Assert.assertTrue(dMAC[2] == 14);
71 Assert.assertTrue(dMAC[3] == 20);
72 Assert.assertTrue(dMAC[4] == 55);
73 Assert.assertTrue(dMAC[5] == 69);
78 public void testSetSourceMACAddress() {
79 Ethernet eth = new Ethernet();
80 byte mac[] = { 120, 30, 25, 80, 66, 99 };
81 eth.setSourceMACAddress(mac);
82 byte[] sMAC = eth.hdrFieldsMap.get("SourceMACAddress");
83 Assert.assertTrue(sMAC[0] == 120);
84 Assert.assertTrue(sMAC[1] == 30);
85 Assert.assertTrue(sMAC[2] == 25);
86 Assert.assertTrue(sMAC[3] == 80);
87 Assert.assertTrue(sMAC[4] == 66);
88 Assert.assertTrue(sMAC[5] == 99);
93 public void testSetEthertype() throws Exception {
94 Ethernet eth = new Ethernet();
96 eth.setEtherType(ethType);
97 byte[] etherType = eth.hdrFieldsMap.get("EtherType");
98 Assert.assertTrue(etherType[0] == 8);
99 Assert.assertTrue(etherType[1] == 6);
104 public void testGetMatch() throws Exception {
105 Ethernet eth = new Ethernet();
106 byte smac[] = { (byte) 0xf0, (byte) 0xde, (byte) 0xf1, (byte) 0x71, (byte) 0x72, (byte) 0x8d };
107 byte dmac[] = { (byte) 0xde, (byte) 0x28, (byte) 0xdb, (byte) 0xb3, (byte) 0x7c, (byte) 0xf8 };
108 short ethType = EtherTypes.IPv4.shortValue();
109 eth.setDestinationMACAddress(dmac);
110 eth.setSourceMACAddress(smac);
111 eth.setEtherType(ethType);
113 Match match = eth.getMatch();
115 Assert.assertTrue(Arrays.equals(smac, (byte[]) match.getField(MatchType.DL_SRC).getValue()));
116 Assert.assertTrue(Arrays.equals(dmac, (byte[]) match.getField(MatchType.DL_DST).getValue()));
117 Assert.assertEquals(ethType, (short) match.getField(MatchType.DL_TYPE).getValue());