2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.pcep.spi;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
14 import io.netty.buffer.ByteBuf;
15 import io.netty.buffer.Unpooled;
16 import java.lang.reflect.Constructor;
17 import java.lang.reflect.InvocationTargetException;
18 import org.junit.Test;
19 import org.opendaylight.protocol.util.ByteArray;
21 public class UtilsTest {
24 public void testLabelUtil() {
25 final byte[] expected = { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 };
26 final ByteBuf out = Unpooled.buffer();
27 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
28 LabelUtil.formatLabel(4, true, true, body, out);
29 assertArrayEquals(expected, ByteArray.readAllBytes(out));
31 final byte[] ex = { 0, 0x05, 0x01, 0x02, 0x03, 0x04 };
32 body.resetReaderIndex();
33 LabelUtil.formatLabel(5, null, null, body, out);
34 assertArrayEquals(ex, ByteArray.getAllBytes(out));
38 public void testMessageUtil() {
39 final byte[] expected = { (byte) 0x20, 0x08, 0, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
40 final ByteBuf out = Unpooled.buffer();
41 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 });
42 MessageUtil.formatMessage(8, body, out);
43 assertArrayEquals(expected, ByteArray.getAllBytes(out));
47 public void testObjectUtil() {
48 final byte[] expected = { 0x08, 0x13, 0, 0x06, 0x01, 0x02 };
49 final ByteBuf out = Unpooled.buffer();
50 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
51 ObjectUtil.formatSubobject(1, 8, true, true, body, out);
52 assertArrayEquals(expected, ByteArray.getAllBytes(out));
56 public void testObjectUtilFalse() {
57 final byte[] expected = { 0x08, 0x10, 0, 0x06, 0x01, 0x02 };
58 final ByteBuf out = Unpooled.buffer();
59 ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
60 ObjectUtil.formatSubobject(1, 8, false, false, body, out);
61 assertArrayEquals(expected, ByteArray.readAllBytes(out));
63 body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
64 ObjectUtil.formatSubobject(1, 8, null, null, body, out);
65 assertArrayEquals(expected, ByteArray.getAllBytes(out));
69 public void testXROSubobjectUtil() {
70 byte[] expected = { (byte) 0x82, 6, 0, 1, 2, 3 };
71 final ByteBuf out = Unpooled.buffer();
72 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 0, 1, 2, 3 });
73 body.markReaderIndex();
74 XROSubobjectUtil.formatSubobject(2, true, body, out);
75 assertArrayEquals(expected, ByteArray.getAllBytes(out));
77 expected = new byte[]{ 2, 6, 0, 1, 2, 3 };
79 body.resetReaderIndex();
80 XROSubobjectUtil.formatSubobject(2, false, body, out);
81 assertArrayEquals(expected, ByteArray.getAllBytes(out));
84 body.resetReaderIndex();
85 XROSubobjectUtil.formatSubobject(2, null, body, out);
86 assertArrayEquals(expected, ByteArray.getAllBytes(out));
90 public void testTlvUtil() {
91 byte[] expected = { 0, 4, 0, 4, 1, 2, 3, 4 };
92 final ByteBuf out = Unpooled.buffer();
93 ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
94 TlvUtil.formatTlv(4, body, out);
95 assertArrayEquals(expected, ByteArray.getAllBytes(out));
97 expected = new byte[]{ 0, 4, 0, 5, 1, 2, 3, 4, 5, 0, 0, 0 };
99 body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5 });
100 TlvUtil.formatTlv(4, body, out);
101 assertArrayEquals(expected, ByteArray.getAllBytes(out));
105 public void testRROSubobjectUtil() {
106 final byte[] expected = { 4, 6, 1, 2, 3, 4 };
107 final ByteBuf out = Unpooled.buffer();
108 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
109 RROSubobjectUtil.formatSubobject(4, body, out);
110 assertArrayEquals(expected, ByteArray.getAllBytes(out));
114 public void testEROSubobjectUtil() {
115 byte[] expected = { (byte) 0x82, 6, 0, 1, 2, 3 };
116 final ByteBuf out = Unpooled.buffer();
117 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 0, 1, 2, 3 });
118 body.markReaderIndex();
119 EROSubobjectUtil.formatSubobject(2, true, body, out);
120 assertArrayEquals(expected, ByteArray.getAllBytes(out));
122 expected = new byte[]{ 2, 6, 0, 1, 2, 3 };
124 body.resetReaderIndex();
125 EROSubobjectUtil.formatSubobject(2, false, body, out);
126 assertArrayEquals(expected, ByteArray.getAllBytes(out));
130 public void testVendorInformationUtil() {
131 assertTrue(VendorInformationUtil.isVendorInformationTlv(VendorInformationUtil.VENDOR_INFORMATION_TLV_TYPE));
132 assertFalse(VendorInformationUtil.isVendorInformationTlv(VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS));
134 assertTrue(VendorInformationUtil.isVendorInformationObject(VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS, VendorInformationUtil.VENDOR_INFORMATION_OBJECT_TYPE));
135 assertFalse(VendorInformationUtil.isVendorInformationObject(VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS, VendorInformationUtil.VENDOR_INFORMATION_TLV_TYPE));
136 assertFalse(VendorInformationUtil.isVendorInformationObject(VendorInformationUtil.VENDOR_INFORMATION_TLV_TYPE, VendorInformationUtil.VENDOR_INFORMATION_OBJECT_TYPE));
137 assertFalse(VendorInformationUtil.isVendorInformationObject(VendorInformationUtil.VENDOR_INFORMATION_OBJECT_TYPE, VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS));
140 @Test(expected=UnsupportedOperationException.class)
141 public void testVendorInformationUtilPrivateConstructor() throws Throwable {
142 final Constructor<VendorInformationUtil> c = VendorInformationUtil.class.getDeclaredConstructor();
143 c.setAccessible(true);
146 } catch (final InvocationTargetException e) {
151 @Test(expected=UnsupportedOperationException.class)
152 public void testEROSubobjectUtilPrivateConstructor() throws Throwable {
153 final Constructor<EROSubobjectUtil> c = EROSubobjectUtil.class.getDeclaredConstructor();
154 c.setAccessible(true);
157 } catch (final InvocationTargetException e) {
162 @Test(expected=UnsupportedOperationException.class)
163 public void testRROSubobjectUtilPrivateConstructor() throws Throwable {
164 final Constructor<RROSubobjectUtil> c = RROSubobjectUtil.class.getDeclaredConstructor();
165 c.setAccessible(true);
168 } catch (final InvocationTargetException e) {
173 @Test(expected=UnsupportedOperationException.class)
174 public void testTlvUtilPrivateConstructor() throws Throwable {
175 final Constructor<TlvUtil> c = TlvUtil.class.getDeclaredConstructor();
176 c.setAccessible(true);
179 } catch (final InvocationTargetException e) {
184 @Test(expected=UnsupportedOperationException.class)
185 public void testXROSubobjectUtilPrivateConstructor() throws Throwable {
186 final Constructor<XROSubobjectUtil> c = XROSubobjectUtil.class.getDeclaredConstructor();
187 c.setAccessible(true);
190 } catch (final InvocationTargetException e) {
195 @Test(expected=UnsupportedOperationException.class)
196 public void testObjectUtilPrivateConstructor() throws Throwable {
197 final Constructor<ObjectUtil> c = ObjectUtil.class.getDeclaredConstructor();
198 c.setAccessible(true);
201 } catch (final InvocationTargetException e) {
206 @Test(expected=UnsupportedOperationException.class)
207 public void testMessageUtilPrivateConstructor() throws Throwable {
208 final Constructor<MessageUtil> c = MessageUtil.class.getDeclaredConstructor();
209 c.setAccessible(true);
212 } catch (final InvocationTargetException e) {
217 @Test(expected=UnsupportedOperationException.class)
218 public void testLabelUtilPrivateConstructor() throws Throwable {
219 final Constructor<LabelUtil> c = LabelUtil.class.getDeclaredConstructor();
220 c.setAccessible(true);
223 } catch (final InvocationTargetException e) {
228 @Test(expected=UnsupportedOperationException.class)
229 public void testPCEPMessageConstantsPrivateConstructor() throws Throwable {
230 final Constructor<PCEPMessageConstants> c = PCEPMessageConstants.class.getDeclaredConstructor();
231 c.setAccessible(true);
234 } catch (final InvocationTargetException e) {