blob: 95b0b04afe3736f83020a44343032748fb10e8f0 [file] [log] [blame]
Jonathan Hart2a655752015-04-07 16:46:33 -07001/*
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
17package org.onlab.packet;
18
19import org.junit.Before;
20import org.junit.Test;
21
22import java.nio.ByteBuffer;
23import java.util.Arrays;
24
25import static org.junit.Assert.assertEquals;
26import static org.junit.Assert.assertTrue;
27
28/**
29 * Unit tests for the LLDP class.
30 */
31public 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
92 @Test
93 public void testDeserialize() throws Exception {
94 LLDP lldp = deserializer.deserialize(bytes, 0, bytes.length);
95
96 assertEquals(LLDP.CHASSIS_TLV_TYPE, lldp.getChassisId().getType());
97 assertEquals(LLDP.CHASSIS_TLV_SIZE, lldp.getChassisId().getLength());
98 assertTrue(Arrays.equals(chassisValue, lldp.getChassisId().getValue()));
99
100 assertEquals(LLDP.PORT_TLV_TYPE, lldp.getPortId().getType());
101 assertEquals(LLDP.PORT_TLV_SIZE, lldp.getPortId().getLength());
102 assertTrue(Arrays.equals(portValue, lldp.getPortId().getValue()));
103
104 assertEquals(LLDP.TTL_TLV_TYPE, lldp.getTtl().getType());
105 assertEquals(LLDP.TTL_TLV_SIZE, lldp.getTtl().getLength());
106 assertTrue(Arrays.equals(ttlValue, lldp.getTtl().getValue()));
107
108 assertEquals(1, lldp.getOptionalTLVList().size());
109 LLDPTLV optionalTlv = lldp.getOptionalTLVList().get(0);
110
111 assertEquals(LLDPOrganizationalTLV.ORGANIZATIONAL_TLV_TYPE, optionalTlv.getType());
112 assertEquals(optionalTlvSize, optionalTlv.getLength());
113 assertTrue(Arrays.equals(optionalTlvValue, optionalTlv.getValue()));
114 }
115}