2 * Copyright (c) 2013 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.bgp.parser.spi;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Mockito.doReturn;
14 import com.google.common.primitives.UnsignedBytes;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.Arrays;
18 import java.util.Optional;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.junit.MockitoJUnitRunner;
24 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
25 import org.opendaylight.protocol.util.ByteArray;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
30 @RunWith(MockitoJUnitRunner.StrictStubs.class)
31 public class UtilsTest {
33 private AddressFamilyRegistry afiReg;
35 private SubsequentAddressFamilyRegistry safiReg;
39 doReturn(1).when(afiReg).numberForClass(Ipv4AddressFamily.VALUE);
40 doReturn(Ipv4AddressFamily.VALUE).when(afiReg).classForFamily(1);
41 doReturn(null).when(afiReg).classForFamily(2);
43 doReturn(1).when(safiReg).numberForClass(UnicastSubsequentAddressFamily.VALUE);
44 doReturn(UnicastSubsequentAddressFamily.VALUE).when(safiReg).classForFamily(1);
45 doReturn(null).when(safiReg).classForFamily(3);
49 public void testCapabilityUtil() {
50 final byte[] result = new byte[] { 1, 2, 4, 8 };
51 final ByteBuf aggregator = Unpooled.buffer();
52 CapabilityUtil.formatCapability(1, Unpooled.wrappedBuffer(new byte[] { 4, 8 }),aggregator);
53 assertArrayEquals(result, ByteArray.getAllBytes(aggregator));
57 public void testMessageUtil() {
58 final byte[] result = new byte[] { UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
59 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
60 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
61 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
62 UnsignedBytes.MAX_VALUE, 0, 23, 3, 32, 5, 14, 21 };
63 final ByteBuf formattedMessage = Unpooled.buffer();
64 MessageUtil.formatMessage(3, Unpooled.wrappedBuffer(new byte[] { 32, 5, 14, 21 }), formattedMessage);
65 assertArrayEquals(result, ByteArray.getAllBytes(formattedMessage));
69 public void testParameterUtil() throws ParameterLengthOverflowException {
70 final byte[] result = new byte[] { 1, 2, 4, 8 };
71 final ByteBuf aggregator = Unpooled.buffer();
72 ParameterUtil.formatParameter(1, Unpooled.wrappedBuffer(new byte[] { 4, 8 }), aggregator);
73 assertArrayEquals(result, ByteArray.getAllBytes(aggregator));
77 public void testAttributeUtil() {
78 final byte[] result = new byte[] { 0x40, 03, 04, 10, 00, 00, 02 };
79 final ByteBuf aggregator = Unpooled.buffer();
80 AttributeUtil.formatAttribute(64 , 3 , Unpooled.wrappedBuffer(new byte[] { 10, 0, 0, 2 }), aggregator);
81 assertArrayEquals(result, ByteArray.getAllBytes(aggregator));
85 public void testAttributeUtilExtended() {
86 final byte[] value = new byte[258];
87 Arrays.fill(value, 0, 258, UnsignedBytes.MAX_VALUE);
88 final byte[] header = new byte[] { (byte) 0x50, 03, 01, 02 };
89 final byte[] result = new byte[262];
90 System.arraycopy(header, 0, result, 0, header.length);
91 System.arraycopy(value, 0, result, 4, value.length);
92 final ByteBuf aggregator = Unpooled.buffer();
93 AttributeUtil.formatAttribute(AttributeUtil.TRANSITIVE , 3 , Unpooled.wrappedBuffer(value), aggregator);
94 assertArrayEquals(result, ByteArray.getAllBytes(aggregator));
98 public void testMultiprotocolCapabilitiesUtil() throws BGPParsingException {
99 final byte[] bytes = new byte[] {0, 1, 0, 1};
100 final ByteBuf bytesBuf = Unpooled.copiedBuffer(bytes);
101 final BgpTableType parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(bytesBuf, afiReg, safiReg)
103 assertEquals(Ipv4AddressFamily.VALUE, parsedAfiSafi.getAfi());
104 assertEquals(UnicastSubsequentAddressFamily.VALUE, parsedAfiSafi.getSafi());
106 final ByteBuf serializedAfiSafi = Unpooled.buffer(4);
107 MultiprotocolCapabilitiesUtil.serializeMPAfiSafi(afiReg, safiReg, Ipv4AddressFamily.VALUE,
108 UnicastSubsequentAddressFamily.VALUE, serializedAfiSafi);
109 assertArrayEquals(bytes, serializedAfiSafi.array());
113 public void testUnsupportedAfi() {
114 final byte[] bytes = new byte[] {0, 2, 0, 1};
115 final ByteBuf bytesBuf = Unpooled.copiedBuffer(bytes);
116 assertEquals(Optional.empty(), MultiprotocolCapabilitiesUtil.parseMPAfiSafi(bytesBuf, afiReg, safiReg));
120 public void testUnsupportedSafi() {
121 final byte[] bytes = new byte[] {0, 1, 0, 3};
122 final ByteBuf bytesBuf = Unpooled.copiedBuffer(bytes);
123 assertEquals(Optional.empty(), MultiprotocolCapabilitiesUtil.parseMPAfiSafi(bytesBuf, afiReg, safiReg));
126 @Test(expected = ParameterLengthOverflowException.class)
127 public void testFormatParameterOverflow() throws ParameterLengthOverflowException {
128 ParameterUtil.formatParameter(2, Unpooled.buffer().writeZero(256), Unpooled.buffer());
132 public void testFormatParameter() throws ParameterLengthOverflowException {
133 final ByteBuf output = Unpooled.buffer();
134 ParameterUtil.formatParameter(2, Unpooled.buffer().writeZero(255), output);
136 assertEquals(257, output.readableBytes());
137 assertEquals(2, output.readUnsignedByte());
138 assertEquals(255, output.readUnsignedByte());
142 public void testFormatExtendedParameter() {
143 final ByteBuf output = Unpooled.buffer();
144 ParameterUtil.formatExtendedParameter(2, Unpooled.buffer().writeZero(256), output);
146 assertEquals(259, output.readableBytes());
147 assertEquals(2, output.readUnsignedByte());
148 assertEquals(256, output.readUnsignedShort());