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;
13 import com.google.common.primitives.UnsignedBytes;
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 java.util.Arrays;
19 import java.util.Optional;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.Ipv4AddressFamily;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev180329.UnicastSubsequentAddressFamily;
32 public class UtilsTest {
34 @Mock private AddressFamilyRegistry afiReg;
35 @Mock private SubsequentAddressFamilyRegistry safiReg;
39 MockitoAnnotations.initMocks(this);
40 Mockito.doReturn(1).when(this.afiReg).numberForClass(Ipv4AddressFamily.class);
41 Mockito.doReturn(Ipv4AddressFamily.class).when(this.afiReg).classForFamily(1);
42 Mockito.doReturn(null).when(this.afiReg).classForFamily(2);
44 Mockito.doReturn(1).when(this.safiReg).numberForClass(UnicastSubsequentAddressFamily.class);
45 Mockito.doReturn(UnicastSubsequentAddressFamily.class).when(this.safiReg).classForFamily(1);
46 Mockito.doReturn(null).when(this.safiReg).classForFamily(3);
50 public void testCapabilityUtil() {
51 final byte[] result = new byte[] { 1, 2, 4, 8 };
52 final ByteBuf aggregator = Unpooled.buffer();
53 CapabilityUtil.formatCapability(1, Unpooled.wrappedBuffer(new byte[] { 4, 8 }),aggregator);
54 assertArrayEquals(result, ByteArray.getAllBytes(aggregator));
58 public void testMessageUtil() {
59 final byte[] result = new byte[] { UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
60 UnsignedBytes.MAX_VALUE, 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, UnsignedBytes.MAX_VALUE,
62 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, 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() {
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, this.afiReg, this.safiReg).get();
102 assertEquals(Ipv4AddressFamily.class, parsedAfiSafi.getAfi());
103 assertEquals(UnicastSubsequentAddressFamily.class, parsedAfiSafi.getSafi());
105 final ByteBuf serializedAfiSafi = Unpooled.buffer(4);
106 MultiprotocolCapabilitiesUtil.serializeMPAfiSafi(this.afiReg, this.safiReg, Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class, serializedAfiSafi);
107 assertArrayEquals(bytes, serializedAfiSafi.array());
111 public void testUnsupportedAfi() {
112 final byte[] bytes = new byte[] {0, 2, 0, 1};
113 final ByteBuf bytesBuf = Unpooled.copiedBuffer(bytes);
114 final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(bytesBuf, this.afiReg, this.safiReg);
115 Assert.assertFalse(parsedAfiSafi.isPresent());
119 public void testUnsupportedSafi() {
120 final byte[] bytes = new byte[] {0, 1, 0, 3};
121 final ByteBuf bytesBuf = Unpooled.copiedBuffer(bytes);
122 final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(bytesBuf, this.afiReg, this.safiReg);
123 Assert.assertFalse(parsedAfiSafi.isPresent());
126 @Test(expected=UnsupportedOperationException.class)
127 public void testAttributeUtilPrivateConstructor() throws Throwable {
128 final Constructor<AttributeUtil> c = AttributeUtil.class.getDeclaredConstructor();
129 c.setAccessible(true);
132 } catch (final InvocationTargetException e) {
137 @Test(expected=UnsupportedOperationException.class)
138 public void testCapabilityUtilPrivateConstructor() throws Throwable {
139 final Constructor<CapabilityUtil> c = CapabilityUtil.class.getDeclaredConstructor();
140 c.setAccessible(true);
143 } catch (final InvocationTargetException e) {
148 @Test(expected=UnsupportedOperationException.class)
149 public void testMessageUtilPrivateConstructor() throws Throwable {
150 final Constructor<MessageUtil> c = MessageUtil.class.getDeclaredConstructor();
151 c.setAccessible(true);
154 } catch (final InvocationTargetException e) {
159 @Test(expected=UnsupportedOperationException.class)
160 public void testParameterUtilPrivateConstructor() throws Throwable {
161 final Constructor<ParameterUtil> c = ParameterUtil.class.getDeclaredConstructor();
162 c.setAccessible(true);
165 } catch (final InvocationTargetException e) {