dd9181374b27d70698d653d092d9855b9175d583
[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
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.rev171207.BgpTableType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.UnicastSubsequentAddressFamily;
31
32 public class UtilsTest {
33
34     @Mock private AddressFamilyRegistry afiReg;
35     @Mock private SubsequentAddressFamilyRegistry safiReg;
36
37     @Before
38     public void setUp() {
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);
43
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);
47     }
48
49     @Test
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));
55     }
56
57     @Test
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));
66     }
67
68     @Test
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));
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, this.afiReg, this.safiReg).get();
102         assertEquals(Ipv4AddressFamily.class, parsedAfiSafi.getAfi());
103         assertEquals(UnicastSubsequentAddressFamily.class, parsedAfiSafi.getSafi());
104
105         final ByteBuf serializedAfiSafi = Unpooled.buffer(4);
106         MultiprotocolCapabilitiesUtil.serializeMPAfiSafi(this.afiReg, this.safiReg, Ipv4AddressFamily.class, UnicastSubsequentAddressFamily.class, serializedAfiSafi);
107         assertArrayEquals(bytes, serializedAfiSafi.array());
108     }
109
110     @Test
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());
116     }
117
118     @Test
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());
124     }
125
126     @Test(expected=UnsupportedOperationException.class)
127     public void testAttributeUtilPrivateConstructor() throws Throwable {
128         final Constructor<AttributeUtil> c = AttributeUtil.class.getDeclaredConstructor();
129         c.setAccessible(true);
130         try {
131             c.newInstance();
132         } catch (final InvocationTargetException e) {
133             throw e.getCause();
134         }
135     }
136
137     @Test(expected=UnsupportedOperationException.class)
138     public void testCapabilityUtilPrivateConstructor() throws Throwable {
139         final Constructor<CapabilityUtil> c = CapabilityUtil.class.getDeclaredConstructor();
140         c.setAccessible(true);
141         try {
142             c.newInstance();
143         } catch (final InvocationTargetException e) {
144             throw e.getCause();
145         }
146     }
147
148     @Test(expected=UnsupportedOperationException.class)
149     public void testMessageUtilPrivateConstructor() throws Throwable {
150         final Constructor<MessageUtil> c = MessageUtil.class.getDeclaredConstructor();
151         c.setAccessible(true);
152         try {
153             c.newInstance();
154         } catch (final InvocationTargetException e) {
155             throw e.getCause();
156         }
157     }
158
159     @Test(expected=UnsupportedOperationException.class)
160     public void testParameterUtilPrivateConstructor() throws Throwable {
161         final Constructor<ParameterUtil> c = ParameterUtil.class.getDeclaredConstructor();
162         c.setAccessible(true);
163         try {
164             c.newInstance();
165         } catch (final InvocationTargetException e) {
166             throw e.getCause();
167         }
168     }
169 }