Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onlab.packet; |
| 18 | |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import java.nio.ByteBuffer; |
| 23 | |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 24 | import java.util.Arrays; |
| 25 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 26 | import static org.junit.Assert.assertEquals; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for the Ethernet class. |
| 30 | */ |
| 31 | public class EthernetTest { |
| 32 | |
| 33 | private MacAddress dstMac; |
| 34 | private MacAddress srcMac; |
| 35 | private short ethertype = 6; |
| 36 | private short vlan = 5; |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 37 | private short qinqVlan = 55; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 38 | |
| 39 | private Deserializer<Ethernet> deserializer; |
| 40 | |
| 41 | private byte[] byteHeader; |
| 42 | private byte[] vlanByteHeader; |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 43 | private byte[] qinq8100ByteHeader; |
| 44 | private byte[] qinq88a8ByteHeader; |
| 45 | |
| 46 | private static byte[] qinqHeaderExpected = { |
| 47 | (byte) 0x88, (byte) 0x88, (byte) 0x88, (byte) 0x88, |
| 48 | (byte) 0x88, (byte) 0x88, (byte) 0xaa, (byte) 0xaa, |
| 49 | (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, |
| 50 | (byte) 0x88, (byte) 0xa8, (byte) 0x00, (byte) 0x37, |
| 51 | (byte) 0x81, (byte) 0x00, (byte) 0x00, (byte) 0x05, |
| 52 | (byte) 0x00, (byte) 0x06 }; |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 53 | |
| 54 | @Before |
| 55 | public void setUp() { |
| 56 | deserializer = Ethernet.deserializer(); |
| 57 | |
| 58 | byte[] dstMacBytes = { |
| 59 | (byte) 0x88, (byte) 0x88, (byte) 0x88, (byte) 0x88, (byte) 0x88, |
| 60 | (byte) 0x88 }; |
| 61 | dstMac = MacAddress.valueOf(dstMacBytes); |
| 62 | byte[] srcMacBytes = { |
| 63 | (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, |
| 64 | (byte) 0xaa }; |
| 65 | srcMac = MacAddress.valueOf(srcMacBytes); |
| 66 | |
| 67 | // Create Ethernet byte array with no VLAN header |
| 68 | ByteBuffer bb = ByteBuffer.allocate(Ethernet.ETHERNET_HEADER_LENGTH); |
| 69 | bb.put(dstMacBytes); |
| 70 | bb.put(srcMacBytes); |
| 71 | bb.putShort(ethertype); |
| 72 | |
| 73 | byteHeader = bb.array(); |
| 74 | |
| 75 | // Create Ethernet byte array with a VLAN header |
| 76 | bb = ByteBuffer.allocate(Ethernet.ETHERNET_HEADER_LENGTH + Ethernet.VLAN_HEADER_LENGTH); |
| 77 | bb.put(dstMacBytes); |
| 78 | bb.put(srcMacBytes); |
| 79 | bb.putShort(Ethernet.TYPE_VLAN); |
| 80 | bb.putShort(vlan); |
| 81 | bb.putShort(ethertype); |
| 82 | |
| 83 | vlanByteHeader = bb.array(); |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 84 | |
| 85 | // Create Ethernet byte array with a QinQ header with TPID 0x8100 |
| 86 | bb = ByteBuffer.allocate(Ethernet.ETHERNET_HEADER_LENGTH + Ethernet.VLAN_HEADER_LENGTH |
| 87 | + Ethernet.VLAN_HEADER_LENGTH); |
| 88 | bb.put(dstMacBytes); |
| 89 | bb.put(srcMacBytes); |
| 90 | bb.putShort(Ethernet.TYPE_VLAN); |
| 91 | bb.putShort(vlan); |
| 92 | bb.putShort(Ethernet.TYPE_VLAN); |
| 93 | bb.putShort(vlan); |
| 94 | bb.putShort(ethertype); |
| 95 | |
| 96 | qinq8100ByteHeader = bb.array(); |
| 97 | |
| 98 | // Create Ethernet byte array with a QinQ header with TPID 0x88a8 |
| 99 | bb = ByteBuffer.allocate(Ethernet.ETHERNET_HEADER_LENGTH + Ethernet.VLAN_HEADER_LENGTH |
| 100 | + Ethernet.VLAN_HEADER_LENGTH); |
| 101 | bb.put(dstMacBytes); |
| 102 | bb.put(srcMacBytes); |
| 103 | bb.putShort(Ethernet.TYPE_QINQ); |
| 104 | bb.putShort(qinqVlan); |
| 105 | bb.putShort(Ethernet.TYPE_VLAN); |
| 106 | bb.putShort(vlan); |
| 107 | bb.putShort(ethertype); |
| 108 | |
| 109 | qinq88a8ByteHeader = bb.array(); |
| 110 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testDeserializeBadInput() throws Exception { |
| 115 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 116 | } |
| 117 | |
| 118 | @Test |
| 119 | public void testDeserializeTruncated() throws DeserializationException { |
| 120 | PacketTestUtils.testDeserializeTruncated(deserializer, vlanByteHeader); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testDeserializeNoVlan() throws Exception { |
| 125 | Ethernet eth = deserializer.deserialize(byteHeader, 0, byteHeader.length); |
| 126 | |
| 127 | assertEquals(dstMac, eth.getDestinationMAC()); |
| 128 | assertEquals(srcMac, eth.getSourceMAC()); |
| 129 | assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID()); |
| 130 | assertEquals(ethertype, eth.getEtherType()); |
| 131 | } |
| 132 | |
| 133 | @Test |
| 134 | public void testDeserializeWithVlan() throws Exception { |
| 135 | Ethernet eth = deserializer.deserialize(vlanByteHeader, 0, vlanByteHeader.length); |
| 136 | |
| 137 | assertEquals(dstMac, eth.getDestinationMAC()); |
| 138 | assertEquals(srcMac, eth.getSourceMAC()); |
| 139 | assertEquals(vlan, eth.getVlanID()); |
| 140 | assertEquals(ethertype, eth.getEtherType()); |
| 141 | } |
| 142 | |
Amit Ghosh | 764a1b4 | 2017-03-18 08:20:06 +0000 | [diff] [blame] | 143 | @Test |
| 144 | public void testDeserializeWithQinQ() throws Exception { |
| 145 | Ethernet eth = deserializer.deserialize(qinq8100ByteHeader, 0, qinq8100ByteHeader.length); |
| 146 | |
| 147 | assertEquals(dstMac, eth.getDestinationMAC()); |
| 148 | assertEquals(srcMac, eth.getSourceMAC()); |
| 149 | assertEquals(vlan, eth.getVlanID()); |
| 150 | assertEquals(vlan, eth.getQinQVID()); |
| 151 | assertEquals(ethertype, eth.getEtherType()); |
| 152 | |
| 153 | eth = deserializer.deserialize(qinq88a8ByteHeader, 0, qinq88a8ByteHeader.length); |
| 154 | |
| 155 | assertEquals(dstMac, eth.getDestinationMAC()); |
| 156 | assertEquals(srcMac, eth.getSourceMAC()); |
| 157 | assertEquals(vlan, eth.getVlanID()); |
| 158 | assertEquals(qinqVlan, eth.getQinQVID()); |
| 159 | assertEquals(ethertype, eth.getEtherType()); |
| 160 | } |
| 161 | |
| 162 | @Test |
| 163 | public void testSerializeWithQinQ() throws Exception { |
| 164 | Ethernet eth = new Ethernet(); |
| 165 | eth.setDestinationMACAddress(dstMac); |
| 166 | eth.setSourceMACAddress(srcMac); |
| 167 | eth.setVlanID(vlan); |
| 168 | eth.setQinQVID(qinqVlan); |
| 169 | eth.setEtherType(ethertype); |
| 170 | |
| 171 | byte[] encoded = eth.serialize(); |
| 172 | |
| 173 | assertEquals(Arrays.toString(encoded), Arrays.toString(qinqHeaderExpected)); |
| 174 | } |
| 175 | |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 176 | } |