Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 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 | |
| 15 | package net.floodlightcontroller.packet; |
| 16 | |
| 17 | import org.junit.Test; |
| 18 | |
| 19 | import java.nio.ByteBuffer; |
| 20 | import java.nio.charset.Charset; |
| 21 | |
| 22 | import static org.hamcrest.CoreMatchers.is; |
| 23 | import static org.junit.Assert.assertThat; |
| 24 | |
| 25 | public class LLDPOrganizationalTLVTest { |
| 26 | 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 |
| 35 | }; |
| 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 |
| 73 | tlv.setOUI(new byte[] {0x0, 0x26, (byte) 0xe1}); |
| 74 | tlv.setSubType((byte) 12); |
| 75 | tlv.setInfoString("ExtraInfo".getBytes(Charset.forName("UTF-8"))); |
| 76 | |
| 77 | 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)); |
| 81 | 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 | |
| 89 | 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)); |
| 93 | assertThat(tlv.getInfoString(), is("ExtraInfo".getBytes(Charset.forName("UTF-8")))); |
| 94 | } |
| 95 | } |