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 | |
| 24 | import static org.junit.Assert.assertEquals; |
| 25 | |
| 26 | /** |
| 27 | * Unit tests for the Ethernet class. |
| 28 | */ |
| 29 | public class EthernetTest { |
| 30 | |
| 31 | private MacAddress dstMac; |
| 32 | private MacAddress srcMac; |
| 33 | private short ethertype = 6; |
| 34 | private short vlan = 5; |
| 35 | |
| 36 | private Deserializer<Ethernet> deserializer; |
| 37 | |
| 38 | private byte[] byteHeader; |
| 39 | private byte[] vlanByteHeader; |
| 40 | |
| 41 | @Before |
| 42 | public void setUp() { |
| 43 | deserializer = Ethernet.deserializer(); |
| 44 | |
| 45 | byte[] dstMacBytes = { |
| 46 | (byte) 0x88, (byte) 0x88, (byte) 0x88, (byte) 0x88, (byte) 0x88, |
| 47 | (byte) 0x88 }; |
| 48 | dstMac = MacAddress.valueOf(dstMacBytes); |
| 49 | byte[] srcMacBytes = { |
| 50 | (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, (byte) 0xaa, |
| 51 | (byte) 0xaa }; |
| 52 | srcMac = MacAddress.valueOf(srcMacBytes); |
| 53 | |
| 54 | // Create Ethernet byte array with no VLAN header |
| 55 | ByteBuffer bb = ByteBuffer.allocate(Ethernet.ETHERNET_HEADER_LENGTH); |
| 56 | bb.put(dstMacBytes); |
| 57 | bb.put(srcMacBytes); |
| 58 | bb.putShort(ethertype); |
| 59 | |
| 60 | byteHeader = bb.array(); |
| 61 | |
| 62 | // Create Ethernet byte array with a VLAN header |
| 63 | bb = ByteBuffer.allocate(Ethernet.ETHERNET_HEADER_LENGTH + Ethernet.VLAN_HEADER_LENGTH); |
| 64 | bb.put(dstMacBytes); |
| 65 | bb.put(srcMacBytes); |
| 66 | bb.putShort(Ethernet.TYPE_VLAN); |
| 67 | bb.putShort(vlan); |
| 68 | bb.putShort(ethertype); |
| 69 | |
| 70 | vlanByteHeader = bb.array(); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testDeserializeBadInput() throws Exception { |
| 75 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testDeserializeTruncated() throws DeserializationException { |
| 80 | PacketTestUtils.testDeserializeTruncated(deserializer, vlanByteHeader); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testDeserializeNoVlan() throws Exception { |
| 85 | Ethernet eth = deserializer.deserialize(byteHeader, 0, byteHeader.length); |
| 86 | |
| 87 | assertEquals(dstMac, eth.getDestinationMAC()); |
| 88 | assertEquals(srcMac, eth.getSourceMAC()); |
| 89 | assertEquals(Ethernet.VLAN_UNTAGGED, eth.getVlanID()); |
| 90 | assertEquals(ethertype, eth.getEtherType()); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testDeserializeWithVlan() throws Exception { |
| 95 | Ethernet eth = deserializer.deserialize(vlanByteHeader, 0, vlanByteHeader.length); |
| 96 | |
| 97 | assertEquals(dstMac, eth.getDestinationMAC()); |
| 98 | assertEquals(srcMac, eth.getSourceMAC()); |
| 99 | assertEquals(vlan, eth.getVlanID()); |
| 100 | assertEquals(ethertype, eth.getEtherType()); |
| 101 | } |
| 102 | |
| 103 | } |