a333d5d89fd9bed9684bd2e694f5cfe29347d43b
[openflowplugin.git] / libraries / liblldp / src / test / java / org / opendaylight / openflowplugin / libraries / liblldp / LLDPTLVTest.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.libraries.liblldp;
9
10 import com.google.common.io.BaseEncoding;
11 import com.google.common.primitives.Bytes;
12 import org.junit.Assert;
13 import org.junit.Test;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 @SuppressWarnings("checkstyle:AbbreviationAsWordInName")
18 public class LLDPTLVTest {
19
20     /** dummy custom tlv value. */
21     private static final String CUSTOM_TLV_ULTIMATE = "What do you get when you multiply 6 by 9?";
22     /** dummy custom tlv value in binary form. */
23     private static final byte[] CUSTOM_TLV_ULTIMATE_BIN = new byte[] {
24         0x57, 0x68, 0x61, 0x74, 0x20, 0x64, 0x6f, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x67, 0x65, 0x74,
25         0x20, 0x77, 0x68, 0x65, 0x6e, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6d, 0x75, 0x6c, 0x74, 0x69,
26         0x70, 0x6c, 0x79, 0x20, 0x36, 0x20, 0x62, 0x79, 0x20, 0x39, 0x3f
27     };
28
29     private static final Logger LOG = LoggerFactory.getLogger(LLDPTLVTest.class);
30
31     /**
32      * Test method for
33      * {@link org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV#createCustomTLVValue(java.lang.String)}
34      * .
35      */
36     @Test
37     public void testCreateCustomTLVValue() {
38         byte[] tlv = LLDPTLV.createCustomTLVValue(CUSTOM_TLV_ULTIMATE);
39
40         byte[] expectedCustomTlv = Bytes.concat(new byte[] {
41             // custom type (7b) + length (9b) = 16b = 2B  (skipped)
42             // 0x7f, 24,
43             // openflow OUI
44             0x00, 0x26, (byte) 0xe1,
45             // subtype
46             0x00},
47             // custom value
48             CUSTOM_TLV_ULTIMATE_BIN);
49
50         BaseEncoding be = BaseEncoding.base16().withSeparator(" ", 2).lowerCase();
51         LOG.debug("expected: {}", be.encode(expectedCustomTlv));
52         LOG.debug("actual  : {}", be.encode(tlv));
53         Assert.assertArrayEquals(expectedCustomTlv, tlv);
54     }
55
56     /**
57      * Test method for
58      * {@link org.opendaylight.openflowplugin.libraries.liblldp.LLDPTLV#getCustomString(byte[], int)}.
59      */
60     @Test
61     public void testGetCustomString() {
62         byte[] inputCustomTlv = Bytes.concat(new byte[] {
63             // custom type (7b) + length (9b) = 16b = 2B  (skipped)
64             // 0x7f, 24,
65             // openflow OUI
66             0x00, 0x26, (byte) 0xe1,
67             // subtype
68             0x00},
69             // custom value
70             CUSTOM_TLV_ULTIMATE_BIN);
71
72         String actual = LLDPTLV.getCustomString(inputCustomTlv, inputCustomTlv.length);
73         LOG.debug("actual custom TLV value as string: {}", actual);
74         Assert.assertEquals(CUSTOM_TLV_ULTIMATE, actual);
75     }
76 }