23f8ae6f96e132739e7880d4c0905bfba1419f45
[controller.git] / opendaylight / commons / liblldp / src / test / java / org / opendaylight / controller / 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.controller.liblldp;
9
10 import org.junit.Assert;
11 import org.junit.Test;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import com.google.common.io.BaseEncoding;
16 import com.google.common.primitives.Bytes;
17
18 /**
19  *
20  */
21 public class LLDPTLVTest {
22
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
30     };
31
32     private static final Logger LOG = LoggerFactory.getLogger(LLDPTLVTest.class);
33
34     /**
35      * Test method for
36      * {@link org.opendaylight.controller.liblldp.LLDPTLV#createCustomTLVValue(java.lang.String)}
37      * .
38      */
39     @Test
40     public void testCreateCustomTLVValue() {
41         byte[] tlv = LLDPTLV.createCustomTLVValue(CUSTOM_TLV_ULTIMATE);
42
43         byte[] expectedCustomTlv = Bytes.concat(new byte[] {
44                 // custom type (7b) + length (9b) = 16b = 2B  (skipped)
45                 // 0x7f, 24,
46                 // openflow OUI
47                 0x00, 0x26, (byte) 0xe1,
48                 // subtype
49                 0x00},
50                 // custom value
51                 CUSTOM_TLV_ULTIMATE_BIN);
52
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);
57     }
58
59     /**
60      * Test method for
61      * {@link org.opendaylight.controller.liblldp.LLDPTLV#getCustomString(byte[], int)}
62      * .
63      * @throws Exception
64      */
65     @Test
66     public void testGetCustomString() throws Exception {
67         byte[] inputCustomTlv = Bytes.concat(new byte[] {
68                 // custom type (7b) + length (9b) = 16b = 2B  (skipped)
69                 // 0x7f, 24,
70                 // openflow OUI
71                 0x00, 0x26, (byte) 0xe1,
72                 // subtype
73                 0x00},
74                 // custom value
75                 CUSTOM_TLV_ULTIMATE_BIN);
76
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);
80     }
81 }