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;
13 import static org.opendaylight.protocol.pcep.spi.VendorInformationUtil.VENDOR_INFORMATION_OBJECT_CLASS;
14 import static org.opendaylight.protocol.pcep.spi.VendorInformationUtil.VENDOR_INFORMATION_OBJECT_TYPE;
15 import static org.opendaylight.protocol.pcep.spi.VendorInformationUtil.VENDOR_INFORMATION_TLV_TYPE;
17 import io.netty.buffer.ByteBuf;
18 import io.netty.buffer.Unpooled;
19 import org.junit.Test;
20 import org.opendaylight.protocol.util.ByteArray;
22 public class UtilsTest {
25 public void testLabelUtil() {
26 final byte[] expected = { (byte) 0x81, 0x04, 0x01, 0x02, 0x03, 0x04 };
27 final ByteBuf out = Unpooled.buffer();
28 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
29 LabelUtil.formatLabel(4, true, true, body, out);
30 assertArrayEquals(expected, ByteArray.readAllBytes(out));
32 final byte[] ex = { 0, 0x05, 0x01, 0x02, 0x03, 0x04 };
33 body.resetReaderIndex();
34 LabelUtil.formatLabel(5, null, null, body, out);
35 assertArrayEquals(ex, ByteArray.getAllBytes(out));
39 public void testMessageUtil() {
40 final byte[] expected = { (byte) 0x20, 0x08, 0, 0x0a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
41 final ByteBuf out = Unpooled.buffer();
42 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 });
43 MessageUtil.formatMessage(8, body, out);
44 assertArrayEquals(expected, ByteArray.getAllBytes(out));
48 public void testObjectUtil() {
49 final byte[] expected = { 0x08, 0x13, 0, 0x06, 0x01, 0x02 };
50 final ByteBuf out = Unpooled.buffer();
51 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
52 ObjectUtil.formatSubobject(1, 8, true, true, body, out);
53 assertArrayEquals(expected, ByteArray.getAllBytes(out));
57 public void testObjectUtilFalse() {
58 final byte[] expected = { 0x08, 0x10, 0, 0x06, 0x01, 0x02 };
59 final ByteBuf out = Unpooled.buffer();
60 ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
61 ObjectUtil.formatSubobject(1, 8, false, false, body, out);
62 assertArrayEquals(expected, ByteArray.readAllBytes(out));
64 body = Unpooled.copiedBuffer(new byte[] { 1, 2 });
65 ObjectUtil.formatSubobject(1, 8, null, null, body, out);
66 assertArrayEquals(expected, ByteArray.getAllBytes(out));
70 public void testXROSubobjectUtil() {
71 byte[] expected = { (byte) 0x82, 6, 0, 1, 2, 3 };
72 final ByteBuf out = Unpooled.buffer();
73 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 0, 1, 2, 3 });
74 body.markReaderIndex();
75 XROSubobjectUtil.formatSubobject(2, true, body, out);
76 assertArrayEquals(expected, ByteArray.getAllBytes(out));
78 expected = new byte[]{ 2, 6, 0, 1, 2, 3 };
80 body.resetReaderIndex();
81 XROSubobjectUtil.formatSubobject(2, false, body, out);
82 assertArrayEquals(expected, ByteArray.getAllBytes(out));
85 body.resetReaderIndex();
86 XROSubobjectUtil.formatSubobject(2, null, body, out);
87 assertArrayEquals(expected, ByteArray.getAllBytes(out));
91 public void testTlvUtil() {
92 byte[] expected = { 0, 4, 0, 4, 1, 2, 3, 4 };
93 final ByteBuf out = Unpooled.buffer();
94 ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
95 TlvUtil.formatTlv(4, body, out);
96 assertArrayEquals(expected, ByteArray.getAllBytes(out));
98 expected = new byte[]{ 0, 4, 0, 5, 1, 2, 3, 4, 5, 0, 0, 0 };
100 body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5 });
101 TlvUtil.formatTlv(4, body, out);
102 assertArrayEquals(expected, ByteArray.getAllBytes(out));
106 public void testRROSubobjectUtil() {
107 final byte[] expected = { 4, 6, 1, 2, 3, 4 };
108 final ByteBuf out = Unpooled.buffer();
109 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 });
110 RROSubobjectUtil.formatSubobject(4, body, out);
111 assertArrayEquals(expected, ByteArray.getAllBytes(out));
115 public void testEROSubobjectUtil() {
116 byte[] expected = { (byte) 0x82, 6, 0, 1, 2, 3 };
117 final ByteBuf out = Unpooled.buffer();
118 final ByteBuf body = Unpooled.copiedBuffer(new byte[] { 0, 1, 2, 3 });
119 body.markReaderIndex();
120 EROSubobjectUtil.formatSubobject(2, true, body, out);
121 assertArrayEquals(expected, ByteArray.getAllBytes(out));
123 expected = new byte[]{ 2, 6, 0, 1, 2, 3 };
125 body.resetReaderIndex();
126 EROSubobjectUtil.formatSubobject(2, false, body, out);
127 assertArrayEquals(expected, ByteArray.getAllBytes(out));
131 public void testVendorInformationUtil() {
132 assertTrue(VendorInformationUtil.isVendorInformationTlv(VENDOR_INFORMATION_TLV_TYPE));
133 assertFalse(VendorInformationUtil.isVendorInformationTlv(VENDOR_INFORMATION_OBJECT_CLASS));
135 assertTrue(VendorInformationUtil.isVendorInformationObject(VENDOR_INFORMATION_OBJECT_CLASS,
136 VENDOR_INFORMATION_OBJECT_TYPE));
137 assertFalse(VendorInformationUtil.isVendorInformationObject(VENDOR_INFORMATION_OBJECT_CLASS,
138 VENDOR_INFORMATION_TLV_TYPE));
139 assertFalse(VendorInformationUtil.isVendorInformationObject(VENDOR_INFORMATION_TLV_TYPE,
140 VENDOR_INFORMATION_OBJECT_TYPE));
141 assertFalse(VendorInformationUtil.isVendorInformationObject(VENDOR_INFORMATION_OBJECT_TYPE,
142 VENDOR_INFORMATION_OBJECT_CLASS));