blob: 1a8367c85b1819a32729c0901f237aa06f42fe60 [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 Hart96892d12014-03-26 20:21:29 -070015package net.onrc.onos.packet;
16
17import net.onrc.onos.packet.LLDPOrganizationalTLV;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080018
19import org.junit.Test;
20
21import java.nio.ByteBuffer;
22import java.nio.charset.Charset;
23
24import static org.hamcrest.CoreMatchers.is;
25import static org.junit.Assert.assertThat;
26
27public class LLDPOrganizationalTLVTest {
28 private final byte[] expected = new byte[] {
29 // Type: 127, Length: 13
30 (byte) 254, 13,
31 // OpenFlow OUI: 00-26-E1
32 0x0, 0x26, (byte)0xe1,
33 // SubType: 12
34 0xc,
35 // Bytes in "ExtraInfo"
36 0x45, 0x78, 0x74, 0x72, 0x61, 0x49, 0x6e, 0x66, 0x6f
37 };
38
39 @Test(expected = IllegalArgumentException.class)
40 public void testShortOUI() {
41 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
42 tlv.setOUI(new byte[2]);
43 }
44
45 @Test(expected = IllegalArgumentException.class)
46 public void testLongOUI() {
47 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
48 tlv.setOUI(new byte[4]);
49 }
50
51 @Test(expected = IllegalArgumentException.class)
52 public void testLongInfoString() {
53 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
54 tlv.setInfoString(new byte[LLDPOrganizationalTLV.MAX_INFOSTRING_LENGTH + 1]);
55 }
56
57 @Test
58 public void testMaxInfoString() {
59 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
60 tlv.setInfoString(new byte[LLDPOrganizationalTLV.MAX_INFOSTRING_LENGTH]);
61 }
62
63 @Test
64 public void testInfoString() {
65 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
66 tlv.setInfoString("ExtraInfo");
67 assertThat(tlv.getInfoString(), is("ExtraInfo".getBytes(Charset.forName("UTF-8"))));
68 }
69
70 @Test
71 public void testSerialize() {
72 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
73 tlv.setLength((short) 13);
74 // OpenFlow OUI is 00-26-E1
75 tlv.setOUI(new byte[] {0x0, 0x26, (byte) 0xe1});
76 tlv.setSubType((byte) 12);
77 tlv.setInfoString("ExtraInfo".getBytes(Charset.forName("UTF-8")));
78
79 assertThat(tlv.getType(), is((byte)127));
80 assertThat(tlv.getLength(), is((short)13));
81 assertThat(tlv.getOUI(), is(new byte[] {0x0, 0x26, (byte) 0xe1}));
82 assertThat(tlv.getSubType(), is((byte)12));
83 assertThat(tlv.serialize(), is(expected));
84 }
85
86 @Test
87 public void testDeserialize() {
88 LLDPOrganizationalTLV tlv = new LLDPOrganizationalTLV();
89 tlv.deserialize(ByteBuffer.wrap(expected));
90
91 assertThat(tlv.getType(), is((byte)127));
92 assertThat(tlv.getLength(), is((short)13));
93 assertThat(tlv.getOUI(), is(new byte[] {0x0, 0x26, (byte) 0xe1}));
94 assertThat(tlv.getSubType(), is((byte)12));
95 assertThat(tlv.getInfoString(), is("ExtraInfo".getBytes(Charset.forName("UTF-8"))));
96 }
97}