Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | import java.util.Arrays; |
| 24 | |
| 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertTrue; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for the LLDP class. |
| 30 | */ |
| 31 | public class LLDPTest { |
| 32 | |
| 33 | private Deserializer<LLDP> deserializer; |
| 34 | |
| 35 | private byte[] chassisValue = new byte[] {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7}; |
| 36 | private byte[] portValue = new byte[] {0x1, 0x2, 0x3, 0x4, 0x5}; |
| 37 | private byte[] ttlValue = new byte[] {0x0, 0x20}; |
| 38 | |
| 39 | private short optionalTlvSize = 6; |
| 40 | private byte[] optionalTlvValue = new byte[] {0x6, 0x5, 0x4, 0x3, 0x2, 0x1}; |
| 41 | |
| 42 | private byte[] bytes; |
| 43 | |
| 44 | @Before |
| 45 | public void setUp() throws Exception { |
| 46 | deserializer = LLDP.deserializer(); |
| 47 | |
| 48 | // Each TLV is 2 bytes for the type+length, plus the size of the value |
| 49 | // There are 2 zero-bytes at the end |
| 50 | ByteBuffer bb = ByteBuffer.allocate(2 + LLDP.CHASSIS_TLV_SIZE + |
| 51 | 2 + LLDP.PORT_TLV_SIZE + |
| 52 | 2 + LLDP.TTL_TLV_SIZE + |
| 53 | 2 + optionalTlvSize + |
| 54 | 2); |
| 55 | |
| 56 | // Chassis TLV |
| 57 | bb.putShort(getTypeLength(LLDP.CHASSIS_TLV_TYPE, LLDP.CHASSIS_TLV_SIZE)); |
| 58 | bb.put(chassisValue); |
| 59 | |
| 60 | // Port TLV |
| 61 | bb.putShort(getTypeLength(LLDP.PORT_TLV_TYPE, LLDP.PORT_TLV_SIZE)); |
| 62 | bb.put(portValue); |
| 63 | |
| 64 | // TTL TLV |
| 65 | bb.putShort(getTypeLength(LLDP.TTL_TLV_TYPE, LLDP.TTL_TLV_SIZE)); |
| 66 | bb.put(ttlValue); |
| 67 | |
| 68 | // Optional TLV |
| 69 | bb.putShort(getTypeLength(LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE, optionalTlvSize)); |
| 70 | bb.put(optionalTlvValue); |
| 71 | |
| 72 | bb.putShort((short) 0); |
| 73 | |
| 74 | bytes = bb.array(); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | private short getTypeLength(byte type, short length) { |
| 79 | return (short) ((0x7f & type) << 9 | 0x1ff & length); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void testDeserializeBadInput() throws Exception { |
| 84 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 85 | } |
| 86 | |
| 87 | @Test |
| 88 | public void testDeserializeTruncated() throws Exception { |
| 89 | PacketTestUtils.testDeserializeTruncated(deserializer, bytes); |
| 90 | } |
| 91 | |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 92 | /** |
| 93 | * Tests deserialize and getters. |
| 94 | */ |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 95 | @Test |
| 96 | public void testDeserialize() throws Exception { |
| 97 | LLDP lldp = deserializer.deserialize(bytes, 0, bytes.length); |
| 98 | |
| 99 | assertEquals(LLDP.CHASSIS_TLV_TYPE, lldp.getChassisId().getType()); |
| 100 | assertEquals(LLDP.CHASSIS_TLV_SIZE, lldp.getChassisId().getLength()); |
| 101 | assertTrue(Arrays.equals(chassisValue, lldp.getChassisId().getValue())); |
| 102 | |
| 103 | assertEquals(LLDP.PORT_TLV_TYPE, lldp.getPortId().getType()); |
| 104 | assertEquals(LLDP.PORT_TLV_SIZE, lldp.getPortId().getLength()); |
| 105 | assertTrue(Arrays.equals(portValue, lldp.getPortId().getValue())); |
| 106 | |
| 107 | assertEquals(LLDP.TTL_TLV_TYPE, lldp.getTtl().getType()); |
| 108 | assertEquals(LLDP.TTL_TLV_SIZE, lldp.getTtl().getLength()); |
| 109 | assertTrue(Arrays.equals(ttlValue, lldp.getTtl().getValue())); |
| 110 | |
| 111 | assertEquals(1, lldp.getOptionalTLVList().size()); |
| 112 | LLDPTLV optionalTlv = lldp.getOptionalTLVList().get(0); |
| 113 | |
| 114 | assertEquals(LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE, optionalTlv.getType()); |
| 115 | assertEquals(optionalTlvSize, optionalTlv.getLength()); |
| 116 | assertTrue(Arrays.equals(optionalTlvValue, optionalTlv.getValue())); |
| 117 | } |
Jian Li | 5fc1429 | 2015-12-04 11:30:46 -0800 | [diff] [blame] | 118 | |
| 119 | /** |
| 120 | * Tests toString. |
| 121 | */ |
| 122 | @Test |
| 123 | public void testToStringLLDP() throws Exception { |
| 124 | LLDP lldp = deserializer.deserialize(bytes, 0, bytes.length); |
| 125 | String str = lldp.toString(); |
| 126 | |
| 127 | // TODO: need to add LLDP toString unit test |
| 128 | } |
Jonathan Hart | 2a65575 | 2015-04-07 16:46:33 -0700 | [diff] [blame] | 129 | } |