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.junit.Assert.assertNull;
13 import static org.junit.Assert.fail;
15 import com.google.common.primitives.UnsignedBytes;
16 import io.netty.buffer.ByteBuf;
17 import io.netty.buffer.Unpooled;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
20 import org.opendaylight.protocol.util.ByteArray;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.update.path.attributes.MpReachNlriBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv4NextHopCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.Ipv6NextHopCase;
26 public class UtilsTest {
29 public void testCapabilityUtil() {
30 final byte[] result = new byte[] { 1, 2, 4, 8 };
31 assertArrayEquals(result, CapabilityUtil.formatCapability(1, new byte[] { 4, 8 }));
35 public void testMessageUtil() {
36 final byte[] result = new byte[] { UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
37 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
38 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
39 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, 0, 23, 3, 32, 5, 14, 21 };
40 ByteBuf formattedMessage = Unpooled.buffer();
41 MessageUtil.formatMessage(3, Unpooled.wrappedBuffer(new byte[] { 32, 5, 14, 21 }), formattedMessage);
42 assertArrayEquals(result, ByteArray.getAllBytes(formattedMessage));
46 public void testNlriUtil() {
47 final MpReachNlriBuilder builder = new MpReachNlriBuilder();
48 final byte[] ipv4 = new byte[] { 42, 42, 42, 42 };
50 NlriUtil.parseNextHop(ipv4, builder);
51 } catch (final BGPParsingException e) {
52 fail("This exception should not happen");
54 CNextHop hop = builder.getCNextHop();
55 assertEquals("42.42.42.42", ((Ipv4NextHopCase) hop).getIpv4NextHop().getGlobal().getValue());
57 final byte[] ipv6 = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 01 };
59 NlriUtil.parseNextHop(ipv6, builder);
60 } catch (final BGPParsingException e) {
61 fail("This exception should not happen");
63 hop = builder.getCNextHop();
64 assertEquals("2001:db8::1", ((Ipv6NextHopCase) hop).getIpv6NextHop().getGlobal().getValue());
65 assertNull(((Ipv6NextHopCase) hop).getIpv6NextHop().getLinkLocal());
67 final byte[] ipv6l = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
68 01, (byte) 0xfe, (byte) 0x80, 00, 00, 00, 00, 00, 00, (byte) 0xc0, 01, 0x0b, (byte) 0xff, (byte) 0xfe, 0x7e, 00, 00 };
70 NlriUtil.parseNextHop(ipv6l, builder);
71 } catch (final BGPParsingException e) {
72 fail("This exception should not happen");
74 hop = builder.getCNextHop();
75 assertEquals("2001:db8::1", ((Ipv6NextHopCase) hop).getIpv6NextHop().getGlobal().getValue());
76 assertEquals("fe80::c001:bff:fe7e:0", ((Ipv6NextHopCase) hop).getIpv6NextHop().getLinkLocal().getValue());
78 final byte[] wrong = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d };
80 NlriUtil.parseNextHop(wrong, builder);
81 fail("Exception should happen");
82 } catch (final BGPParsingException e) {
83 assertEquals("Cannot parse NEXT_HOP attribute. Wrong bytes length: 3", e.getMessage());
88 public void testParameterUtil() {
89 final byte[] result = new byte[] { 1, 2, 4, 8 };
90 ByteBuf aggregator = Unpooled.buffer();
91 ParameterUtil.formatParameter(1, Unpooled.wrappedBuffer(new byte[] { 4, 8 }), aggregator);
92 assertArrayEquals(result, ByteArray.getAllBytes(aggregator));