2 * Copyright (c) 2014 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.assertTrue;
13 import static org.junit.Assert.fail;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.opendaylight.protocol.bgp.parser.BGPDocumentedException;
20 import org.opendaylight.protocol.bgp.parser.BGPParsingException;
21 import org.opendaylight.protocol.bgp.parser.spi.pojo.ServiceLoaderBGPExtensionProviderContext;
22 import org.opendaylight.protocol.util.ByteArray;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.Keepalive;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev180329.KeepaliveBuilder;
25 import org.opendaylight.yangtools.yang.binding.Notification;
27 public class AbstractMessageRegistryTest {
29 public static final byte[] KEEPALIVE_BMSG = new byte[] {
30 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
31 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
32 (byte) 0x00, (byte) 0x13, (byte) 0x04
35 private final AbstractMessageRegistry registry = new AbstractMessageRegistry() {
38 protected void serializeMessageImpl(final Notification message, final ByteBuf buffer) {
39 buffer.writeBytes(KEEPALIVE_BMSG);
43 protected Notification parseBody(final int type, final ByteBuf body, final int messageLength,
44 final PeerSpecificParserConstraint constraint) throws BGPDocumentedException {
45 return new KeepaliveBuilder().build();
50 public void testRegistry() throws BGPDocumentedException, BGPParsingException {
51 final Notification keepAlive = new KeepaliveBuilder().build();
52 final ByteBuf buffer = Unpooled.buffer();
53 this.registry.serializeMessage(keepAlive, buffer);
54 assertArrayEquals(KEEPALIVE_BMSG, ByteArray.getAllBytes(buffer));
56 final Notification not = this.registry.parseMessage(Unpooled.copiedBuffer(KEEPALIVE_BMSG), null);
57 assertTrue(not instanceof Keepalive);
61 public void testIncompleteMarker() {
62 final byte[] testBytes = new byte[] {
63 (byte) 0x00, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
64 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
65 (byte) 0x00, (byte) 0x13, (byte) 0x04
68 this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
70 } catch (BGPDocumentedException | BGPParsingException e) {
71 assertTrue(e instanceof BGPDocumentedException);
72 Assert.assertEquals("Marker not set to ones.", e.getMessage());
77 public void testInvalidLength() {
78 final byte[] testBytes = new byte[] {
79 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
80 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
81 (byte) 0x00, (byte) 0x12, (byte) 0x04
84 this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
86 } catch (BGPDocumentedException | BGPParsingException e) {
87 assertTrue(e instanceof BGPDocumentedException);
88 Assert.assertEquals("Message length field not within valid range.", e.getMessage());
93 public void testInvalidSpecifiedSize() {
94 final byte[] testBytes = new byte[] {
95 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
96 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
97 (byte) 0x00, (byte) 0x13, (byte) 0x04, (byte) 0x04
100 this.registry.parseMessage(Unpooled.copiedBuffer(testBytes), null);
102 } catch (BGPDocumentedException | BGPParsingException e) {
103 assertTrue(e instanceof BGPParsingException);
104 Assert.assertTrue(e.getMessage().startsWith("Size doesn't match size specified in header."));
109 public void testBGPHeaderParser() throws Exception {
110 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
111 .getMessageRegistry();
113 msgReg.parseMessage(Unpooled.copiedBuffer(new byte[] { (byte) 0, (byte) 0 }), null);
114 fail("Exception should have occured.");
115 } catch (final IllegalArgumentException e) {
116 assertEquals("Too few bytes in passed array. Passed: 2. Expected: >= 19.", e.getMessage());
121 public void testMessageParser() throws Exception {
122 final MessageRegistry msgReg = ServiceLoaderBGPExtensionProviderContext.getSingletonInstance()
123 .getMessageRegistry();
126 msgReg.serializeMessage(null, Unpooled.EMPTY_BUFFER);
127 } catch (final NullPointerException e) {
130 assertEquals("BGPMessage is mandatory.", ex);