f67bfab2607ea078f282ad8f16878c2aad589a3f
[bgpcep.git] / bgp / parser-spi / src / test / java / org / opendaylight / protocol / bgp / parser / spi / UtilsTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.parser.spi;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.mockito.Mockito.doReturn;
14
15 import com.google.common.primitives.UnsignedBytes;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import java.util.Arrays;
19 import java.util.Optional;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
26 import org.opendaylight.protocol.util.ByteArray;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev180329.BgpTableType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.Ipv4AddressFamily;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev200120.UnicastSubsequentAddressFamily;
30
31 @RunWith(MockitoJUnitRunner.StrictStubs.class)
32 public class UtilsTest {
33
34     @Mock private AddressFamilyRegistry afiReg;
35     @Mock private SubsequentAddressFamilyRegistry safiReg;
36
37     @Before
38     public void setUp() {
39         doReturn(1).when(afiReg).numberForClass(Ipv4AddressFamily.VALUE);
40         doReturn(Ipv4AddressFamily.VALUE).when(afiReg).classForFamily(1);
41         doReturn(null).when(afiReg).classForFamily(2);
42
43         doReturn(1).when(safiReg).numberForClass(UnicastSubsequentAddressFamily.VALUE);
44         doReturn(UnicastSubsequentAddressFamily.VALUE).when(safiReg).classForFamily(1);
45         doReturn(null).when(safiReg).classForFamily(3);
46     }
47
48     @Test
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));
54     }
55
56     @Test
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));
66     }
67
68     @Test
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));
74     }
75
76     @Test
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));
82     }
83
84     @Test
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));
95     }
96
97     @Test
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,
102             safiReg).get();
103         assertEquals(Ipv4AddressFamily.VALUE, parsedAfiSafi.getAfi());
104         assertEquals(UnicastSubsequentAddressFamily.VALUE, parsedAfiSafi.getSafi());
105
106         final ByteBuf serializedAfiSafi = Unpooled.buffer(4);
107         MultiprotocolCapabilitiesUtil.serializeMPAfiSafi(afiReg, safiReg, Ipv4AddressFamily.VALUE,
108             UnicastSubsequentAddressFamily.VALUE, serializedAfiSafi);
109         assertArrayEquals(bytes, serializedAfiSafi.array());
110     }
111
112     @Test
113     public void testUnsupportedAfi() {
114         final byte[] bytes = new byte[] {0, 2, 0, 1};
115         final ByteBuf bytesBuf = Unpooled.copiedBuffer(bytes);
116         final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(bytesBuf, afiReg,
117             safiReg);
118         assertFalse(parsedAfiSafi.isPresent());
119     }
120
121     @Test
122     public void testUnsupportedSafi() {
123         final byte[] bytes = new byte[] {0, 1, 0, 3};
124         final ByteBuf bytesBuf = Unpooled.copiedBuffer(bytes);
125         final Optional<BgpTableType> parsedAfiSafi = MultiprotocolCapabilitiesUtil.parseMPAfiSafi(bytesBuf, afiReg,
126             safiReg);
127         assertFalse(parsedAfiSafi.isPresent());
128     }
129
130     @Test(expected = ParameterLengthOverflowException.class)
131     public void testFormatParameterOverflow() throws ParameterLengthOverflowException {
132         ParameterUtil.formatParameter(2, Unpooled.buffer().writeZero(256), Unpooled.buffer());
133     }
134
135     @Test
136     public void testFormatParameter() throws ParameterLengthOverflowException {
137         final ByteBuf output = Unpooled.buffer();
138         ParameterUtil.formatParameter(2, Unpooled.buffer().writeZero(255), output);
139
140         assertEquals(257, output.readableBytes());
141         assertEquals(2, output.readUnsignedByte());
142         assertEquals(255, output.readUnsignedByte());
143     }
144
145     @Test
146     public void testFormatExtendedParameter() {
147         final ByteBuf output = Unpooled.buffer();
148         ParameterUtil.formatExtendedParameter(2, Unpooled.buffer().writeZero(256), output);
149
150         assertEquals(259, output.readableBytes());
151         assertEquals(2, output.readUnsignedByte());
152         assertEquals(256, output.readUnsignedShort());
153     }
154 }