2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.liblldp;
10 import org.junit.Assert;
11 import org.junit.Test;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
15 import com.google.common.io.BaseEncoding;
16 import com.google.common.primitives.Bytes;
21 public class LLDPTLVTest {
23 /** dummy custom tlv value */
24 private static final String CUSTOM_TLV_ULTIMATE = "What do you get when you multiply 6 by 9?";
25 /** dummy custom tlv value in binary form */
26 private static final byte[] CUSTOM_TLV_ULTIMATE_BIN = new byte[] {
27 0x57, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x67, 0x65, 0x74,
28 0x20, 0x77, 0x68, 0x65, 0x6e, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69,
29 0x70, 0x6c, 0x79, 0x20, 0x36, 0x20, 0x62, 0x79, 0x20, 0x39, 0x3f
32 private static final Logger LOG = LoggerFactory.getLogger(LLDPTLVTest.class);
36 * {@link org.opendaylight.controller.liblldp.LLDPTLV#createCustomTLVValue(java.lang.String)}
40 public void testCreateCustomTLVValue() {
41 byte[] tlv = LLDPTLV.createCustomTLVValue(CUSTOM_TLV_ULTIMATE);
43 byte[] expectedCustomTlv = Bytes.concat(new byte[] {
44 // custom type (7b) + length (9b) = 16b = 2B (skipped)
47 0x00, 0x26, (byte) 0xe1,
51 CUSTOM_TLV_ULTIMATE_BIN);
53 BaseEncoding be = BaseEncoding.base16().withSeparator(" ", 2).lowerCase();
54 LOG.debug("expected: {}", be.encode(expectedCustomTlv));
55 LOG.debug("actual : {}", be.encode(tlv));
56 Assert.assertArrayEquals(expectedCustomTlv, tlv);
61 * {@link org.opendaylight.controller.liblldp.LLDPTLV#getCustomString(byte[], int)}
66 public void testGetCustomString() throws Exception {
67 byte[] inputCustomTlv = Bytes.concat(new byte[] {
68 // custom type (7b) + length (9b) = 16b = 2B (skipped)
71 0x00, 0x26, (byte) 0xe1,
75 CUSTOM_TLV_ULTIMATE_BIN);
77 String actual = LLDPTLV.getCustomString(inputCustomTlv, inputCustomTlv.length);
78 LOG.debug("actual custom TLV value as string: {}", actual);
79 Assert.assertEquals(CUSTOM_TLV_ULTIMATE, actual);