blob: f680e93e0d1fc579991bb7a84145d3e4dc8c99b9 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2 * Licensed under the Apache License, Version 2.0 (the "License"); you may
3 * not use this file except in compliance with the License. You may obtain
4 * a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11 * License for the specific language governing permissions and limitations
12 * under the License.
13 **/
14
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070015package net.onrc.onos.core.packet;
Jonathan Hart96892d12014-03-26 20:21:29 -070016
Jonathan Harta88fd242014-04-03 11:24:54 -070017import static org.hamcrest.CoreMatchers.is;
18import static org.junit.Assert.assertThat;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080019
20import java.nio.ByteBuffer;
21import java.nio.charset.Charset;
22
Jonathan Harta88fd242014-04-03 11:24:54 -070023import org.junit.Test;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080024
25public class LLDPOrganizationalTLVTest {
Ray Milkey269ffb92014-04-03 14:43:30 -070026 private final byte[] expected = new byte[]{
27 // Type: 127, Length: 13
28 (byte) 254, 13,
29 // OpenFlow OUI: 00-26-E1
30 0x0, 0x26, (byte) 0xe1,
31 // SubType: 12
32 0xc,
33 // Bytes in "ExtraInfo"
34 0x45, 0x78, 0x74, 0x72, 0x61, 0x49, 0x6e, 0x66, 0x6f
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080035 };
36
37 @Test(expected = IllegalArgumentException.class)
38 public void testShortOUI() {
39 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
40 tlv.setOUI(new byte[2]);
41 }
42
43 @Test(expected = IllegalArgumentException.class)
44 public void testLongOUI() {
45 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
46 tlv.setOUI(new byte[4]);
47 }
48
49 @Test(expected = IllegalArgumentException.class)
50 public void testLongInfoString() {
51 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
52 tlv.setInfoString(new byte[LLDPOrganizationalTLV.MAX_INFOSTRING_LENGTH + 1]);
53 }
54
55 @Test
56 public void testMaxInfoString() {
57 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
58 tlv.setInfoString(new byte[LLDPOrganizationalTLV.MAX_INFOSTRING_LENGTH]);
59 }
60
61 @Test
62 public void testInfoString() {
63 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
64 tlv.setInfoString("ExtraInfo");
65 assertThat(tlv.getInfoString(), is("ExtraInfo".getBytes(Charset.forName("UTF-8"))));
66 }
67
68 @Test
69 public void testSerialize() {
70 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
71 tlv.setLength((short) 13);
72 // OpenFlow OUI is 00-26-E1
Ray Milkey269ffb92014-04-03 14:43:30 -070073 tlv.setOUI(new byte[]{0x0, 0x26, (byte) 0xe1});
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080074 tlv.setSubType((byte) 12);
75 tlv.setInfoString("ExtraInfo".getBytes(Charset.forName("UTF-8")));
76
Ray Milkey269ffb92014-04-03 14:43:30 -070077 assertThat(tlv.getType(), is((byte) 127));
78 assertThat(tlv.getLength(), is((short) 13));
79 assertThat(tlv.getOUI(), is(new byte[]{0x0, 0x26, (byte) 0xe1}));
80 assertThat(tlv.getSubType(), is((byte) 12));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080081 assertThat(tlv.serialize(), is(expected));
82 }
83
84 @Test
85 public void testDeserialize() {
86 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
87 tlv.deserialize(ByteBuffer.wrap(expected));
88
Ray Milkey269ffb92014-04-03 14:43:30 -070089 assertThat(tlv.getType(), is((byte) 127));
90 assertThat(tlv.getLength(), is((short) 13));
91 assertThat(tlv.getOUI(), is(new byte[]{0x0, 0x26, (byte) 0xe1}));
92 assertThat(tlv.getSubType(), is((byte) 12));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080093 assertThat(tlv.getInfoString(), is("ExtraInfo".getBytes(Charset.forName("UTF-8"))));
94 }
95}