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 org.junit.Test;
16 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130918.update.path.attributes.MpReachNlriBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.CNextHop;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.CIpv4NextHop;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.next.hop.c.next.hop.CIpv6NextHop;
22 import com.google.common.primitives.UnsignedBytes;
24 public class UtilsTest {
27 public void testCapabilityUtil() {
28 final byte[] result = new byte[] { 1, 2, 4, 8 };
29 assertArrayEquals(result, CapabilityUtil.formatCapability(1, new byte[] { 4, 8 }));
33 public void testMessageUtil() {
34 final byte[] result = new byte[] { UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
35 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
36 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
37 UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE, UnsignedBytes.MAX_VALUE,
38 UnsignedBytes.MAX_VALUE, 0, 23, 3, 32, 5, 14, 21 };
39 assertArrayEquals(result, MessageUtil.formatMessage(3, new byte[] { 32, 5, 14, 21 }));
43 public void testNlriUtil() {
44 final MpReachNlriBuilder builder = new MpReachNlriBuilder();
45 final byte[] ipv4 = new byte[] { 42, 42, 42, 42 };
47 NlriUtil.parseNextHop(ipv4, builder);
48 } catch (final BGPParsingException e) {
49 fail("This exception should not happen");
51 CNextHop hop = builder.getCNextHop();
52 assertEquals("42.42.42.42", ((CIpv4NextHop) hop).getIpv4NextHop().getGlobal().getValue());
54 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 };
56 NlriUtil.parseNextHop(ipv6, builder);
57 } catch (final BGPParsingException e) {
58 fail("This exception should not happen");
60 hop = builder.getCNextHop();
61 assertEquals("2001:db8::1", ((CIpv6NextHop) hop).getIpv6NextHop().getGlobal().getValue());
62 assertNull(((CIpv6NextHop) hop).getIpv6NextHop().getLinkLocal());
64 final byte[] ipv6l = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d, (byte) 0xb8, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00,
65 01, (byte) 0xfe, (byte) 0x80, 00, 00, 00, 00, 00, 00, (byte) 0xc0, 01, 0x0b, (byte) 0xff, (byte) 0xfe, 0x7e, 00, 00 };
67 NlriUtil.parseNextHop(ipv6l, builder);
68 } catch (final BGPParsingException e) {
69 fail("This exception should not happen");
71 hop = builder.getCNextHop();
72 assertEquals("2001:db8::1", ((CIpv6NextHop) hop).getIpv6NextHop().getGlobal().getValue());
73 assertEquals("fe80::c001:bff:fe7e:0", ((CIpv6NextHop) hop).getIpv6NextHop().getLinkLocal().getValue());
75 final byte[] wrong = new byte[] { (byte) 0x20, (byte) 0x01, (byte) 0x0d };
77 NlriUtil.parseNextHop(wrong, builder);
78 fail("Exception should happen");
79 } catch (final BGPParsingException e) {
80 assertEquals("Cannot parse NEXT_HOP attribute. Wrong bytes length: 3", e.getMessage());
85 public void testParameterUtil() {
86 final byte[] result = new byte[] { 1, 2, 4, 8 };
87 assertArrayEquals(result, ParameterUtil.formatParameter(1, new byte[] { 4, 8 }));